Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Jetpack_Carousel often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Jetpack_Carousel, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Jetpack_Carousel { |
||
| 19 | |||
| 20 | public $prebuilt_widths = array( 370, 700, 1000, 1200, 1400, 2000 ); |
||
| 21 | |||
| 22 | public $first_run = true; |
||
| 23 | |||
| 24 | public $in_gallery = false; |
||
| 25 | |||
| 26 | public $in_jetpack = true; |
||
| 27 | |||
| 28 | public $single_image_gallery_enabled = false; |
||
| 29 | |||
| 30 | public $single_image_gallery_enabled_media_file = false; |
||
| 31 | |||
| 32 | function __construct() { |
||
| 35 | |||
| 36 | function init() { |
||
| 92 | |||
| 93 | function maybe_disable_jp_carousel() { |
||
| 105 | |||
| 106 | function maybe_disable_jp_carousel_single_images() { |
||
| 118 | |||
| 119 | function maybe_enable_jp_carousel_single_images_media_file() { |
||
| 132 | |||
| 133 | function asset_version( $version ) { |
||
| 145 | |||
| 146 | function display_bail_message( $output = '' ) { |
||
| 155 | |||
| 156 | function check_if_shortcode_processed_and_enqueue_assets( $output ) { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Check if the content of a post uses gallery blocks. To be used by 'the_content' filter. |
||
| 207 | * |
||
| 208 | * @since 6.8.0 |
||
| 209 | * |
||
| 210 | * @param string $content Post content. |
||
| 211 | * |
||
| 212 | * @return string $content Post content. |
||
| 213 | */ |
||
| 214 | function check_content_for_blocks( $content ) { |
||
| 228 | |||
| 229 | function enqueue_assets() { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Generate the HTML skeleton that will be picked up by the Carousel JS and used for showing the carousel. |
||
| 348 | */ |
||
| 349 | public function add_carousel_skeleton() { |
||
| 350 | $localize_strings = $this->localize_strings; |
||
| 351 | $is_light = ( 'white' === $localize_strings['background_color'] ); |
||
| 352 | // Determine whether to fall back to standard local comments. |
||
| 353 | $use_local_comments = ! isset( $localize_strings['jetpack_comments_iframe_src'] ) || empty( $localize_strings['jetpack_comments_iframe_src'] ); |
||
| 354 | $current_user = wp_get_current_user(); |
||
| 355 | $require_name_email = (int) get_option( 'require_name_email' ); |
||
| 356 | /* translators: %s is replaced with a field name in the form, e.g. "Email" */ |
||
| 357 | $required = ( $require_name_email ) ? __( '%s (Required)', 'jetpack' ) : '%s'; |
||
| 358 | ?> |
||
| 359 | |||
| 360 | <div |
||
| 361 | class="jp-carousel-wrap jp-carousel-transitions<?php echo( $is_light ? ' jp-carousel-light' : '' ); ?>" |
||
| 362 | itemscope |
||
| 363 | itemtype="https://schema.org/ImageGallery" |
||
| 364 | style="display: none;"> |
||
| 365 | <div class="jp-carousel-overlay"></div> |
||
| 366 | <div class="jp-carousel"></div> |
||
| 367 | <div class="jp-carousel-fadeaway"></div> |
||
| 368 | <div class="jp-carousel-info"> |
||
| 369 | <div class="jp-carousel-photo-info"> |
||
| 370 | <h2 class="jp-carousel-caption" itemprop="caption description"></h2> |
||
| 371 | </div> |
||
| 372 | <div class="jp-carousel-info-columns"> |
||
| 373 | <div class="jp-carousel-left-column-wrapper"> |
||
| 374 | <div class="jp-carousel-titleanddesc"></div> |
||
| 375 | <!-- Intentional duplicate --> |
||
| 376 | <div class="jp-carousel-photo-info"> |
||
| 377 | <h2 class="jp-carousel-caption" itemprop="caption description"></h2> |
||
| 378 | </div> |
||
| 379 | <?php if ( $localize_strings['display_comments'] ) : ?> |
||
| 380 | <div id="jp-carousel-comment-form-container"> |
||
| 381 | <?php if ( $use_local_comments ) : ?> |
||
| 382 | <?php if ( ! $localize_strings['is_logged_in'] && $localize_strings['comment_registration'] ) : ?> |
||
| 383 | <div id="jp-carousel-comment-form-commenting-as"> |
||
| 384 | <p id="jp-carousel-commenting-as"> |
||
| 385 | <?php |
||
| 386 | echo wp_kses( |
||
| 387 | __( 'You must be <a href="#" class="jp-carousel-comment-login">logged in</a> to post a comment.', 'jetpack' ), |
||
| 388 | array( |
||
| 389 | 'a' => array( |
||
| 390 | 'href' => array(), |
||
| 391 | 'class' => array(), |
||
| 392 | ), |
||
| 393 | ) |
||
| 394 | ); |
||
| 395 | ?> |
||
| 396 | </p> |
||
| 397 | </div> |
||
| 398 | <?php else : ?> |
||
| 399 | <form id="jp-carousel-comment-form"> |
||
| 400 | <label for="jp-carousel-comment-form-comment-field" class="screen-reader-text"><?php echo esc_attr( $localize_strings['write_comment'] ); ?></label> |
||
| 401 | <textarea |
||
| 402 | name="comment" |
||
| 403 | class="jp-carousel-comment-form-field jp-carousel-comment-form-textarea" |
||
| 404 | id="jp-carousel-comment-form-comment-field" |
||
| 405 | placeholder="<?php echo esc_attr( $localize_strings['write_comment'] ); ?>" |
||
| 406 | ></textarea> |
||
| 407 | <div id="jp-carousel-comment-form-submit-and-info-wrapper"> |
||
| 408 | <div id="jp-carousel-comment-form-commenting-as"> |
||
| 409 | <?php if ( $localize_strings['is_logged_in'] ) : ?> |
||
| 410 | <p id="jp-carousel-commenting-as"> |
||
| 411 | <?php |
||
| 412 | printf( |
||
| 413 | /* translators: %s is replaced with the user's display name */ |
||
| 414 | esc_html__( 'Commenting as %s', 'jetpack' ), |
||
| 415 | esc_html( $current_user->data->display_name ) |
||
| 416 | ); |
||
| 417 | ?> |
||
| 418 | </p> |
||
| 419 | <?php else : ?> |
||
| 420 | <fieldset> |
||
| 421 | <label for="jp-carousel-comment-form-email-field"><?php echo esc_html( sprintf( $required, __( 'Email', 'jetpack' ) ) ); ?></label> |
||
| 422 | <input type="text" name="email" class="jp-carousel-comment-form-field jp-carousel-comment-form-text-field" id="jp-carousel-comment-form-email-field" /> |
||
| 423 | </fieldset> |
||
| 424 | <fieldset> |
||
| 425 | <label for="jp-carousel-comment-form-author-field"><?php echo esc_html( sprintf( $required, __( 'Name', 'jetpack' ) ) ); ?></label> |
||
| 426 | <input type="text" name="author" class="jp-carousel-comment-form-field jp-carousel-comment-form-text-field" id="jp-carousel-comment-form-author-field" /> |
||
| 427 | </fieldset> |
||
| 428 | <fieldset> |
||
| 429 | <label for="jp-carousel-comment-form-url-field"><?php esc_html_e( 'Website', 'jetpack' ); ?></label> |
||
| 430 | <input type="text" name="url" class="jp-carousel-comment-form-field jp-carousel-comment-form-text-field" id="jp-carousel-comment-form-url-field" /> |
||
| 431 | </fieldset> |
||
| 432 | <?php endif ?> |
||
| 433 | </div> |
||
| 434 | <input |
||
| 435 | type="submit" |
||
| 436 | name="submit" |
||
| 437 | class="jp-carousel-comment-form-button" |
||
| 438 | id="jp-carousel-comment-form-button-submit" |
||
| 439 | value="<?php echo esc_attr( $localize_strings['post_comment'] ); ?>" /> |
||
| 440 | <span id="jp-carousel-comment-form-spinner"> </span> |
||
| 441 | <div id="jp-carousel-comment-post-results"></div> |
||
| 442 | </div> |
||
| 443 | </form> |
||
| 444 | <?php endif ?> |
||
| 445 | <?php endif ?> |
||
| 446 | </div> |
||
| 447 | <div class="jp-carousel-comments"></div> |
||
| 448 | <div id="jp-carousel-comments-loading"> |
||
| 449 | <span><?php echo esc_html( $localize_strings['loading_comments'] ); ?></span> |
||
| 450 | </div> |
||
| 451 | <?php endif ?> |
||
| 452 | </div> |
||
| 453 | <div class="jp-carousel-image-meta"> |
||
| 454 | <div class="jp-carousel-buttons"> |
||
| 455 | <?php if ( $localize_strings['display_comments'] ) : ?> |
||
| 456 | <a class="jp-carousel-commentlink" href="#"><?php echo esc_html( $localize_strings['comment'] ); ?></a> |
||
| 457 | <?php endif ?> |
||
| 458 | </div> |
||
| 459 | <ul class="jp-carousel-image-exif" style="display: none;"></ul> |
||
| 460 | <a class="jp-carousel-image-download" style="display: none;"></a> |
||
| 461 | <div class="jp-carousel-image-map" style="display: none;"></div> |
||
| 462 | </div> |
||
| 463 | </div> |
||
| 464 | </div> |
||
| 465 | <div class="jp-carousel-next-button" style="display: none;"> |
||
| 466 | <span></span> |
||
| 467 | </div> |
||
| 468 | <div class="jp-carousel-previous-button" style="display: none;"> |
||
| 469 | <span></span> |
||
| 470 | </div> |
||
| 471 | <div class="jp-carousel-close-hint"><span>×</span></div> |
||
| 472 | </div> |
||
| 473 | |||
| 474 | <?php |
||
| 475 | } |
||
| 476 | |||
| 477 | function set_in_gallery( $output ) { |
||
| 478 | if ( |
||
| 479 | class_exists( 'Jetpack_AMP_Support' ) |
||
| 480 | && Jetpack_AMP_Support::is_amp_request() |
||
| 481 | ) { |
||
| 482 | return $output; |
||
| 483 | } |
||
| 484 | $this->in_gallery = true; |
||
| 485 | return $output; |
||
| 486 | } |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Adds data-* attributes required by carousel to img tags in post HTML |
||
| 490 | * content. To be used by 'the_content' filter. |
||
| 491 | * |
||
| 492 | * @see add_data_to_images() |
||
| 493 | * @see wp_make_content_images_responsive() in wp-includes/media.php |
||
| 494 | * |
||
| 495 | * @param string $content HTML content of the post |
||
| 496 | * @return string Modified HTML content of the post |
||
| 497 | */ |
||
| 498 | function add_data_img_tags_and_enqueue_assets( $content ) { |
||
| 499 | if ( |
||
| 500 | class_exists( 'Jetpack_AMP_Support' ) |
||
| 501 | && Jetpack_AMP_Support::is_amp_request() |
||
| 502 | ) { |
||
| 503 | return $this->maybe_add_amp_lightbox( $content ); |
||
| 504 | } |
||
| 505 | |||
| 506 | if ( ! preg_match_all( '/<img [^>]+>/', $content, $matches ) ) { |
||
| 507 | return $content; |
||
| 508 | } |
||
| 509 | $selected_images = array(); |
||
| 510 | foreach ( $matches[0] as $image_html ) { |
||
| 511 | if ( preg_match( '/(wp-image-|data-id=)\"?([0-9]+)\"?/i', $image_html, $class_id ) && |
||
| 512 | ! preg_match( '/wp-block-jetpack-slideshow_image/', $image_html ) ) { |
||
| 513 | $attachment_id = absint( $class_id[2] ); |
||
| 514 | /** |
||
| 515 | * If exactly the same image tag is used more than once, overwrite it. |
||
| 516 | * All identical tags will be replaced later with 'str_replace()'. |
||
| 517 | */ |
||
| 518 | $selected_images[ $attachment_id ] = $image_html; |
||
| 519 | } |
||
| 520 | } |
||
| 521 | |||
| 522 | $find = array(); |
||
| 523 | $replace = array(); |
||
| 524 | if ( empty( $selected_images ) ) { |
||
| 525 | return $content; |
||
| 526 | } |
||
| 527 | |||
| 528 | $attachments = get_posts( |
||
| 529 | array( |
||
| 530 | 'include' => array_keys( $selected_images ), |
||
| 531 | 'post_type' => 'any', |
||
| 532 | 'post_status' => 'any', |
||
| 533 | 'suppress_filters' => false, |
||
| 534 | ) |
||
| 535 | ); |
||
| 536 | |||
| 537 | foreach ( $attachments as $attachment ) { |
||
| 538 | $image_html = $selected_images[ $attachment->ID ]; |
||
| 539 | |||
| 540 | $attributes = $this->add_data_to_images( array(), $attachment ); |
||
| 541 | $attributes_html = ''; |
||
| 542 | foreach ( $attributes as $k => $v ) { |
||
| 543 | $attributes_html .= esc_attr( $k ) . '="' . esc_attr( $v ) . '" '; |
||
| 544 | } |
||
| 545 | |||
| 546 | $find[] = $image_html; |
||
| 547 | $replace[] = str_replace( '<img ', "<img $attributes_html", $image_html ); |
||
| 548 | } |
||
| 549 | |||
| 550 | $content = str_replace( $find, $replace, $content ); |
||
| 551 | $this->enqueue_assets(); |
||
| 552 | return $content; |
||
| 553 | } |
||
| 554 | |||
| 555 | function add_data_to_images( $attr, $attachment = null ) { |
||
| 556 | if ( |
||
| 557 | class_exists( 'Jetpack_AMP_Support' ) |
||
| 558 | && Jetpack_AMP_Support::is_amp_request() |
||
| 559 | ) { |
||
| 560 | return $attr; |
||
| 561 | } |
||
| 562 | |||
| 563 | $attachment_id = (int) $attachment->ID; |
||
| 564 | if ( ! wp_attachment_is_image( $attachment_id ) ) { |
||
| 565 | return $attr; |
||
| 566 | } |
||
| 567 | |||
| 568 | $orig_file = wp_get_attachment_image_src( $attachment_id, 'full' ); |
||
| 569 | $orig_file = isset( $orig_file[0] ) ? $orig_file[0] : wp_get_attachment_url( $attachment_id ); |
||
| 570 | $meta = wp_get_attachment_metadata( $attachment_id ); |
||
| 571 | $size = isset( $meta['width'] ) ? (int) $meta['width'] . ',' . (int) $meta['height'] : ''; |
||
| 572 | $img_meta = ( ! empty( $meta['image_meta'] ) ) ? (array) $meta['image_meta'] : array(); |
||
| 573 | $comments_opened = (int) comments_open( $attachment_id ); |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Note: Cannot generate a filename from the width and height wp_get_attachment_image_src() returns because |
||
| 577 | * it takes the $content_width global variable themes can set in consideration, therefore returning sizes |
||
| 578 | * which when used to generate a filename will likely result in a 404 on the image. |
||
| 579 | * $content_width has no filter we could temporarily de-register, run wp_get_attachment_image_src(), then |
||
| 580 | * re-register. So using returned file URL instead, which we can define the sizes from through filename |
||
| 581 | * parsing in the JS, as this is a failsafe file reference. |
||
| 582 | * |
||
| 583 | * EG with Twenty Eleven activated: |
||
| 584 | * 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) } |
||
| 585 | * |
||
| 586 | * EG with Twenty Ten activated: |
||
| 587 | * 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) } |
||
| 588 | */ |
||
| 589 | |||
| 590 | $medium_file_info = wp_get_attachment_image_src( $attachment_id, 'medium' ); |
||
| 591 | $medium_file = isset( $medium_file_info[0] ) ? $medium_file_info[0] : ''; |
||
| 592 | |||
| 593 | $large_file_info = wp_get_attachment_image_src( $attachment_id, 'large' ); |
||
| 594 | $large_file = isset( $large_file_info[0] ) ? $large_file_info[0] : ''; |
||
| 595 | |||
| 596 | $attachment = get_post( $attachment_id ); |
||
| 597 | $attachment_title = wptexturize( $attachment->post_title ); |
||
| 598 | $attachment_desc = wpautop( wptexturize( $attachment->post_content ) ); |
||
| 599 | // Not yet providing geo-data, need to "fuzzify" for privacy |
||
| 600 | View Code Duplication | if ( ! empty( $img_meta ) ) { |
|
| 601 | foreach ( $img_meta as $k => $v ) { |
||
| 602 | if ( 'latitude' == $k || 'longitude' == $k ) { |
||
| 603 | unset( $img_meta[ $k ] ); |
||
| 604 | } |
||
| 605 | } |
||
| 606 | } |
||
| 607 | |||
| 608 | // See https://github.com/Automattic/jetpack/issues/2765 |
||
| 609 | if ( isset( $img_meta['keywords'] ) ) { |
||
| 610 | unset( $img_meta['keywords'] ); |
||
| 611 | } |
||
| 612 | |||
| 613 | $img_meta = json_encode( array_map( 'strval', array_filter( $img_meta, 'is_scalar' ) ) ); |
||
| 614 | |||
| 615 | $attr['data-attachment-id'] = $attachment_id; |
||
| 616 | $attr['data-permalink'] = esc_attr( get_permalink( $attachment->ID ) ); |
||
| 617 | $attr['data-orig-file'] = esc_attr( $orig_file ); |
||
| 618 | $attr['data-orig-size'] = $size; |
||
| 619 | $attr['data-comments-opened'] = $comments_opened; |
||
| 620 | $attr['data-image-meta'] = esc_attr( $img_meta ); |
||
| 621 | $attr['data-image-title'] = esc_attr( htmlspecialchars( $attachment_title ) ); |
||
| 622 | $attr['data-image-description'] = esc_attr( htmlspecialchars( $attachment_desc ) ); |
||
| 623 | $attr['data-medium-file'] = esc_attr( $medium_file ); |
||
| 624 | $attr['data-large-file'] = esc_attr( $large_file ); |
||
| 625 | |||
| 626 | return $attr; |
||
| 627 | } |
||
| 628 | |||
| 629 | function add_data_to_container( $html ) { |
||
| 630 | global $post; |
||
| 631 | if ( |
||
| 632 | class_exists( 'Jetpack_AMP_Support' ) |
||
| 633 | && Jetpack_AMP_Support::is_amp_request() |
||
| 634 | ) { |
||
| 635 | return $html; |
||
| 636 | } |
||
| 637 | |||
| 638 | if ( isset( $post ) ) { |
||
| 639 | $blog_id = (int) get_current_blog_id(); |
||
| 640 | |||
| 641 | $extra_data = array( |
||
| 642 | 'data-carousel-extra' => array( |
||
| 643 | 'blog_id' => $blog_id, |
||
| 644 | 'permalink' => get_permalink( $post->ID ), |
||
| 645 | ), |
||
| 646 | ); |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Filter the data added to the Gallery container. |
||
| 650 | * |
||
| 651 | * @module carousel |
||
| 652 | * |
||
| 653 | * @since 1.6.0 |
||
| 654 | * |
||
| 655 | * @param array $extra_data Array of data about the site and the post. |
||
| 656 | */ |
||
| 657 | $extra_data = apply_filters( 'jp_carousel_add_data_to_container', $extra_data ); |
||
| 658 | foreach ( (array) $extra_data as $data_key => $data_values ) { |
||
| 659 | $html = str_replace( '<div ', '<div ' . esc_attr( $data_key ) . "='" . wp_json_encode( $data_values ) . "' ", $html ); |
||
| 660 | $html = str_replace( '<ul class="wp-block-gallery', '<ul ' . esc_attr( $data_key ) . "='" . wp_json_encode( $data_values ) . "' class=\"wp-block-gallery", $html ); |
||
| 661 | $html = str_replace( '<ul class="blocks-gallery-grid', '<ul ' . esc_attr( $data_key ) . "='" . wp_json_encode( $data_values ) . "' class=\"blocks-gallery-grid", $html ); |
||
| 662 | $html = str_replace( '<figure class="wp-block-gallery blocks-gallery-grid', '<figure ' . esc_attr( $data_key ) . "='" . wp_json_encode( $data_values ) . "' class=\"wp-block-gallery blocks-gallery-grid", $html ); |
||
| 663 | } |
||
| 664 | } |
||
| 665 | |||
| 666 | return $html; |
||
| 667 | } |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Conditionally adds amp-lightbox to galleries and images. |
||
| 671 | * |
||
| 672 | * This applies to gallery blocks and shortcodes, |
||
| 673 | * in addition to images that are wrapped in a link to the page. |
||
| 674 | * Images wrapped in a link to the media file shouldn't get an amp-lightbox. |
||
| 675 | * |
||
| 676 | * @param string $content The content to possibly add amp-lightbox to. |
||
| 677 | * @return string The content, with amp-lightbox possibly added. |
||
| 678 | */ |
||
| 679 | public function maybe_add_amp_lightbox( $content ) { |
||
| 680 | $content = preg_replace( |
||
| 681 | array( |
||
| 682 | '#(<figure)[^>]*(?=class=(["\']?)[^>]*wp-block-gallery[^>]*\2)#is', // Gallery block. |
||
| 683 | '#(\[gallery)(?=\s+)#', // Gallery shortcode. |
||
| 684 | ), |
||
| 685 | array( |
||
| 686 | '\1 data-amp-lightbox="true" ', // https://github.com/ampproject/amp-wp/blob/1094ea03bd5dc92889405a47a8c41de1a88908de/includes/sanitizers/class-amp-gallery-block-sanitizer.php#L84. |
||
| 687 | '\1 amp-lightbox="true"', // https://github.com/ampproject/amp-wp/blob/1094ea03bd5dc92889405a47a8c41de1a88908de/includes/embeds/class-amp-gallery-embed.php#L64. |
||
| 688 | ), |
||
| 689 | $content |
||
| 690 | ); |
||
| 691 | |||
| 692 | return preg_replace_callback( |
||
| 693 | '#(<a[^>]* href=(["\']?)(\S+)\2>)\s*(<img[^>]*)(class=(["\']?)[^>]*wp-image-[0-9]+[^>]*\6.*>)\s*</a>#is', |
||
| 694 | static function( $matches ) { |
||
| 695 | View Code Duplication | if ( ! preg_match( '#\.\w+$#', $matches[3] ) ) { |
|
| 696 | // 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. |
||
| 697 | 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. |
||
| 698 | } |
||
| 699 | |||
| 700 | return $matches[0]; |
||
| 701 | }, |
||
| 702 | $content |
||
| 703 | ); |
||
| 704 | } |
||
| 705 | |||
| 706 | function get_attachment_comments() { |
||
| 707 | if ( ! headers_sent() ) { |
||
| 708 | header( 'Content-type: text/javascript' ); |
||
| 709 | } |
||
| 710 | |||
| 711 | /** |
||
| 712 | * Allows for the checking of privileges of the blog user before comments |
||
| 713 | * are packaged as JSON and sent back from the get_attachment_comments |
||
| 714 | * AJAX endpoint |
||
| 715 | * |
||
| 716 | * @module carousel |
||
| 717 | * |
||
| 718 | * @since 1.6.0 |
||
| 719 | */ |
||
| 720 | do_action( 'jp_carousel_check_blog_user_privileges' ); |
||
| 721 | |||
| 722 | $attachment_id = ( isset( $_REQUEST['id'] ) ) ? (int) $_REQUEST['id'] : 0; |
||
| 723 | $offset = ( isset( $_REQUEST['offset'] ) ) ? (int) $_REQUEST['offset'] : 0; |
||
| 724 | |||
| 725 | if ( ! $attachment_id ) { |
||
| 726 | echo json_encode( __( 'Missing attachment ID.', 'jetpack' ) ); |
||
| 727 | die(); |
||
| 728 | } |
||
| 729 | |||
| 730 | if ( $offset < 1 ) { |
||
| 731 | $offset = 0; |
||
| 732 | } |
||
| 733 | |||
| 734 | $comments = get_comments( |
||
| 735 | array( |
||
| 736 | 'status' => 'approve', |
||
| 737 | 'order' => ( 'asc' == get_option( 'comment_order' ) ) ? 'ASC' : 'DESC', |
||
| 738 | 'number' => 10, |
||
| 739 | 'offset' => $offset, |
||
| 740 | 'post_id' => $attachment_id, |
||
| 741 | ) |
||
| 742 | ); |
||
| 743 | |||
| 744 | $out = array(); |
||
| 745 | |||
| 746 | // Can't just send the results, they contain the commenter's email address. |
||
| 747 | foreach ( $comments as $comment ) { |
||
| 748 | $avatar = get_avatar( $comment->comment_author_email, 64 ); |
||
| 749 | if ( ! $avatar ) { |
||
| 750 | $avatar = ''; |
||
| 751 | } |
||
| 752 | $out[] = array( |
||
| 753 | 'id' => $comment->comment_ID, |
||
| 754 | 'parent_id' => $comment->comment_parent, |
||
| 755 | 'author_markup' => get_comment_author_link( $comment->comment_ID ), |
||
| 756 | 'gravatar_markup' => $avatar, |
||
| 757 | 'date_gmt' => $comment->comment_date_gmt, |
||
| 758 | 'content' => wpautop( $comment->comment_content ), |
||
| 759 | ); |
||
| 760 | } |
||
| 761 | |||
| 762 | die( json_encode( $out ) ); |
||
| 763 | } |
||
| 764 | |||
| 765 | function post_attachment_comment() { |
||
| 766 | if ( ! headers_sent() ) { |
||
| 767 | header( 'Content-type: text/javascript' ); |
||
| 768 | } |
||
| 769 | |||
| 770 | if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'carousel_nonce' ) ) { |
||
| 771 | die( json_encode( array( 'error' => __( 'Nonce verification failed.', 'jetpack' ) ) ) ); |
||
| 772 | } |
||
| 773 | |||
| 774 | $_blog_id = (int) $_POST['blog_id']; |
||
| 775 | $_post_id = (int) $_POST['id']; |
||
| 776 | $comment = $_POST['comment']; |
||
| 777 | |||
| 778 | if ( empty( $_blog_id ) ) { |
||
| 779 | die( json_encode( array( 'error' => __( 'Missing target blog ID.', 'jetpack' ) ) ) ); |
||
| 780 | } |
||
| 781 | |||
| 782 | if ( empty( $_post_id ) ) { |
||
| 783 | die( json_encode( array( 'error' => __( 'Missing target post ID.', 'jetpack' ) ) ) ); |
||
| 784 | } |
||
| 785 | |||
| 786 | if ( empty( $comment ) ) { |
||
| 787 | die( json_encode( array( 'error' => __( 'No comment text was submitted.', 'jetpack' ) ) ) ); |
||
| 788 | } |
||
| 789 | |||
| 790 | // Used in context like NewDash |
||
| 791 | $switched = false; |
||
| 792 | if ( is_multisite() && $_blog_id != get_current_blog_id() ) { |
||
| 793 | switch_to_blog( $_blog_id ); |
||
| 794 | $switched = true; |
||
| 795 | } |
||
| 796 | |||
| 797 | /** This action is documented in modules/carousel/jetpack-carousel.php */ |
||
| 798 | do_action( 'jp_carousel_check_blog_user_privileges' ); |
||
| 799 | |||
| 800 | View Code Duplication | if ( ! comments_open( $_post_id ) ) { |
|
| 801 | if ( $switched ) { |
||
| 802 | restore_current_blog(); |
||
| 803 | } |
||
| 804 | die( json_encode( array( 'error' => __( 'Comments on this post are closed.', 'jetpack' ) ) ) ); |
||
| 805 | } |
||
| 806 | |||
| 807 | if ( is_user_logged_in() ) { |
||
| 808 | $user = wp_get_current_user(); |
||
| 809 | $user_id = $user->ID; |
||
| 810 | $display_name = $user->display_name; |
||
| 811 | $email = $user->user_email; |
||
| 812 | $url = $user->user_url; |
||
| 813 | |||
| 814 | View Code Duplication | if ( empty( $user_id ) ) { |
|
| 815 | if ( $switched ) { |
||
| 816 | restore_current_blog(); |
||
| 817 | } |
||
| 818 | die( json_encode( array( 'error' => __( 'Sorry, but we could not authenticate your request.', 'jetpack' ) ) ) ); |
||
| 819 | } |
||
| 820 | } else { |
||
| 821 | $user_id = 0; |
||
| 822 | $display_name = $_POST['author']; |
||
| 823 | $email = $_POST['email']; |
||
| 824 | $url = $_POST['url']; |
||
| 825 | |||
| 826 | if ( get_option( 'require_name_email' ) ) { |
||
| 827 | View Code Duplication | if ( empty( $display_name ) ) { |
|
| 828 | if ( $switched ) { |
||
| 829 | restore_current_blog(); |
||
| 830 | } |
||
| 831 | die( json_encode( array( 'error' => __( 'Please provide your name.', 'jetpack' ) ) ) ); |
||
| 832 | } |
||
| 833 | |||
| 834 | View Code Duplication | if ( empty( $email ) ) { |
|
| 835 | if ( $switched ) { |
||
| 836 | restore_current_blog(); |
||
| 837 | } |
||
| 838 | die( json_encode( array( 'error' => __( 'Please provide an email address.', 'jetpack' ) ) ) ); |
||
| 839 | } |
||
| 840 | |||
| 841 | View Code Duplication | if ( ! is_email( $email ) ) { |
|
| 842 | if ( $switched ) { |
||
| 843 | restore_current_blog(); |
||
| 844 | } |
||
| 845 | die( json_encode( array( 'error' => __( 'Please provide a valid email address.', 'jetpack' ) ) ) ); |
||
| 846 | } |
||
| 847 | } |
||
| 848 | } |
||
| 849 | |||
| 850 | $comment_data = array( |
||
| 851 | 'comment_content' => $comment, |
||
| 852 | 'comment_post_ID' => $_post_id, |
||
| 853 | 'comment_author' => $display_name, |
||
| 854 | 'comment_author_email' => $email, |
||
| 855 | 'comment_author_url' => $url, |
||
| 856 | 'comment_approved' => 0, |
||
| 857 | 'comment_type' => 'comment', |
||
| 858 | ); |
||
| 859 | |||
| 860 | if ( ! empty( $user_id ) ) { |
||
| 861 | $comment_data['user_id'] = $user_id; |
||
| 862 | } |
||
| 863 | |||
| 864 | // Note: wp_new_comment() sanitizes and validates the values (too). |
||
| 865 | $comment_id = wp_new_comment( $comment_data ); |
||
| 866 | |||
| 867 | /** |
||
| 868 | * Fires before adding a new comment to the database via the get_attachment_comments ajax endpoint. |
||
| 869 | * |
||
| 870 | * @module carousel |
||
| 871 | * |
||
| 872 | * @since 1.6.0 |
||
| 873 | */ |
||
| 874 | do_action( 'jp_carousel_post_attachment_comment' ); |
||
| 875 | $comment_status = wp_get_comment_status( $comment_id ); |
||
| 876 | |||
| 877 | if ( true == $switched ) { |
||
| 878 | restore_current_blog(); |
||
| 879 | } |
||
| 880 | |||
| 881 | die( |
||
| 882 | json_encode( |
||
| 883 | array( |
||
| 884 | 'comment_id' => $comment_id, |
||
| 885 | 'comment_status' => $comment_status, |
||
| 886 | ) |
||
| 887 | ) |
||
| 888 | ); |
||
| 889 | } |
||
| 890 | |||
| 891 | function register_settings() { |
||
| 892 | add_settings_section( 'carousel_section', __( 'Image Gallery Carousel', 'jetpack' ), array( $this, 'carousel_section_callback' ), 'media' ); |
||
| 893 | |||
| 894 | if ( ! $this->in_jetpack ) { |
||
| 895 | add_settings_field( 'carousel_enable_it', __( 'Enable carousel', 'jetpack' ), array( $this, 'carousel_enable_it_callback' ), 'media', 'carousel_section' ); |
||
| 896 | register_setting( 'media', 'carousel_enable_it', array( $this, 'carousel_enable_it_sanitize' ) ); |
||
| 897 | } |
||
| 898 | |||
| 899 | add_settings_field( 'carousel_background_color', __( 'Background color', 'jetpack' ), array( $this, 'carousel_background_color_callback' ), 'media', 'carousel_section' ); |
||
| 900 | register_setting( 'media', 'carousel_background_color', array( $this, 'carousel_background_color_sanitize' ) ); |
||
| 901 | |||
| 902 | add_settings_field( 'carousel_display_exif', __( 'Metadata', 'jetpack' ), array( $this, 'carousel_display_exif_callback' ), 'media', 'carousel_section' ); |
||
| 903 | register_setting( 'media', 'carousel_display_exif', array( $this, 'carousel_display_exif_sanitize' ) ); |
||
| 904 | |||
| 905 | add_settings_field( 'carousel_display_comments', __( 'Comments', 'jetpack' ), array( $this, 'carousel_display_comments_callback' ), 'media', 'carousel_section' ); |
||
| 906 | register_setting( 'media', 'carousel_display_comments', array( $this, 'carousel_display_comments_sanitize' ) ); |
||
| 907 | |||
| 908 | // No geo setting yet, need to "fuzzify" data first, for privacy |
||
| 909 | // add_settings_field('carousel_display_geo', __( 'Geolocation', 'jetpack' ), array( $this, 'carousel_display_geo_callback' ), 'media', 'carousel_section' ); |
||
| 910 | // register_setting( 'media', 'carousel_display_geo', array( $this, 'carousel_display_geo_sanitize' ) ); |
||
| 911 | } |
||
| 912 | |||
| 913 | // Fulfill the settings section callback requirement by returning nothing |
||
| 914 | function carousel_section_callback() { |
||
| 915 | return; |
||
| 916 | } |
||
| 917 | |||
| 918 | function test_1or0_option( $value, $default_to_1 = true ) { |
||
| 919 | if ( true == $default_to_1 ) { |
||
| 920 | // Binary false (===) of $value means it has not yet been set, in which case we do want to default sites to 1 |
||
| 921 | if ( false === $value ) { |
||
| 922 | $value = 1; |
||
| 923 | } |
||
| 924 | } |
||
| 925 | return ( 1 == $value ) ? 1 : 0; |
||
| 926 | } |
||
| 927 | |||
| 928 | function sanitize_1or0_option( $value ) { |
||
| 929 | return ( 1 == $value ) ? 1 : 0; |
||
| 930 | } |
||
| 931 | |||
| 932 | function settings_checkbox( $name, $label_text, $extra_text = '', $default_to_checked = true ) { |
||
| 933 | if ( empty( $name ) ) { |
||
| 934 | return; |
||
| 935 | } |
||
| 936 | $option = $this->test_1or0_option( get_option( $name ), $default_to_checked ); |
||
| 937 | echo '<fieldset>'; |
||
| 938 | echo '<input type="checkbox" name="' . esc_attr( $name ) . '" id="' . esc_attr( $name ) . '" value="1" '; |
||
| 939 | checked( '1', $option ); |
||
| 940 | echo '/> <label for="' . esc_attr( $name ) . '">' . $label_text . '</label>'; |
||
| 941 | if ( ! empty( $extra_text ) ) { |
||
| 942 | echo '<p class="description">' . $extra_text . '</p>'; |
||
| 943 | } |
||
| 944 | echo '</fieldset>'; |
||
| 945 | } |
||
| 946 | |||
| 947 | function settings_select( $name, $values, $extra_text = '' ) { |
||
| 948 | if ( empty( $name ) || ! is_array( $values ) || empty( $values ) ) { |
||
| 949 | return; |
||
| 950 | } |
||
| 951 | $option = get_option( $name ); |
||
| 952 | echo '<fieldset>'; |
||
| 953 | echo '<select name="' . esc_attr( $name ) . '" id="' . esc_attr( $name ) . '">'; |
||
| 954 | foreach ( $values as $key => $value ) { |
||
| 955 | echo '<option value="' . esc_attr( $key ) . '" '; |
||
| 956 | selected( $key, $option ); |
||
| 957 | echo '>' . esc_html( $value ) . '</option>'; |
||
| 958 | } |
||
| 959 | echo '</select>'; |
||
| 960 | if ( ! empty( $extra_text ) ) { |
||
| 961 | echo '<p class="description">' . $extra_text . '</p>'; |
||
| 962 | } |
||
| 963 | echo '</fieldset>'; |
||
| 964 | } |
||
| 965 | |||
| 966 | function carousel_display_exif_callback() { |
||
| 967 | $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' ) ); |
||
| 968 | } |
||
| 969 | |||
| 970 | /** |
||
| 971 | * Callback for checkbox and label of field that allows to toggle comments. |
||
| 972 | */ |
||
| 973 | public function carousel_display_comments_callback() { |
||
| 974 | $this->settings_checkbox( 'carousel_display_comments', esc_html__( 'Show comments area in carousel', 'jetpack' ) ); |
||
| 975 | } |
||
| 976 | |||
| 977 | function carousel_display_exif_sanitize( $value ) { |
||
| 978 | return $this->sanitize_1or0_option( $value ); |
||
| 979 | } |
||
| 980 | |||
| 981 | /** |
||
| 982 | * Return sanitized option for value that controls whether comments will be hidden or not. |
||
| 983 | * |
||
| 984 | * @param number $value Value to sanitize. |
||
| 985 | * |
||
| 986 | * @return number Sanitized value, only 1 or 0. |
||
| 987 | */ |
||
| 988 | public function carousel_display_comments_sanitize( $value ) { |
||
| 989 | return $this->sanitize_1or0_option( $value ); |
||
| 990 | } |
||
| 991 | |||
| 992 | function carousel_display_geo_callback() { |
||
| 993 | $this->settings_checkbox( 'carousel_display_geo', __( 'Show map of photo location in carousel, when available.', 'jetpack' ) ); |
||
| 994 | } |
||
| 995 | |||
| 996 | function carousel_display_geo_sanitize( $value ) { |
||
| 999 | |||
| 1000 | function carousel_background_color_callback() { |
||
| 1001 | $this->settings_select( |
||
| 1002 | 'carousel_background_color', array( |
||
| 1003 | 'black' => __( 'Black', 'jetpack' ), |
||
| 1004 | 'white' => __( 'White', 'jetpack' ), |
||
| 1005 | ) |
||
| 1006 | ); |
||
| 1007 | } |
||
| 1008 | |||
| 1009 | function carousel_background_color_sanitize( $value ) { |
||
| 1010 | return ( 'white' == $value ) ? 'white' : 'black'; |
||
| 1011 | } |
||
| 1012 | |||
| 1013 | function carousel_enable_it_callback() { |
||
| 1014 | $this->settings_checkbox( 'carousel_enable_it', __( 'Display images in full-size carousel slideshow.', 'jetpack' ) ); |
||
| 1015 | } |
||
| 1016 | |||
| 1017 | function carousel_enable_it_sanitize( $value ) { |
||
| 1018 | return $this->sanitize_1or0_option( $value ); |
||
| 1019 | } |
||
| 1020 | } |
||
| 1021 | |||
| 1022 | new Jetpack_Carousel; |
||
| 1023 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: