php 压平WP Utility Script Runner的短代码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 压平WP Utility Script Runner的短代码相关的知识,希望对你有一定的参考价值。

<?php if(!defined('ABSPATH')) { die(); } // This line ensures that the script is not run directly
/**
 * Utility Name: Remove and Flatten Shortcodes
 * Description: Strip Shortcodes from all posts, with several processing options
 * Supports: input
 * Author: Greg Schoppe
 * Author URI: https://gschoppe.com
 * Version: 0.0.2
 **/

function gjs_strip_shortcodes_input_html( $html ) {
    global $shortcode_tags;
    $post_types = get_post_types( array(
        'exclude_from_search' => false
    ), 'names' );
    $post_types = array_diff( $post_types, array( 'revision', 'attachment' ) );
?>
<p>
    <strong>WARNING! DO NOT RUN THIS ON A LIVE SITE!</strong><br>
    This utility may cause data loss. Even restoring a backup may wipe out any changes since the last run of this utility.
</p>
<label>
    <input type="checkbox" name="restore" value="restore"/>
    <span>Restore From Backup</span>
</label>
<label>
    <span>Post Types to Process</span>
    <select name="post_types" multiple="multiple">
        <option value="any" selected="selected">all</option>
<?php
        foreach( $post_types as $post_type ) {
            echo '<option value="' . esc_attr( $post_type ) . '">' . $post_type . '</option>';
        }
?>
    </select>
</label>
<hr/>
<p>
    Select what you would like to do with each shortcode.
</p>
<?php
    if( !empty( $shortcode_tags ) ) {
        foreach( $shortcode_tags as $tag => $callable ) {
?>
<label>
    <span>[<?php echo $tag; ?>]</span>
    <select name="shortcodes[<?php echo esc_attr( $tag ); ?>]"/>
        <option value="skip">Skip (do not process)</option>
        <option value="strip">Remove (deletes shortcode content)</option>
        <option value="unwrap">Unwrap Content</option>
        <option value="parse">Flatten (parses to HTML)</option>
    </select>
</label>
<?php
        }
    }
    ?>

    <?php
    return ob_get_clean();
}
add_filter('wp_util_input_html', 'gjs_strip_shortcodes_input_html');

function gjs_strip_shortcodes_run( $legacy, $state, $atts ) {
    $batch = 10;
    $offset = 0;
    if( !empty( $state['offset'] ) ) {
        $offset = $state['offset'];
    }
    $result = array(
        'state'   => 'error',
        'message' => 'an unknown error has occurred',
    );
    $post_types = 'any';
    if( !empty( $atts['post_types'] ) && !in_array( 'any', $atts['post_types'] ) ) {
        $post_types = $atts['post_types'];
    }
    $shortcode_settings = array();
    if( !empty( $atts['shortcodes'] ) ) {
        $shortcode_settings = $atts['shortcodes'];
    }
    $restore = true;
    if( empty( $atts['restore'] ) ) {
        gjs_replace_shortcode_functions( $shortcode_settings );
        $restore = false;
    }
    $query = new WP_Query( array(
        'posts_per_page' => $batch,
        'offset' => $offset,
        'post_type' => $post_types
    ) );
    if( !$query->have_posts() ) {
        $result = array(
            'state' => 'complete',
            'message' => 'successfully processed all posts'
        );
    } else {
        $offset += $query->post_count;
        while( $query->have_posts() ) {
            $query->the_post();
            $post = get_post();
            $backup = get_post_meta( $post->ID, '_gjs_flatten_shortcodes_backup', true );
            if( $restore ) {
                if( $backup ) {
                    $post->post_content = $backup;
                    wp_update_post( $post );
                    delete_post_meta( $post->ID, '_gjs_flatten_shortcodes_backup' );
                }
            } else {
                if( !$backup ) {
                    update_post_meta( $post->ID, '_gjs_flatten_shortcodes_backup', $post->post_content );
                }
                $post->post_content = do_shortcode( $post->post_content );
                wp_update_post( $post );
            }
        }
        $result = array(
            'state' => array(
                'offset' => $offset
            ),
            'message' => $offset . ' posts processed'
        );
    }

    return $result;
}
add_filter('wp_util_script', 'gjs_strip_shortcodes_run', 10, 3);

function gjs_replace_shortcode_functions( $settings = array() ) {
    global $shortcode_tags;
    foreach( $shortcode_tags as $tag => $callable ) {
        $setting = 'skip';
        if( !empty( $settings[$tag] ) ) {
            $setting = $settings[$tag];
        }
        switch( $setting ) {
            case 'strip' :
                $shortcode_tags[$tag] = "__return_empty_string";
                break;
            case 'unwrap':
                $shortcode_tags[$tag] = "gjs_replace_shortcode_unwrap";
                break;
            case 'parse' :
                // nothing needed
                break;
            case 'skip'  :
            default      :
                unset( $shortcode_tags[$tag] );
        }

    }
}
function gjs_replace_shortcode_unwrap( $atts=array(), $content='' ) {
    return $content;
}

以上是关于php 压平WP Utility Script Runner的短代码的主要内容,如果未能解决你的问题,请参考以下文章

php TRADURRE wordpress con wp_localize_script e ACF

Wordpress / WooCommerce wp_localize_script 将 php 变量日期添加到 javascript

引用脚本js文件与css文件

类 Foo\Bar\Baz 位于 ./foo/bar/utility/baz.php 不符合 psr-4 自动加载标准。跳过

Intel(R) Extreme Tuning Utility是啥软件

wp_localize_script 用于插入图像