Skip to content

ID Missing in filter code_snippets/allow_execute_snippet #36

@signo

Description

@signo

There is a bug in the new filter code_snippets/allow_execute_snippet, i suggest the code bellow to correct the error. The safe mode code is working perfect with this filter, now whenever i make a mistake i append &safe_mode=2&sid=<snippet id> to the url and it disables the snippet, as a suggestion later you could add an option to show a snippet id column.

Edit: I updated the code to add the enhancement suggested in issue # 37

function execute_active_snippets() {

    /* Bail early if safe mode is active */
    if ( defined( 'CODE_SNIPPETS_SAFE_MODE' ) && CODE_SNIPPETS_SAFE_MODE ) {
        return false;
    }

    global $wpdb;

    if ( ! isset( $wpdb->snippets, $wpdb->ms_snippets ) ) {
        set_snippet_table_vars();
    }

    /* Check if the snippets table exists */
    if ( $wpdb->get_var( "SHOW TABLES LIKE '$wpdb->snippets'" ) === $wpdb->snippets ) {
        $sql = "SELECT id, code  FROM {$wpdb->snippets} WHERE active=1";
    }

    /* Check if the multisite snippets table exists */
    if ( is_multisite() && $wpdb->get_var( "SHOW TABLES LIKE '$wpdb->ms_snippets'" ) === $wpdb->ms_snippets ) {
        $sql = ( isset( $sql ) ? $sql . "\nUNION ALL\n" : '' );
        $sql .= "SELECT code FROM {$wpdb->ms_snippets} WHERE active=1";
    }

    if ( ! empty( $sql ) ) {
        $sql .= sprintf( ' AND (scope=0 OR scope=%d)', is_admin() ? 1 : 2 );

        /* Grab the active snippets from the database */
        $active_snippets = $wpdb->get_results( $sql, OBJECT_K );

        foreach ( $active_snippets as $snippet_id => $snippet ) {
            if ( apply_filters( 'code_snippets/allow_snippet_execute', true, $snippet_id ) ) {
                /* Execute the PHP code */
                $result = execute_snippet( $snippet->code );
                do_action( 'code_snippets/after_snippet_execute', $result, $snippet_id );
            }
        }

        return true;
    }

    /* If we're made it this far without returning true, assume failure */
    return false;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions