M_Attach_To_Post

The NextGEN Gallery M Attach To Post class.

Defined (1)

The class is defined in the following location(s).

/products/photocrati_nextgen/modules/attach_to_post/module.attach_to_post.php  
  1. class M_Attach_To_Post extends C_Base_Module 
  2. var $attach_to_post_tinymce_plugin = 'NextGEN_AttachToPost'; 
  3. var $_event_publisher = NULL; 
  4. static $substitute_placeholders = TRUE; 
  5.  
  6. /** 
  7. * Defines the module 
  8. * @param string|bool $context 
  9. */ 
  10. function define($context=FALSE) 
  11. parent::define( 
  12. 'photocrati-attach_to_post',  
  13. 'Attach To Post',  
  14. 'Provides the "Attach to Post" interface for displaying galleries and albums',  
  15. '0.14',  
  16. 'https://www.imagely.com/wordpress-gallery-plugin/nextgen-gallery/',  
  17. 'Photocrati Media',  
  18. 'https://www.imagely.com',  
  19. $context 
  20. ); 
  21.  
  22. C_NextGen_Settings::get_instance()->add_option_handler('C_Attach_To_Post_Option_Handler', array( 
  23. 'attach_to_post_url',  
  24. 'gallery_preview_url',  
  25. 'attach_to_post_display_tab_js_url' 
  26. )); 
  27. if (is_multisite()) C_NextGen_Global_Settings::get_instance()->add_option_handler('C_Attach_To_Post_Option_Handler', array( 
  28. 'attach_to_post_url',  
  29. 'gallery_preview_url',  
  30. 'attach_to_post_display_tab_js_url' 
  31. )); 
  32.  
  33. C_Photocrati_Installer::add_handler($this->module_id, 'C_Attach_To_Post_Installer'); 
  34.  
  35. static function is_atp_url() 
  36. return (strpos(strtolower($_SERVER['REQUEST_URI']), NGG_ATTACH_TO_POST_SLUG) !== false) ? TRUE : FALSE; 
  37.  
  38. /** 
  39. * Gets the Frame Event Publisher 
  40. * @return C_Component 
  41. */ 
  42. function _get_frame_event_publisher() 
  43. if (is_null($this->_event_publisher)) { 
  44. $this->_event_publisher = $this->get_registry()->get_utility('I_Frame_Event_Publisher', 'attach_to_post'); 
  45.  
  46. return $this->_event_publisher; 
  47.  
  48.  
  49. /** 
  50. * Registers requires the utilites that this module provides 
  51. */ 
  52. function _register_utilities() 
  53. // This utility provides a controller that renders the 
  54. // Attach to Post interface, used to manage Displayed Galleries 
  55. $this->get_registry()->add_utility( 
  56. 'I_Attach_To_Post_Controller',  
  57. 'C_Attach_Controller' 
  58. ); 
  59.  
  60. /** 
  61. * Registers the adapters that this module provides 
  62. */ 
  63. function _register_adapters() 
  64. // Installs the Attach to Post module 
  65. $this->get_registry()->add_adapter( 
  66. 'I_Installer', 'A_Attach_To_Post_Installer' 
  67. ); 
  68.  
  69. // Provides AJAX actions for the Attach To Post interface 
  70. $this->get_registry()->add_adapter( 
  71. 'I_Ajax_Controller', 'A_Attach_To_Post_Ajax' 
  72. ); 
  73.  
  74. // Applies a post hook to the generate_thumbnail method of the 
  75. // gallery storage component 
  76. $this->get_registry()->add_adapter( 
  77. 'I_Gallery_Storage', 'A_Gallery_Storage_Frame_Event' 
  78. ); 
  79.  
  80. function does_request_require_frame_communication() 
  81. return (strpos($_SERVER['REQUEST_URI'], 'attach_to_post') !== FALSE OR strpos($_SERVER['HTTP_REFERER'], 'attach_to_post') !== FALSE OR array_key_exists('attach_to_post', $_REQUEST)); 
  82.  
  83.  
  84. function _register_hooks() 
  85. add_action('ngg_routes', array(&$this, 'define_routes'), 2); 
  86.  
  87. // We use two hooks here because we need it to execute for both the post-new.php 
  88. // page and ATP interface 
  89. add_action('plugins_loaded', array(&$this, 'fix_ie11'), 1); 
  90. add_action('admin_init', array(&$this, 'fix_ie11'), PHP_INT_MAX-1); 
  91. add_action('admin_enqueue_scripts', array(&$this, 'fix_ie11'), 1); 
  92. add_action('admin_enqueue_scripts', array(&$this, 'fix_ie11'), PHP_INT_MAX-1); 
  93. add_filter('wpseo_pre_analysis_post_content', array(&$this, 'remove_preview_images_from_yoast_opengraph')); 
  94. add_filter('wpseo_sitemap_urlimages', array(&$this, 'remove_preview_images_from_yoast_sitemap'), NULL, 2); 
  95.  
  96. // Emit frame communication events 
  97. if ($this->does_request_require_frame_communication()) { 
  98. add_action('ngg_created_new_gallery', array(&$this, 'new_gallery_event')); 
  99. add_action('ngg_after_new_images_added', array(&$this, 'images_added_event')); 
  100. add_action('ngg_page_event', array(&$this, 'nextgen_page_event')); 
  101. add_action('ngg_manage_tags', array(&$this, 'manage_tags_event')); 
  102.  
  103. // Admin-only hooks 
  104. if (is_admin()) { 
  105. add_action( 
  106. 'admin_enqueue_scripts',  
  107. array(&$this, 'enqueue_static_resources'),  
  108. ); 
  109.  
  110. add_action('admin_init', array(&$this, 'route_insert_gallery_window')); 
  111.  
  112. // TODO: In 2.0.69, we're going to change the ATP placeholder urls 
  113. if (FALSE) { 
  114. add_filter('the_editor_content', array($this, 'fix_preview_images')); 
  115.  
  116. add_action('media_buttons', array($this, 'add_media_button'), 15); 
  117.  
  118. // Frontend-only hooks 
  119. if (!is_admin()) { 
  120. // Add hook to subsitute displayed gallery placeholders 
  121. add_filter('the_content', array(&$this, 'substitute_placeholder_imgs'), PHP_INT_MAX, 1); 
  122.  
  123. /** 
  124. * Removes IGW preview/placeholder images from Yoast OpenGraph analysis 
  125. * @param $content 
  126. * @return mixed 
  127. */ 
  128. function remove_preview_images_from_yoast_opengraph($content) 
  129. $content = $this->fix_preview_images($content); 
  130. $content = preg_replace('/http(s)?:\/\/(.*?)'.NGG_ATTACH_TO_POST_SLUG.'\/preview\/id--\d+/', "", $content); 
  131. $content = preg_replace('/http(s)?:\/\/(.*?)'.NGG_ATTACH_TO_POST_SLUG.'\/preview\/id\/\d+/', "", $content); 
  132. return $content; 
  133.  
  134. /** 
  135. * Removes IGW preview/placeholder images from Yoast's sitemap 
  136. * @param $images 
  137. * @param $post_id 
  138. * @return array 
  139. */ 
  140. function remove_preview_images_from_yoast_sitemap($images, $post_id) 
  141. $retval = array(); 
  142.  
  143. foreach ($images as $image) { 
  144. if (strpos($image['src'], NGG_ATTACH_TO_POST_SLUG) === FALSE) { 
  145. $retval[] = $image; 
  146. else { 
  147. // Lookup images for the displayed gallery 
  148. if (preg_match('/(\d+)$/', $image['src'], $match)) { 
  149. $displayed_gallery_id = $match[1]; 
  150. $mapper = C_Displayed_Gallery_Mapper::get_instance(); 
  151. $displayed_gallery = $mapper->find($displayed_gallery_id, TRUE); 
  152. if ($displayed_gallery) { 
  153. $gallery_storage = C_Gallery_Storage::get_instance(); 
  154. $settings = C_NextGen_Settings::get_instance(); 
  155. $source_obj = $displayed_gallery->get_source(); 
  156. if (in_array('image', $source_obj->returns)) { 
  157. foreach ($displayed_gallery->get_entities() as $image) { 
  158. $named_image_size = $settings->imgAutoResize ? 'full' : 'thumb'; 
  159.  
  160. $sitemap_image = array( 
  161. 'src' => $gallery_storage->get_image_url($image, $named_image_size),  
  162. 'alt' => $image->alttext,  
  163. 'title' => $image->description ? $image->description: $image->alttext 
  164. ); 
  165. $retval[] = $sitemap_image; 
  166.  
  167.  
  168. return $retval; 
  169.  
  170. /** 
  171. * In 2.0.66.X and earlier, ATP placeholder images used a different url than 
  172. * what 2.0.69 uses. Therefore, we need to convert those 
  173. * @param $content 
  174. * @return mixed 
  175. */ 
  176. function fix_preview_images($content) 
  177. $content = str_replace( 
  178. site_url('/'.NGG_ATTACH_TO_POST_SLUG.'/preview/id--'),  
  179. admin_url('/?'.NGG_ATTACH_TO_POST_SLUG.'='.NGG_ATTACH_TO_POST_SLUG.'/preview/id--'),  
  180. $content 
  181. ); 
  182.  
  183. $content = str_replace( 
  184. site_url('/index.php/'.NGG_ATTACH_TO_POST_SLUG.'/preview/id--'),  
  185. admin_url('/?'.NGG_ATTACH_TO_POST_SLUG.'='.NGG_ATTACH_TO_POST_SLUG.'/preview/id--'),  
  186. $content 
  187. ); 
  188.  
  189. return $content; 
  190.  
  191. function add_media_button() 
  192. $router = C_Router::get_instance(); 
  193.  
  194. echo '<a href="#" data-editor="content" class="button ngg-add-gallery" id="ngg-media-button" class="button" onclick="tinymce.EditorManager.execCommand(\'ngg_attach_to_post\'); return false;"><img src="' . $router->get_static_url('photocrati-attach_to_post#atp_button.png') . '" style="padding:0; margin-top:-3px;"> ' . __('Add Gallery', 'nggallery') . '</a>'; 
  195.  
  196. /** 
  197. * Route the IGW requests using wp-admin 
  198. * @throws E_Clean_Exit 
  199. */ 
  200. function route_insert_gallery_window() 
  201. if (isset($_REQUEST[NGG_ATTACH_TO_POST_SLUG])) { 
  202. $controller = C_Attach_Controller::get_instance(); 
  203. if ($_REQUEST[NGG_ATTACH_TO_POST_SLUG] == 'js') { 
  204. $controller->display_tab_js_action(); 
  205. elseif (strpos($_REQUEST[NGG_ATTACH_TO_POST_SLUG], '/preview') !== FALSE) { 
  206. $controller->preview_action(); 
  207. else { 
  208. $controller->index_action(); 
  209.  
  210. throw new E_Clean_Exit; 
  211.  
  212. function define_routes($router) 
  213. $app = $router->create_app('/'.NGG_ATTACH_TO_POST_SLUG); 
  214. $app->rewrite('/preview/{id}', '/preview/id--{id}'); 
  215. $app->rewrite('/display_tab_js/{id}', '/display_tab_js/id--{id}'); 
  216. $app->route('/preview', 'I_Attach_To_Post_Controller#preview'); 
  217. $app->route('/display_tab_js', 'I_Attach_To_Post_Controller#display_tab_js'); 
  218. $app->route('/', 'I_Attach_To_Post_Controller#index'); 
  219.  
  220. /** 
  221. * WordPress sets the X-UA-Compatible header to IE=edge. Unfortunately, this causes problems with Plupload,  
  222. * so we have the send this header 
  223. */ 
  224. function fix_ie11() 
  225. if ((array_search('attach_to_post', array_keys($_REQUEST)) !== FALSE OR strpos($_SERVER['REQUEST_URI'], NGG_ATTACH_TO_POST_SLUG) !== FALSE OR strpos($_SERVER['REQUEST_URI'], 'wp-admin/post.php') !== FALSE OR strpos($_SERVER['REQUEST_URI'], 'wp-admin/post-new.php') !== FALSE)) { 
  226. if (!headers_sent()) { 
  227. header('X-UA-Compatible: IE=EmulateIE10'); 
  228.  
  229. /** 
  230. * Substitutes the gallery placeholder content with the gallery type frontend 
  231. * view, returns a list of static resources that need to be loaded 
  232. * @param string $content 
  233. */ 
  234. function substitute_placeholder_imgs($content) 
  235. $content = $this->fix_preview_images($content); 
  236.  
  237. // To match ATP entries we compare the stored url against a generic path; entries MUST have a gallery ID 
  238. if (preg_match_all("#<img.*http(s)?://(.*)?".NGG_ATTACH_TO_POST_SLUG."(=|/)preview(/|&|&)id(=|--)(\\d+).*?>#mi", $content, $matches, PREG_SET_ORDER)) { 
  239.  
  240. $mapper = C_Displayed_Gallery_Mapper::get_instance(); 
  241. foreach ($matches as $match) { 
  242. // Find the displayed gallery 
  243. $displayed_gallery_id = $match[6]; 
  244. $displayed_gallery = $mapper->find($displayed_gallery_id, TRUE); 
  245.  
  246. // Get the content for the displayed gallery 
  247. $retval = '<p>'._('Invalid Displayed Gallery').'</p>'; 
  248. if ($displayed_gallery) { 
  249. $retval = ''; 
  250. $renderer = C_Displayed_Gallery_Renderer::get_instance(); 
  251. if (defined('NGG_SHOW_DISPLAYED_GALLERY_ERRORS') && NGG_SHOW_DISPLAYED_GALLERY_ERRORS && $displayed_gallery->is_invalid()) { 
  252. $retval .= var_export($displayed_gallery->get_errors(), TRUE); 
  253. if (self::$substitute_placeholders) $retval .= $renderer->render($displayed_gallery, TRUE); 
  254.  
  255. $content = str_replace($match[0], $retval, $content); 
  256.  
  257. return $content; 
  258.  
  259.  
  260. /** 
  261. * Enqueues static resources required by the Attach to Post interface 
  262. */ 
  263. function enqueue_static_resources() 
  264. $router = C_Router::get_instance(); 
  265.  
  266. // Enqueue resources needed at post/page level 
  267. if (preg_match("/\/wp-admin\/(post|post-new)\.php$/", $_SERVER['SCRIPT_NAME'])) { 
  268. $this->_enqueue_tinymce_resources(); 
  269. wp_enqueue_style( 
  270. 'ngg_attach_to_post_dialog',  
  271. $router->get_static_url('photocrati-attach_to_post#attach_to_post_dialog.css'),  
  272. FALSE,  
  273. NGG_SCRIPT_VERSION 
  274. ); 
  275.  
  276. elseif (isset($_REQUEST['attach_to_post']) OR 
  277. (isset($_REQUEST['page']) && strpos($_REQUEST['page'], 'nggallery') !== FALSE)) { 
  278. wp_enqueue_script('iframely', $router->get_static_url('photocrati-attach_to_post#iframely.js'), FALSE, NGG_SCRIPT_VERSION); 
  279. wp_enqueue_style('iframely', $router->get_static_url('photocrati-attach_to_post#iframely.css'), FALSE, NGG_SCRIPT_VERSION); 
  280.  
  281.  
  282. /** 
  283. * Enqueues resources needed by the TinyMCE editor 
  284. */ 
  285. function _enqueue_tinymce_resources() 
  286. wp_localize_script( 
  287. 'media-editor',  
  288. 'nextgen_gallery_attach_to_post_url',  
  289. C_NextGen_Settings::get_instance()->attach_to_post_url 
  290. ); 
  291.  
  292. // Registers our tinymce button and plugin for attaching galleries 
  293. $security = $this->get_registry()->get_utility('I_Security_Manager'); 
  294. $sec_actor = $security->get_current_actor(); 
  295. $checks = array( 
  296. $sec_actor->is_allowed('NextGEN Attach Interface'),  
  297. $sec_actor->is_allowed('NextGEN Use TinyMCE') 
  298. ); 
  299. if (!in_array(FALSE, $checks)) { 
  300. if (get_user_option('rich_editing') == 'true') { 
  301. add_filter('mce_buttons', array(&$this, 'add_attach_to_post_button')); 
  302. add_filter('mce_external_plugins', array(&$this, 'add_attach_to_post_tinymce_plugin')); 
  303. add_filter('wp_mce_translation', array($this, 'add_attach_to_post_tinymce_i18n')); 
  304.  
  305.  
  306. /** 
  307. * Adds a TinyMCE button for the Attach To Post plugin 
  308. * @param array $buttons 
  309. * @returns array 
  310. */ 
  311. function add_attach_to_post_button($buttons) 
  312. array_push( 
  313. $buttons,  
  314. 'separator',  
  315. $this->attach_to_post_tinymce_plugin 
  316. ); 
  317. return $buttons; 
  318.  
  319.  
  320. /** 
  321. * Adds the Attach To Post TinyMCE plugin 
  322. * @param array $plugins 
  323. * @return array 
  324. * @uses mce_external_plugins filter 
  325. */ 
  326. function add_attach_to_post_tinymce_plugin($plugins) 
  327. global $wp_version; 
  328. $router = C_Router::get_instance(); 
  329.  
  330. wp_enqueue_script('photocrati_ajax'); 
  331.  
  332. if ($wp_version >= 3.9) 
  333. $file = $router->get_static_url('photocrati-attach_to_post#ngg_attach_to_post_tinymce_plugin.js'); 
  334. else 
  335. $file = $router->get_static_url('photocrati-attach_to_post#ngg_attach_to_post_tinymce_plugin_wp38_compat.js'); 
  336.  
  337. $plugins[$this->attach_to_post_tinymce_plugin] = $file; 
  338. return $plugins; 
  339.  
  340.  
  341. /** 
  342. * Adds the Attach To Post TinyMCE i18n strings 
  343. * @param $mce_translation 
  344. * @return mixed 
  345. */ 
  346. function add_attach_to_post_tinymce_i18n($mce_translation) 
  347. $mce_translation['ngg_attach_to_post.title'] = __('Attach NextGEN Gallery to Post', 'nggallery'); 
  348. return $mce_translation; 
  349.  
  350.  
  351. /** 
  352. * Notify frames that a new gallery has been created 
  353. * @param int $gallery_id 
  354. */ 
  355. function new_gallery_event($gallery_id) 
  356. $gallery = C_Gallery_Mapper::get_instance()->find($gallery_id); 
  357. if ($gallery) { 
  358. $this->_get_frame_event_publisher()->add_event(array( 
  359. 'event' => 'new_gallery',  
  360. 'gallery_id'=> intval($gallery_id),  
  361. 'gallery_title' => $gallery->title 
  362. )); 
  363.  
  364. /** 
  365. * Notifies a frame that images have been added to a gallery 
  366. * @param int $gallery_id 
  367. * @param array $image_ids 
  368. */ 
  369. function images_added_event($gallery_id, $image_ids=array()) 
  370. $this->_get_frame_event_publisher()->add_event(array( 
  371. 'event' => 'images_added',  
  372. 'gallery_id' => intval($gallery_id) 
  373. )); 
  374.  
  375. /** 
  376. * Notifies a frame that the tags have changed 
  377. * @param array $tags 
  378. */ 
  379. function manage_tags_event($tags = array()) 
  380. $this->_get_frame_event_publisher()->add_event(array( 
  381. 'event' => 'manage_tags',  
  382. 'tags' => $tags 
  383. )); 
  384.  
  385. /** 
  386. * Notifies a frame that an action has been performed on a particular 
  387. * NextGEN page 
  388. * @param array $event 
  389. */ 
  390. function nextgen_page_event($event) 
  391. $this->_get_frame_event_publisher()->add_event($event); 
  392.  
  393. function get_type_list() 
  394. return array( 
  395. 'A_Attach_To_Post_Ajax' => 'adapter.attach_to_post_ajax.php',  
  396. 'C_Attach_To_Post_Installer' => 'class.attach_to_post_installer.php',  
  397. 'A_Gallery_Storage_Frame_Event' => 'adapter.gallery_storage_frame_event.php',  
  398. 'C_Attach_Controller' => 'class.attach_controller.php',  
  399. 'C_Attach_To_Post_Proxy_Controller' => 'class.attach_to_post_proxy_controller.php',  
  400. 'Mixin_Attach_To_Post_Display_Tab' => 'mixin.attach_to_post_display_tab.php' 
  401. );