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