| @@ -54,9 +54,9 @@ | ||
| 54 | 54 | add_action( 'wp_ajax_sharing_image_generate', array( __CLASS__, 'handle_ajax_generator' ) ); |
| 55 | 55 | add_action( 'rest_api_init', array( __CLASS__, 'add_generate_endpoint' ) ); |
| 56 | 56 | |
| 57 | 57 | // Try to autogenerate poster if it is needed. |
| 58 | - add_action( 'wp_after_insert_post', array( __CLASS__, 'autogenerate_poster' ), 20, 3 ); | |
| 58 | + add_action( 'wp_after_insert_post', array( __CLASS__, 'prepare_autogenerated_poster' ), 20, 2 ); | |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | /** |
| 62 | 62 | * Init post sidebar for gutenberg enabled dashboard. |
| @@ -271,8 +271,15 @@ | ||
| 271 | 271 | public static function enqueue_sidebar_assets() { |
| 272 | 272 | if ( ! is_admin() ) { |
| 273 | 273 | return; |
| 274 | 274 | } |
| 275 | + | |
| 276 | + $screen = get_current_screen(); | |
| 277 | + | |
| 278 | + if ( ! in_array( $screen->post_type, self::get_metabox_post_types(), true ) ) { | |
| 279 | + return; | |
| 280 | + } | |
| 281 | + | |
| 275 | 282 | $asset = require SHARING_IMAGE_DIR . 'assets/sidebar/index.asset.php'; |
| 276 | 283 | |
| 277 | 284 | wp_enqueue_script( |
| 278 | 285 | 'sharing-image-sidebar', |
| @@ -293,13 +300,14 @@ | ||
| 293 | 300 | // Translations availible only for WP 5.0+. |
| 294 | 301 | wp_set_script_translations( 'sharing-image-sidebar', 'sharing-image' ); |
| 295 | 302 | |
| 296 | 303 | $data = array( |
| 297 | - 'meta' => array( | |
| 304 | + 'meta' => array( | |
| 298 | 305 | 'source' => self::META_SOURCE, |
| 299 | 306 | 'fieldset' => self::META_FIELDSET, |
| 300 | 307 | ), |
| 301 | - 'templates' => Templates::get_templates(), | |
| 308 | + 'autogenerate' => Config::get_autogenerate_index(), | |
| 309 | + 'templates' => Templates::get_templates(), | |
| 302 | 310 | ); |
| 303 | 311 | |
| 304 | 312 | // Add widget script object. |
| 305 | 313 | wp_localize_script( 'sharing-image-sidebar', 'sharingImageSidebar', $data ); |
| @@ -576,23 +584,34 @@ | ||
| 576 | 584 | wp_send_json_success( $result ); |
| 577 | 585 | } |
| 578 | 586 | |
| 579 | 587 | /** |
| 580 | - * Try to autogenerate poster on post insert or update. | |
| 588 | + * Prepare poster for autogeneration on post insert or update. | |
| 581 | 589 | * |
| 582 | 590 | * @param int $post_id Updated post_id. |
| 583 | 591 | * @param object $post Updated post object. |
| 584 | - * @param bool $update Whether this is an existing post being updated. | |
| 585 | 592 | */ |
| 586 | - public static function autogenerate_poster( $post_id, $post, $update ) { | |
| 593 | + public static function prepare_autogenerated_poster( $post_id, $post ) { | |
| 587 | 594 | if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 588 | 595 | return; |
| 589 | 596 | } |
| 590 | 597 | |
| 591 | - if ( ! $update ) { | |
| 598 | + if ( wp_is_post_revision( $post_id ) ) { | |
| 592 | 599 | return; |
| 593 | 600 | } |
| 594 | 601 | |
| 602 | + if ( ( ! defined( 'DOING_CRON' ) || ! DOING_CRON ) && ! current_user_can( 'edit_post', $post_id ) ) { | |
| 603 | + return; | |
| 604 | + } | |
| 605 | + | |
| 606 | + if ( 'trash' === $post->post_status || 'auto-draft' === $post->post_status ) { | |
| 607 | + return; | |
| 608 | + } | |
| 609 | + | |
| 610 | + if ( ! in_array( $post->post_type, self::get_metabox_post_types(), true ) ) { | |
| 611 | + return; | |
| 612 | + } | |
| 613 | + | |
| 595 | 614 | $meta = get_post_meta( $post_id, self::META_SOURCE, true ); |
| 596 | 615 | |
| 597 | 616 | if ( ! empty( $meta['mode'] ) && 'manual' === $meta['mode'] ) { |
| 598 | 617 | return; |
| @@ -597,16 +616,27 @@ | ||
| 597 | 616 | if ( ! empty( $meta['mode'] ) && 'manual' === $meta['mode'] ) { |
| 598 | 617 | return; |
| 599 | 618 | } |
| 600 | 619 | |
| 601 | - $config = Config::get_config(); | |
| 620 | + self::autogenerate_poster( $post_id ); | |
| 621 | + } | |
| 602 | 622 | |
| 603 | - if ( empty( $config['autogenerate'] ) ) { | |
| 623 | + /** | |
| 624 | + * Autogenerate poster for post_id and custom index if defined. | |
| 625 | + * Use this public method to autogenerate poster manually. | |
| 626 | + * | |
| 627 | + * @param int $post_id Post ID. | |
| 628 | + * @param string|null $index Template index. | |
| 629 | + */ | |
| 630 | + public static function autogenerate_poster( $post_id, $index = null ) { | |
| 631 | + if ( is_null( $index ) ) { | |
| 632 | + $index = Config::get_autogenerate_index(); | |
| 633 | + } | |
| 634 | + | |
| 635 | + if ( ! Templates::has_template( $index ) ) { | |
| 604 | 636 | return; |
| 605 | 637 | } |
| 606 | 638 | |
| 607 | - $index = sanitize_key( $config['autogenerate'] ); | |
| 608 | - | |
| 609 | 639 | // Compose fieldset by template index. |
| 610 | 640 | $fieldset = self::compose_fields( $index, $post_id ); |
| 611 | 641 | |
| 612 | 642 | /** |
| @@ -626,8 +656,12 @@ | ||
| 626 | 656 | |
| 627 | 657 | // Invoke poster generation. |
| 628 | 658 | $result = self::generate_poster( $fieldset, $index, $post_id, 'post', true ); |
| 629 | 659 | |
| 660 | + if ( is_wp_error( $result ) ) { | |
| 661 | + return; | |
| 662 | + } | |
| 663 | + | |
| 630 | 664 | $result['template'] = $index; |
| 631 | 665 | |
| 632 | 666 | /** |
| 633 | 667 | * Filters autogenerated poster data. |
| @@ -638,19 +672,15 @@ | ||
| 638 | 672 | * @param int $post_id Post ID. |
| 639 | 673 | */ |
| 640 | 674 | $result = apply_filters( 'sharing_image_autogenerated_poster', $result, $post_id ); |
| 641 | 675 | |
| 642 | - if ( is_wp_error( $result ) ) { | |
| 643 | - return; | |
| 644 | - } | |
| 645 | - | |
| 646 | 676 | update_post_meta( $post_id, self::META_SOURCE, $result ); |
| 647 | 677 | update_post_meta( $post_id, self::META_FIELDSET, $fieldset ); |
| 648 | 678 | } |
| 649 | 679 | |
| 650 | 680 | /** |
| 651 | - * Generate poster by AJAX request. | |
| 652 | - * Used in widget and sidebar. | |
| 681 | + * Generate poster. | |
| 682 | + * Used in widget, sidebar and for auto-generation. | |
| 653 | 683 | * |
| 654 | 684 | * @param array $fieldset Fieldset data from widget. |
| 655 | 685 | * @param int $index Template index from editor. |
| 656 | 686 | * @param int $screen_id Post or term ID from admin screen. |
| @@ -709,20 +739,21 @@ | ||
| 709 | 739 | * @return array Filtered widget script object. |
| 710 | 740 | */ |
| 711 | 741 | private static function create_script_object( $context, $screen_id ) { |
| 712 | 742 | $object = array( |
| 713 | - 'nonce' => wp_create_nonce( self::WIDGET_NONCE ), | |
| 714 | - 'context' => $context, | |
| 715 | - 'screen' => $screen_id, | |
| 743 | + 'nonce' => wp_create_nonce( self::WIDGET_NONCE ), | |
| 744 | + 'context' => $context, | |
| 745 | + 'screen' => $screen_id, | |
| 716 | 746 | |
| 717 | - 'name' => array( | |
| 747 | + 'name' => array( | |
| 718 | 748 | 'source' => self::META_SOURCE, |
| 719 | 749 | 'fieldset' => self::META_FIELDSET, |
| 720 | 750 | ), |
| 721 | - 'links' => array( | |
| 751 | + 'links' => array( | |
| 722 | 752 | 'uploads' => esc_url( admin_url( 'upload.php' ) ), |
| 723 | 753 | ), |
| 724 | - 'templates' => Templates::get_templates(), | |
| 754 | + 'templates' => Templates::get_templates(), | |
| 755 | + 'autogenerate' => Config::get_autogenerate_index(), | |
| 725 | 756 | ); |
| 726 | 757 | |
| 727 | 758 | /** |
| 728 | 759 | * Filter widget script object. |