| @@ -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, 2 ); | |
| 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,14 +584,14 @@ | ||
| 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 | 592 | */ |
| 593 | - public static function autogenerate_poster( $post_id, $post ) { | |
| 593 | + public static function prepare_autogenerated_poster( $post_id, $post ) { | |
| 594 | 594 | if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 595 | 595 | return; |
| 596 | 596 | } |
| 597 | 597 | |
| @@ -598,9 +598,9 @@ | ||
| 598 | 598 | if ( wp_is_post_revision( $post_id ) ) { |
| 599 | 599 | return; |
| 600 | 600 | } |
| 601 | 601 | |
| 602 | - if ( ! current_user_can( 'edit_post', $post_id ) ) { | |
| 602 | + if ( ( ! defined( 'DOING_CRON' ) || ! DOING_CRON ) && ! current_user_can( 'edit_post', $post_id ) ) { | |
| 603 | 603 | return; |
| 604 | 604 | } |
| 605 | 605 | |
| 606 | 606 | if ( 'trash' === $post->post_status || 'auto-draft' === $post->post_status ) { |
| @@ -616,10 +616,23 @@ | ||
| 616 | 616 | if ( ! empty( $meta['mode'] ) && 'manual' === $meta['mode'] ) { |
| 617 | 617 | return; |
| 618 | 618 | } |
| 619 | 619 | |
| 620 | - $index = Config::get_autogenerate_index(); | |
| 620 | + self::autogenerate_poster( $post_id ); | |
| 621 | + } | |
| 621 | 622 | |
| 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 | + | |
| 622 | 635 | if ( ! Templates::has_template( $index ) ) { |
| 623 | 636 | return; |
| 624 | 637 | } |
| 625 | 638 | |
| @@ -664,10 +677,10 @@ | ||
| 664 | 677 | update_post_meta( $post_id, self::META_FIELDSET, $fieldset ); |
| 665 | 678 | } |
| 666 | 679 | |
| 667 | 680 | /** |
| 668 | - * Generate poster by AJAX request. | |
| 669 | - * Used in widget and sidebar. | |
| 681 | + * Generate poster. | |
| 682 | + * Used in widget, sidebar and for auto-generation. | |
| 670 | 683 | * |
| 671 | 684 | * @param array $fieldset Fieldset data from widget. |
| 672 | 685 | * @param int $index Template index from editor. |
| 673 | 686 | * @param int $screen_id Post or term ID from admin screen. |