Beispiel #1
0
/**
 * Create the snippet tables
 * This function will only execute once per page load, except if $redo is true
 *
 * @since 1.7.1
 *
 * @param bool $upgrade Run the table creation code even if the table exists
 */
function create_code_snippets_tables($upgrade = false)
{
    global $wpdb;
    /* Set the table name variables if not yet defined */
    if (!isset($wpdb->snippets, $wpdb->ms_snippets)) {
        set_snippet_table_vars();
    }
    if (is_multisite()) {
        /* Create the network snippets table if it doesn't exist, or upgrade it */
        if ($upgrade || $wpdb->get_var("SHOW TABLES LIKE '{$wpdb->ms_snippets}'") !== $wpdb->ms_snippets) {
            create_code_snippets_table($wpdb->ms_snippets);
        }
    }
    /* Create the table if it doesn't exist, or upgrade it */
    if ($upgrade || $wpdb->get_var("SHOW TABLES LIKE '{$wpdb->snippets}'") !== $wpdb->snippets) {
        create_code_snippets_table($wpdb->snippets);
    }
}
Beispiel #2
0
/**
 * Run the active snippets
 *
 * @since 2.0
 *
 * @return bool true on success, false on failure
 */
function execute_active_snippets()
{
    /* Bail early if safe mode is active */
    if (defined('CODE_SNIPPETS_SAFE_MODE') && CODE_SNIPPETS_SAFE_MODE) {
        return false;
    }
    if (isset($_GET['code_snippets_safe_mode']) && $_GET['code_snippets_safe_mode'] && current_user_can(get_snippets_cap())) {
        return false;
    }
    /** @var wpdb $wpdb */
    global $wpdb;
    if (!isset($wpdb->snippets, $wpdb->ms_snippets)) {
        set_snippet_table_vars();
    }
    $current_scope = is_admin() ? 1 : 2;
    /* Check if the snippets tables exist */
    $table_exists = $wpdb->get_var("SHOW TABLES LIKE '{$wpdb->snippets}'") === $wpdb->snippets;
    $ms_table_exists = is_multisite() && $wpdb->get_var("SHOW TABLES LIKE '{$wpdb->ms_snippets}'") === $wpdb->ms_snippets;
    $sql = '';
    /* Fetch snippets from site table */
    if ($table_exists) {
        $sql = $wpdb->prepare("SELECT id, code FROM {$wpdb->snippets} WHERE active=1 AND (scope=0 OR scope=%d)", $current_scope);
    }
    /* Fetch snippets from the network table */
    if ($ms_table_exists) {
        if (!empty($sql)) {
            $sql .= ' UNION ALL ';
        }
        /* Only select snippets in the current scope */
        $sql .= $wpdb->prepare("SELECT id, code FROM {$wpdb->ms_snippets} WHERE active=1 AND (scope=0 OR scope=%d)", $current_scope);
        /* Add shared network snippets */
        if ($active_shared_ids = get_option('active_shared_network_snippets', false)) {
            $sql .= ' UNION ALL ';
            $sql .= $wpdb->prepare(sprintf("SELECT id, code FROM {$wpdb->ms_snippets} WHERE id IN (%s)", implode(',', array_fill(0, count($active_shared_ids), '%d'))), $active_shared_ids);
        }
    }
    /* Return false if there is no query */
    if (empty($sql)) {
        return false;
    }
    /* Grab the snippets from the database */
    $active_snippets = $wpdb->get_results($sql, OBJECT_K);
    /* Loop through the returned snippets and execute the PHP code */
    foreach ($active_snippets as $snippet_id => $snippet) {
        if (apply_filters('code_snippets/allow_execute_snippet', true, $snippet_id)) {
            execute_snippet($snippet->code, $snippet_id);
        }
    }
    return true;
}
 *
 * This can later be passed to functions such as
 * plugin_dir_path(), plugins_url() and plugin_basename()
 * to retrieve information about plugin paths
 *
 * @since 2.0
 * @var string
 */
define('CODE_SNIPPETS_FILE', __FILE__);
/**
 * Load plugin files
 */
foreach (array('db.php', 'caps.php', 'snippet-ops.php', 'upgrade.php', 'admin.php', 'editor.php', 'manage/manage.php', 'edit/edit.php', 'import/import.php', 'settings/editor-preview.php', 'settings/settings-fields.php', 'settings/settings.php', 'settings/admin.php') as $include) {
    require plugin_dir_path(__FILE__) . "includes/{$include}";
}
/* Initialize database table variables */
set_snippet_table_vars();
/* Execute the snippets once the plugins are loaded */
add_action('plugins_loaded', 'execute_active_snippets', 1);
/**
 * Load up the localization file if we're using WordPress in a different language.
 * Place it in this plugin's "languages" folder and name it "code-snippets-[language_COUNTRY].mo"
 *
 * If you wish to contribute a language file to be included in the Code Snippets package,
 * please see create an issue on GitHub: https://github.com/sheabunge/code-snippets/issues
 */
function code_snippets_load_textdomain()
{
    load_plugin_textdomain('code-snippets', false, dirname(plugin_basename(__FILE__)) . '/languages');
}
add_action('plugins_loaded', 'code_snippets_load_textdomain');
/**
 * Run the active snippets
 *
 * @since 2.0
 * @return boolean true on success, false on failure
 */
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 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_col($sql);
        foreach ($active_snippets as $snippet_id => $snippet_code) {
            if (apply_filters('code_snippets/allow_execute_snippet', true, $snippet_id)) {
                /* Execute the PHP code */
                execute_snippet($snippet_code);
            }
        }
        return true;
    }
    /* If we're made it this far without returning true, assume failure */
    return false;
}