WPBakery单个图像-循环此语句以查找Wordpress Post中的所有实例

时间:2018-07-12 12:31:10

标签: php wordpress wpmu

此代码在Movedo Theme中的WPBakery中查找单个图像的简码,并使用WPMUDEV的Multisite Content Copier将其呈现到重复的帖子中。但是,它似乎只能找到第一个实例并将其复制,我需要循环此代码,以便它在帖子中找到所有实例并传输所有图像。有什么想法可以实现吗?

如果(!class_exists('MCC_VC_Shortcode_Render')){

class MCC_VC_Shortcode_Render {
    private static $_instance = null;
    public static function get_instance() {
        if( is_null( self::$_instance ) ){
            self::$_instance = new MCC_VC_Shortcode_Render();
        }
        return self::$_instance;
    }
    private function __construct() {            
        add_action( 'mcc_copy_posts', array( $this, 'mcc_vc_shortcode_render' ), 10, 3 );
    }

    public function mcc_vc_shortcode_render( $copied_posts, $source_blog_id, $args ) {

        foreach ( $copied_posts as $source_post_id => $new_post_id ) {

            switch_to_blog( $source_blog_id  );

            $post = get_post( $source_post_id );

            if( strpos( $post->post_content, 'movedo_single_image' ) ) {
                $shortcode_attributes = $this->get_string_between( $post->post_content, '[movedo_single_image', ']' );
                $shortcode = "[movedo_single_image$shortcode_attributes]";
                $source_image_id = $this->get_string_between( $shortcode_attributes, 'image="', '"' );
                $image_html = get_the_attachment_link( $source_image_id, 'large' );
            } else {
                continue;
            }

            restore_current_blog();

            $new_post   = get_post( $new_post_id );

            $content = str_replace(
                $shortcode,
                $image_html,
                $new_post->post_content
            );

            wp_update_post(
                array( 
                    'ID'=> $new_post_id, 
                    'post_content' => $content 
                )
            );
        }
    }

    public function get_string_between($string, $start, $end){
        $string = ' ' . $string;
        $ini = strpos($string, $start);
        if ($ini == 0) return '';
        $ini += strlen($start);
        $len = strpos($string, $end, $ini) - $ini;
        return substr($string, $ini, $len);
    }
}
add_action( 'plugins_loaded', 'mcc_vc_shortcode_render_load');
function mcc_vc_shortcode_render_load(){
    $GLOBALS['MCC_VC_Shortcode_Render'] = MCC_VC_Shortcode_Render::get_instance();
}

}

0 个答案:

没有答案