M_Gallery_Display

The NextGEN Gallery M Gallery Display class.

Defined (1)

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

/products/photocrati_nextgen/modules/nextgen_gallery_display/module.nextgen_gallery_display.php  
  1. class M_Gallery_Display extends C_Base_Module 
  2. function define($id = 'pope-module',  
  3. $name = 'Pope Module',  
  4. $description = '',  
  5. $version = '',  
  6. $uri = '',  
  7. $author = '',  
  8. $author_uri = '',  
  9. $context = FALSE) 
  10. parent::define( 
  11. 'photocrati-nextgen_gallery_display',  
  12. 'Gallery Display',  
  13. 'Provides the ability to display gallery of images',  
  14. '0.17',  
  15. 'https://www.imagely.com/wordpress-gallery-plugin/nextgen-gallery/',  
  16. 'Imagely',  
  17. 'https://www.imagely.com' 
  18. ); 
  19.  
  20. C_Photocrati_Installer::add_handler($this->module_id, 'C_Display_Type_Installer'); 
  21.  
  22.  
  23. /** 
  24. * Register utilities required for this module 
  25. */ 
  26. function _register_utilities() 
  27. // Register frontend-only components 
  28. if (apply_filters('ngg_load_frontend_logic', TRUE, $this->module_id)) 
  29. // This utility provides a controller to render the settings form 
  30. // for a display type, or render the front-end of a display type 
  31. $this->get_registry()->add_utility( 
  32. 'I_Display_Type_Controller',  
  33. 'C_Display_Type_Controller' 
  34. ); 
  35.  
  36. // This utility provides the capabilities of rendering a display type 
  37. $this->get_registry()->add_utility( 
  38. 'I_Displayed_Gallery_Renderer',  
  39. 'C_Displayed_Gallery_Renderer' 
  40. ); 
  41.  
  42. // This utility provides a datamapper for Display Types 
  43. $this->get_registry()->add_utility( 
  44. 'I_Display_Type_Mapper',  
  45. 'C_Display_Type_Mapper' 
  46. ); 
  47.  
  48. // This utility provides a datamapper for Displayed Galleries. A 
  49. // displayed gallery is the association between some entities (images 
  50. //or galleries) and a display type 
  51. $this->get_registry()->add_utility( 
  52. 'I_Displayed_Gallery_Mapper',  
  53. 'C_Displayed_Gallery_Mapper' 
  54. ); 
  55.  
  56. /** 
  57. * Registers adapters required for this module 
  58. */ 
  59. function _register_adapters() 
  60. // Provides factory methods for creating display type and 
  61. // displayed gallery instances 
  62. $this->get_registry()->add_adapter( 
  63. 'I_Component_Factory', 'A_Gallery_Display_Factory' 
  64. ); 
  65.  
  66. if (is_admin()) { 
  67. $this->get_registry()->add_adapter( 
  68. 'I_Page_Manager',  
  69. 'A_Display_Settings_Page' 
  70. ); 
  71.  
  72. $this->get_registry()->add_adapter( 
  73. 'I_NextGen_Admin_Page',  
  74. 'A_Display_Settings_Controller',  
  75. NGG_DISPLAY_SETTINGS_SLUG 
  76. ); 
  77.  
  78. // Frontend-only components 
  79. if (apply_filters('ngg_load_frontend_logic', TRUE, $this->module_id)) 
  80. $this->get_registry()->add_adapter('I_MVC_View', 'A_Gallery_Display_View'); 
  81. $this->get_registry()->add_adapter('I_MVC_View', 'A_Displayed_Gallery_Trigger_Element'); 
  82. $this->get_registry()->add_adapter('I_Display_Type_Controller', 'A_Displayed_Gallery_Trigger_Resources'); 
  83.  
  84. /** 
  85. * Registers hooks for the WordPress framework 
  86. */ 
  87. function _register_hooks() 
  88. if (apply_filters('ngg_load_frontend_logic', TRUE, $this->module_id)) 
  89. C_NextGen_Shortcode_Manager::add('ngg_images', array(&$this, 'display_images')); 
  90. add_action('wp_enqueue_scripts', array(&$this, 'no_resources_mode'), PHP_INT_MAX-1); 
  91. add_filter('the_content', array($this, '_render_related_images')); 
  92.  
  93. add_action('init', array(&$this, 'register_resources'), 12); 
  94. add_action('admin_bar_menu', array(&$this, 'add_admin_bar_menu'), 100); 
  95. add_filter('run_ngg_resource_manager', array(&$this, 'no_resources_mode')); 
  96. add_action('init', array(&$this, 'serve_fontawesome'), 15); 
  97.  
  98. // Add hook to delete displayed galleries when removed from a post 
  99. add_action('pre_post_update', array(&$this, 'locate_stale_displayed_galleries')); 
  100. add_action('before_delete_post', array(&$this, 'locate_stale_displayed_galleries')); 
  101. add_action('post_updated', array(&$this, 'cleanup_displayed_galleries')); 
  102. add_action('after_delete_post', array(&$this, 'cleanup_displayed_galleries')); 
  103.  
  104. add_action('wp_print_styles', array($this, 'fix_nextgen_custom_css_order'), PHP_INT_MAX-1); 
  105.  
  106. /** 
  107. * This moves the NextGen custom CSS to the last of the queue 
  108. */ 
  109. function fix_nextgen_custom_css_order() 
  110. global $wp_styles; 
  111. if (in_array('nggallery', $wp_styles->queue)) 
  112. foreach ($wp_styles->queue as $ndx => $style) { 
  113. if ($style == 'nggallery') 
  114. unset($wp_styles->queue[$ndx]); 
  115. $wp_styles->queue[] = 'nggallery'; 
  116. break; 
  117.  
  118. /** 
  119. * Locates the ids of displayed galleries that have been 
  120. * removed from the post, and flags then for cleanup (deletion) 
  121. * @global array $displayed_galleries_to_cleanup 
  122. * @param int $post_id 
  123. */ 
  124. function locate_stale_displayed_galleries($post_id) 
  125. global $displayed_galleries_to_cleanup; 
  126. $displayed_galleries_to_cleanup = array(); 
  127. $post = get_post($post_id); 
  128. $gallery_preview_url = C_NextGen_Settings::get_instance()->get('gallery_preview_url'); 
  129. $preview_url = preg_quote($gallery_preview_url, '#'); 
  130. if (preg_match_all("#{$preview_url}/id--(\d+)#", html_entity_decode($post->post_content), $matches, PREG_SET_ORDER)) { 
  131. foreach ($matches as $match) { 
  132. $preview_url = preg_quote($match[0], '/'); 
  133. // The post was edited, and the displayed gallery placeholder was removed 
  134. if (isset($_REQUEST['post_content']) && (!preg_match("/{$preview_url}/", $_POST['post_content']))) { 
  135. $displayed_galleries_to_cleanup[] = intval($match[1]); 
  136. // The post was deleted 
  137. elseif (!isset($_REQUEST['action'])) { 
  138. $displayed_galleries_to_cleanup[] = intval($match[1]); 
  139.  
  140. /** 
  141. * Deletes any displayed galleries that are no longer associated with a post/page 
  142. * @global array $displayed_galleries_to_cleanup 
  143. * @param int $post_id 
  144. */ 
  145. function cleanup_displayed_galleries($post_id) 
  146. if (!apply_filters('ngg_cleanup_displayed_galleries', true, $post_id)) 
  147. return; 
  148.  
  149. global $displayed_galleries_to_cleanup; 
  150. $mapper = C_Displayed_Gallery_Mapper::get_instance(); 
  151. foreach ($displayed_galleries_to_cleanup as $id) { 
  152. $mapper->destroy($id); 
  153.  
  154. /** 
  155. * Serves the fontawesome woff file via PHP. We do this, as IIS won't serve .woff files. 
  156. * @throws E_Clean_Exit 
  157. */ 
  158. function serve_fontawesome() 
  159. if (isset($_REQUEST['ngg_serve_fontawesome_woff'])) { 
  160. $fs = C_Fs::get_instance(); 
  161. $abspath = $fs->find_static_abspath('photocrati-nextgen_gallery_display#fonts/fontawesome-webfont.woff'); 
  162. if ($abspath) { 
  163. header("Content-Type: application/x-font-woff"); 
  164. readfile($abspath); 
  165. throw new E_Clean_Exit(); 
  166. elseif (isset($_REQUEST['ngg_serve_fontawesome_css'])) { 
  167. $fs = C_Fs::get_instance(); 
  168. $abspath = $fs->find_static_abspath('photocrati-nextgen_gallery_display#fontawesome/font-awesome.css'); 
  169. if ($abspath) { 
  170. $router = C_Router::get_instance(); 
  171. $file_content = file_get_contents($abspath); 
  172. $file_content = str_replace('../fonts/fontawesome-webfont.eot', $router->get_static_url($this->module_id . '#fonts/fontawesome-webfont.eot'), $file_content); 
  173. $file_content = str_replace('../fonts/fontawesome-webfont.svg', $router->get_static_url($this->module_id . '#fonts/fontawesome-webfont.svg'), $file_content); 
  174. $file_content = str_replace('../fonts/fontawesome-webfont.ttf', $router->get_static_url($this->module_id . '#fonts/fontawesome-webfont.ttf'), $file_content); 
  175. $file_content = str_replace('../fonts/fontawesome-webfont.woff2', $router->get_static_url($this->module_id . '#fonts/fontawesome-webfont.woff2'), $file_content); 
  176. $file_content = str_replace('../fonts/fontawesome-webfont.woff', site_url('/?ngg_serve_fontawesome_woff=1'), $file_content); 
  177. header('Content-Type: text/css'); 
  178. echo $file_content; 
  179. throw new E_Clean_Exit(); 
  180.  
  181. /** 
  182. * Enqueues fontawesome. First checks to see if fontawesome is provided by another plugin or already enqueued,  
  183. * and if not, enqueues a version of fontawesome that will work with or without IIS 
  184. */ 
  185. static function enqueue_fontawesome() 
  186. if (!wp_style_is('fontawesome', 'registered')) 
  187. wp_enqueue_style( 
  188. 'fontawesome',  
  189. self::get_fontawesome_url(TRUE),  
  190. FALSE,  
  191. '4.6.1' 
  192. ); 
  193.  
  194. wp_enqueue_style('fontawesome'); 
  195.  
  196. /** 
  197. * Gets the src url for the registered fontawesome handler 
  198. * @param bool $ngg_provided_only 
  199. * @return null|string|void 
  200. */ 
  201. static function get_fontawesome_url($ngg_provided_only=FALSE) 
  202. $retval = NULL; 
  203.  
  204. if (wp_style_is('fontawesome', 'registered') && !$ngg_provided_only) { 
  205. $style = wp_styles()->registered['fontawesome']; 
  206. $retval = $style->src; 
  207. else { 
  208. if (strpos(strtolower($_SERVER['SERVER_SOFTWARE']), 'microsoft-iis') !== FALSE) { 
  209. $retval = site_url('/?ngg_serve_fontawesome_css=1'); 
  210. else { 
  211. $router = C_Router::get_instance(); 
  212. $retval = $router->get_static_url('photocrati-nextgen_gallery_display#fontawesome/font-awesome.css'); 
  213.  
  214. return $retval; 
  215.  
  216. function no_resources_mode($valid_request=TRUE) 
  217. if (isset($_REQUEST['ngg_no_resources'])) { 
  218. global $wp_scripts, $wp_styles; 
  219.  
  220. // Don't enqueue any stylesheets 
  221. if ($wp_scripts) 
  222. $wp_scripts->queue = $wp_styles->queue = array(); 
  223.  
  224. // Don't run the resource manager 
  225. $valid_request = FALSE; 
  226.  
  227. return $valid_request; 
  228.  
  229. static function _render_related_string($sluglist=array(), $maxImages=NULL, $type=NULL) 
  230. $settings = C_NextGen_Settings::get_instance(); 
  231. if (is_null($type)) $type = $settings->appendType; 
  232. if (is_null($maxImages)) $maxImages = $settings->maxImages; 
  233.  
  234. if (!$sluglist) { 
  235. switch ($type) { 
  236. case 'tags': 
  237. if (function_exists('get_the_tags')) 
  238. $taglist = get_the_tags(); 
  239. if (is_array($taglist)) { 
  240. foreach ($taglist as $tag) { 
  241. $sluglist[] = $tag->slug; 
  242. break; 
  243. case 'category': 
  244. $catlist = get_the_category(); 
  245. if (is_array($catlist)) 
  246. foreach ($catlist as $cat) { 
  247. $sluglist[] = $cat->category_nicename; 
  248. break; 
  249.  
  250. $taglist = implode(', ', $sluglist); 
  251.  
  252. if ($taglist === 'uncategorized' || empty($taglist)) 
  253. return; 
  254.  
  255. $renderer = C_Displayed_Gallery_Renderer::get_instance(); 
  256. $view = C_Component_Factory::get_instance()->create('mvc_view', ''); 
  257. $retval = $renderer->display_images(array( 
  258. 'source' => 'tags',  
  259. 'container_ids' => $taglist,  
  260. 'display_type' => NGG_BASIC_THUMBNAILS,  
  261. 'images_per_page' => $maxImages,  
  262. 'maximum_entity_count' => $maxImages,  
  263. 'template' => $view->get_template_abspath('photocrati-nextgen_gallery_display#related'),  
  264. 'show_all_in_lightbox' => FALSE,  
  265. 'show_slideshow_link' => FALSE,  
  266. 'disable_pagination' => TRUE,  
  267. 'display_no_images_error' => FALSE 
  268. )); 
  269.  
  270. if ($retval) wp_enqueue_style('nextgen_gallery_related_images'); 
  271.  
  272. return apply_filters('ngg_show_related_gallery_content', $retval, $taglist); 
  273.  
  274. function _render_related_images($content) 
  275. $settings = C_NextGen_Settings::get_instance(); 
  276.  
  277. if ($settings->get('activateTags')) { 
  278. $related = self::_render_related_string(); 
  279.  
  280. if ($related != null) { 
  281. $heading = $settings->relatedHeading; 
  282. $content .= $heading . $related; 
  283.  
  284. return $content; 
  285.  
  286. /** 
  287. * Adds menu item to the admin bar 
  288. */ 
  289. function add_admin_bar_menu() 
  290. global $wp_admin_bar; 
  291.  
  292. if ( current_user_can('NextGEN Change options') ) { 
  293. $wp_admin_bar->add_menu(array( 
  294. 'parent' => 'ngg-menu',  
  295. 'id' => 'ngg-menu-display_settings',  
  296. 'title' => __('Gallery Settings', 'nggallery'),  
  297. 'href' => admin_url('admin.php?page=ngg_display_settings') 
  298. )); 
  299.  
  300. /** 
  301. * Registers our static settings resources so the ATP module can find them later 
  302. */ 
  303. function register_resources() 
  304. // Register custom post types for compatibility 
  305. $types = array( 
  306. 'displayed_gallery' => 'NextGEN Gallery - Displayed Gallery',  
  307. 'display_type' => 'NextGEN Gallery - Display Type',  
  308. 'gal_display_source' => 'NextGEN Gallery - Displayed Gallery Source' 
  309. ); 
  310. foreach ($types as $type => $label) { 
  311. register_post_type($type, array( 
  312. 'label' => $label,  
  313. 'publicly_queryable' => FALSE,  
  314. 'exclude_from_search' => TRUE,  
  315. )); 
  316. $router = C_Router::get_instance(); 
  317.  
  318. wp_register_script( 
  319. 'nextgen_gallery_display_settings',  
  320. $router->get_static_url('photocrati-nextgen_gallery_display#nextgen_gallery_display_settings.js'),  
  321. array('jquery-ui-accordion', 'jquery-ui-tooltip'),  
  322. NGG_SCRIPT_VERSION 
  323. ); 
  324.  
  325. wp_register_style( 
  326. 'nextgen_gallery_display_settings',  
  327. $router->get_static_url('photocrati-nextgen_gallery_display#nextgen_gallery_display_settings.css'),  
  328. FALSE,  
  329. NGG_SCRIPT_VERSION 
  330. ); 
  331.  
  332. if (apply_filters('ngg_load_frontend_logic', TRUE, $this->module_id)) 
  333. wp_register_style( 
  334. 'nextgen_gallery_related_images',  
  335. $router->get_static_url('photocrati-nextgen_gallery_display#nextgen_gallery_related_images.css'),  
  336. FALSE,  
  337. NGG_SCRIPT_VERSION 
  338. ); 
  339. wp_register_script( 
  340. 'ngg_common',  
  341. $router->get_static_url('photocrati-nextgen_gallery_display#common.js'),  
  342. array('jquery', 'photocrati_ajax'),  
  343. NGG_SCRIPT_VERSION,  
  344. TRUE 
  345. ); 
  346. wp_register_style( 
  347. 'ngg_trigger_buttons',  
  348. $router->get_static_url('photocrati-nextgen_gallery_display#trigger_buttons.css'),  
  349. FALSE,  
  350. NGG_SCRIPT_VERSION 
  351. ); 
  352.  
  353.  
  354. /** 
  355. * Adds the display settings page to wp-admin 
  356. */ 
  357. function add_display_settings_page() 
  358. add_submenu_page( 
  359. NGGFOLDER,  
  360. __('NextGEN Gallery & Album Settings', 'nggallery'),  
  361. __('Gallery Settings', 'nggallery'),  
  362. 'NextGEN Change options',  
  363. NGG_DISPLAY_SETTINGS_SLUG,  
  364. array(&$this->controller, 'index_action') 
  365. ); 
  366.  
  367. /** 
  368. * Provides the [display_images] shortcode 
  369. * @param array $params 
  370. * @param string $inner_content 
  371. * @return string 
  372. */ 
  373. function display_images($params, $inner_content=NULL) 
  374. $renderer = C_Displayed_Gallery_Renderer::get_instance(); 
  375. return $renderer->display_images($params, $inner_content); 
  376.  
  377. /** 
  378. * Gets a value from the parameter array, and if not available, uses the default value 
  379. * @param string $name 
  380. * @param mixed $default 
  381. * @param array $params 
  382. * @return mixed 
  383. */ 
  384. function _get_param($name, $default, $params) 
  385. return (isset($params[$name])) ? $params[$name] : $default; 
  386.  
  387. function get_type_list() 
  388. return array( 
  389. 'C_Displayed_Gallery_Trigger' => 'class.displayed_gallery_trigger.php',  
  390. 'C_Displayed_Gallery_Trigger_Manager' => 'class.displayed_gallery_trigger_manager.php',  
  391. 'A_Displayed_Gallery_Trigger_Element' => 'adapter.displayed_gallery_trigger_element.php',  
  392. 'A_Displayed_Gallery_Trigger_Resources' => 'adapter.displayed_gallery_trigger_resources.php',  
  393. 'A_Display_Settings_Controller' => 'adapter.display_settings_controller.php',  
  394. 'A_Display_Settings_Page' => 'adapter.display_settings_page.php',  
  395. 'A_Gallery_Display_Factory' => 'adapter.gallery_display_factory.php',  
  396. 'C_Display_Type_Installer' => 'class.gallery_display_installer.php',  
  397. 'A_Gallery_Display_View' => 'adapter.gallery_display_view.php',  
  398. 'C_Displayed_Gallery' => 'class.displayed_gallery.php',  
  399. 'C_Displayed_Gallery_Mapper' => 'class.displayed_gallery_mapper.php',  
  400. 'C_Displayed_Gallery_Renderer' => 'class.displayed_gallery_renderer.php',  
  401. 'C_Displayed_Gallery_Source_Manager' => 'class.displayed_gallery_source_manager.php',  
  402. 'C_Display_Type' => 'class.display_type.php',  
  403. 'C_Display_Type_Controller' => 'class.display_type_controller.php',  
  404. 'C_Display_Type_Mapper' => 'class.display_type_mapper.php',  
  405. 'Hook_Propagate_Thumbnail_Dimensions_To_Settings' => 'hook.propagate_thumbnail_dimensions_to_settings.php',  
  406. 'Mixin_Display_Type_Form' => 'mixin.display_type_form.php' 
  407. ); 
  408.  
  409. /** 
  410. * Gets a list of directories in which display type template might be stored 
  411. * @param C_Display_Type $display_type 
  412. * @return array 
  413. */ 
  414. static function get_display_type_view_dirs($display_type) 
  415. if (!is_object($display_type)) $display_type = C_Display_Type_Mapper::get_instance()->find_by_name($display_type); 
  416.  
  417. /** Create array of directories to scan */ 
  418. $dirs = array( 
  419. 'default' => C_Component_Registry::get_instance()->get_module_dir($display_type->name) . DIRECTORY_SEPARATOR . 'templates',  
  420. 'custom' => WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'ngg' . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . $display_type->name . DIRECTORY_SEPARATOR . 'templates',  
  421. ); 
  422.  
  423. /** Apply filters so third party devs can add directories for their templates */ 
  424. $dirs = apply_filters('ngg_display_type_template_dirs', $dirs, $display_type); 
  425. $dirs = apply_filters('ngg_' . $display_type->name . '_template_dirs', $dirs, $display_type); 
  426. foreach ($display_type->aliases as $alias) { 
  427. $dirs = apply_filters("ngg_{$alias}_template_dirs", $dirs, $display_type); 
  428.  
  429. return $dirs; 
  430.