Brak opisu

jetpack-carousel.php 45KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  1. <?php
  2. use Automattic\Jetpack\Assets;
  3. use Automattic\Jetpack\Status;
  4. /*
  5. Plugin Name: Jetpack Carousel
  6. Plugin URL: https://wordpress.com/
  7. Description: Transform your standard image galleries into an immersive full-screen experience.
  8. Version: 0.1
  9. Author: Automattic
  10. Released under the GPL v.2 license.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. */
  16. class Jetpack_Carousel {
  17. public $prebuilt_widths = array( 370, 700, 1000, 1200, 1400, 2000 );
  18. public $first_run = true;
  19. public $in_gallery = false;
  20. public $in_jetpack = true;
  21. public $single_image_gallery_enabled = false;
  22. public $single_image_gallery_enabled_media_file = false;
  23. function __construct() {
  24. add_action( 'init', array( $this, 'init' ) );
  25. }
  26. function init() {
  27. if ( $this->maybe_disable_jp_carousel() ) {
  28. return;
  29. }
  30. $this->in_jetpack = ( class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'enable_module_configurable' ) ) ? true : false;
  31. $this->single_image_gallery_enabled = ! $this->maybe_disable_jp_carousel_single_images();
  32. $this->single_image_gallery_enabled_media_file = $this->maybe_enable_jp_carousel_single_images_media_file();
  33. if ( is_admin() ) {
  34. // Register the Carousel-related related settings
  35. add_action( 'admin_init', array( $this, 'register_settings' ), 5 );
  36. if ( ! $this->in_jetpack ) {
  37. if ( 0 == $this->test_1or0_option( get_option( 'carousel_enable_it' ), true ) ) {
  38. return; // Carousel disabled, abort early, but still register setting so user can switch it back on
  39. }
  40. }
  41. // If in admin, register the ajax endpoints.
  42. add_action( 'wp_ajax_get_attachment_comments', array( $this, 'get_attachment_comments' ) );
  43. add_action( 'wp_ajax_nopriv_get_attachment_comments', array( $this, 'get_attachment_comments' ) );
  44. add_action( 'wp_ajax_post_attachment_comment', array( $this, 'post_attachment_comment' ) );
  45. add_action( 'wp_ajax_nopriv_post_attachment_comment', array( $this, 'post_attachment_comment' ) );
  46. } else {
  47. if ( ! $this->in_jetpack ) {
  48. if ( 0 == $this->test_1or0_option( get_option( 'carousel_enable_it' ), true ) ) {
  49. return; // Carousel disabled, abort early
  50. }
  51. }
  52. // If on front-end, do the Carousel thang.
  53. /**
  54. * Filter the array of default prebuilt widths used in Carousel.
  55. *
  56. * @module carousel
  57. *
  58. * @since 1.6.0
  59. *
  60. * @param array $this->prebuilt_widths Array of default widths.
  61. */
  62. $this->prebuilt_widths = apply_filters( 'jp_carousel_widths', $this->prebuilt_widths );
  63. // below: load later than other callbacks hooked it (e.g. 3rd party plugins handling gallery shortcode)
  64. add_filter( 'post_gallery', array( $this, 'check_if_shortcode_processed_and_enqueue_assets' ), 1000, 2 );
  65. add_filter( 'post_gallery', array( $this, 'set_in_gallery' ), -1000 );
  66. add_filter( 'gallery_style', array( $this, 'add_data_to_container' ) );
  67. add_filter( 'wp_get_attachment_image_attributes', array( $this, 'add_data_to_images' ), 10, 2 );
  68. add_filter( 'the_content', array( $this, 'check_content_for_blocks' ), 1 );
  69. add_filter( 'jetpack_tiled_galleries_block_content', array( $this, 'add_data_img_tags_and_enqueue_assets' ) );
  70. if ( $this->single_image_gallery_enabled ) {
  71. add_filter( 'the_content', array( $this, 'add_data_img_tags_and_enqueue_assets' ) );
  72. }
  73. }
  74. if ( $this->in_jetpack ) {
  75. Jetpack::enable_module_configurable( dirname( dirname( __FILE__ ) ) . '/carousel.php' );
  76. }
  77. }
  78. function maybe_disable_jp_carousel() {
  79. /**
  80. * Allow third-party plugins or themes to disable Carousel.
  81. *
  82. * @module carousel
  83. *
  84. * @since 1.6.0
  85. *
  86. * @param bool false Should Carousel be disabled? Default to false.
  87. */
  88. return apply_filters( 'jp_carousel_maybe_disable', false );
  89. }
  90. function maybe_disable_jp_carousel_single_images() {
  91. /**
  92. * Allow third-party plugins or themes to disable Carousel for single images.
  93. *
  94. * @module carousel
  95. *
  96. * @since 4.5.0
  97. *
  98. * @param bool false Should Carousel be disabled for single images? Default to false.
  99. */
  100. return apply_filters( 'jp_carousel_maybe_disable_single_images', false );
  101. }
  102. function maybe_enable_jp_carousel_single_images_media_file() {
  103. /**
  104. * Allow third-party plugins or themes to enable Carousel
  105. * for single images linking to 'Media File' (full size image).
  106. *
  107. * @module carousel
  108. *
  109. * @since 4.5.0
  110. *
  111. * @param bool false Should Carousel be enabled for single images linking to 'Media File'? Default to false.
  112. */
  113. return apply_filters( 'jp_carousel_load_for_images_linked_to_file', false );
  114. }
  115. function asset_version( $version ) {
  116. /**
  117. * Filter the version string used when enqueuing Carousel assets.
  118. *
  119. * @module carousel
  120. *
  121. * @since 1.6.0
  122. *
  123. * @param string $version Asset version.
  124. */
  125. return apply_filters( 'jp_carousel_asset_version', $version );
  126. }
  127. function display_bail_message( $output = '' ) {
  128. // Displays a message on top of gallery if carousel has bailed
  129. $message = '<div class="jp-carousel-msg"><p>';
  130. $message .= __( 'Jetpack\'s Carousel has been disabled, because another plugin or your theme is overriding the [gallery] shortcode.', 'jetpack' );
  131. $message .= '</p></div>';
  132. // put before gallery output
  133. $output = $message . $output;
  134. return $output;
  135. }
  136. function check_if_shortcode_processed_and_enqueue_assets( $output ) {
  137. if (
  138. class_exists( 'Jetpack_AMP_Support' )
  139. && Jetpack_AMP_Support::is_amp_request()
  140. ) {
  141. return $output;
  142. }
  143. if (
  144. ! empty( $output ) &&
  145. /**
  146. * Allow third-party plugins or themes to force-enable Carousel.
  147. *
  148. * @module carousel
  149. *
  150. * @since 1.9.0
  151. *
  152. * @param bool false Should we force enable Carousel? Default to false.
  153. */
  154. ! apply_filters( 'jp_carousel_force_enable', false )
  155. ) {
  156. // Bail because someone is overriding the [gallery] shortcode.
  157. remove_filter( 'gallery_style', array( $this, 'add_data_to_container' ) );
  158. remove_filter( 'wp_get_attachment_image_attributes', array( $this, 'add_data_to_images' ) );
  159. remove_filter( 'the_content', array( $this, 'add_data_img_tags_and_enqueue_assets' ) );
  160. // Display message that carousel has bailed, if user is super_admin, and if we're not on WordPress.com.
  161. if (
  162. is_super_admin() &&
  163. ! ( defined( 'IS_WPCOM' ) && IS_WPCOM )
  164. ) {
  165. add_filter( 'post_gallery', array( $this, 'display_bail_message' ) );
  166. }
  167. return $output;
  168. }
  169. /**
  170. * Fires when thumbnails are shown in Carousel.
  171. *
  172. * @module carousel
  173. *
  174. * @since 1.6.0
  175. **/
  176. do_action( 'jp_carousel_thumbnails_shown' );
  177. $this->enqueue_assets();
  178. return $output;
  179. }
  180. /**
  181. * Check if the content of a post uses gallery blocks. To be used by 'the_content' filter.
  182. *
  183. * @since 6.8.0
  184. *
  185. * @param string $content Post content.
  186. *
  187. * @return string $content Post content.
  188. */
  189. function check_content_for_blocks( $content ) {
  190. if (
  191. class_exists( 'Jetpack_AMP_Support' )
  192. && Jetpack_AMP_Support::is_amp_request()
  193. ) {
  194. return $content;
  195. }
  196. if ( has_block( 'gallery', $content ) || has_block( 'jetpack/tiled-gallery', $content ) ) {
  197. $this->enqueue_assets();
  198. $content = $this->add_data_to_container( $content );
  199. }
  200. return $content;
  201. }
  202. function enqueue_assets() {
  203. if ( $this->first_run ) {
  204. wp_enqueue_script(
  205. 'jetpack-carousel',
  206. Assets::get_file_url_for_environment(
  207. '_inc/build/carousel/jetpack-carousel.min.js',
  208. 'modules/carousel/jetpack-carousel.js'
  209. ),
  210. array(),
  211. $this->asset_version( JETPACK__VERSION ),
  212. true
  213. );
  214. $swiper_library_path = array(
  215. 'url' => Assets::get_file_url_for_environment(
  216. '_inc/build/carousel/swiper-bundle.min.js',
  217. 'modules/carousel/swiper-bundle.js'
  218. ),
  219. );
  220. wp_localize_script( 'jetpack-carousel', 'jetpackSwiperLibraryPath', $swiper_library_path );
  221. // 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)
  222. // Also: not hardcoding path since there is no guarantee site is running on site root in self-hosted context.
  223. $is_logged_in = is_user_logged_in();
  224. $comment_registration = (int) get_option( 'comment_registration' );
  225. $require_name_email = (int) get_option( 'require_name_email' );
  226. $localize_strings = array(
  227. 'widths' => $this->prebuilt_widths,
  228. 'is_logged_in' => $is_logged_in,
  229. 'lang' => strtolower( substr( get_locale(), 0, 2 ) ),
  230. 'ajaxurl' => set_url_scheme( admin_url( 'admin-ajax.php' ) ),
  231. 'nonce' => wp_create_nonce( 'carousel_nonce' ),
  232. 'display_exif' => $this->test_1or0_option( Jetpack_Options::get_option_and_ensure_autoload( 'carousel_display_exif', true ) ),
  233. 'display_comments' => $this->test_1or0_option( Jetpack_Options::get_option_and_ensure_autoload( 'carousel_display_comments', true ) ),
  234. 'display_geo' => $this->test_1or0_option( Jetpack_Options::get_option_and_ensure_autoload( 'carousel_display_geo', true ) ),
  235. 'single_image_gallery' => $this->single_image_gallery_enabled,
  236. 'single_image_gallery_media_file' => $this->single_image_gallery_enabled_media_file,
  237. 'background_color' => $this->carousel_background_color_sanitize( Jetpack_Options::get_option_and_ensure_autoload( 'carousel_background_color', '' ) ),
  238. 'comment' => __( 'Comment', 'jetpack' ),
  239. 'post_comment' => __( 'Post Comment', 'jetpack' ),
  240. 'write_comment' => __( 'Write a Comment...', 'jetpack' ),
  241. 'loading_comments' => __( 'Loading Comments...', 'jetpack' ),
  242. '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}' ),
  243. 'no_comment_text' => __( 'Please be sure to submit some text with your comment.', 'jetpack' ),
  244. 'no_comment_email' => __( 'Please provide an email address to comment.', 'jetpack' ),
  245. 'no_comment_author' => __( 'Please provide your name to comment.', 'jetpack' ),
  246. 'comment_post_error' => __( 'Sorry, but there was an error posting your comment. Please try again later.', 'jetpack' ),
  247. 'comment_approved' => __( 'Your comment was approved.', 'jetpack' ),
  248. 'comment_unapproved' => __( 'Your comment is in moderation.', 'jetpack' ),
  249. 'camera' => __( 'Camera', 'jetpack' ),
  250. 'aperture' => __( 'Aperture', 'jetpack' ),
  251. 'shutter_speed' => __( 'Shutter Speed', 'jetpack' ),
  252. 'focal_length' => __( 'Focal Length', 'jetpack' ),
  253. 'copyright' => __( 'Copyright', 'jetpack' ),
  254. 'comment_registration' => $comment_registration,
  255. 'require_name_email' => $require_name_email,
  256. /** This action is documented in core/src/wp-includes/link-template.php */
  257. 'login_url' => wp_login_url( apply_filters( 'the_permalink', get_permalink() ) ),
  258. 'blog_id' => (int) get_current_blog_id(),
  259. 'meta_data' => array( 'camera', 'aperture', 'shutter_speed', 'focal_length', 'copyright' ),
  260. );
  261. /**
  262. * Handle WP stats for images in full-screen.
  263. * Build string with tracking info.
  264. */
  265. /**
  266. * Filter if Jetpack should enable stats collection on carousel views
  267. *
  268. * @module carousel
  269. *
  270. * @since 4.3.2
  271. *
  272. * @param bool Enable Jetpack Carousel stat collection. Default false.
  273. */
  274. if ( apply_filters( 'jetpack_enable_carousel_stats', false ) && in_array( 'stats', Jetpack::get_active_modules(), true ) && ! ( new Status() )->is_offline_mode() ) {
  275. $localize_strings['stats'] = 'blog=' . Jetpack_Options::get_option( 'id' ) . '&host=' . wp_parse_url( get_option( 'home' ), PHP_URL_HOST ) . '&v=ext&j=' . JETPACK__API_VERSION . ':' . JETPACK__VERSION;
  276. // Set the stats as empty if user is logged in but logged-in users shouldn't be tracked.
  277. if ( is_user_logged_in() && function_exists( 'stats_get_options' ) ) {
  278. $stats_options = stats_get_options();
  279. $track_loggedin_users = isset( $stats_options['reg_users'] ) ? (bool) $stats_options['reg_users'] : false;
  280. if ( ! $track_loggedin_users ) {
  281. $localize_strings['stats'] = '';
  282. }
  283. }
  284. }
  285. /**
  286. * Filter the strings passed to the Carousel's js file.
  287. *
  288. * @module carousel
  289. *
  290. * @since 1.6.0
  291. *
  292. * @param array $localize_strings Array of strings passed to the Jetpack js file.
  293. */
  294. $localize_strings = apply_filters( 'jp_carousel_localize_strings', $localize_strings );
  295. wp_localize_script( 'jetpack-carousel', 'jetpackCarouselStrings', $localize_strings );
  296. wp_enqueue_style(
  297. 'jetpack-carousel-swiper-css',
  298. plugins_url( 'swiper-bundle.css', __FILE__ ),
  299. array(),
  300. $this->asset_version( JETPACK__VERSION )
  301. );
  302. wp_enqueue_style( 'jetpack-carousel', plugins_url( 'jetpack-carousel.css', __FILE__ ), array(), $this->asset_version( JETPACK__VERSION ) );
  303. wp_style_add_data( 'jetpack-carousel', 'rtl', 'replace' );
  304. /**
  305. * Fires after carousel assets are enqueued for the first time.
  306. * Allows for adding additional assets to the carousel page.
  307. *
  308. * @module carousel
  309. *
  310. * @since 1.6.0
  311. *
  312. * @param bool $first_run First load if Carousel on the page.
  313. * @param array $localized_strings Array of strings passed to the Jetpack js file.
  314. */
  315. do_action( 'jp_carousel_enqueue_assets', $this->first_run, $localize_strings );
  316. // Add the carousel skeleton to the page.
  317. $this->localize_strings = $localize_strings;
  318. add_action( 'wp_footer', array( $this, 'add_carousel_skeleton' ) );
  319. $this->first_run = false;
  320. }
  321. }
  322. /**
  323. * Generate the HTML skeleton that will be picked up by the Carousel JS and used for showing the carousel.
  324. */
  325. public function add_carousel_skeleton() {
  326. $localize_strings = $this->localize_strings;
  327. $is_light = ( 'white' === $localize_strings['background_color'] );
  328. // Determine whether to fall back to standard local comments.
  329. $use_local_comments = ! isset( $localize_strings['jetpack_comments_iframe_src'] ) || empty( $localize_strings['jetpack_comments_iframe_src'] );
  330. $current_user = wp_get_current_user();
  331. $require_name_email = (int) get_option( 'require_name_email' );
  332. /* translators: %s is replaced with a field name in the form, e.g. "Email" */
  333. $required = ( $require_name_email ) ? __( '%s (Required)', 'jetpack' ) : '%s';
  334. ?>
  335. <div id="jp-carousel-loading-overlay">
  336. <div id="jp-carousel-loading-wrapper">
  337. <span id="jp-carousel-library-loading">&nbsp;</span>
  338. </div>
  339. </div>
  340. <div class="jp-carousel-overlay<?php echo( $is_light ? ' jp-carousel-light' : '' ); ?>" style="display: none;">
  341. <div class="jp-carousel-container<?php echo( $is_light ? ' jp-carousel-light' : '' ); ?>">
  342. <!-- The Carousel Swiper -->
  343. <div
  344. class="jp-carousel-wrap swiper-container jp-carousel-swiper-container jp-carousel-transitions"
  345. itemscope
  346. itemtype="https://schema.org/ImageGallery">
  347. <div class="jp-carousel swiper-wrapper"></div>
  348. <div class="jp-swiper-button-prev swiper-button-prev">
  349. <svg width="25" height="24" viewBox="0 0 25 24" fill="none" xmlns="http://www.w3.org/2000/svg">
  350. <mask id="maskPrev" mask-type="alpha" maskUnits="userSpaceOnUse" x="8" y="6" width="9" height="12">
  351. <path d="M16.2072 16.59L11.6496 12L16.2072 7.41L14.8041 6L8.8335 12L14.8041 18L16.2072 16.59Z" fill="white"/>
  352. </mask>
  353. <g mask="url(#maskPrev)">
  354. <rect x="0.579102" width="23.8823" height="24" fill="#FFFFFF"/>
  355. </g>
  356. </svg>
  357. </div>
  358. <div class="jp-swiper-button-next swiper-button-next">
  359. <svg width="25" height="24" viewBox="0 0 25 24" fill="none" xmlns="http://www.w3.org/2000/svg">
  360. <mask id="maskNext" mask-type="alpha" maskUnits="userSpaceOnUse" x="8" y="6" width="8" height="12">
  361. <path d="M8.59814 16.59L13.1557 12L8.59814 7.41L10.0012 6L15.9718 12L10.0012 18L8.59814 16.59Z" fill="white"/>
  362. </mask>
  363. <g mask="url(#maskNext)">
  364. <rect x="0.34375" width="23.8822" height="24" fill="#FFFFFF"/>
  365. </g>
  366. </svg>
  367. </div>
  368. </div>
  369. <!-- The main close buton -->
  370. <div class="jp-carousel-close-hint">
  371. <svg width="25" height="24" viewBox="0 0 25 24" fill="none" xmlns="http://www.w3.org/2000/svg">
  372. <mask id="maskClose" mask-type="alpha" maskUnits="userSpaceOnUse" x="5" y="5" width="15" height="14">
  373. <path d="M19.3166 6.41L17.9135 5L12.3509 10.59L6.78834 5L5.38525 6.41L10.9478 12L5.38525 17.59L6.78834 19L12.3509 13.41L17.9135 19L19.3166 17.59L13.754 12L19.3166 6.41Z" fill="white"/>
  374. </mask>
  375. <g mask="url(#maskClose)">
  376. <rect x="0.409668" width="23.8823" height="24" fill="#FFFFFF"/>
  377. </g>
  378. </svg>
  379. </div>
  380. <!-- Image info, comments and meta -->
  381. <div class="jp-carousel-info">
  382. <div class="jp-carousel-info-footer">
  383. <div class="jp-carousel-pagination-container">
  384. <div class="jp-swiper-pagination swiper-pagination"></div>
  385. <div class="jp-carousel-pagination"></div>
  386. </div>
  387. <div class="jp-carousel-photo-title-container">
  388. <h2 class="jp-carousel-photo-caption"></h2>
  389. </div>
  390. <div class="jp-carousel-photo-icons-container">
  391. <a href="#" class="jp-carousel-icon-btn jp-carousel-icon-info" aria-label="<?php esc_attr_e( 'Toggle photo metadata visibility', 'jetpack' ); ?>">
  392. <span class="jp-carousel-icon">
  393. <svg width="25" height="24" viewBox="0 0 25 24" fill="none" xmlns="http://www.w3.org/2000/svg">
  394. <mask id="maskInfo" mask-type="alpha" maskUnits="userSpaceOnUse" x="2" y="2" width="21" height="20">
  395. <path fill-rule="evenodd" clip-rule="evenodd" d="M12.7537 2C7.26076 2 2.80273 6.48 2.80273 12C2.80273 17.52 7.26076 22 12.7537 22C18.2466 22 22.7046 17.52 22.7046 12C22.7046 6.48 18.2466 2 12.7537 2ZM11.7586 7V9H13.7488V7H11.7586ZM11.7586 11V17H13.7488V11H11.7586ZM4.79292 12C4.79292 16.41 8.36531 20 12.7537 20C17.142 20 20.7144 16.41 20.7144 12C20.7144 7.59 17.142 4 12.7537 4C8.36531 4 4.79292 7.59 4.79292 12Z" fill="white"/>
  396. </mask>
  397. <g mask="url(#maskInfo)">
  398. <rect x="0.8125" width="23.8823" height="24" fill="#FFFFFF"/>
  399. </g>
  400. </svg>
  401. </span>
  402. </a>
  403. <?php if ( $localize_strings['display_comments'] ) : ?>
  404. <a href="#" class="jp-carousel-icon-btn jp-carousel-icon-comments" aria-label="<?php esc_attr_e( 'Toggle photo comments visibility', 'jetpack' ); ?>">
  405. <span class="jp-carousel-icon">
  406. <svg width="25" height="24" viewBox="0 0 25 24" fill="none" xmlns="http://www.w3.org/2000/svg">
  407. <mask id="maskComments" mask-type="alpha" maskUnits="userSpaceOnUse" x="2" y="2" width="21" height="20">
  408. <path fill-rule="evenodd" clip-rule="evenodd" d="M4.3271 2H20.2486C21.3432 2 22.2388 2.9 22.2388 4V16C22.2388 17.1 21.3432 18 20.2486 18H6.31729L2.33691 22V4C2.33691 2.9 3.2325 2 4.3271 2ZM6.31729 16H20.2486V4H4.3271V18L6.31729 16Z" fill="white"/>
  409. </mask>
  410. <g mask="url(#maskComments)">
  411. <rect x="0.34668" width="23.8823" height="24" fill="#FFFFFF"/>
  412. </g>
  413. </svg>
  414. <span class="jp-carousel-has-comments-indicator" aria-label="<?php esc_attr_e( 'This image has comments.', 'jetpack' ); ?>"></span>
  415. </span>
  416. </a>
  417. <?php endif; ?>
  418. </div>
  419. </div>
  420. <div class="jp-carousel-info-extra">
  421. <div class="jp-carousel-info-content-wrapper">
  422. <div class="jp-carousel-photo-title-container">
  423. <h2 class="jp-carousel-photo-title"></h2>
  424. </div>
  425. <div class="jp-carousel-comments-wrapper">
  426. <?php if ( $localize_strings['display_comments'] ) : ?>
  427. <div id="jp-carousel-comments-loading">
  428. <span><?php echo esc_html( $localize_strings['loading_comments'] ); ?></span>
  429. </div>
  430. <div class="jp-carousel-comments"></div>
  431. <div id="jp-carousel-comment-form-container">
  432. <span id="jp-carousel-comment-form-spinner">&nbsp;</span>
  433. <div id="jp-carousel-comment-post-results"></div>
  434. <?php if ( $use_local_comments ) : ?>
  435. <?php if ( ! $localize_strings['is_logged_in'] && $localize_strings['comment_registration'] ) : ?>
  436. <div id="jp-carousel-comment-form-commenting-as">
  437. <p id="jp-carousel-commenting-as">
  438. <?php
  439. echo wp_kses(
  440. __( 'You must be <a href="#" class="jp-carousel-comment-login">logged in</a> to post a comment.', 'jetpack' ),
  441. array(
  442. 'a' => array(
  443. 'href' => array(),
  444. 'class' => array(),
  445. ),
  446. )
  447. );
  448. ?>
  449. </p>
  450. </div>
  451. <?php else : ?>
  452. <form id="jp-carousel-comment-form">
  453. <label for="jp-carousel-comment-form-comment-field" class="screen-reader-text"><?php echo esc_attr( $localize_strings['write_comment'] ); ?></label>
  454. <textarea
  455. name="comment"
  456. class="jp-carousel-comment-form-field jp-carousel-comment-form-textarea"
  457. id="jp-carousel-comment-form-comment-field"
  458. placeholder="<?php echo esc_attr( $localize_strings['write_comment'] ); ?>"
  459. ></textarea>
  460. <div id="jp-carousel-comment-form-submit-and-info-wrapper">
  461. <div id="jp-carousel-comment-form-commenting-as">
  462. <?php if ( $localize_strings['is_logged_in'] ) : ?>
  463. <p id="jp-carousel-commenting-as">
  464. <?php
  465. printf(
  466. /* translators: %s is replaced with the user's display name */
  467. esc_html__( 'Commenting as %s', 'jetpack' ),
  468. esc_html( $current_user->data->display_name )
  469. );
  470. ?>
  471. </p>
  472. <?php else : ?>
  473. <fieldset>
  474. <label for="jp-carousel-comment-form-email-field"><?php echo esc_html( sprintf( $required, __( 'Email', 'jetpack' ) ) ); ?></label>
  475. <input type="text" name="email" class="jp-carousel-comment-form-field jp-carousel-comment-form-text-field" id="jp-carousel-comment-form-email-field" />
  476. </fieldset>
  477. <fieldset>
  478. <label for="jp-carousel-comment-form-author-field"><?php echo esc_html( sprintf( $required, __( 'Name', 'jetpack' ) ) ); ?></label>
  479. <input type="text" name="author" class="jp-carousel-comment-form-field jp-carousel-comment-form-text-field" id="jp-carousel-comment-form-author-field" />
  480. </fieldset>
  481. <fieldset>
  482. <label for="jp-carousel-comment-form-url-field"><?php esc_html_e( 'Website', 'jetpack' ); ?></label>
  483. <input type="text" name="url" class="jp-carousel-comment-form-field jp-carousel-comment-form-text-field" id="jp-carousel-comment-form-url-field" />
  484. </fieldset>
  485. <?php endif ?>
  486. </div>
  487. <input
  488. type="submit"
  489. name="submit"
  490. class="jp-carousel-comment-form-button"
  491. id="jp-carousel-comment-form-button-submit"
  492. value="<?php echo esc_attr( $localize_strings['post_comment'] ); ?>" />
  493. </div>
  494. </form>
  495. <?php endif ?>
  496. <?php endif ?>
  497. </div>
  498. <?php endif ?>
  499. </div>
  500. <div class="jp-carousel-image-meta">
  501. <div class="jp-carousel-title-and-caption">
  502. <div class="jp-carousel-photo-info">
  503. <h3 class="jp-carousel-caption" itemprop="caption description"></h3>
  504. </div>
  505. <div class="jp-carousel-photo-description"></div>
  506. </div>
  507. <ul class="jp-carousel-image-exif" style="display: none;"></ul>
  508. <a class="jp-carousel-image-download" target="_blank" style="display: none;">
  509. <svg width="25" height="24" viewBox="0 0 25 24" fill="none" xmlns="http://www.w3.org/2000/svg">
  510. <mask id="mask0" mask-type="alpha" maskUnits="userSpaceOnUse" x="3" y="3" width="19" height="18">
  511. <path fill-rule="evenodd" clip-rule="evenodd" d="M5.84615 5V19H19.7775V12H21.7677V19C21.7677 20.1 20.8721 21 19.7775 21H5.84615C4.74159 21 3.85596 20.1 3.85596 19V5C3.85596 3.9 4.74159 3 5.84615 3H12.8118V5H5.84615ZM14.802 5V3H21.7677V10H19.7775V6.41L9.99569 16.24L8.59261 14.83L18.3744 5H14.802Z" fill="white"/>
  512. </mask>
  513. <g mask="url(#mask0)">
  514. <rect x="0.870605" width="23.8823" height="24" fill="#FFFFFF"/>
  515. </g>
  516. </svg>
  517. <span class="jp-carousel-download-text"></span>
  518. </a>
  519. <div class="jp-carousel-image-map" style="display: none;"></div>
  520. </div>
  521. </div>
  522. </div>
  523. </div>
  524. </div>
  525. </div>
  526. <?php
  527. }
  528. function set_in_gallery( $output ) {
  529. if (
  530. class_exists( 'Jetpack_AMP_Support' )
  531. && Jetpack_AMP_Support::is_amp_request()
  532. ) {
  533. return $output;
  534. }
  535. $this->in_gallery = true;
  536. return $output;
  537. }
  538. /**
  539. * Adds data-* attributes required by carousel to img tags in post HTML
  540. * content. To be used by 'the_content' filter.
  541. *
  542. * @see add_data_to_images()
  543. * @see wp_make_content_images_responsive() in wp-includes/media.php
  544. *
  545. * @param string $content HTML content of the post
  546. * @return string Modified HTML content of the post
  547. */
  548. function add_data_img_tags_and_enqueue_assets( $content ) {
  549. if (
  550. class_exists( 'Jetpack_AMP_Support' )
  551. && Jetpack_AMP_Support::is_amp_request()
  552. ) {
  553. return $this->maybe_add_amp_lightbox( $content );
  554. }
  555. if ( ! preg_match_all( '/<img [^>]+>/', $content, $matches ) ) {
  556. return $content;
  557. }
  558. $selected_images = array();
  559. foreach ( $matches[0] as $image_html ) {
  560. if ( preg_match( '/(wp-image-|data-id=)\"?([0-9]+)\"?/i', $image_html, $class_id ) &&
  561. ! preg_match( '/wp-block-jetpack-slideshow_image/', $image_html ) ) {
  562. $attachment_id = absint( $class_id[2] );
  563. /**
  564. * The same image tag may be used more than once but with different attribs,
  565. * so save each of them against the attachment id.
  566. */
  567. if ( ! isset( $selected_images[ $attachment_id ] ) || ! in_array( $image_html, $selected_images[ $attachment_id ], true ) ) {
  568. $selected_images[ $attachment_id ][] = $image_html;
  569. }
  570. }
  571. }
  572. $find = array();
  573. $replace = array();
  574. if ( empty( $selected_images ) ) {
  575. return $content;
  576. }
  577. $attachments = get_posts(
  578. array(
  579. 'include' => array_keys( $selected_images ),
  580. 'post_type' => 'any',
  581. 'post_status' => 'any',
  582. 'suppress_filters' => false,
  583. )
  584. );
  585. foreach ( $attachments as $attachment ) {
  586. $image_elements = $selected_images[ $attachment->ID ];
  587. $attributes = $this->add_data_to_images( array(), $attachment );
  588. $attributes_html = '';
  589. foreach ( $attributes as $k => $v ) {
  590. $attributes_html .= esc_attr( $k ) . '="' . esc_attr( $v ) . '" ';
  591. }
  592. foreach ( $image_elements as $image_html ) {
  593. $find[] = $image_html;
  594. $replace[] = str_replace( '<img ', "<img $attributes_html", $image_html );
  595. }
  596. }
  597. $content = str_replace( $find, $replace, $content );
  598. $this->enqueue_assets();
  599. return $content;
  600. }
  601. function add_data_to_images( $attr, $attachment = null ) {
  602. if (
  603. class_exists( 'Jetpack_AMP_Support' )
  604. && Jetpack_AMP_Support::is_amp_request()
  605. ) {
  606. return $attr;
  607. }
  608. $attachment_id = (int) $attachment->ID;
  609. if ( ! wp_attachment_is_image( $attachment_id ) ) {
  610. return $attr;
  611. }
  612. $orig_file = wp_get_attachment_image_src( $attachment_id, 'full' );
  613. $orig_file = isset( $orig_file[0] ) ? $orig_file[0] : wp_get_attachment_url( $attachment_id );
  614. $meta = wp_get_attachment_metadata( $attachment_id );
  615. $size = isset( $meta['width'] ) ? (int) $meta['width'] . ',' . (int) $meta['height'] : '';
  616. $img_meta = ( ! empty( $meta['image_meta'] ) ) ? (array) $meta['image_meta'] : array();
  617. $comments_opened = (int) comments_open( $attachment_id );
  618. /**
  619. * Note: Cannot generate a filename from the width and height wp_get_attachment_image_src() returns because
  620. * it takes the $content_width global variable themes can set in consideration, therefore returning sizes
  621. * which when used to generate a filename will likely result in a 404 on the image.
  622. * $content_width has no filter we could temporarily de-register, run wp_get_attachment_image_src(), then
  623. * re-register. So using returned file URL instead, which we can define the sizes from through filename
  624. * parsing in the JS, as this is a failsafe file reference.
  625. *
  626. * EG with Twenty Eleven activated:
  627. * 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) }
  628. *
  629. * EG with Twenty Ten activated:
  630. * 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) }
  631. */
  632. $medium_file_info = wp_get_attachment_image_src( $attachment_id, 'medium' );
  633. $medium_file = isset( $medium_file_info[0] ) ? $medium_file_info[0] : '';
  634. $large_file_info = wp_get_attachment_image_src( $attachment_id, 'large' );
  635. $large_file = isset( $large_file_info[0] ) ? $large_file_info[0] : '';
  636. $attachment = get_post( $attachment_id );
  637. $attachment_title = ! empty( $attachment ) ? wptexturize( $attachment->post_title ) : '';
  638. $attachment_desc = ! empty( $attachment ) ? wpautop( wptexturize( $attachment->post_content ) ) : '';
  639. $attachment_caption = ! empty( $attachment ) ? wpautop( wptexturize( $attachment->post_excerpt ) ) : '';
  640. // Not yet providing geo-data, need to "fuzzify" for privacy
  641. if ( ! empty( $img_meta ) ) {
  642. foreach ( $img_meta as $k => $v ) {
  643. if ( 'latitude' == $k || 'longitude' == $k ) {
  644. unset( $img_meta[ $k ] );
  645. }
  646. }
  647. }
  648. // See https://github.com/Automattic/jetpack/issues/2765
  649. if ( isset( $img_meta['keywords'] ) ) {
  650. unset( $img_meta['keywords'] );
  651. }
  652. $img_meta = json_encode( array_map( 'strval', array_filter( $img_meta, 'is_scalar' ) ) );
  653. $attr['data-attachment-id'] = $attachment_id;
  654. $attr['data-permalink'] = esc_attr( get_permalink( $attachment_id ) );
  655. $attr['data-orig-file'] = esc_attr( $orig_file );
  656. $attr['data-orig-size'] = $size;
  657. $attr['data-comments-opened'] = $comments_opened;
  658. $attr['data-image-meta'] = esc_attr( $img_meta );
  659. $attr['data-image-title'] = esc_attr( htmlspecialchars( $attachment_title ) );
  660. $attr['data-image-description'] = esc_attr( htmlspecialchars( $attachment_desc ) );
  661. $attr['data-image-caption'] = esc_attr( htmlspecialchars( $attachment_caption ) );
  662. $attr['data-medium-file'] = esc_attr( $medium_file );
  663. $attr['data-large-file'] = esc_attr( $large_file );
  664. return $attr;
  665. }
  666. function add_data_to_container( $html ) {
  667. global $post;
  668. if (
  669. class_exists( 'Jetpack_AMP_Support' )
  670. && Jetpack_AMP_Support::is_amp_request()
  671. ) {
  672. return $html;
  673. }
  674. if ( isset( $post ) ) {
  675. $blog_id = (int) get_current_blog_id();
  676. $extra_data = array(
  677. 'data-carousel-extra' => array(
  678. 'blog_id' => $blog_id,
  679. 'permalink' => get_permalink( $post->ID ),
  680. ),
  681. );
  682. /**
  683. * Filter the data added to the Gallery container.
  684. *
  685. * @module carousel
  686. *
  687. * @since 1.6.0
  688. *
  689. * @param array $extra_data Array of data about the site and the post.
  690. */
  691. $extra_data = apply_filters( 'jp_carousel_add_data_to_container', $extra_data );
  692. foreach ( (array) $extra_data as $data_key => $data_values ) {
  693. $html = str_replace( '<div ', '<div ' . esc_attr( $data_key ) . "='" . wp_json_encode( $data_values ) . "' ", $html );
  694. $html = str_replace( '<ul class="wp-block-gallery', '<ul ' . esc_attr( $data_key ) . "='" . wp_json_encode( $data_values ) . "' class=\"wp-block-gallery", $html );
  695. $html = str_replace( '<ul class="blocks-gallery-grid', '<ul ' . esc_attr( $data_key ) . "='" . wp_json_encode( $data_values ) . "' class=\"blocks-gallery-grid", $html );
  696. $html = preg_replace( '/\<figure([^>]*)class="(wp-block-gallery[^"]*?has-nested-images.*?)"/', '<figure ' . esc_attr( $data_key ) . "='" . wp_json_encode( $data_values ) . "' $1 class=\"$2\"", $html );
  697. }
  698. }
  699. return $html;
  700. }
  701. /**
  702. * Conditionally adds amp-lightbox to galleries and images.
  703. *
  704. * This applies to gallery blocks and shortcodes,
  705. * in addition to images that are wrapped in a link to the page.
  706. * Images wrapped in a link to the media file shouldn't get an amp-lightbox.
  707. *
  708. * @param string $content The content to possibly add amp-lightbox to.
  709. * @return string The content, with amp-lightbox possibly added.
  710. */
  711. public function maybe_add_amp_lightbox( $content ) {
  712. $content = preg_replace(
  713. array(
  714. '#(<figure)[^>]*(?=class=(["\']?)[^>]*wp-block-gallery[^>]*\2)#is', // Gallery block.
  715. '#(\[gallery)(?=\s+)#', // Gallery shortcode.
  716. ),
  717. array(
  718. '\1 data-amp-lightbox="true" ', // https://github.com/ampproject/amp-wp/blob/1094ea03bd5dc92889405a47a8c41de1a88908de/includes/sanitizers/class-amp-gallery-block-sanitizer.php#L84.
  719. '\1 amp-lightbox="true"', // https://github.com/ampproject/amp-wp/blob/1094ea03bd5dc92889405a47a8c41de1a88908de/includes/embeds/class-amp-gallery-embed.php#L64.
  720. ),
  721. $content
  722. );
  723. return preg_replace_callback(
  724. '#(<a[^>]* href=(["\']?)(\S+)\2>)\s*(<img[^>]*)(class=(["\']?)[^>]*wp-image-[0-9]+[^>]*\6.*>)\s*</a>#is',
  725. static function( $matches ) {
  726. if ( ! preg_match( '#\.\w+$#', $matches[3] ) ) {
  727. // The a[href] doesn't end in a file extension like .jpeg, so this is not a link to the media file, and should get a lightbox.
  728. return $matches[4] . ' data-amp-lightbox="true" lightbox="true" ' . $matches[5]; // https://github.com/ampproject/amp-wp/blob/1094ea03bd5dc92889405a47a8c41de1a88908de/includes/sanitizers/class-amp-img-sanitizer.php#L419.
  729. }
  730. return $matches[0];
  731. },
  732. $content
  733. );
  734. }
  735. function get_attachment_comments() {
  736. if ( ! headers_sent() ) {
  737. header( 'Content-type: text/javascript' );
  738. }
  739. /**
  740. * Allows for the checking of privileges of the blog user before comments
  741. * are packaged as JSON and sent back from the get_attachment_comments
  742. * AJAX endpoint
  743. *
  744. * @module carousel
  745. *
  746. * @since 1.6.0
  747. */
  748. do_action( 'jp_carousel_check_blog_user_privileges' );
  749. $attachment_id = ( isset( $_REQUEST['id'] ) ) ? (int) $_REQUEST['id'] : 0;
  750. $offset = ( isset( $_REQUEST['offset'] ) ) ? (int) $_REQUEST['offset'] : 0;
  751. if ( ! $attachment_id ) {
  752. wp_send_json_error(
  753. __( 'Missing attachment ID.', 'jetpack' ),
  754. 403
  755. );
  756. return;
  757. }
  758. $attachment_post = get_post( $attachment_id );
  759. // If we have no info about that attachment, bail.
  760. if ( ! ( $attachment_post instanceof WP_Post ) ) {
  761. wp_send_json_error(
  762. __( 'Missing attachment info.', 'jetpack' ),
  763. 403
  764. );
  765. return;
  766. }
  767. // This AJAX call should only be used to fetch comments of attachments.
  768. if ( 'attachment' !== $attachment_post->post_type ) {
  769. wp_send_json_error(
  770. __( 'You aren’t authorized to do that.', 'jetpack' ),
  771. 403
  772. );
  773. return;
  774. }
  775. $parent_post = get_post_parent( $attachment_id );
  776. /*
  777. * If we have no info about that parent post, no extra checks.
  778. * The attachment doesn't have a parent post, so is public.
  779. * If we have a parent post, let's ensure the user has access to it.
  780. */
  781. if ( $parent_post instanceof WP_Post ) {
  782. /*
  783. * Fetch info about user making the request.
  784. * If we have no info, bail.
  785. * Even logged out users should get a WP_User user with id 0.
  786. */
  787. $current_user = wp_get_current_user();
  788. if ( ! ( $current_user instanceof WP_User ) ) {
  789. wp_send_json_error(
  790. __( 'Missing user info.', 'jetpack' ),
  791. 403
  792. );
  793. return;
  794. }
  795. /*
  796. * If a post is private / draft
  797. * and the current user doesn't have access to it,
  798. * bail.
  799. */
  800. if (
  801. 'publish' !== $parent_post->post_status
  802. && ! current_user_can( 'read_post', $parent_post->ID )
  803. ) {
  804. wp_send_json_error(
  805. __( 'You aren’t authorized to do that.', 'jetpack' ),
  806. 403
  807. );
  808. return;
  809. }
  810. }
  811. if ( $offset < 1 ) {
  812. $offset = 0;
  813. }
  814. $comments = get_comments(
  815. array(
  816. 'status' => 'approve',
  817. 'order' => ( 'asc' == get_option( 'comment_order' ) ) ? 'ASC' : 'DESC',
  818. 'number' => 10,
  819. 'offset' => $offset,
  820. 'post_id' => $attachment_id,
  821. )
  822. );
  823. $out = array();
  824. // Can't just send the results, they contain the commenter's email address.
  825. foreach ( $comments as $comment ) {
  826. $avatar = get_avatar( $comment->comment_author_email, 64 );
  827. if ( ! $avatar ) {
  828. $avatar = '';
  829. }
  830. $out[] = array(
  831. 'id' => $comment->comment_ID,
  832. 'parent_id' => $comment->comment_parent,
  833. 'author_markup' => get_comment_author_link( $comment->comment_ID ),
  834. 'gravatar_markup' => $avatar,
  835. 'date_gmt' => $comment->comment_date_gmt,
  836. 'content' => wpautop( $comment->comment_content ),
  837. );
  838. }
  839. die( json_encode( $out ) );
  840. }
  841. function post_attachment_comment() {
  842. if ( ! headers_sent() ) {
  843. header( 'Content-type: text/javascript' );
  844. }
  845. if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'carousel_nonce' ) ) {
  846. die( json_encode( array( 'error' => __( 'Nonce verification failed.', 'jetpack' ) ) ) );
  847. }
  848. $_blog_id = (int) $_POST['blog_id'];
  849. $_post_id = (int) $_POST['id'];
  850. $comment = $_POST['comment'];
  851. if ( empty( $_blog_id ) ) {
  852. die( json_encode( array( 'error' => __( 'Missing target blog ID.', 'jetpack' ) ) ) );
  853. }
  854. if ( empty( $_post_id ) ) {
  855. die( json_encode( array( 'error' => __( 'Missing target post ID.', 'jetpack' ) ) ) );
  856. }
  857. if ( empty( $comment ) ) {
  858. die( json_encode( array( 'error' => __( 'No comment text was submitted.', 'jetpack' ) ) ) );
  859. }
  860. // Used in context like NewDash
  861. $switched = false;
  862. if ( is_multisite() && $_blog_id != get_current_blog_id() ) {
  863. switch_to_blog( $_blog_id );
  864. $switched = true;
  865. }
  866. /** This action is documented in modules/carousel/jetpack-carousel.php */
  867. do_action( 'jp_carousel_check_blog_user_privileges' );
  868. if ( ! comments_open( $_post_id ) ) {
  869. if ( $switched ) {
  870. restore_current_blog();
  871. }
  872. die( json_encode( array( 'error' => __( 'Comments on this post are closed.', 'jetpack' ) ) ) );
  873. }
  874. if ( is_user_logged_in() ) {
  875. $user = wp_get_current_user();
  876. $user_id = $user->ID;
  877. $display_name = $user->display_name;
  878. $email = $user->user_email;
  879. $url = $user->user_url;
  880. if ( empty( $user_id ) ) {
  881. if ( $switched ) {
  882. restore_current_blog();
  883. }
  884. die( json_encode( array( 'error' => __( 'Sorry, but we could not authenticate your request.', 'jetpack' ) ) ) );
  885. }
  886. } else {
  887. $user_id = 0;
  888. $display_name = $_POST['author'];
  889. $email = $_POST['email'];
  890. $url = $_POST['url'];
  891. if ( get_option( 'require_name_email' ) ) {
  892. if ( empty( $display_name ) ) {
  893. if ( $switched ) {
  894. restore_current_blog();
  895. }
  896. die( json_encode( array( 'error' => __( 'Please provide your name.', 'jetpack' ) ) ) );
  897. }
  898. if ( empty( $email ) ) {
  899. if ( $switched ) {
  900. restore_current_blog();
  901. }
  902. die( json_encode( array( 'error' => __( 'Please provide an email address.', 'jetpack' ) ) ) );
  903. }
  904. if ( ! is_email( $email ) ) {
  905. if ( $switched ) {
  906. restore_current_blog();
  907. }
  908. die( json_encode( array( 'error' => __( 'Please provide a valid email address.', 'jetpack' ) ) ) );
  909. }
  910. }
  911. }
  912. $comment_data = array(
  913. 'comment_content' => $comment,
  914. 'comment_post_ID' => $_post_id,
  915. 'comment_author' => $display_name,
  916. 'comment_author_email' => $email,
  917. 'comment_author_url' => $url,
  918. 'comment_approved' => 0,
  919. 'comment_type' => 'comment',
  920. );
  921. if ( ! empty( $user_id ) ) {
  922. $comment_data['user_id'] = $user_id;
  923. }
  924. // Note: wp_new_comment() sanitizes and validates the values (too).
  925. $comment_id = wp_new_comment( $comment_data );
  926. /**
  927. * Fires before adding a new comment to the database via the get_attachment_comments ajax endpoint.
  928. *
  929. * @module carousel
  930. *
  931. * @since 1.6.0
  932. */
  933. do_action( 'jp_carousel_post_attachment_comment' );
  934. $comment_status = wp_get_comment_status( $comment_id );
  935. if ( true == $switched ) {
  936. restore_current_blog();
  937. }
  938. die(
  939. json_encode(
  940. array(
  941. 'comment_id' => $comment_id,
  942. 'comment_status' => $comment_status,
  943. )
  944. )
  945. );
  946. }
  947. function register_settings() {
  948. add_settings_section( 'carousel_section', __( 'Image Gallery Carousel', 'jetpack' ), array( $this, 'carousel_section_callback' ), 'media' );
  949. if ( ! $this->in_jetpack ) {
  950. add_settings_field( 'carousel_enable_it', __( 'Enable carousel', 'jetpack' ), array( $this, 'carousel_enable_it_callback' ), 'media', 'carousel_section' );
  951. register_setting( 'media', 'carousel_enable_it', array( $this, 'carousel_enable_it_sanitize' ) );
  952. }
  953. add_settings_field( 'carousel_background_color', __( 'Background color', 'jetpack' ), array( $this, 'carousel_background_color_callback' ), 'media', 'carousel_section' );
  954. register_setting( 'media', 'carousel_background_color', array( $this, 'carousel_background_color_sanitize' ) );
  955. add_settings_field( 'carousel_display_exif', __( 'Metadata', 'jetpack' ), array( $this, 'carousel_display_exif_callback' ), 'media', 'carousel_section' );
  956. register_setting( 'media', 'carousel_display_exif', array( $this, 'carousel_display_exif_sanitize' ) );
  957. add_settings_field( 'carousel_display_comments', __( 'Comments', 'jetpack' ), array( $this, 'carousel_display_comments_callback' ), 'media', 'carousel_section' );
  958. register_setting( 'media', 'carousel_display_comments', array( $this, 'carousel_display_comments_sanitize' ) );
  959. // No geo setting yet, need to "fuzzify" data first, for privacy
  960. // add_settings_field('carousel_display_geo', __( 'Geolocation', 'jetpack' ), array( $this, 'carousel_display_geo_callback' ), 'media', 'carousel_section' );
  961. // register_setting( 'media', 'carousel_display_geo', array( $this, 'carousel_display_geo_sanitize' ) );
  962. }
  963. // Fulfill the settings section callback requirement by returning nothing
  964. function carousel_section_callback() {
  965. return;
  966. }
  967. function test_1or0_option( $value, $default_to_1 = true ) {
  968. if ( true == $default_to_1 ) {
  969. // Binary false (===) of $value means it has not yet been set, in which case we do want to default sites to 1
  970. if ( false === $value ) {
  971. $value = 1;
  972. }
  973. }
  974. return ( 1 == $value ) ? 1 : 0;
  975. }
  976. function sanitize_1or0_option( $value ) {
  977. return ( 1 == $value ) ? 1 : 0;
  978. }
  979. function settings_checkbox( $name, $label_text, $extra_text = '', $default_to_checked = true ) {
  980. if ( empty( $name ) ) {
  981. return;
  982. }
  983. $option = $this->test_1or0_option( get_option( $name ), $default_to_checked );
  984. echo '<fieldset>';
  985. echo '<input type="checkbox" name="' . esc_attr( $name ) . '" id="' . esc_attr( $name ) . '" value="1" ';
  986. checked( '1', $option );
  987. echo '/> <label for="' . esc_attr( $name ) . '">' . $label_text . '</label>';
  988. if ( ! empty( $extra_text ) ) {
  989. echo '<p class="description">' . $extra_text . '</p>';
  990. }
  991. echo '</fieldset>';
  992. }
  993. function settings_select( $name, $values, $extra_text = '' ) {
  994. if ( empty( $name ) || ! is_array( $values ) || empty( $values ) ) {
  995. return;
  996. }
  997. $option = get_option( $name );
  998. echo '<fieldset>';
  999. echo '<select name="' . esc_attr( $name ) . '" id="' . esc_attr( $name ) . '">';
  1000. foreach ( $values as $key => $value ) {
  1001. echo '<option value="' . esc_attr( $key ) . '" ';
  1002. selected( $key, $option );
  1003. echo '>' . esc_html( $value ) . '</option>';
  1004. }
  1005. echo '</select>';
  1006. if ( ! empty( $extra_text ) ) {
  1007. echo '<p class="description">' . $extra_text . '</p>';
  1008. }
  1009. echo '</fieldset>';
  1010. }
  1011. function carousel_display_exif_callback() {
  1012. $this->settings_checkbox( 'carousel_display_exif', __( 'Show photo metadata (<a href="https://en.wikipedia.org/wiki/Exchangeable_image_file_format" rel="noopener noreferrer" target="_blank">Exif</a>) in carousel, when available.', 'jetpack' ) );
  1013. }
  1014. /**
  1015. * Callback for checkbox and label of field that allows to toggle comments.
  1016. */
  1017. public function carousel_display_comments_callback() {
  1018. $this->settings_checkbox( 'carousel_display_comments', esc_html__( 'Show comments area in carousel', 'jetpack' ) );
  1019. }
  1020. function carousel_display_exif_sanitize( $value ) {
  1021. return $this->sanitize_1or0_option( $value );
  1022. }
  1023. /**
  1024. * Return sanitized option for value that controls whether comments will be hidden or not.
  1025. *
  1026. * @param number $value Value to sanitize.
  1027. *
  1028. * @return number Sanitized value, only 1 or 0.
  1029. */
  1030. public function carousel_display_comments_sanitize( $value ) {
  1031. return $this->sanitize_1or0_option( $value );
  1032. }
  1033. function carousel_display_geo_callback() {
  1034. $this->settings_checkbox( 'carousel_display_geo', __( 'Show map of photo location in carousel, when available.', 'jetpack' ) );
  1035. }
  1036. function carousel_display_geo_sanitize( $value ) {
  1037. return $this->sanitize_1or0_option( $value );
  1038. }
  1039. function carousel_background_color_callback() {
  1040. $this->settings_select(
  1041. 'carousel_background_color', array(
  1042. 'black' => __( 'Black', 'jetpack' ),
  1043. 'white' => __( 'White', 'jetpack' ),
  1044. )
  1045. );
  1046. }
  1047. function carousel_background_color_sanitize( $value ) {
  1048. return ( 'white' == $value ) ? 'white' : 'black';
  1049. }
  1050. function carousel_enable_it_callback() {
  1051. $this->settings_checkbox( 'carousel_enable_it', __( 'Display images in full-size carousel slideshow.', 'jetpack' ) );
  1052. }
  1053. function carousel_enable_it_sanitize( $value ) {
  1054. return $this->sanitize_1or0_option( $value );
  1055. }
  1056. }
  1057. new Jetpack_Carousel;