| @@ -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. |
| @@ -584,32 +584,27 @@ | ||
| 584 | 584 | wp_send_json_success( $result ); |
| 585 | 585 | } |
| 586 | 586 | |
| 587 | 587 | /** |
| 588 | - * Try to autogenerate poster on post insert or update. | |
| 588 | + * Prepare poster for autogeneration on post insert or update. | |
| 589 | 589 | * |
| 590 | 590 | * @param int $post_id Updated post_id. |
| 591 | 591 | * @param object $post Updated post object. |
| 592 | - * @param bool $update Whether this is an existing post being updated. | |
| 593 | 592 | */ |
| 594 | - public static function autogenerate_poster( $post_id, $post, $update ) { | |
| 593 | + public static function prepare_autogenerated_poster( $post_id, $post ) { | |
| 595 | 594 | if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 596 | 595 | return; |
| 597 | 596 | } |
| 598 | 597 | |
| 599 | - if ( ! $update ) { | |
| 600 | - return; | |
| 601 | - } | |
| 602 | - | |
| 603 | 598 | if ( wp_is_post_revision( $post_id ) ) { |
| 604 | 599 | return; |
| 605 | 600 | } |
| 606 | 601 | |
| 607 | - if ( ! current_user_can( 'edit_post', $post_id ) ) { | |
| 602 | + if ( ( ! defined( 'DOING_CRON' ) || ! DOING_CRON ) && ! current_user_can( 'edit_post', $post_id ) ) { | |
| 608 | 603 | return; |
| 609 | 604 | } |
| 610 | 605 | |
| 611 | - if ( 'trash' === $post->post_status ) { | |
| 606 | + if ( 'trash' === $post->post_status || 'auto-draft' === $post->post_status ) { | |
| 612 | 607 | return; |
| 613 | 608 | } |
| 614 | 609 | |
| 615 | 610 | if ( ! in_array( $post->post_type, self::get_metabox_post_types(), true ) ) { |
| @@ -621,11 +616,24 @@ | ||
| 621 | 616 | if ( ! empty( $meta['mode'] ) && 'manual' === $meta['mode'] ) { |
| 622 | 617 | return; |
| 623 | 618 | } |
| 624 | 619 | |
| 625 | - $index = Config::get_autogenerate_index(); | |
| 620 | + self::autogenerate_poster( $post_id ); | |
| 621 | + } | |
| 626 | 622 | |
| 627 | - if ( empty( $index ) ) { | |
| 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 ) ) { | |
| 628 | 636 | return; |
| 629 | 637 | } |
| 630 | 638 | |
| 631 | 639 | // Compose fieldset by template index. |
| @@ -648,8 +656,12 @@ | ||
| 648 | 656 | |
| 649 | 657 | // Invoke poster generation. |
| 650 | 658 | $result = self::generate_poster( $fieldset, $index, $post_id, 'post', true ); |
| 651 | 659 | |
| 660 | + if ( is_wp_error( $result ) ) { | |
| 661 | + return; | |
| 662 | + } | |
| 663 | + | |
| 652 | 664 | $result['template'] = $index; |
| 653 | 665 | |
| 654 | 666 | /** |
| 655 | 667 | * Filters autogenerated poster data. |
| @@ -660,19 +672,15 @@ | ||
| 660 | 672 | * @param int $post_id Post ID. |
| 661 | 673 | */ |
| 662 | 674 | $result = apply_filters( 'sharing_image_autogenerated_poster', $result, $post_id ); |
| 663 | 675 | |
| 664 | - if ( is_wp_error( $result ) ) { | |
| 665 | - return; | |
| 666 | - } | |
| 667 | - | |
| 668 | 676 | update_post_meta( $post_id, self::META_SOURCE, $result ); |
| 669 | 677 | update_post_meta( $post_id, self::META_FIELDSET, $fieldset ); |
| 670 | 678 | } |
| 671 | 679 | |
| 672 | 680 | /** |
| 673 | - * Generate poster by AJAX request. | |
| 674 | - * Used in widget and sidebar. | |
| 681 | + * Generate poster. | |
| 682 | + * Used in widget, sidebar and for auto-generation. | |
| 675 | 683 | * |
| 676 | 684 | * @param array $fieldset Fieldset data from widget. |
| 677 | 685 | * @param int $index Template index from editor. |
| 678 | 686 | * @param int $screen_id Post or term ID from admin screen. |
| @@ -731,20 +739,21 @@ | ||
| 731 | 739 | * @return array Filtered widget script object. |
| 732 | 740 | */ |
| 733 | 741 | private static function create_script_object( $context, $screen_id ) { |
| 734 | 742 | $object = array( |
| 735 | - 'nonce' => wp_create_nonce( self::WIDGET_NONCE ), | |
| 736 | - 'context' => $context, | |
| 737 | - 'screen' => $screen_id, | |
| 743 | + 'nonce' => wp_create_nonce( self::WIDGET_NONCE ), | |
| 744 | + 'context' => $context, | |
| 745 | + 'screen' => $screen_id, | |
| 738 | 746 | |
| 739 | - 'name' => array( | |
| 747 | + 'name' => array( | |
| 740 | 748 | 'source' => self::META_SOURCE, |
| 741 | 749 | 'fieldset' => self::META_FIELDSET, |
| 742 | 750 | ), |
| 743 | - 'links' => array( | |
| 751 | + 'links' => array( | |
| 744 | 752 | 'uploads' => esc_url( admin_url( 'upload.php' ) ), |
| 745 | 753 | ), |
| 746 | - 'templates' => Templates::get_templates(), | |
| 754 | + 'templates' => Templates::get_templates(), | |
| 755 | + 'autogenerate' => Config::get_autogenerate_index(), | |
| 747 | 756 | ); |
| 748 | 757 | |
| 749 | 758 | /** |
| 750 | 759 | * Filter widget script object. |