public static function meta_reset() { global $pagenow; self::$reset_meta = apply_filters('piklist_reset_meta_admin_pages', self::$reset_meta); if (in_array($pagenow, self::$reset_meta)) { foreach (self::$reset_meta as $page => $data) { if (isset($_REQUEST[$data['id']])) { wp_cache_replace($_REQUEST[$data['id']], false, $data['group']); break; } } } }
function filter_object_item($terms, $object_ids, $taxonomies) { global $current_user; switch ($taxonomies) { case '\'category\'': // Gets the current Post Categories Cache $_categories = wp_cache_get($object_ids, 'category_relationships'); if (is_array($_categories)) { foreach ($_categories as $id => $cat) { if (!capa_protect::user_can_access($id, $current_user, 'category')) { unset($_categories[$id]); } } wp_cache_replace($object_ids, $_categories, 'category_relationships'); } break; } return $terms; }
function update_var($field = null) { $options = array(); $flag = true; if ($field == null) { $options['cf_registered_sidebars'] = $this->cf_registered_sidebars; $options['sidebars_fields'] = $this->sidebars_fields; $options['cf_registered_fields'] = $this->cf_registered_fields; } else { if (empty($this->options)) { $this->options = wp_cache_get('cf_options-' . $this->post_type, FLAG_CACHE); } $options = $this->options; if (!isset($options[$field]) || isset($options[$field]) && $options[$field] !== $this->{$field}) { $options[$field] = $this->{$field}; } else { $flag = false; } } if ($flag == true) { update_option('cf_options-' . $this->post_type, $options); wp_cache_replace('cf_options-' . $this->post_type, $options, FLAG_CACHE, 3600); } }
/** * Process any EasyRecipes in all the posts on the page * We need to do this here rather than in the_content hook because by then it's too late to queue up the scripts/styles we'll need * * @param $posts * * @return array */ function thePosts($posts) { /* @var $wp_rewrite WP_Rewrite */ global $wp_rewrite; /** @global $wpdb wpdb */ global $wpdb; /** * We don't want to process anything if it's a missing URL */ if (is_404()) { return $posts; } global $shortcode_tags; $guestpost = null; $newPosts = array(); /** * Process each post and replace placeholders with relevant data */ foreach ($posts as $post) { /** * Have we already processed this post? */ if (isset($this->easyrecipes[$post->ID])) { $post->post_content = $this->postContent[$post->ID]; $newPosts[] = $post; continue; } /** * We may have to change the rating method (e.g. for Ziplist recipes) so make a local copy */ $this->ratingMethod = $this->settings->ratings; /** * Do we want to take over processing of other recipe plugins posts? * If this is a post from another plugin and we are displaying it, make the rating method "Self Rated" unless ratings are disabled */ if (!is_admin()) { $this->converted = false; if ($this->settings->displayZiplist) { $post->post_content = preg_replace_callback('/\\[amd-(zlrecipe)-recipe:(\\d+)\\]/', array($this, 'doConvert'), $post->post_content); if ($this->converted && $this->settings->ratings != 'Disabled') { $this->ratingMethod = 'SelfRated'; } } if ($this->settings->displayRecipeCard) { $post->post_content = preg_replace_callback('/\\[(yumprint)-recipe id=[\'"]"(\\d+)[\'"]\\]/', array($this, 'doConvert'), $post->post_content); if ($this->converted && $this->settings->ratings != 'Disabled') { $this->ratingMethod = 'SelfRated'; } } if ($this->settings->displayGMC) { $post->post_content = preg_replace_callback('/\\[(gmc_recipe) (\\d+)\\]/', array($this, 'doConvert'), $post->post_content); if ($this->converted && $this->settings->ratings != 'Disabled') { $this->ratingMethod = 'SelfRated'; } } if ($this->settings->displayUltimateRecipe) { if ($post->post_type == 'recipe') { $post->post_content = "[ultimate-recipe id='{$post->ID}']"; $post->post_type = 'post'; } $post->post_content = preg_replace_callback('/\\[(ultimate-recipe) id=["\'](\\d+|random)["\']\\]/i', array($this, 'doConvert'), $post->post_content); if ($this->converted && $this->settings->ratings != 'Disabled') { $this->ratingMethod = 'SelfRated'; } } if ($this->settings->displayRecipage) { /** * Do a quick check before we go to the expense of instantiating a DOMDocument */ if (strpos($post->post_content, 'hrecipe f') !== false) { $document = new EasyRecipePlusDOMDocument($post->post_content); if ($document->isValid()) { /** @var DOMElement $hrecipe */ $hrecipe = $document->getElementByClassName('hrecipe'); if ($hrecipe) { $matches = array(); $matches[1] = 'recipage'; $matches[2] = $post->ID; $convertedRecipe = $this->doConvert($matches); /** @var DOMDocumentFragment $fragment */ $fragment = $document->createDocumentFragment(); $fragment->appendXML($convertedRecipe); $hrecipe->parentNode->replaceChild($fragment, $hrecipe); $content = $document->saveHTML(); $post->post_content = preg_replace('%^.*</head><body>(.*)</body></html>\\s*$%sm', '$1', $content); } } } } } /** * Handle the guest post shortcodes here because WP doesn't process them until much later * We may need to redirect so we need to do it before anything else has a chance to do output * We may also need to process a recipe */ if ($post->ID == $this->settings->gpDetailsPage || $post->ID == $this->settings->gpEntryPage || $post->ID == $this->settings->gpThanksPage) { if (empty($guestPost)) { $guestPost = new EasyRecipePlusGuestPost($this); } $gpResult = $guestPost->process($post); /** * If $guestPost->process() returns something, we processed the GP entry page * In this case, all we need to do is save it in $newPosts and continue * Otherwise, continue with processing since there may conceivably be an EasyRecipe on the post/page * * TODO - do we really need to do this? Won't it get picked up just below anyway? */ if ($gpResult) { $newPosts[] = $gpResult; continue; } } $postDOM = new EasyRecipePlusDocument($post->post_content); if (!$postDOM->isEasyRecipe) { if (strncasecmp($post->post_content, '[easyrecipe_page]', 17) === 0) { $this->easyrecipes[$post->ID] = true; $post->post_content = str_replace('[easyrecipe_page]', '', $post->post_content); } $newPosts[] = $post; continue; } $postDOM->setSettings($this->settings); /** * Mark this post as an easyrecipe so that the comment and rating processing know */ $this->easyrecipes[$post->ID] = true; /** * Make sure we haven't already formatted this post. This can happen in preview mode where WP replaces the post_content * of the parent with the autosave content which we've already processed. * If this is the case, save the formatted code and mark this post as having been processed * TODO - are there implications for the object cache for themes that re-read posts? */ if ($postDOM->isFormatted) { $this->postContent[$post->ID] = $post->post_content; $newPosts[] = $post; continue; } /** * Fix possibly broken times in older posts * Fix the Cholesterol typo oops in early versions */ if ($postDOM->recipeVersion < '3') { $postDOM->fixTimes("preptime"); $postDOM->fixTimes("cooktime"); $postDOM->fixTimes("duration"); $postDOM->setParentValueByClassName("cholestrol", $this->settings->lblCholesterol, "Cholestrol"); } $data = new stdClass(); /** * Get the ratings from the comment meta table if we use the EasyRecipe comment method * Other rating methods are handled in EasyRecipePlusDocument->applyStyle() * hasRatings is left unset for Self Rating */ if ($this->ratingMethod == 'EasyRecipe') { $q = "SELECT COUNT(*) AS count, SUM(meta_value) AS sum FROM {$wpdb->comments} JOIN {$wpdb->commentmeta} ON {$wpdb->commentmeta}.comment_id = {$wpdb->comments}.comment_ID "; $q .= "WHERE comment_approved = 1 AND meta_key = 'ERRating' AND comment_post_ID = {$post->ID} AND meta_value > 0"; $ratings = $wpdb->get_row($q); if ((int) $ratings->count > 0) { $data->ratingCount = $ratings->count; $data->ratingValue = number_format($ratings->sum / $ratings->count, 1); $data->ratingPC = $data->ratingValue * 100 / 5; $data->hasRating = true; } else { $data->hasRating = false; } } else { if ($this->ratingMethod == 'Disabled') { $data->hasRating = false; } } switch ($this->settings->saveButton) { case 'Ziplist': $data->saveButtonJS = self::ZIPLISTJS; $data->saveButton = sprintf(self::ZIPLISTBUTTON, $this->settings->ziplistPartnerKey, urlencode(get_permalink($post->ID)), $this->settings->lblSave); $data->hasSave = true; break; case 'BigOven': $data->saveButtonJS = ''; $data->saveButton = sprintf(self::BIGOVENBUTTON, self::$EasyRecipePlusUrl); $data->hasSave = true; break; } $this->settings->getLabels($data); $data->hasLinkback = $this->settings->allowLink; $data->displayPrint = $this->settings->displayPrint; $data->style = $this->styleName; $data->title = $post->post_title; $data->blogname = get_option("blogname"); // TODO - do all this stuff at initialise time? $data->siteURL = $this->homeURL; /** * If the site isn't using permalinks then just pass the print stuff as a qurerystring param */ if ($wp_rewrite->using_permalinks()) { $data->sitePrintURL = $data->siteURL; } else { $data->sitePrintURL = $data->siteURL . "?"; } $data->postID = $post->ID; $data->recipeurl = get_permalink($post->ID); $data->convertFractions = $this->settings->convertFractions; if ($this->styleName[0] == '_') { $styleName = substr($this->styleName, 1); $templateFile = $this->settings->customTemplates . "/styles/{$styleName}/style.html"; } else { $templateFile = self::$EasyRecipePlusDir . "/styles/{$this->styleName}/style.html"; } $template = new EasyRecipePlusTemplate($templateFile); $data->isLoggedIn = is_user_logged_in(); /** * Apply styles to the recipe data and return the content with recipes replace by a shortcode and also each recipe's HTML * Also keep a copy so we don't have to reformat in the case where the theme asks for the same post again * * This didn't work! Some themes don't call the_content() (esp for excerpts) so we can't rely on hooking into that to supply the formatted html * We need to do it right here - it seems that the_posts is the only reliable place to replace the base recipe HTML with the formatted recipe HTML */ /** * Replace the original content with the one that has the easyrecipe(s) nicely formatted and marked up * Also keep a copy so we don't have to reformat in the case where the theme asks for the same post again */ $this->postContent[$post->ID] = $post->post_content = $postDOM->applyStyle($template, $data); /** * If we haven't already done so, hook into the_content filter to stop wpauto() messing with recipe HTML */ if (empty($shortcode_tags['easyrecipe'])) { add_filter('the_content', array($this, 'theContent'), 0); add_shortcode('easyrecipe', array($this, 'replaceRecipeShortcode')); } /** * Some themes do a get_post() again instead of using the posts as modified by plugins * So make sure our modified post is in cache so the get_post() picks up the modified version not the original * Need to do both add and replace since add doesn't replace and replace doesn't add and we can't be sure if the cache key exists at this point */ wp_cache_add($post->ID, $post, 'posts'); wp_cache_replace($post->ID, $post, 'posts'); $newPosts[] = $post; } return $newPosts; }
public function test_wp_cache_replace() { $key = 'my-key'; $val1 = 'first-val'; $val2 = 'second-val'; $fake_key = 'my-fake-key'; // Save the first value to cache and verify wp_cache_set($key, $val1); $this->assertEquals($val1, wp_cache_get($key)); // Replace the value and verify wp_cache_replace($key, $val2); $this->assertEquals($val2, wp_cache_get($key)); // Non-existant key should fail $this->assertFalse(wp_cache_replace($fake_key, $val1)); // Make sure $fake_key is not stored $this->assertFalse(wp_cache_get($fake_key)); }
/** * Replace a value in the object cache, if the value already exists. * * Errors if the value can't be replaced. * * ## OPTIONS * * <key> * : Cache key. * * <value> * : Value to replace. * * [<group>] * : Method for grouping data within the cache which allows the same key to be used across groups. * * [<expiration>] * : Define how long to keep the value, in seconds. `0` means as long as possible. * --- * default: 0 * --- * * ## EXAMPLES * * # Replace cache. * $ wp cache replace my_key new_value my_group * Success: Replaced object 'my_key' in group 'my_group'. */ public function replace($args, $assoc_args) { list($key, $value) = $args; $group = \WP_CLI\Utils\get_flag_value($args, 2, ''); $expiration = \WP_CLI\Utils\get_flag_value($args, 3, 0); $result = wp_cache_replace($key, $value, $group, $expiration); if (false === $result) { WP_CLI::error("Could not replace object '{$key}' in group '{$group}'. Does it already exist?"); } WP_CLI::success("Replaced object '{$key}' in group '{$group}'."); }
/** * Update or add option value to hidden post type. * * @param string $name Option name. * @param mixed $value Option value. * * @return mixed Returns false on failure. */ function xu_update_option($name, $value) { $name = xu_get_option_name($name); if (empty($name)) { return false; } $old = xu_get_option($name); if ($old === $value) { return false; } if ($old === false) { return xu_add_option($name, $value); } if (!($post_id = xu_get_option_post_id($name))) { return $default; } if (!update_post_meta($post_id, 'option_value', $value)) { return false; } wp_cache_replace($name, $value, 'xu_large_option'); return true; }
function &get_category(&$category, $output = OBJECT) { global $wpdb; if ( empty($category) ) return null; if ( is_object($category) ) { wp_cache_add($category->cat_ID, $category, 'category'); $_category = $category; } else { if ( ! $_category = wp_cache_get($category, 'category') ) { $_category = $wpdb->get_row("SELECT * FROM $wpdb->categories WHERE cat_ID = '$category' LIMIT 1"); wp_cache_add($category, $_category, 'category'); } } $_category = apply_filters('get_category', $_category); if ( !isset($_category->fullpath) ) { $_category = set_category_path($_category); wp_cache_replace($_category->cat_ID, $_category, 'category'); } if ( $output == OBJECT ) { return $_category; } elseif ( $output == ARRAY_A ) { return get_object_vars($_category); } elseif ( $output == ARRAY_N ) { return array_values(get_object_vars($_category)); } else { return $_category; } }
/** * Replace an existing value in the object cache. * * @uses wp_cache_replace * * @param array $args Function arguments. * @param array $assoc_args Function arguments with parameter key. * @return void */ public function replace($args, $assoc_args) { if (count($assoc_args) + count($args) < 2) { WP_CLI::line('usage: wp cache replace <key> <value> [group] [expiration]'); exit; } list($key, $value) = $args; $group = isset($args[2]) ? $args[2] : ''; $expiration = isset($args[3]) ? $args[3] : 0; $result = wp_cache_replace($key, $value, $group, $expiration); if (false === $result) { WP_CLI::error("Could not replace object '{$key}' in group '{$group}'. Does it already exist?"); exit; } WP_CLI::success("Replaced object '{$key}' in group '{$group}'."); }
/** * get_body_class() * * @param int $post_id * @param $query * @return int $is_parent */ function get_body_class($post_id, $query) { $var = false; $post_id = (int) $post_id; $post = wp_cache_get($post_id, 'posts'); if ($post !== false) { if (isset($post->is_parent)) { $var = $post->is_parent; $this->cache_hits++; } else { $var = (int) self::$wpdb->get_var($query); $post->is_parent = $var; wp_cache_replace($post->ID, $post, 'posts'); } } return $var; }
$r = wp_cache_get('key', 'test-group'); $rf = wp_cache_get('key', 'test-group', true); echo "wp_cache_add > wp_cache_get force: " . bool_to_str($r == $rf); echo "wp_cache_add > wp_cache_get: " . bool_to_str($r == 'value'); $r = wp_cache_get('integer', 'test-group'); $rf = wp_cache_get('integer', 'test-group', true); echo "wp_cache_add exist > wp_cache_get force: " . bool_to_str($r == $rf); echo "wp_cache_add exist > wp_cache_get: " . bool_to_str($r == -1); // test wp_cache_delete wp_cache_delete('key', 'test-group'); wp_cache_delete('integer', 'test-group'); $false = wp_cache_get('key', 'test-group'); $falsef = wp_cache_get('integer', 'test-group', true); echo "wp_cache_delete > wp_cache_get force:" . bool_to_str($falsef === $false); echo "wp_cache_delete > wp_cache_get: " . bool_to_str($false === false); // test wp_cache_replace $false = wp_cache_replace('key', 'value', 'test-group'); wp_cache_set('key', 'value', 'test-group'); $true = wp_cache_replace('key', 'value', 'test-group'); echo "wp_cache_replace: " . bool_to_str($true === true); echo "wp_cache_replace no exist: " . bool_to_str($false == false); $r = wp_cache_get('key', 'test-group'); $rf = wp_cache_get('key', 'test-group', true); echo "wp_cache_replace > wp_cache_get force: " . bool_to_str($r == $rf); echo "wp_cache_replace > wp_cache_get: " . bool_to_str($r == 'value'); // test wp_cache_set wp_cache_set('a', 'b', 'test-group'); $r = wp_cache_get('a', 'test-group'); $rf = wp_cache_get('a', 'test-group', true); echo "wp_cache_set > wp_cache_get force: " . bool_to_str($r == $rf); echo "wp_cache_set > wp_cache_get: " . bool_to_str($r == 'b');
/** * Get HTML Unique ID for each action box */ function getHtmlId() { //this to have unique ID attribute of the main containing div in case there is more //than 1 of the same action box. Cache key is composed of $_htmlUniqueId prefix and //ID of the action box. $actionBoxId = $this->getId(); $htmlUniqueId = wp_cache_get($this->_html_UniqueId . $actionBoxId); if (false === $htmlUniqueId) { //this is the first $htmlUniqueId = 1; wp_cache_set($this->_html_UniqueId . $actionBoxId, $htmlUniqueId); } else { //another box is already displayed $htmlUniqueId++; wp_cache_replace($this->_html_UniqueId . $actionBoxId, $htmlUniqueId); } return $actionBoxId . $htmlUniqueId; }
/** * Process any EasyRecipes in all the posts on the page * We need to do this here rather than in the_content hook because by then it's too late to queue up the scripts/styles we'll need * * @param $posts * * @return array */ function thePosts($posts) { /* @var $wp_rewrite WP_Rewrite */ global $wp_rewrite; /** @global $wpdb wpdb */ global $wpdb; /** * We don't want to process anything if it's a missing URL */ if (is_404()) { return $posts; } global $shortcode_tags; $guestpost = null; $newPosts = array(); /** * Process each post and replace placeholders with relevant data */ foreach ($posts as $post) { /** * Have we already processed this post? */ if (isset($this->easyrecipes[$post->ID])) { $post->post_content = $this->postContent[$post->ID]; $newPosts[] = $post; continue; } /** * We may have to change the rating method (e.g. for Ziplist recipes) so make a local copy */ $this->ratingMethod = $this->settings->ratings; $postDOM = new EasyRecipeDocument($post->post_content); if (!$postDOM->isEasyRecipe) { $newPosts[] = $post; continue; } $postDOM->setSettings($this->settings); /** * Mark this post as an easyrecipe so that the comment and rating processing know */ $this->easyrecipes[$post->ID] = true; /** * Make sure we haven't already formatted this post. This can happen in preview mode where WP replaces the post_content * of the parent with the autosave content which we've already processed. * If this is the case, save the formatted code and mark this post as having been processed * TODO - are there implications for the object cache for themes that re-read posts? */ if ($postDOM->isFormatted) { $this->postContent[$post->ID] = $post->post_content; $newPosts[] = $post; continue; } /** * Fix possibly broken times in older posts * Fix the Cholesterol typo oops in early versions */ if ($postDOM->recipeVersion < '3') { $postDOM->fixTimes("preptime"); $postDOM->fixTimes("cooktime"); $postDOM->fixTimes("duration"); $postDOM->setParentValueByClassName("cholestrol", $this->settings->lblCholesterol, "Cholestrol"); } $data = new stdClass(); /** * Get the ratings from the comment meta table if we use the EasyRecipe comment method * Other rating methods are handled in EasyRecipeDocument->applyStyle() * hasRatings is left unset for Self Rating */ if ($this->ratingMethod == 'EasyRecipe') { $q = "SELECT COUNT(*) AS count, SUM(meta_value) AS sum FROM {$wpdb->comments} JOIN {$wpdb->commentmeta} ON {$wpdb->commentmeta}.comment_id = {$wpdb->comments}.comment_ID "; $q .= "WHERE comment_approved = 1 AND meta_key = 'ERRating' AND comment_post_ID = {$post->ID} AND meta_value > 0"; $ratings = $wpdb->get_row($q); if ((int) $ratings->count > 0) { $data->ratingCount = $ratings->count; $data->ratingValue = number_format($ratings->sum / $ratings->count, 1); $data->ratingPC = $data->ratingValue * 100 / 5; $data->hasRating = true; } else { $data->hasRating = false; } } else { if ($this->ratingMethod == 'Disabled') { $data->hasRating = false; } } $this->settings->getLabels($data); $data->hasLinkback = $this->settings->allowLink; $data->displayPrint = $this->settings->displayPrint; $data->style = $this->styleName; $data->title = $post->post_title; $data->blogname = get_option("blogname"); // TODO - do all this stuff at initialise time? $data->siteURL = $this->homeURL; /** * If the site isn't using permalinks then just pass the print stuff as a qurerystring param */ if ($wp_rewrite->using_permalinks()) { $data->sitePrintURL = $data->siteURL; } else { $data->sitePrintURL = $data->siteURL . "?"; } $data->postID = $post->ID; $data->recipeurl = get_permalink($post->ID); $data->convertFractions = $this->settings->convertFractions; if ($this->styleName[0] == '_') { $styleName = substr($this->styleName, 1); $templateFile = $this->settings->customTemplates . "/styles/{$styleName}/style.html"; } else { $templateFile = self::$EasyRecipeDir . "/styles/{$this->styleName}/style.html"; } $template = new EasyRecipeTemplate($templateFile); /** * Apply styles to the recipe data and return the content with recipes replace by a shortcode and also each recipe's HTML * Also keep a copy so we don't have to reformat in the case where the theme asks for the same post again * * This didn't work! Some themes don't call the_content() (esp for excerpts) so we can't rely on hooking into that to supply the formatted html * We need to do it right here - it seems that the_posts is the only reliable place to replace the base recipe HTML with the formatted recipe HTML */ /** * Replace the original content with the one that has the easyrecipe(s) nicely formatted and marked up * Also keep a copy so we don't have to reformat in the case where the theme asks for the same post again */ $this->postContent[$post->ID] = $post->post_content = $postDOM->applyStyle($template, $data); /** * If we haven't already done so, hook into the_content filter to stop wpauto() messing with recipe HTML */ if (empty($shortcode_tags['easyrecipe'])) { add_filter('the_content', array($this, 'theContent'), 0); add_shortcode('easyrecipe', array($this, 'replaceRecipeShortcode')); } /** * Some themes do a get_post() again instead of using the posts as modified by plugins * So make sure our modified post is in cache so the get_post() picks up the modified version not the original * Need to do both add and replace since add doesn't replace and replace doesn't add and we can't be sure if the cache key exists at this point */ wp_cache_add($post->ID, $post, 'posts'); wp_cache_replace($post->ID, $post, 'posts'); $newPosts[] = $post; } return $newPosts; }
/** * Replace an existing value in the object cache. * * @synopsis <key> <value> [<group>] [<expiration>] */ public function replace($args, $assoc_args) { list($key, $value) = $args; $group = isset($args[2]) ? $args[2] : ''; $expiration = isset($args[3]) ? $args[3] : 0; $result = wp_cache_replace($key, $value, $group, $expiration); if (false === $result) { WP_CLI::error("Could not replace object '{$key}' in group '{$group}'. Does it already exist?"); } WP_CLI::success("Replaced object '{$key}' in group '{$group}'."); }
private function cache_get($key, $cache_time) { $t = wp_cache_get($key, 'dymoloader1.0'); if ($t !== false && isset($t['data'])) { // check soft expire if ($t['softexpire'] < time()) { // update cache with new soft expire time $t['softexpire'] = time() + ($cache_time - 5 * MINUTE_IN_SECONDS); wp_cache_replace($key, $t, 'dymoloader1.0', $cache_time); } return json_decode(gzuncompress($t['data']), true); } else { return NULL; } }
/** * Filter the list of terms attached to the given post. * * @since 1.0.0 * @access public * * @see get_the_terms() * * @param array $terms List of attached terms. * @param int $post_id Post ID. * @param string $taxonomy Name of the taxonomy. */ function get_the_terms($terms, $post_id, $taxonomy) { $terms = wp_get_object_terms(array($post_id), $taxonomy, array('orderby' => 'term_order')); wp_cache_replace($post_id, $terms, $taxonomy . '_relationships'); return $terms; }