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