functions.gallery.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * Renders extra controls in the Gallery Settings section of the new media UI.
  4. */
  5. class Jetpack_Gallery_Settings {
  6. function __construct() {
  7. add_action( 'admin_init', array( $this, 'admin_init' ) );
  8. }
  9. function admin_init() {
  10. /**
  11. * Filter the available gallery types.
  12. *
  13. * @module shortcodes, tiled-gallery
  14. *
  15. * @since 2.5.1
  16. *
  17. * @param array $value Array of the default thumbnail grid gallery type. Default array contains one key, ‘default’.
  18. *
  19. */
  20. $this->gallery_types = apply_filters( 'jetpack_gallery_types', array( 'default' => __( 'Thumbnail Grid', 'jetpack' ) ) );
  21. // Enqueue the media UI only if needed.
  22. if ( count( $this->gallery_types ) > 1 ) {
  23. add_action( 'wp_enqueue_media', array( $this, 'wp_enqueue_media' ) );
  24. add_action( 'print_media_templates', array( $this, 'print_media_templates' ) );
  25. }
  26. // Add Slideshow and Galleries functionality to core's media gallery widget.
  27. add_filter( 'widget_media_gallery_instance_schema', array( $this, 'core_media_widget_compat' ) );
  28. }
  29. /**
  30. * Updates the schema of the core gallery widget so we can save the
  31. * fields that we add to Gallery Widgets, like `type` and `conditions`
  32. *
  33. * @param $schema The current schema for the core gallery widget
  34. *
  35. * @return array the updated schema
  36. */
  37. public function core_media_widget_compat( $schema ) {
  38. $schema['type'] = array(
  39. 'type' => 'string',
  40. 'enum' => array_keys( $this->gallery_types ),
  41. 'description' => __( 'Type of gallery.', 'jetpack' ),
  42. 'default' => 'default',
  43. );
  44. return $schema;
  45. }
  46. /**
  47. * Registers/enqueues the gallery settings admin js.
  48. */
  49. function wp_enqueue_media() {
  50. if ( ! wp_script_is( 'jetpack-gallery-settings', 'registered' ) ) {
  51. /**
  52. * This only happens if we're not in Jetpack, but on WPCOM instead.
  53. * This is the correct path for WPCOM.
  54. */
  55. wp_register_script(
  56. 'jetpack-gallery-settings',
  57. Jetpack::get_file_url_for_environment( '_inc/build/gallery-settings.min.js', '_inc/gallery-settings.js' ),
  58. array( 'media-views' ),
  59. '20121225'
  60. );
  61. }
  62. wp_enqueue_script( 'jetpack-gallery-settings' );
  63. }
  64. /**
  65. * Outputs a view template which can be used with wp.media.template
  66. */
  67. function print_media_templates() {
  68. /**
  69. * Filter the default gallery type.
  70. *
  71. * @module tiled-gallery
  72. *
  73. * @since 2.5.1
  74. *
  75. * @param string $value A string of the gallery type. Default is ‘default’.
  76. *
  77. */
  78. $default_gallery_type = apply_filters( 'jetpack_default_gallery_type', 'default' );
  79. ?>
  80. <script type="text/html" id="tmpl-jetpack-gallery-settings">
  81. <label class="setting">
  82. <span><?php _e( 'Type', 'jetpack' ); ?></span>
  83. <select class="type" name="type" data-setting="type">
  84. <?php foreach ( $this->gallery_types as $value => $caption ) : ?>
  85. <option value="<?php echo esc_attr( $value ); ?>" <?php selected( $value, $default_gallery_type ); ?>><?php echo esc_html( $caption ); ?></option>
  86. <?php endforeach; ?>
  87. </select>
  88. </label>
  89. </script>
  90. <?php
  91. }
  92. }
  93. new Jetpack_Gallery_Settings;