/modules/carousel/jetpack-carousel.php

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