save_post函数与MailOptin冲突

时间:2019-04-09 08:59:57

标签: php wordpress function action

我创建了一个插件,该插件使用来自自定义字段的数字填充数据库中的表。该函数连接到save_post操作并进行一系列检查,以确保仅在发布新帖子时才触发代码,而在将其设置为auto-draft时才触发代码。在我决定安装MailOptin之前,该代码运行良好,它在发布新帖子时自动通过Mailchimp发送电子邮件。

由于某种原因,安装此命令时,它忽略了my_save_post函数中设置的条件语句,并两次触发了该函数。查看MailOptin代码,可以看到它们正在运行自己的功能。

是否有办法确保我仍然可以进行条件检查?我尝试更改插件中的优先级,因此较早调用了该函数,但这没有用。

邮递区号:

  public function new_publish_post($new_status, $old_status, $post)
    {
        if (defined('DOING_AJAX')) return;

        // hopefully this will cause all custom field to be updated before new post is triggered.
        do_action('save_post', $post->ID, $post, true);

        if (get_post_meta($post->ID, '_mo_disable_npp', true) == 'yes') return;

        if ($new_status == 'publish' && $old_status != 'publish') {

            $new_publish_post_campaigns = ER::get_by_email_campaign_type(ER::NEW_PUBLISH_POST);

            foreach ($new_publish_post_campaigns as $npp_campaign) {
                $email_campaign_id = absint($npp_campaign['id']);

                if (ER::is_campaign_active($email_campaign_id) === false) continue;

                $custom_post_type = ER::get_merged_customizer_value($email_campaign_id, 'custom_post_type');

                $post_type_support = ['post'];

                if ($custom_post_type != 'post') {
                    $post_type_support = [$custom_post_type];
                }

                $post_type_support = apply_filters('mo_new_publish_post_post_types_support', $post_type_support, $email_campaign_id);

                if ( ! in_array($post->post_type, $post_type_support)) continue;

                $custom_post_type_settings = ER::get_merged_customizer_value($email_campaign_id, 'custom_post_type_settings');

                if ($custom_post_type != 'post' && ! empty($custom_post_type_settings)) {
                    $custom_post_type_settings = json_decode($custom_post_type_settings, true);

                    if (is_array($custom_post_type_settings)) {
                        foreach ($custom_post_type_settings as $taxonomy => $npp_terms) {
                            if ( ! empty($npp_terms)) {
                                $npp_terms  = array_map('absint', $npp_terms);
                                $post_terms = array_map('absint', wp_get_object_terms($post->ID, $taxonomy, ['fields' => 'ids']));

                                // do not check if $post_terms is empty because if no term is on the post, wp_get_object_terms return empty array
                                // so we can use the empty to check against if NPP requires certain term(s)
                                if (is_array($npp_terms) && ! empty($npp_terms)) {
                                    $result = array_intersect($post_terms, $npp_terms);
                                    if (empty($result)) continue 2;
                                }
                            }
                        }
                    }
                } else {
                    $npp_categories  = ER::get_merged_customizer_value($email_campaign_id, 'post_categories');
                    $npp_tags        = ER::get_merged_customizer_value($email_campaign_id, 'post_tags');
                    $post_categories = wp_get_post_categories($post->ID, ['fields' => 'ids']);
                    $post_tags       = wp_get_post_tags($post->ID, ['fields' => 'ids']);

                    // do not check if $post_categories is empty because if no category is on the post, wp_get_post_categories return empty array
                    // so we can use the empty to check against if NPP requires certain category(s)
                    if (is_array($npp_categories) && ! empty($npp_categories)) {
                        // use intersect to check if categories match.
                        $result = array_intersect($post_categories, $npp_categories);
                        if (empty($result)) continue;
                    }

                    if (is_array($npp_tags) && ! empty($npp_tags)) {
                        // use intersect to check if categories match.
                        $result = array_intersect($post_tags, $npp_tags);
                        if (empty($result)) continue;
                    }
                }

                $send_immediately_active = $this->send_immediately($email_campaign_id);
                $email_subject           = ER::get_merged_customizer_value($email_campaign_id, 'email_campaign_subject');

                $content_html = (new Templatify($email_campaign_id, $post))->forge();

                $campaign_id = $this->save_campaign_log(
                    $email_campaign_id,
                    self::format_campaign_subject($email_subject, $post),
                    $content_html
                );

                if ($send_immediately_active) {
                    $this->send_campaign($email_campaign_id, $campaign_id);
                } else {

                    if ( ! $this->schedule_time($email_campaign_id)) continue;

                    // convert schedule time to timestamp.
                    $schedule_time_timestamp = strtotime($this->schedule_time($email_campaign_id));

                    $response = wp_schedule_single_event(
                        $schedule_time_timestamp,
                        'mailoptin_send_scheduled_email_campaign',
                        [$email_campaign_id, $campaign_id]
                    );

                    // wp_schedule_single_event() return false if event wasn't scheduled.
                    if (false !== $response) {
                        $this->update_campaign_status($campaign_id, 'queued', $schedule_time_timestamp);
                    }
                }
            }
        }
    }

我的结构如下:

add_action('save_post', array($this, 'my_save_post'), 10, 3);

function my_save_post( $post_id, $post, $update ) {

        // Autosave, do nothing
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;
        // AJAX? Not used here
        if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
        return;
        // Check user permissions
        if ( ! current_user_can( 'edit_post', $post_id ) )
        return;

    // don't run the echo if the function is called for saving revision.
    if ( $post->post_type == 'revision' )
        return;

      if($post->post_status == 'draft')
          return;

}

0 个答案:

没有答案