Нема описа

functions.gallery.php 3.0KB

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