Jetpack_Carousel

Plugin Name: Jetpack Carousel Plugin URL: https://wordpress.com/ Description: Transform your standard image galleries into an immersive full-screen experience.

Defined (1)

The class is defined in the following location(s).

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