jetpack-carousel.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. <?php
  2. /*
  3. Plugin Name: Jetpack Carousel
  4. Plugin URL: https://wordpress.com/
  5. Description: Transform your standard image galleries into an immersive full-screen experience.
  6. Version: 0.1
  7. Author: Automattic
  8. Released under the GPL v.2 license.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. */
  14. class Jetpack_Carousel {
  15. public $prebuilt_widths = array( 370, 700, 1000, 1200, 1400, 2000 );
  16. public $first_run = true;
  17. public $in_gallery = false;
  18. public $in_jetpack = true;
  19. public $single_image_gallery_enabled = false;
  20. public $single_image_gallery_enabled_media_file = false;
  21. function __construct() {
  22. add_action( 'init', array( $this, 'init' ) );
  23. }
  24. function init() {
  25. if ( $this->maybe_disable_jp_carousel() )
  26. return;
  27. $this->in_jetpack = ( class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'enable_module_configurable' ) ) ? true : false;
  28. $this->single_image_gallery_enabled = !$this->maybe_disable_jp_carousel_single_images();
  29. $this->single_image_gallery_enabled_media_file = $this->maybe_enable_jp_carousel_single_images_media_file();
  30. if ( is_admin() ) {
  31. // Register the Carousel-related related settings
  32. add_action( 'admin_init', array( $this, 'register_settings' ), 5 );
  33. if ( ! $this->in_jetpack ) {
  34. if ( 0 == $this->test_1or0_option( get_option( 'carousel_enable_it' ), true ) )
  35. return; // Carousel disabled, abort early, but still register setting so user can switch it back on
  36. }
  37. // If in admin, register the ajax endpoints.
  38. add_action( 'wp_ajax_get_attachment_comments', array( $this, 'get_attachment_comments' ) );
  39. add_action( 'wp_ajax_nopriv_get_attachment_comments', array( $this, 'get_attachment_comments' ) );
  40. add_action( 'wp_ajax_post_attachment_comment', array( $this, 'post_attachment_comment' ) );
  41. add_action( 'wp_ajax_nopriv_post_attachment_comment', array( $this, 'post_attachment_comment' ) );
  42. } else {
  43. if ( ! $this->in_jetpack ) {
  44. if ( 0 == $this->test_1or0_option( get_option( 'carousel_enable_it' ), true ) )
  45. return; // Carousel disabled, abort early
  46. }
  47. // If on front-end, do the Carousel thang.
  48. /**
  49. * Filter the array of default prebuilt widths used in Carousel.
  50. *
  51. * @module carousel
  52. *
  53. * @since 1.6.0
  54. *
  55. * @param array $this->prebuilt_widths Array of default widths.
  56. */
  57. $this->prebuilt_widths = apply_filters( 'jp_carousel_widths', $this->prebuilt_widths );
  58. // below: load later than other callbacks hooked it (e.g. 3rd party plugins handling gallery shortcode)
  59. add_filter( 'post_gallery', array( $this, 'check_if_shortcode_processed_and_enqueue_assets' ), 1000, 2 );
  60. add_filter( 'post_gallery', array( $this, 'set_in_gallery' ), -1000 );
  61. add_filter( 'gallery_style', array( $this, 'add_data_to_container' ) );
  62. add_filter( 'wp_get_attachment_image_attributes', array( $this, 'add_data_to_images' ), 10, 2 );
  63. if ( $this->single_image_gallery_enabled ) {
  64. add_filter( 'the_content', array( $this, 'add_data_img_tags_and_enqueue_assets' ) );
  65. }
  66. }
  67. if ( $this->in_jetpack && method_exists( 'Jetpack', 'module_configuration_load' ) ) {
  68. Jetpack::enable_module_configurable( dirname( dirname( __FILE__ ) ) . '/carousel.php' );
  69. Jetpack::module_configuration_load( dirname( dirname( __FILE__ ) ) . '/carousel.php', array( $this, 'jetpack_configuration_load' ) );
  70. }
  71. }
  72. function maybe_disable_jp_carousel() {
  73. /**
  74. * Allow third-party plugins or themes to disable Carousel.
  75. *
  76. * @module carousel
  77. *
  78. * @since 1.6.0
  79. *
  80. * @param bool false Should Carousel be disabled? Default to false.
  81. */
  82. return apply_filters( 'jp_carousel_maybe_disable', false );
  83. }
  84. function maybe_disable_jp_carousel_single_images() {
  85. /**
  86. * Allow third-party plugins or themes to disable Carousel for single images.
  87. *
  88. * @module carousel
  89. *
  90. * @since 4.5.0
  91. *
  92. * @param bool false Should Carousel be disabled for single images? Default to false.
  93. */
  94. return apply_filters( 'jp_carousel_maybe_disable_single_images', false );
  95. }
  96. function maybe_enable_jp_carousel_single_images_media_file() {
  97. /**
  98. * Allow third-party plugins or themes to enable Carousel
  99. * for single images linking to 'Media File' (full size image).
  100. *
  101. * @module carousel
  102. *
  103. * @since 4.5.0
  104. *
  105. * @param bool false Should Carousel be enabled for single images linking to 'Media File'? Default to false.
  106. */
  107. return apply_filters( 'jp_carousel_load_for_images_linked_to_file', false );
  108. }
  109. function jetpack_configuration_load() {
  110. wp_safe_redirect( admin_url( 'options-media.php#carousel_background_color' ) );
  111. exit;
  112. }
  113. function asset_version( $version ) {
  114. /**
  115. * Filter the version string used when enqueuing Carousel assets.
  116. *
  117. * @module carousel
  118. *
  119. * @since 1.6.0
  120. *
  121. * @param string $version Asset version.
  122. */
  123. return apply_filters( 'jp_carousel_asset_version', $version );
  124. }
  125. function display_bail_message( $output= '' ) {
  126. // Displays a message on top of gallery if carousel has bailed
  127. $message = '<div class="jp-carousel-msg"><p>';
  128. $message .= __( 'Jetpack\'s Carousel has been disabled, because another plugin or your theme is overriding the [gallery] shortcode.', 'jetpack' );
  129. $message .= '</p></div>';
  130. // put before gallery output
  131. $output = $message . $output;
  132. return $output;
  133. }
  134. function check_if_shortcode_processed_and_enqueue_assets( $output ) {
  135. if (
  136. ! empty( $output ) &&
  137. /**
  138. * Allow third-party plugins or themes to force-enable Carousel.
  139. *
  140. * @module carousel
  141. *
  142. * @since 1.9.0
  143. *
  144. * @param bool false Should we force enable Carousel? Default to false.
  145. */
  146. ! apply_filters( 'jp_carousel_force_enable', false )
  147. ) {
  148. // Bail because someone is overriding the [gallery] shortcode.
  149. remove_filter( 'gallery_style', array( $this, 'add_data_to_container' ) );
  150. remove_filter( 'wp_get_attachment_image_attributes', array( $this, 'add_data_to_images' ) );
  151. remove_filter( 'the_content', array( $this, 'add_data_img_tags_and_enqueue_assets' ) );
  152. // Display message that carousel has bailed, if user is super_admin, and if we're not on WordPress.com.
  153. if (
  154. is_super_admin() &&
  155. ! ( defined( 'IS_WPCOM' ) && IS_WPCOM )
  156. ) {
  157. add_filter( 'post_gallery', array( $this, 'display_bail_message' ) );
  158. }
  159. return $output;
  160. }
  161. /**
  162. * Fires when thumbnails are shown in Carousel.
  163. *
  164. * @module carousel
  165. *
  166. * @since 1.6.0
  167. **/
  168. do_action( 'jp_carousel_thumbnails_shown' );
  169. $this->enqueue_assets();
  170. return $output;
  171. }
  172. function enqueue_assets() {
  173. if ( $this->first_run ) {
  174. wp_enqueue_script(
  175. 'jetpack-carousel',
  176. Jetpack::get_file_url_for_environment(
  177. '_inc/build/carousel/jetpack-carousel.min.js',
  178. 'modules/carousel/jetpack-carousel.js'
  179. ),
  180. array( 'jquery.spin' ),
  181. $this->asset_version( '20170209' ),
  182. true
  183. );
  184. // Note: using home_url() instead of admin_url() for ajaxurl to be sure to get same domain on wpcom when using mapped domains (also works on self-hosted)
  185. // Also: not hardcoding path since there is no guarantee site is running on site root in self-hosted context.
  186. $is_logged_in = is_user_logged_in();
  187. $current_user = wp_get_current_user();
  188. $comment_registration = intval( get_option( 'comment_registration' ) );
  189. $require_name_email = intval( get_option( 'require_name_email' ) );
  190. $localize_strings = array(
  191. 'widths' => $this->prebuilt_widths,
  192. 'is_logged_in' => $is_logged_in,
  193. 'lang' => strtolower( substr( get_locale(), 0, 2 ) ),
  194. 'ajaxurl' => set_url_scheme( admin_url( 'admin-ajax.php' ) ),
  195. 'nonce' => wp_create_nonce( 'carousel_nonce' ),
  196. 'display_exif' => $this->test_1or0_option( Jetpack_Options::get_option_and_ensure_autoload( 'carousel_display_exif', true ) ),
  197. 'display_geo' => $this->test_1or0_option( Jetpack_Options::get_option_and_ensure_autoload( 'carousel_display_geo', true ) ),
  198. 'single_image_gallery' => $this->single_image_gallery_enabled,
  199. 'single_image_gallery_media_file' => $this->single_image_gallery_enabled_media_file,
  200. 'background_color' => $this->carousel_background_color_sanitize( Jetpack_Options::get_option_and_ensure_autoload( 'carousel_background_color', '' ) ),
  201. 'comment' => __( 'Comment', 'jetpack' ),
  202. 'post_comment' => __( 'Post Comment', 'jetpack' ),
  203. 'write_comment' => __( 'Write a Comment...', 'jetpack' ),
  204. 'loading_comments' => __( 'Loading Comments...', 'jetpack' ),
  205. 'download_original' => sprintf( __( 'View full size <span class="photo-size">%1$s<span class="photo-size-times">&times;</span>%2$s</span>', 'jetpack' ), '{0}', '{1}' ),
  206. 'no_comment_text' => __( 'Please be sure to submit some text with your comment.', 'jetpack' ),
  207. 'no_comment_email' => __( 'Please provide an email address to comment.', 'jetpack' ),
  208. 'no_comment_author' => __( 'Please provide your name to comment.', 'jetpack' ),
  209. 'comment_post_error' => __( 'Sorry, but there was an error posting your comment. Please try again later.', 'jetpack' ),
  210. 'comment_approved' => __( 'Your comment was approved.', 'jetpack' ),
  211. 'comment_unapproved' => __( 'Your comment is in moderation.', 'jetpack' ),
  212. 'camera' => __( 'Camera', 'jetpack' ),
  213. 'aperture' => __( 'Aperture', 'jetpack' ),
  214. 'shutter_speed' => __( 'Shutter Speed', 'jetpack' ),
  215. 'focal_length' => __( 'Focal Length', 'jetpack' ),
  216. 'copyright' => __( 'Copyright', 'jetpack' ),
  217. 'comment_registration' => $comment_registration,
  218. 'require_name_email' => $require_name_email,
  219. /** This action is documented in core/src/wp-includes/link-template.php */
  220. 'login_url' => wp_login_url( apply_filters( 'the_permalink', get_permalink() ) ),
  221. 'blog_id' => (int) get_current_blog_id(),
  222. 'meta_data' => array( 'camera', 'aperture', 'shutter_speed', 'focal_length', 'copyright' )
  223. );
  224. if ( ! isset( $localize_strings['jetpack_comments_iframe_src'] ) || empty( $localize_strings['jetpack_comments_iframe_src'] ) ) {
  225. // We're not using Comments after all, so fallback to standard local comments.
  226. if ( $is_logged_in ) {
  227. $localize_strings['local_comments_commenting_as'] = '<p id="jp-carousel-commenting-as">' . sprintf( __( 'Commenting as %s', 'jetpack' ), $current_user->data->display_name ) . '</p>';
  228. } else {
  229. if ( $comment_registration ) {
  230. $localize_strings['local_comments_commenting_as'] = '<p id="jp-carousel-commenting-as">' . __( 'You must be <a href="#" class="jp-carousel-comment-login">logged in</a> to post a comment.', 'jetpack' ) . '</p>';
  231. } else {
  232. $required = ( $require_name_email ) ? __( '%s (Required)', 'jetpack' ) : '%s';
  233. $localize_strings['local_comments_commenting_as'] = ''
  234. . '<fieldset><label for="email">' . sprintf( $required, __( 'Email', 'jetpack' ) ) . '</label> '
  235. . '<input type="text" name="email" class="jp-carousel-comment-form-field jp-carousel-comment-form-text-field" id="jp-carousel-comment-form-email-field" /></fieldset>'
  236. . '<fieldset><label for="author">' . sprintf( $required, __( 'Name', 'jetpack' ) ) . '</label> '
  237. . '<input type="text" name="author" class="jp-carousel-comment-form-field jp-carousel-comment-form-text-field" id="jp-carousel-comment-form-author-field" /></fieldset>'
  238. . '<fieldset><label for="url">' . __( 'Website', 'jetpack' ) . '</label> '
  239. . '<input type="text" name="url" class="jp-carousel-comment-form-field jp-carousel-comment-form-text-field" id="jp-carousel-comment-form-url-field" /></fieldset>';
  240. }
  241. }
  242. }
  243. /**
  244. * Handle WP stats for images in full-screen.
  245. * Build string with tracking info.
  246. */
  247. /**
  248. * Filter if Jetpack should enable stats collection on carousel views
  249. *
  250. * @module carousel
  251. *
  252. * @since 4.3.2
  253. *
  254. * @param bool Enable Jetpack Carousel stat collection. Default false.
  255. */
  256. if ( apply_filters( 'jetpack_enable_carousel_stats', false ) && in_array( 'stats', Jetpack::get_active_modules() ) && ! Jetpack::is_development_mode() ) {
  257. $localize_strings['stats'] = 'blog=' . Jetpack_Options::get_option( 'id' ) . '&host=' . parse_url( get_option( 'home' ), PHP_URL_HOST ) . '&v=ext&j=' . JETPACK__API_VERSION . ':' . JETPACK__VERSION;
  258. // Set the stats as empty if user is logged in but logged-in users shouldn't be tracked.
  259. if ( is_user_logged_in() && function_exists( 'stats_get_options' ) ) {
  260. $stats_options = stats_get_options();
  261. $track_loggedin_users = isset( $stats_options['reg_users'] ) ? (bool) $stats_options['reg_users'] : false;
  262. if ( ! $track_loggedin_users ) {
  263. $localize_strings['stats'] = '';
  264. }
  265. }
  266. }
  267. /**
  268. * Filter the strings passed to the Carousel's js file.
  269. *
  270. * @module carousel
  271. *
  272. * @since 1.6.0
  273. *
  274. * @param array $localize_strings Array of strings passed to the Jetpack js file.
  275. */
  276. $localize_strings = apply_filters( 'jp_carousel_localize_strings', $localize_strings );
  277. wp_localize_script( 'jetpack-carousel', 'jetpackCarouselStrings', $localize_strings );
  278. wp_enqueue_style( 'jetpack-carousel', plugins_url( 'jetpack-carousel.css', __FILE__ ), array(), $this->asset_version( '20120629' ) );
  279. wp_style_add_data( 'jetpack-carousel', 'rtl', 'replace' );
  280. wp_register_style( 'jetpack-carousel-ie8fix', plugins_url( 'jetpack-carousel-ie8fix.css', __FILE__ ), array(), $this->asset_version( '20121024' ) );
  281. $GLOBALS['wp_styles']->add_data( 'jetpack-carousel-ie8fix', 'conditional', 'lte IE 8' );
  282. wp_enqueue_style( 'jetpack-carousel-ie8fix' );
  283. /**
  284. * Fires after carousel assets are enqueued for the first time.
  285. * Allows for adding additional assets to the carousel page.
  286. *
  287. * @module carousel
  288. *
  289. * @since 1.6.0
  290. *
  291. * @param bool $first_run First load if Carousel on the page.
  292. * @param array $localized_strings Array of strings passed to the Jetpack js file.
  293. */
  294. do_action( 'jp_carousel_enqueue_assets', $this->first_run, $localize_strings );
  295. $this->first_run = false;
  296. }
  297. }
  298. function set_in_gallery( $output ) {
  299. $this->in_gallery = true;
  300. return $output;
  301. }
  302. /**
  303. * Adds data-* attributes required by carousel to img tags in post HTML
  304. * content. To be used by 'the_content' filter.
  305. *
  306. * @see add_data_to_images()
  307. * @see wp_make_content_images_responsive() in wp-includes/media.php
  308. *
  309. * @param string $content HTML content of the post
  310. * @return string Modified HTML content of the post
  311. */
  312. function add_data_img_tags_and_enqueue_assets( $content ) {
  313. if ( ! preg_match_all( '/<img [^>]+>/', $content, $matches ) ) {
  314. return $content;
  315. }
  316. $selected_images = array();
  317. foreach( $matches[0] as $image_html ) {
  318. if ( preg_match( '/wp-image-([0-9]+)/i', $image_html, $class_id ) &&
  319. ( $attachment_id = absint( $class_id[1] ) ) ) {
  320. /*
  321. * If exactly the same image tag is used more than once, overwrite it.
  322. * All identical tags will be replaced later with 'str_replace()'.
  323. */
  324. $selected_images[ $attachment_id ] = $image_html;
  325. }
  326. }
  327. $find = array();
  328. $replace = array();
  329. if ( empty( $selected_images ) ) {
  330. return $content;
  331. }
  332. $attachments = get_posts( array(
  333. 'include' => array_keys( $selected_images ),
  334. 'post_type' => 'any',
  335. 'post_status' => 'any',
  336. 'suppress_filters' => false,
  337. ) );
  338. foreach ( $attachments as $attachment ) {
  339. $image_html = $selected_images[ $attachment->ID ];
  340. $attributes = $this->add_data_to_images( array(), $attachment );
  341. $attributes_html = '';
  342. foreach( $attributes as $k => $v ) {
  343. $attributes_html .= esc_attr( $k ) . '="' . esc_attr( $v ) . '" ';
  344. }
  345. $find[] = $image_html;
  346. $replace[] = str_replace( '<img ', "<img $attributes_html", $image_html );
  347. }
  348. $content = str_replace( $find, $replace, $content );
  349. $this->enqueue_assets();
  350. return $content;
  351. }
  352. function add_data_to_images( $attr, $attachment = null ) {
  353. $attachment_id = intval( $attachment->ID );
  354. if ( ! wp_attachment_is_image( $attachment_id ) ) {
  355. return $attr;
  356. }
  357. $orig_file = wp_get_attachment_image_src( $attachment_id, 'full' );
  358. $orig_file = isset( $orig_file[0] ) ? $orig_file[0] : wp_get_attachment_url( $attachment_id );
  359. $meta = wp_get_attachment_metadata( $attachment_id );
  360. $size = isset( $meta['width'] ) ? intval( $meta['width'] ) . ',' . intval( $meta['height'] ) : '';
  361. $img_meta = ( ! empty( $meta['image_meta'] ) ) ? (array) $meta['image_meta'] : array();
  362. $comments_opened = intval( comments_open( $attachment_id ) );
  363. /*
  364. * Note: Cannot generate a filename from the width and height wp_get_attachment_image_src() returns because
  365. * it takes the $content_width global variable themes can set in consideration, therefore returning sizes
  366. * which when used to generate a filename will likely result in a 404 on the image.
  367. * $content_width has no filter we could temporarily de-register, run wp_get_attachment_image_src(), then
  368. * re-register. So using returned file URL instead, which we can define the sizes from through filename
  369. * parsing in the JS, as this is a failsafe file reference.
  370. *
  371. * EG with Twenty Eleven activated:
  372. * array(4) { [0]=> string(82) "http://vanillawpinstall.blah/wp-content/uploads/2012/06/IMG_3534-1024x764.jpg" [1]=> int(584) [2]=> int(435) [3]=> bool(true) }
  373. *
  374. * EG with Twenty Ten activated:
  375. * array(4) { [0]=> string(82) "http://vanillawpinstall.blah/wp-content/uploads/2012/06/IMG_3534-1024x764.jpg" [1]=> int(640) [2]=> int(477) [3]=> bool(true) }
  376. */
  377. $medium_file_info = wp_get_attachment_image_src( $attachment_id, 'medium' );
  378. $medium_file = isset( $medium_file_info[0] ) ? $medium_file_info[0] : '';
  379. $large_file_info = wp_get_attachment_image_src( $attachment_id, 'large' );
  380. $large_file = isset( $large_file_info[0] ) ? $large_file_info[0] : '';
  381. $attachment = get_post( $attachment_id );
  382. $attachment_title = wptexturize( $attachment->post_title );
  383. $attachment_desc = wpautop( wptexturize( $attachment->post_content ) );
  384. // Not yet providing geo-data, need to "fuzzify" for privacy
  385. if ( ! empty( $img_meta ) ) {
  386. foreach ( $img_meta as $k => $v ) {
  387. if ( 'latitude' == $k || 'longitude' == $k )
  388. unset( $img_meta[$k] );
  389. }
  390. }
  391. // See https://github.com/Automattic/jetpack/issues/2765
  392. if ( isset( $img_meta['keywords'] ) ) {
  393. unset( $img_meta['keywords'] );
  394. }
  395. $img_meta = json_encode( array_map( 'strval', array_filter( $img_meta, 'is_scalar' ) ) );
  396. $attr['data-attachment-id'] = $attachment_id;
  397. $attr['data-permalink'] = esc_attr( get_permalink( $attachment->ID ) );
  398. $attr['data-orig-file'] = esc_attr( $orig_file );
  399. $attr['data-orig-size'] = $size;
  400. $attr['data-comments-opened'] = $comments_opened;
  401. $attr['data-image-meta'] = esc_attr( $img_meta );
  402. $attr['data-image-title'] = esc_attr( htmlspecialchars( $attachment_title ) );
  403. $attr['data-image-description'] = esc_attr( htmlspecialchars( $attachment_desc ) );
  404. $attr['data-medium-file'] = esc_attr( $medium_file );
  405. $attr['data-large-file'] = esc_attr( $large_file );
  406. return $attr;
  407. }
  408. function add_data_to_container( $html ) {
  409. global $post;
  410. if ( isset( $post ) ) {
  411. $blog_id = (int) get_current_blog_id();
  412. $extra_data = array(
  413. 'data-carousel-extra' => array(
  414. 'blog_id' => $blog_id,
  415. 'permalink' => get_permalink( $post->ID ),
  416. )
  417. );
  418. /**
  419. * Filter the data added to the Gallery container.
  420. *
  421. * @module carousel
  422. *
  423. * @since 1.6.0
  424. *
  425. * @param array $extra_data Array of data about the site and the post.
  426. */
  427. $extra_data = apply_filters( 'jp_carousel_add_data_to_container', $extra_data );
  428. foreach ( (array) $extra_data as $data_key => $data_values ) {
  429. $html = str_replace( '<div ', '<div ' . esc_attr( $data_key ) . "='" . json_encode( $data_values ) . "' ", $html );
  430. }
  431. }
  432. return $html;
  433. }
  434. function get_attachment_comments() {
  435. if ( ! headers_sent() )
  436. header('Content-type: text/javascript');
  437. /**
  438. * Allows for the checking of privileges of the blog user before comments
  439. * are packaged as JSON and sent back from the get_attachment_comments
  440. * AJAX endpoint
  441. *
  442. * @module carousel
  443. *
  444. * @since 1.6.0
  445. */
  446. do_action('jp_carousel_check_blog_user_privileges');
  447. $attachment_id = ( isset( $_REQUEST['id'] ) ) ? (int) $_REQUEST['id'] : 0;
  448. $offset = ( isset( $_REQUEST['offset'] ) ) ? (int) $_REQUEST['offset'] : 0;
  449. if ( ! $attachment_id ) {
  450. echo json_encode( __( 'Missing attachment ID.', 'jetpack' ) );
  451. die();
  452. }
  453. if ( $offset < 1 )
  454. $offset = 0;
  455. $comments = get_comments( array(
  456. 'status' => 'approve',
  457. 'order' => ( 'asc' == get_option('comment_order') ) ? 'ASC' : 'DESC',
  458. 'number' => 10,
  459. 'offset' => $offset,
  460. 'post_id' => $attachment_id,
  461. ) );
  462. $out = array();
  463. // Can't just send the results, they contain the commenter's email address.
  464. foreach ( $comments as $comment ) {
  465. $avatar = get_avatar( $comment->comment_author_email, 64 );
  466. if( ! $avatar )
  467. $avatar = '';
  468. $out[] = array(
  469. 'id' => $comment->comment_ID,
  470. 'parent_id' => $comment->comment_parent,
  471. 'author_markup' => get_comment_author_link( $comment->comment_ID ),
  472. 'gravatar_markup' => $avatar,
  473. 'date_gmt' => $comment->comment_date_gmt,
  474. 'content' => wpautop($comment->comment_content),
  475. );
  476. }
  477. die( json_encode( $out ) );
  478. }
  479. function post_attachment_comment() {
  480. if ( ! headers_sent() )
  481. header('Content-type: text/javascript');
  482. if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce($_POST['nonce'], 'carousel_nonce') )
  483. die( json_encode( array( 'error' => __( 'Nonce verification failed.', 'jetpack' ) ) ) );
  484. $_blog_id = (int) $_POST['blog_id'];
  485. $_post_id = (int) $_POST['id'];
  486. $comment = $_POST['comment'];
  487. if ( empty( $_blog_id ) )
  488. die( json_encode( array( 'error' => __( 'Missing target blog ID.', 'jetpack' ) ) ) );
  489. if ( empty( $_post_id ) )
  490. die( json_encode( array( 'error' => __( 'Missing target post ID.', 'jetpack' ) ) ) );
  491. if ( empty( $comment ) )
  492. die( json_encode( array( 'error' => __( 'No comment text was submitted.', 'jetpack' ) ) ) );
  493. // Used in context like NewDash
  494. $switched = false;
  495. if ( is_multisite() && $_blog_id != get_current_blog_id() ) {
  496. switch_to_blog( $_blog_id );
  497. $switched = true;
  498. }
  499. /** This action is documented in modules/carousel/jetpack-carousel.php */
  500. do_action('jp_carousel_check_blog_user_privileges');
  501. if ( ! comments_open( $_post_id ) )
  502. die( json_encode( array( 'error' => __( 'Comments on this post are closed.', 'jetpack' ) ) ) );
  503. if ( is_user_logged_in() ) {
  504. $user = wp_get_current_user();
  505. $user_id = $user->ID;
  506. $display_name = $user->display_name;
  507. $email = $user->user_email;
  508. $url = $user->user_url;
  509. if ( empty( $user_id ) )
  510. die( json_encode( array( 'error' => __( 'Sorry, but we could not authenticate your request.', 'jetpack' ) ) ) );
  511. } else {
  512. $user_id = 0;
  513. $display_name = $_POST['author'];
  514. $email = $_POST['email'];
  515. $url = $_POST['url'];
  516. if ( get_option( 'require_name_email' ) ) {
  517. if ( empty( $display_name ) )
  518. die( json_encode( array( 'error' => __( 'Please provide your name.', 'jetpack' ) ) ) );
  519. if ( empty( $email ) )
  520. die( json_encode( array( 'error' => __( 'Please provide an email address.', 'jetpack' ) ) ) );
  521. if ( ! is_email( $email ) )
  522. die( json_encode( array( 'error' => __( 'Please provide a valid email address.', 'jetpack' ) ) ) );
  523. }
  524. }
  525. $comment_data = array(
  526. 'comment_content' => $comment,
  527. 'comment_post_ID' => $_post_id,
  528. 'comment_author' => $display_name,
  529. 'comment_author_email' => $email,
  530. 'comment_author_url' => $url,
  531. 'comment_approved' => 0,
  532. 'comment_type' => '',
  533. );
  534. if ( ! empty( $user_id ) )
  535. $comment_data['user_id'] = $user_id;
  536. // Note: wp_new_comment() sanitizes and validates the values (too).
  537. $comment_id = wp_new_comment( $comment_data );
  538. /**
  539. * Fires before adding a new comment to the database via the get_attachment_comments ajax endpoint.
  540. *
  541. * @module carousel
  542. *
  543. * @since 1.6.0
  544. */
  545. do_action( 'jp_carousel_post_attachment_comment' );
  546. $comment_status = wp_get_comment_status( $comment_id );
  547. if ( true == $switched )
  548. restore_current_blog();
  549. die( json_encode( array( 'comment_id' => $comment_id, 'comment_status' => $comment_status ) ) );
  550. }
  551. function register_settings() {
  552. add_settings_section('carousel_section', __( 'Image Gallery Carousel', 'jetpack' ), array( $this, 'carousel_section_callback' ), 'media');
  553. if ( ! $this->in_jetpack ) {
  554. add_settings_field('carousel_enable_it', __( 'Enable carousel', 'jetpack' ), array( $this, 'carousel_enable_it_callback' ), 'media', 'carousel_section' );
  555. register_setting( 'media', 'carousel_enable_it', array( $this, 'carousel_enable_it_sanitize' ) );
  556. }
  557. add_settings_field('carousel_background_color', __( 'Background color', 'jetpack' ), array( $this, 'carousel_background_color_callback' ), 'media', 'carousel_section' );
  558. register_setting( 'media', 'carousel_background_color', array( $this, 'carousel_background_color_sanitize' ) );
  559. add_settings_field('carousel_display_exif', __( 'Metadata', 'jetpack'), array( $this, 'carousel_display_exif_callback' ), 'media', 'carousel_section' );
  560. register_setting( 'media', 'carousel_display_exif', array( $this, 'carousel_display_exif_sanitize' ) );
  561. // No geo setting yet, need to "fuzzify" data first, for privacy
  562. // add_settings_field('carousel_display_geo', __( 'Geolocation', 'jetpack' ), array( $this, 'carousel_display_geo_callback' ), 'media', 'carousel_section' );
  563. // register_setting( 'media', 'carousel_display_geo', array( $this, 'carousel_display_geo_sanitize' ) );
  564. }
  565. // Fulfill the settings section callback requirement by returning nothing
  566. function carousel_section_callback() {
  567. return;
  568. }
  569. function test_1or0_option( $value, $default_to_1 = true ) {
  570. if ( true == $default_to_1 ) {
  571. // Binary false (===) of $value means it has not yet been set, in which case we do want to default sites to 1
  572. if ( false === $value )
  573. $value = 1;
  574. }
  575. return ( 1 == $value ) ? 1 : 0;
  576. }
  577. function sanitize_1or0_option( $value ) {
  578. return ( 1 == $value ) ? 1 : 0;
  579. }
  580. function settings_checkbox($name, $label_text, $extra_text = '', $default_to_checked = true) {
  581. if ( empty( $name ) )
  582. return;
  583. $option = $this->test_1or0_option( get_option( $name ), $default_to_checked );
  584. echo '<fieldset>';
  585. echo '<input type="checkbox" name="'.esc_attr($name).'" id="'.esc_attr($name).'" value="1" ';
  586. checked( '1', $option );
  587. echo '/> <label for="'.esc_attr($name).'">'.$label_text.'</label>';
  588. if ( ! empty( $extra_text ) )
  589. echo '<p class="description">'.$extra_text.'</p>';
  590. echo '</fieldset>';
  591. }
  592. function settings_select($name, $values, $extra_text = '') {
  593. if ( empty( $name ) || ! is_array( $values ) || empty( $values ) )
  594. return;
  595. $option = get_option( $name );
  596. echo '<fieldset>';
  597. echo '<select name="'.esc_attr($name).'" id="'.esc_attr($name).'">';
  598. foreach( $values as $key => $value ) {
  599. echo '<option value="'.esc_attr($key).'" ';
  600. selected( $key, $option );
  601. echo '>'.esc_html($value).'</option>';
  602. }
  603. echo '</select>';
  604. if ( ! empty( $extra_text ) )
  605. echo '<p class="description">'.$extra_text.'</p>';
  606. echo '</fieldset>';
  607. }
  608. function carousel_display_exif_callback() {
  609. $this->settings_checkbox( 'carousel_display_exif', __( 'Show photo metadata (<a href="http://en.wikipedia.org/wiki/Exchangeable_image_file_format" rel="noopener noreferrer" target="_blank">Exif</a>) in carousel, when available.', 'jetpack' ) );
  610. }
  611. function carousel_display_exif_sanitize( $value ) {
  612. return $this->sanitize_1or0_option( $value );
  613. }
  614. function carousel_display_geo_callback() {
  615. $this->settings_checkbox( 'carousel_display_geo', __( 'Show map of photo location in carousel, when available.', 'jetpack' ) );
  616. }
  617. function carousel_display_geo_sanitize( $value ) {
  618. return $this->sanitize_1or0_option( $value );
  619. }
  620. function carousel_background_color_callback() {
  621. $this->settings_select( 'carousel_background_color', array( 'black' => __( 'Black', 'jetpack' ), 'white' => __( 'White', 'jetpack' ) ) );
  622. }
  623. function carousel_background_color_sanitize( $value ) {
  624. return ( 'white' == $value ) ? 'white' : 'black';
  625. }
  626. function carousel_enable_it_callback() {
  627. $this->settings_checkbox( 'carousel_enable_it', __( 'Display images in full-size carousel slideshow.', 'jetpack' ) );
  628. }
  629. function carousel_enable_it_sanitize( $value ) {
  630. return $this->sanitize_1or0_option( $value );
  631. }
  632. }
  633. new Jetpack_Carousel;