PageRenderTime 37ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/Wordpress-Project/wp-content/plugins/foogallery/includes/functions.php

https://bitbucket.org/hinzanhilmy/hinzan-sample-works
PHP | 997 lines | 515 code | 130 blank | 352 comment | 49 complexity | 52d2da21cce504bf062c5eb85a043c1c MD5 | raw file
Possible License(s): GPL-3.0, 0BSD, Apache-2.0, BSD-2-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1, GPL-2.0, BSD-3-Clause, MIT
  1. <?php
  2. /**
  3. * FooGallery global functions
  4. *
  5. * @package FooGallery
  6. * @author Brad Vincent <brad@fooplugins.com>
  7. * @license GPL-2.0+
  8. * @link https://github.com/fooplugins/foogallery
  9. * @copyright 2014 FooPlugins LLC
  10. */
  11. /**
  12. * Returns the name of the plugin. (Allows the name to be overridden from extensions or functions.php)
  13. * @return string
  14. */
  15. function foogallery_plugin_name() {
  16. return apply_filters( 'foogallery_plugin_name', 'FooGallery' );
  17. }
  18. /**
  19. * Return all the gallery templates used within FooGallery
  20. *
  21. * @return array
  22. */
  23. function foogallery_gallery_templates() {
  24. return apply_filters( 'foogallery_gallery_templates', array() );
  25. }
  26. /**
  27. * Return a specific gallery template based on the slug
  28. * @param $slug
  29. *
  30. * @return bool|array
  31. */
  32. function foogallery_get_gallery_template( $slug ) {
  33. foreach ( foogallery_gallery_templates() as $template ) {
  34. if ( $slug == $template['slug'] ) {
  35. return $template;
  36. }
  37. }
  38. return false;
  39. }
  40. /**
  41. * Return the FooGallery extension API class
  42. *
  43. * @return FooGallery_Extensions_API
  44. */
  45. function foogallery_extensions_api() {
  46. return new FooGallery_Extensions_API();
  47. }
  48. /**
  49. * Returns the default gallery template
  50. *
  51. * @return string
  52. */
  53. function foogallery_default_gallery_template() {
  54. return foogallery_get_setting( 'gallery_template' );
  55. }
  56. /**
  57. * Returns if gallery permalinks are enabled
  58. *
  59. * @return bool
  60. */
  61. function foogallery_permalinks_enabled() {
  62. return foogallery_get_setting( 'gallery_permalinks_enabled' );
  63. }
  64. /**
  65. * Returns the gallery permalink
  66. *
  67. * @return string
  68. */
  69. function foogallery_permalink() {
  70. return foogallery_get_setting( 'gallery_permalink' );
  71. }
  72. /**
  73. * Return the FooGallery saved setting, or a default value
  74. *
  75. * @param string $key The key for the setting
  76. *
  77. * @param bool $default The default if no value is saved or found
  78. *
  79. * @return mixed
  80. */
  81. function foogallery_get_setting( $key, $default = false ) {
  82. $foogallery = FooGallery_Plugin::get_instance();
  83. return $foogallery->options()->get( $key, foogallery_get_default( $key, $default ) );
  84. }
  85. /**
  86. * Builds up a FooGallery gallery shortcode
  87. *
  88. * @param $gallery_id
  89. *
  90. * @return string
  91. */
  92. function foogallery_build_gallery_shortcode( $gallery_id ) {
  93. return '[' . foogallery_gallery_shortcode_tag() . ' id="' . $gallery_id . '"]';
  94. }
  95. /**
  96. * Returns the gallery shortcode tag
  97. *
  98. * @return string
  99. */
  100. function foogallery_gallery_shortcode_tag() {
  101. return apply_filters( 'foogallery_gallery_shortcode_tag', FOOGALLERY_CPT_GALLERY );
  102. }
  103. /**
  104. * Helper method for getting default settings
  105. *
  106. * @param string $key The default config key to retrieve.
  107. *
  108. * @param bool $default The default if no default is set or found
  109. *
  110. * @return string Key value on success, false on failure.
  111. */
  112. function foogallery_get_default( $key, $default = false ) {
  113. $defaults = array(
  114. 'gallery_template' => 'default',
  115. 'gallery_permalinks_enabled' => false,
  116. 'gallery_permalink' => 'gallery',
  117. 'lightbox' => 'none',
  118. 'thumb_jpeg_quality' => '80',
  119. 'thumb_resize_animations' => true,
  120. 'gallery_sorting' => '',
  121. 'datasource' => 'media_library'
  122. );
  123. // A handy filter to override the defaults
  124. $defaults = apply_filters( 'foogallery_defaults', $defaults );
  125. // Return the key specified.
  126. return isset($defaults[ $key ]) ? $defaults[ $key ] : $default;
  127. }
  128. /**
  129. * Returns the FooGallery Add Gallery Url within the admin
  130. *
  131. * @return string The Url to the FooGallery Add Gallery page in admin
  132. */
  133. function foogallery_admin_add_gallery_url() {
  134. return admin_url( 'post-new.php?post_type=' . FOOGALLERY_CPT_GALLERY );
  135. }
  136. /**
  137. * Returns the FooGallery help page Url within the admin
  138. *
  139. * @return string The Url to the FooGallery help page in admin
  140. */
  141. function foogallery_admin_help_url() {
  142. return admin_url( add_query_arg( array( 'page' => FOOGALLERY_ADMIN_MENU_HELP_SLUG ), foogallery_admin_menu_parent_slug() ) );
  143. }
  144. /**
  145. * Returns the FooGallery settings page Url within the admin
  146. *
  147. * @return string The Url to the FooGallery settings page in admin
  148. */
  149. function foogallery_admin_settings_url() {
  150. return admin_url( add_query_arg( array( 'page' => FOOGALLERY_ADMIN_MENU_SETTINGS_SLUG ), foogallery_admin_menu_parent_slug() ) );
  151. }
  152. /**
  153. * Returns the FooGallery extensions page Url within the admin
  154. *
  155. * @return string The Url to the FooGallery extensions page in admin
  156. */
  157. function foogallery_admin_extensions_url() {
  158. return admin_url( add_query_arg( array( 'page' => FOOGALLERY_ADMIN_MENU_EXTENSIONS_SLUG ), foogallery_admin_menu_parent_slug() ) );
  159. }
  160. /**
  161. * Returns the FooGallery system info page Url within the admin
  162. *
  163. * @return string The Url to the FooGallery system info page in admin
  164. */
  165. function foogallery_admin_systeminfo_url() {
  166. return admin_url( add_query_arg( array( 'page' => FOOGALLERY_ADMIN_MENU_SYSTEMINFO_SLUG ), foogallery_admin_menu_parent_slug() ) );
  167. }
  168. /**
  169. * Get a foogallery template setting for the current foogallery that is being output to the frontend
  170. * @param string $key
  171. * @param string $default
  172. *
  173. * @return bool
  174. */
  175. function foogallery_gallery_template_setting( $key, $default = '' ) {
  176. global $current_foogallery;
  177. global $current_foogallery_arguments;
  178. global $current_foogallery_template;
  179. $settings_key = "{$current_foogallery_template}_{$key}";
  180. if ( $current_foogallery_arguments && array_key_exists( $key, $current_foogallery_arguments ) ) {
  181. //try to get the value from the arguments
  182. $value = $current_foogallery_arguments[ $key ];
  183. } else if ( !empty( $current_foogallery ) && $current_foogallery->settings && array_key_exists( $settings_key, $current_foogallery->settings ) ) {
  184. //then get the value out of the saved gallery settings
  185. $value = $current_foogallery->settings[ $settings_key ];
  186. } else {
  187. //otherwise set it to the default
  188. $value = $default;
  189. }
  190. $value = apply_filters( 'foogallery_gallery_template_setting-' . $key, $value );
  191. return $value;
  192. }
  193. /**
  194. * Get the admin menu parent slug
  195. * @return string
  196. */
  197. function foogallery_admin_menu_parent_slug() {
  198. return apply_filters( 'foogallery_admin_menu_parent_slug', FOOGALLERY_ADMIN_MENU_PARENT_SLUG );
  199. }
  200. /**
  201. * Helper function to build up the admin menu Url
  202. * @param array $extra_args
  203. *
  204. * @return string|void
  205. */
  206. function foogallery_build_admin_menu_url( $extra_args = array() ) {
  207. $url = admin_url( foogallery_admin_menu_parent_slug() );
  208. if ( ! empty( $extra_args ) ) {
  209. $url = add_query_arg( $extra_args, $url );
  210. }
  211. return $url;
  212. }
  213. /**
  214. * Helper function for adding a foogallery sub menu
  215. *
  216. * @param $menu_title
  217. * @param string $capability
  218. * @param string $menu_slug
  219. * @param $function
  220. */
  221. function foogallery_add_submenu_page( $menu_title, $capability, $menu_slug, $function ) {
  222. add_submenu_page(
  223. foogallery_admin_menu_parent_slug(),
  224. $menu_title,
  225. $menu_title,
  226. $capability,
  227. $menu_slug,
  228. $function
  229. );
  230. }
  231. /**
  232. * Returns all FooGallery galleries
  233. *
  234. * @return FooGallery[] array of FooGallery galleries
  235. */
  236. function foogallery_get_all_galleries( $excludes = false ) {
  237. $args = array(
  238. 'post_type' => FOOGALLERY_CPT_GALLERY,
  239. 'post_status' => array( 'publish', 'draft' ),
  240. 'cache_results' => false,
  241. 'nopaging' => true,
  242. );
  243. if ( is_array( $excludes ) ) {
  244. $args['post__not_in'] = $excludes;
  245. }
  246. $gallery_posts = get_posts( $args );
  247. if ( empty( $gallery_posts ) ) {
  248. return array();
  249. }
  250. $galleries = array();
  251. foreach ( $gallery_posts as $post ) {
  252. $galleries[] = FooGallery::get( $post );
  253. }
  254. return $galleries;
  255. }
  256. /**
  257. * Parse some content and return an array of all gallery shortcodes that are used inside it
  258. *
  259. * @param $content The content to search for gallery shortcodes
  260. *
  261. * @return array An array of all the foogallery shortcodes found in the content
  262. */
  263. function foogallery_extract_gallery_shortcodes( $content ) {
  264. $shortcodes = array();
  265. $regex_pattern = foogallery_gallery_shortcode_regex();
  266. if ( preg_match_all( '/' . $regex_pattern . '/s', $content, $matches ) ) {
  267. for ( $i = 0; $i < count( $matches[0] ); ++$i ) {
  268. $shortcode = $matches[0][$i];
  269. $args = $matches[3][$i];
  270. $attribure_string = str_replace( ' ', '&', trim( $args ) );
  271. $attribure_string = str_replace( '"', '', $attribure_string );
  272. $attributes = wp_parse_args( $attribure_string );
  273. if ( array_key_exists( 'id', $attributes ) ) {
  274. $id = intval( $attributes['id'] );
  275. $shortcodes[ $id ] = $shortcode;
  276. }
  277. }
  278. }
  279. return $shortcodes;
  280. }
  281. /**
  282. * Build up the FooGallery shortcode regex
  283. *
  284. * @return string
  285. */
  286. function foogallery_gallery_shortcode_regex() {
  287. $tag = foogallery_gallery_shortcode_tag();
  288. return
  289. '\\[' // Opening bracket
  290. . '(\\[?)' // 1: Optional second opening bracket for escaping shortcodes: [[tag]]
  291. . "($tag)" // 2: Shortcode name
  292. . '(?![\\w-])' // Not followed by word character or hyphen
  293. . '(' // 3: Unroll the loop: Inside the opening shortcode tag
  294. . '[^\\]\\/]*' // Not a closing bracket or forward slash
  295. . '(?:'
  296. . '\\/(?!\\])' // A forward slash not followed by a closing bracket
  297. . '[^\\]\\/]*' // Not a closing bracket or forward slash
  298. . ')*?'
  299. . ')'
  300. . '(?:'
  301. . '(\\/)' // 4: Self closing tag ...
  302. . '\\]' // ... and closing bracket
  303. . '|'
  304. . '\\]' // Closing bracket
  305. . '(?:'
  306. . '(' // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags
  307. . '[^\\[]*+' // Not an opening bracket
  308. . '(?:'
  309. . '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag
  310. . '[^\\[]*+' // Not an opening bracket
  311. . ')*+'
  312. . ')'
  313. . '\\[\\/\\2\\]' // Closing shortcode tag
  314. . ')?'
  315. . ')'
  316. . '(\\]?)'; // 6: Optional second closing bracket for escaping shortcodes: [[tag]]
  317. }
  318. /**
  319. * Builds up a class attribute that can be used in a gallery template
  320. * @param $gallery FooGallery
  321. *
  322. * @return string the classname based on the gallery and any extra attributes
  323. */
  324. function foogallery_build_class_attribute( $gallery ) {
  325. $classes[] = 'foogallery';
  326. $classes[] = 'foogallery-container';
  327. $classes[] = "foogallery-{$gallery->gallery_template}";
  328. $num_args = func_num_args();
  329. if ( $num_args > 1 ) {
  330. $arg_list = func_get_args();
  331. for ( $i = 1; $i < $num_args; $i++ ) {
  332. $classes[] = $arg_list[$i];
  333. }
  334. }
  335. $classes = apply_filters( 'foogallery_build_class_attribute', $classes, $gallery );
  336. return implode( ' ', $classes );
  337. }
  338. /**
  339. * Builds up a SAFE class attribute that can be used in a gallery template
  340. * @param $gallery FooGallery
  341. *
  342. * @return string the classname based on the gallery and any extra attributes
  343. */
  344. function foogallery_build_class_attribute_safe( $gallery ) {
  345. $args = func_get_args();
  346. $result = call_user_func_array("foogallery_build_class_attribute", $args);
  347. return esc_attr( $result );
  348. }
  349. /**
  350. * Renders an escaped class attribute that can be used directly by gallery templates
  351. *
  352. * @param $gallery FooGallery
  353. */
  354. function foogallery_build_class_attribute_render_safe( $gallery ) {
  355. $args = func_get_args();
  356. $result = call_user_func_array("foogallery_build_class_attribute_safe", $args);
  357. echo $result;
  358. }
  359. /**
  360. * Builds up the attributes that are appended to a gallery template container
  361. *
  362. * @param $gallery FooGallery
  363. * @param $attributes array
  364. *
  365. * @return string
  366. */
  367. function foogallery_build_container_attributes_safe( $gallery, $attributes ) {
  368. //add the default gallery id
  369. $attributes['id'] = 'foogallery-gallery-' . $gallery->ID;
  370. //add the standard data-foogallery attribute so that the JS initializes correctly
  371. $attributes['data-foogallery'] = foogallery_build_container_data_options( $gallery, $attributes );
  372. //allow others to add their own attributes globally
  373. $attributes = apply_filters( 'foogallery_build_container_attributes', $attributes, $gallery );
  374. //allow others to add their own attributes for a specific gallery template
  375. $attributes = apply_filters( 'foogallery_build_container_attributes-' . $gallery->gallery_template, $attributes, $gallery );
  376. //clean up the attributes to make them safe for output
  377. $html = '';
  378. foreach( $attributes as $key=>$value) {
  379. $safe_value = esc_attr( $value );
  380. $html .= "{$key}=\"{$safe_value}\" ";
  381. }
  382. return $html;
  383. }
  384. /**
  385. * Builds up the data-foogallery attribute options that is used by the core javascript
  386. *
  387. * @param $gallery
  388. * @param $attributes
  389. *
  390. * @return string
  391. */
  392. function foogallery_build_container_data_options( $gallery, $attributes ) {
  393. $options = apply_filters( 'foogallery_build_container_data_options', array(), $gallery, $attributes );
  394. $options = apply_filters( 'foogallery_build_container_data_options-'. $gallery->gallery_template, $options, $gallery, $attributes );
  395. return json_encode( $options );
  396. }
  397. /**
  398. * Render a foogallery
  399. *
  400. * @param $gallery_id int The id of the foogallery you want to render
  401. * @param array $args
  402. */
  403. function foogallery_render_gallery( $gallery_id, $args = array()) {
  404. //create new instance of template engine
  405. $engine = new FooGallery_Template_Loader();
  406. $shortcode_args = wp_parse_args( $args, array(
  407. 'id' => $gallery_id
  408. ) );
  409. $engine->render_template( $shortcode_args );
  410. }
  411. /**
  412. * Returns the available sorting options that can be chosen for galleries and albums
  413. */
  414. function foogallery_sorting_options() {
  415. return apply_filters( 'foogallery_sorting_options', array(
  416. '' => __('Default', 'foogallery'),
  417. 'date_desc' => __('Date created - newest first', 'foogallery'),
  418. 'date_asc' => __('Date created - oldest first', 'foogallery'),
  419. 'modified_desc' => __('Date modified - most recent first', 'foogallery'),
  420. 'modified_asc' => __('Date modified - most recent last', 'foogallery'),
  421. 'title_asc' => __('Title - alphabetically', 'foogallery'),
  422. 'title_desc' => __('Title - reverse', 'foogallery'),
  423. 'rand' => __('Random', 'foogallery')
  424. ) );
  425. }
  426. function foogallery_sorting_get_posts_orderby_arg( $sorting_option ) {
  427. $orderby_arg = 'post__in';
  428. switch ( $sorting_option ) {
  429. case 'date_desc':
  430. case 'date_asc':
  431. $orderby_arg = 'date';
  432. break;
  433. case 'modified_desc':
  434. case 'modified_asc':
  435. $orderby_arg = 'modified';
  436. break;
  437. case 'title_asc':
  438. case 'title_desc':
  439. $orderby_arg = 'title';
  440. break;
  441. case 'rand':
  442. $orderby_arg = 'rand';
  443. break;
  444. }
  445. return apply_filters( 'foogallery_sorting_get_posts_orderby_arg', $orderby_arg, $sorting_option );
  446. }
  447. function foogallery_sorting_get_posts_order_arg( $sorting_option ) {
  448. $order_arg = 'DESC';
  449. switch ( $sorting_option ) {
  450. case 'date_asc':
  451. case 'modified_asc':
  452. case 'title_asc':
  453. $order_arg = 'ASC';
  454. break;
  455. }
  456. return apply_filters( 'foogallery_sorting_get_posts_order_arg', $order_arg, $sorting_option );
  457. }
  458. /**
  459. * @deprecated 1.4.7 Default templates loaded by default and no longer activated via extension
  460. *
  461. * Activate the default templates extension when there are no gallery templates loaded
  462. */
  463. function foogallery_activate_default_templates_extension() {
  464. //no longer needed but left in case any 3rd party extensions call this function
  465. _deprecated_function( __FUNCTION__, '1.4.7' );
  466. }
  467. /**
  468. * Allow FooGallery to enqueue stylesheet and allow them to be enqueued in the head on the next page load
  469. *
  470. * @param $handle string
  471. * @param $src string
  472. * @param array $deps
  473. * @param bool $ver
  474. * @param string $media
  475. */
  476. function foogallery_enqueue_style( $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
  477. wp_enqueue_style( $handle, $src, $deps, $ver, $media );
  478. do_action( 'foogallery_enqueue_style', $handle, $src, $deps, $ver, $media );
  479. }
  480. /**
  481. * Returns all foogallery post objects that are attached to the post
  482. *
  483. * @param $post_id int The ID of the post
  484. *
  485. * @return array List of foogallery posts.
  486. */
  487. function foogallery_get_galleries_attached_to_post( $post_id ) {
  488. $gallery_ids = get_post_meta( $post_id, FOOGALLERY_META_POST_USAGE, false );
  489. if ( !empty( $gallery_ids ) ) {
  490. return get_posts( array(
  491. 'post_type' => array( FOOGALLERY_CPT_GALLERY, ),
  492. 'post_status' => array( 'draft', 'publish' ),
  493. 'posts_per_page' => -1,
  494. 'include' => $gallery_ids
  495. ) );
  496. }
  497. return array();
  498. }
  499. /**
  500. * Clears all css load optimization post meta
  501. */
  502. function foogallery_clear_all_css_load_optimizations() {
  503. delete_post_meta_by_key( FOOGALLERY_META_POST_USAGE_CSS );
  504. }
  505. /**
  506. * Performs a check to see if the plugin has been updated, and perform any housekeeping if necessary
  507. */
  508. function foogallery_perform_version_check() {
  509. $checker = new FooGallery_Version_Check();
  510. $checker->perform_check();
  511. }
  512. /**
  513. * Returns the JPEG quality used when generating thumbnails
  514. *
  515. * @return int The quality value stored in settings
  516. */
  517. function foogallery_thumbnail_jpeg_quality() {
  518. $quality = intval( foogallery_get_setting( 'thumb_jpeg_quality' ) );
  519. //check if we get an invalid value for whatever reason and if so return a default of 80
  520. if ( $quality <= 0 ) {
  521. $quality = 80;
  522. }
  523. return $quality;
  524. }
  525. /**
  526. * Returns the caption title source setting
  527. *
  528. * @return string
  529. */
  530. function foogallery_caption_title_source() {
  531. $source = foogallery_get_setting( 'caption_title_source', 'caption' );
  532. if ( empty( $source ) ) {
  533. $source = 'caption';
  534. }
  535. return $source;
  536. }
  537. /**
  538. * Returns the attachment caption title based on the caption_title_source setting
  539. *
  540. * @param WP_Post $attachment_post
  541. * @param bool $source
  542. *
  543. * @return string
  544. */
  545. function foogallery_get_caption_title_for_attachment($attachment_post, $source = false) {
  546. if ( false === $source ) {
  547. $source = foogallery_caption_title_source();
  548. }
  549. switch ( $source ) {
  550. case 'title':
  551. $caption = trim( $attachment_post->post_title );
  552. break;
  553. case 'desc':
  554. $caption = trim( $attachment_post->post_content );
  555. break;
  556. case 'alt':
  557. $caption = trim( get_post_meta( $attachment_post->ID, '_wp_attachment_image_alt', true ) );
  558. break;
  559. default:
  560. $caption = trim( $attachment_post->post_excerpt );
  561. }
  562. return apply_filters( 'foogallery_get_caption_title_for_attachment', $caption, $attachment_post );
  563. }
  564. /**
  565. * Returns the caption description source setting
  566. *
  567. * @return string
  568. */
  569. function foogallery_caption_desc_source() {
  570. $source = foogallery_get_setting( 'caption_desc_source', 'desc' );
  571. if ( empty( $source ) ) {
  572. $source = 'desc';
  573. }
  574. return $source;
  575. }
  576. /**
  577. * Returns the attachment caption description based on the caption_desc_source setting
  578. *
  579. * @param WP_Post $attachment_post
  580. * @param bool $source
  581. *
  582. * @return string
  583. */
  584. function foogallery_get_caption_desc_for_attachment($attachment_post, $source = false) {
  585. if ( false === $source ) {
  586. $source = foogallery_caption_desc_source();
  587. }
  588. switch ( $source ) {
  589. case 'title':
  590. $caption = trim( $attachment_post->post_title );
  591. break;
  592. case 'caption':
  593. $caption = trim( $attachment_post->post_excerpt );
  594. break;
  595. case 'alt':
  596. $caption = trim( get_post_meta( $attachment_post->ID, '_wp_attachment_image_alt', true ) );
  597. break;
  598. default:
  599. $caption = trim( $attachment_post->post_content );
  600. }
  601. return apply_filters( 'foogallery_get_caption_desc_for_attachment', $caption, $attachment_post );
  602. }
  603. /**
  604. * Runs thumbnail tests and outputs results in a table format
  605. */
  606. function foogallery_output_thumbnail_generation_results() {
  607. $thumbs = new FooGallery_Thumbnails();
  608. try {
  609. $results = $thumbs->run_thumbnail_generation_tests();
  610. if ( $results['success'] ) {
  611. echo '<span style="color:#0c0">' . __('Thumbnail generation test ran successfully.', 'foogallery') . '</span>';
  612. } else {
  613. echo '<span style="color:#c00">' . __('Thumbnail generation test failed!', 'foogallery') . '</span>';
  614. var_dump( $results['error'] );
  615. var_dump( $results['file_info'] );
  616. }
  617. }
  618. catch (Exception $e) {
  619. echo 'Exception: ' . $e->getMessage();
  620. }
  621. }
  622. /**
  623. * Returns the URL to the test image
  624. *
  625. * @return string
  626. */
  627. function foogallery_test_thumb_url() {
  628. return apply_filters( 'foogallery_test_thumb_url', FOOGALLERY_URL . 'assets/logo.png' );
  629. }
  630. /**
  631. * Return all the gallery datasources used within FooGallery
  632. *
  633. * @return array
  634. */
  635. function foogallery_gallery_datasources() {
  636. $default_datasource = foogallery_default_datasource();
  637. $datasources[$default_datasource] = 'FooGalleryDatasource_MediaLibrary';
  638. return apply_filters( 'foogallery_gallery_datasources', $datasources );
  639. }
  640. /**
  641. * Returns the default gallery datasource
  642. *
  643. * @return string
  644. */
  645. function foogallery_default_datasource() {
  646. return foogallery_get_default( 'datasource', 'media_library' );
  647. }
  648. /**
  649. * Instantiates a FooGallery datasource based on a datasource name
  650. *
  651. * @param $datasource_name string
  652. *
  653. * @return IFooGalleryDatasource
  654. */
  655. function foogallery_instantiate_datasource( $datasource_name ) {
  656. $datasources = foogallery_gallery_datasources();
  657. if ( array_key_exists( $datasource_name, $datasources ) ) {
  658. return new $datasources[$datasource_name];
  659. }
  660. return new FooGalleryDatasource_MediaLibrary();
  661. }
  662. /**
  663. * Returns the src to the built-in image placeholder
  664. * @return string
  665. */
  666. function foogallery_image_placeholder_src() {
  667. return apply_filters( 'foogallery_image_placeholder_src', FOOGALLERY_URL . 'assets/image-placeholder.png' );
  668. }
  669. /**
  670. * Returns the image html for the built-in image placeholder
  671. *
  672. * @param array $args
  673. *
  674. * @return string
  675. */
  676. function foogallery_image_placeholder_html( $args ) {
  677. if ( !isset( $args ) ) {
  678. $args = array(
  679. 'width' => 150,
  680. 'height' => 150
  681. );
  682. }
  683. $args['src'] = foogallery_image_placeholder_src();
  684. $args = array_map( 'esc_attr', $args );
  685. $html = '<img ';
  686. foreach ( $args as $name => $value ) {
  687. $html .= " $name=" . '"' . $value . '"';
  688. }
  689. $html .= ' />';
  690. return apply_filters( 'foogallery_image_placeholder_html', $html, $args );
  691. }
  692. /**
  693. * Returns the thumbnail html for the featured attachment for a gallery.
  694. * If no featured attachment can be found, then a placeholder image src is returned instead
  695. *
  696. * @param FooGallery $gallery
  697. * @param array $args
  698. *
  699. * @return string
  700. */
  701. function foogallery_find_featured_attachment_thumbnail_html( $gallery, $args = null ){
  702. if ( !isset( $gallery ) || false === $gallery ) return '';
  703. if ( !isset( $args ) ) {
  704. $args = array(
  705. 'width' => 150,
  706. 'height' => 150
  707. );
  708. }
  709. $featuredAttachment = $gallery->featured_attachment();
  710. if ( $featuredAttachment ) {
  711. return $featuredAttachment->html_img( $args );
  712. } else {
  713. //if we have no featured attachment, then use the built-in image placeholder
  714. return foogallery_image_placeholder_html( $args );
  715. }
  716. }
  717. /**
  718. * Returns the thumbnail src for the featured attachment for a gallery.
  719. * If no featured attachment can be found, then a placeholder image src is returned instead
  720. *
  721. * @param FooGallery $gallery
  722. * @param array $args
  723. *
  724. * @return string
  725. */
  726. function foogallery_find_featured_attachment_thumbnail_src( $gallery, $args = null ){
  727. if ( !isset( $gallery ) || false === $gallery ) return '';
  728. if ( !isset( $args ) ) {
  729. $args = array(
  730. 'width' => 150,
  731. 'height' => 150
  732. );
  733. }
  734. $featuredAttachment = $gallery->featured_attachment();
  735. if ( $featuredAttachment ) {
  736. return $featuredAttachment->html_img_src( $args );
  737. } else {
  738. //if we have no featured attachment, then use the built-in image placeholder
  739. return foogallery_image_placeholder_src();
  740. }
  741. }
  742. /**
  743. * Returns the available retina options that can be chosen
  744. */
  745. function foogallery_retina_options() {
  746. return apply_filters( 'foogallery_retina_options', array(
  747. '2x' => __('2x', 'foogallery'),
  748. '3x' => __('3x', 'foogallery'),
  749. '4x' => __('4x', 'foogallery')
  750. ) );
  751. }
  752. /**
  753. * Does a full uninstall of the plugin including all data and settings!
  754. */
  755. function foogallery_uninstall() {
  756. if ( !current_user_can( 'install_plugins' ) ) exit;
  757. //delete all gallery posts first
  758. global $wpdb;
  759. $query = "SELECT p.ID FROM {$wpdb->posts} AS p WHERE p.post_type IN (%s)";
  760. $gallery_post_ids = $wpdb->get_col( $wpdb->prepare( $query, FOOGALLERY_CPT_GALLERY ) );
  761. if ( !empty( $gallery_post_ids ) ) {
  762. $deleted = 0;
  763. foreach ( $gallery_post_ids as $post_id ) {
  764. $del = wp_delete_post( $post_id );
  765. if ( false !== $del ) {
  766. ++$deleted;
  767. }
  768. }
  769. }
  770. //delete all options
  771. if ( is_network_admin() ) {
  772. delete_site_option( FOOGALLERY_SLUG );
  773. } else {
  774. delete_option( FOOGALLERY_SLUG );
  775. }
  776. delete_option( FOOGALLERY_OPTION_VERSION );
  777. delete_option( FOOGALLERY_OPTION_THUMB_TEST );
  778. delete_option( FOOGALLERY_EXTENSIONS_SLUGS_OPTIONS_KEY );
  779. delete_option( FOOGALLERY_EXTENSIONS_LOADING_ERRORS );
  780. delete_option( FOOGALLERY_EXTENSIONS_LOADING_ERRORS_RESPONSE );
  781. delete_option( FOOGALLERY_EXTENSIONS_SLUGS_OPTIONS_KEY );
  782. delete_option( FOOGALLERY_EXTENSIONS_ACTIVATED_OPTIONS_KEY );
  783. delete_option( FOOGALLERY_EXTENSIONS_ERRORS_OPTIONS_KEY );
  784. //let any extensions clean up after themselves
  785. do_action( 'foogallery_uninstall' );
  786. }
  787. /**
  788. * Returns an attachment field friendly name, based on a field name that is passed in
  789. *
  790. * @param $field
  791. *
  792. * @return string
  793. */
  794. function foogallery_get_attachment_field_friendly_name( $field ) {
  795. switch ( $field ) {
  796. case 'title':
  797. return __( 'Attachment Title', 'foogallery' );
  798. case 'caption':
  799. return __( 'Attachment Caption', 'foogallery' );
  800. case 'desc':
  801. return __( 'Attachment Description', 'foogallery' );
  802. case 'alt':
  803. return __( 'Attachment Alt', 'foogallery' );
  804. }
  805. }
  806. /**
  807. * Returns the fields for a specific gallery template
  808. *
  809. * @param $template mixed
  810. * @return mixed
  811. */
  812. function foogallery_get_fields_for_template( $template ) {
  813. if ( is_string( $template ) ) {
  814. $template = foogallery_get_gallery_template( $template );
  815. }
  816. $fields = $template['fields'];
  817. // Allow for extensions to override fields for every gallery template.
  818. // Also passes the $template along so you can inspect and conditionally alter fields based on the template properties
  819. $fields = apply_filters( 'foogallery_override_gallery_template_fields', $fields, $template );
  820. // Allow for extensions to override fields for a specific gallery template.
  821. // Also passes the $template along so you can inspect and conditionally alter fields based on the template properties
  822. $fields = apply_filters( "foogallery_override_gallery_template_fields-{$template['slug']}", $fields, $template );
  823. foreach ( $fields as &$field ) {
  824. //allow for the field to be altered by extensions. Also used by the build-in fields, e.g. lightbox
  825. $field = apply_filters( 'foogallery_alter_gallery_template_field', $field, $template['slug'] );
  826. }
  827. return $fields;
  828. }
  829. /**
  830. * Builds default settings for the supplied gallery template
  831. *
  832. * @param $template_name
  833. * @return array
  834. */
  835. function foogallery_build_default_settings_for_gallery_template( $template_name ) {
  836. $fields = foogallery_get_fields_for_template( $template_name );
  837. $settings = array();
  838. //loop through the fields and build up an array of keys and default values
  839. foreach( $fields as $field ) {
  840. $default = array_key_exists( 'default', $field ) ? $field['default'] : false;
  841. if ( !empty( $default ) ) {
  842. $settings["{$template_name}_{$field['id']}"] = $default;
  843. }
  844. }
  845. return $settings;
  846. }
  847. /**
  848. * Returns the choices used for the thumb link field type
  849. * @return array
  850. */
  851. function foogallery_gallery_template_field_thumb_link_choices() {
  852. return apply_filters( 'foogallery_gallery_template_field_thumb_links', array(
  853. 'image' => __( 'Full Size Image (Lightbox)', 'foogallery' ),
  854. 'page' => __( 'Image Attachment Page', 'foogallery' ),
  855. 'custom' => __( 'Custom URL', 'foogallery' ),
  856. 'none' => __( 'Not linked', 'foogallery' ),
  857. ) );
  858. }
  859. /**
  860. * Returns the choices used for the lightbox field type
  861. * @return array
  862. */
  863. function foogallery_gallery_template_field_lightbox_choices() {
  864. $lightboxes = apply_filters( 'foogallery_gallery_template_field_lightboxes', array() );
  865. $lightboxes['none'] = __( 'None', 'foogallery' );
  866. return $lightboxes;
  867. }