Brak opisu

tiled-gallery.php 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. use Automattic\Jetpack\Assets;
  3. use Automattic\Jetpack\Status;
  4. // Include the class file containing methods for rounding constrained array elements.
  5. // Here the constrained array element is the dimension of a row, group or an image in the tiled gallery.
  6. require_once dirname( __FILE__ ) . '/math/class-constrained-array-rounding.php';
  7. // Layouts
  8. require_once dirname( __FILE__ ) . '/tiled-gallery/tiled-gallery-rectangular.php';
  9. require_once dirname( __FILE__ ) . '/tiled-gallery/tiled-gallery-square.php';
  10. require_once dirname( __FILE__ ) . '/tiled-gallery/tiled-gallery-circle.php';
  11. class Jetpack_Tiled_Gallery {
  12. private static $talaveras = array( 'rectangular', 'square', 'circle', 'rectangle', 'columns' );
  13. public function __construct() {
  14. add_action( 'admin_init', array( $this, 'settings_api_init' ) );
  15. add_filter( 'jetpack_gallery_types', array( $this, 'jetpack_gallery_types' ), 9 );
  16. add_filter( 'jetpack_default_gallery_type', array( $this, 'jetpack_default_gallery_type' ) );
  17. }
  18. public function tiles_enabled() {
  19. // Check the setting status
  20. return '' != Jetpack_Options::get_option_and_ensure_autoload( 'tiled_galleries', '' );
  21. }
  22. public function set_atts( $atts ) {
  23. global $post;
  24. $this->atts = shortcode_atts(
  25. array(
  26. 'order' => 'ASC',
  27. 'orderby' => 'menu_order ID',
  28. 'id' => isset( $post->ID ) ? $post->ID : 0,
  29. 'include' => '',
  30. 'exclude' => '',
  31. 'type' => '',
  32. 'grayscale' => false,
  33. 'link' => '',
  34. 'columns' => 3,
  35. ),
  36. $atts,
  37. 'gallery'
  38. );
  39. $this->atts['id'] = (int) $this->atts['id'];
  40. $this->float = is_rtl() ? 'right' : 'left';
  41. // Default to rectangular is tiled galleries are checked
  42. if ( $this->tiles_enabled() && ( ! $this->atts['type'] || 'default' == $this->atts['type'] ) ) {
  43. /** This filter is already documented in functions.gallery.php */
  44. $this->atts['type'] = apply_filters( 'jetpack_default_gallery_type', 'rectangular' );
  45. }
  46. if ( ! $this->atts['orderby'] ) {
  47. $this->atts['orderby'] = sanitize_sql_orderby( $this->atts['orderby'] );
  48. if ( ! $this->atts['orderby'] ) {
  49. $this->atts['orderby'] = 'menu_order ID';
  50. }
  51. }
  52. if ( 'rand' == strtolower( $this->atts['order'] ) ) {
  53. $this->atts['orderby'] = 'rand';
  54. }
  55. // We shouldn't have more than 20 columns.
  56. if ( ! is_numeric( $this->atts['columns'] ) || 20 < $this->atts['columns'] ) {
  57. $this->atts['columns'] = 3;
  58. }
  59. }
  60. public function get_attachments() {
  61. $atts = $this->atts;
  62. if ( ! empty( $atts['include'] ) ) {
  63. $include = preg_replace( '/[^0-9,]+/', '', $atts['include'] );
  64. $_attachments = get_posts(
  65. array(
  66. 'include' => $include,
  67. 'post_status' => 'inherit',
  68. 'post_type' => 'attachment',
  69. 'post_mime_type' => 'image',
  70. 'order' => $atts['order'],
  71. 'orderby' => $atts['orderby'],
  72. 'suppress_filters' => false,
  73. )
  74. );
  75. $attachments = array();
  76. foreach ( $_attachments as $key => $val ) {
  77. $attachments[ $val->ID ] = $_attachments[ $key ];
  78. }
  79. } elseif ( 0 === $atts['id'] ) {
  80. /*
  81. * Should NEVER Happen but infinite_scroll_load_other_plugins_scripts means it does
  82. * Querying with post_parent == 0 can generate stupidly memcache sets
  83. * on sites with 10000's of unattached attachments as get_children puts every post in the cache.
  84. * TODO Fix this properly.
  85. */
  86. $attachments = array();
  87. } elseif ( ! empty( $atts['exclude'] ) ) {
  88. $exclude = preg_replace( '/[^0-9,]+/', '', $atts['exclude'] );
  89. $attachments = get_children(
  90. array(
  91. 'post_parent' => $atts['id'],
  92. 'exclude' => $exclude,
  93. 'post_status' => 'inherit',
  94. 'post_type' => 'attachment',
  95. 'post_mime_type' => 'image',
  96. 'order' => $atts['order'],
  97. 'orderby' => $atts['orderby'],
  98. 'suppress_filters' => false,
  99. )
  100. );
  101. } else {
  102. $attachments = get_children(
  103. array(
  104. 'post_parent' => $atts['id'],
  105. 'post_status' => 'inherit',
  106. 'post_type' => 'attachment',
  107. 'post_mime_type' => 'image',
  108. 'order' => $atts['order'],
  109. 'orderby' => $atts['orderby'],
  110. 'suppress_filters' => false,
  111. )
  112. );
  113. }
  114. return $attachments;
  115. }
  116. public static function default_scripts_and_styles() {
  117. wp_enqueue_script(
  118. 'tiled-gallery',
  119. Assets::get_file_url_for_environment(
  120. '_inc/build/tiled-gallery/tiled-gallery/tiled-gallery.min.js',
  121. 'modules/tiled-gallery/tiled-gallery/tiled-gallery.js'
  122. ),
  123. array()
  124. );
  125. wp_enqueue_style( 'tiled-gallery', plugins_url( 'tiled-gallery/tiled-gallery.css', __FILE__ ), array(), '2012-09-21' );
  126. wp_style_add_data( 'tiled-gallery', 'rtl', 'replace' );
  127. }
  128. public function gallery_shortcode( $val, $atts ) {
  129. if ( ! empty( $val ) ) { // something else is overriding post_gallery, like a custom VIP shortcode
  130. return $val;
  131. }
  132. global $post;
  133. $this->set_atts( $atts );
  134. $attachments = $this->get_attachments();
  135. if ( empty( $attachments ) ) {
  136. return '';
  137. }
  138. if ( is_feed() || defined( 'IS_HTML_EMAIL' ) ) {
  139. return '';
  140. }
  141. if (
  142. in_array(
  143. $this->atts['type'],
  144. /**
  145. * Filters the permissible Tiled Gallery types.
  146. *
  147. * @module tiled-gallery
  148. *
  149. * @since 3.7.0
  150. *
  151. * @param array Array of allowed types. Default: 'rectangular', 'square', 'circle', 'rectangle', 'columns'.
  152. */
  153. $talaveras = apply_filters( 'jetpack_tiled_gallery_types', self::$talaveras )
  154. )
  155. ) {
  156. // Enqueue styles and scripts
  157. self::default_scripts_and_styles();
  158. // Generate gallery HTML
  159. $gallery_class = 'Jetpack_Tiled_Gallery_Layout_' . ucfirst( $this->atts['type'] );
  160. $gallery = new $gallery_class( $attachments, $this->atts['link'], $this->atts['grayscale'], (int) $this->atts['columns'] );
  161. $gallery_html = $gallery->HTML();
  162. if ( $gallery_html && class_exists( 'Jetpack' ) && class_exists( 'Jetpack_Photon' ) ) {
  163. // Tiled Galleries in Jetpack require that Photon be active.
  164. // If it's not active, run it just on the gallery output.
  165. if ( ! in_array( 'photon', Jetpack::get_active_modules(), true ) && ! ( new Status() )->is_offline_mode() ) {
  166. $gallery_html = Jetpack_Photon::filter_the_content( $gallery_html );
  167. }
  168. }
  169. return trim( preg_replace( '/\s+/', ' ', $gallery_html ) ); // remove any new lines from the output so that the reader parses it better
  170. }
  171. return '';
  172. }
  173. public static function gallery_already_redefined() {
  174. global $shortcode_tags;
  175. $redefined = false;
  176. if ( ! isset( $shortcode_tags['gallery'] ) || $shortcode_tags['gallery'] !== 'gallery_shortcode' ) {
  177. $redefined = true;
  178. }
  179. /**
  180. * Filter the output of the check for another plugin or theme affecting WordPress galleries.
  181. *
  182. * This will let folks that replace core’s shortcode confirm feature parity with it, so Jetpack's Tiled Galleries can still work.
  183. *
  184. * @module tiled-gallery
  185. *
  186. * @since 3.1.0
  187. *
  188. * @param bool $redefined Does another plugin or theme already redefines the default WordPress gallery?
  189. */
  190. return apply_filters( 'jetpack_tiled_gallery_shortcode_redefined', $redefined );
  191. }
  192. public static function init() {
  193. if ( self::gallery_already_redefined() ) {
  194. return;
  195. }
  196. $gallery = new Jetpack_Tiled_Gallery();
  197. add_filter( 'post_gallery', array( $gallery, 'gallery_shortcode' ), 1001, 2 );
  198. }
  199. public static function get_content_width() {
  200. $tiled_gallery_content_width = Jetpack::get_content_width();
  201. if ( ! $tiled_gallery_content_width ) {
  202. $tiled_gallery_content_width = 500;
  203. }
  204. /**
  205. * Filter overwriting the default content width.
  206. *
  207. * @module tiled-gallery
  208. *
  209. * @since 2.1.0
  210. *
  211. * @param string $tiled_gallery_content_width Default Tiled Gallery content width.
  212. */
  213. return apply_filters( 'tiled_gallery_content_width', $tiled_gallery_content_width );
  214. }
  215. /**
  216. * Media UI integration
  217. */
  218. function jetpack_gallery_types( $types ) {
  219. if ( get_option( 'tiled_galleries' ) && isset( $types['default'] ) ) {
  220. // Tiled is set as the default, meaning that type='default'
  221. // will still display the mosaic.
  222. $types['thumbnails'] = $types['default'];
  223. unset( $types['default'] );
  224. }
  225. $types['rectangular'] = __( 'Tiled Mosaic', 'jetpack' );
  226. $types['square'] = __( 'Square Tiles', 'jetpack' );
  227. $types['circle'] = __( 'Circles', 'jetpack' );
  228. $types['columns'] = __( 'Tiled Columns', 'jetpack' );
  229. return $types;
  230. }
  231. function jetpack_default_gallery_type() {
  232. return ( get_option( 'tiled_galleries' ) ? 'rectangular' : 'default' );
  233. }
  234. static function get_talaveras() {
  235. return self::$talaveras;
  236. }
  237. /**
  238. * Add a checkbox field to the Carousel section in Settings > Media
  239. * for setting tiled galleries as the default.
  240. */
  241. function settings_api_init() {
  242. global $wp_settings_sections;
  243. // Add the setting field [tiled_galleries] and place it in Settings > Media
  244. if ( isset( $wp_settings_sections['media']['carousel_section'] ) ) {
  245. $section = 'carousel_section';
  246. } else {
  247. $section = 'default';
  248. }
  249. add_settings_field( 'tiled_galleries', __( 'Tiled Galleries', 'jetpack' ), array( $this, 'setting_html' ), 'media', $section );
  250. register_setting( 'media', 'tiled_galleries', 'esc_attr' );
  251. }
  252. function setting_html() {
  253. echo '<label><input name="tiled_galleries" type="checkbox" value="1" ' .
  254. checked( 1, '' != get_option( 'tiled_galleries' ), false ) . ' /> ' .
  255. __( 'Display all your gallery pictures in a cool mosaic.', 'jetpack' ) . '</br></label>';
  256. }
  257. }
  258. add_action( 'init', array( 'Jetpack_Tiled_Gallery', 'init' ) );