/** * 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; $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; }
/** * 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; }