WordPress.org

Plugin Directory

Changeset 619920


Ignore:
Timestamp:
11/01/12 17:03:13 (18 months ago)
Author:
cfinke
Message:

Custom CSS: Caching the post ID of the custom CSS post and doing some additional checking to make sure the custom CSS post exists before making decisions based on it.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • jetpack/trunk/modules/custom-css/custom-css.php

    r619533 r619920  
    122122 
    123123/** 
    124  * Get the safecss record 
     124 * Get the published custom CSS post. 
    125125 * 
    126126 * @return array 
    127127 */ 
    128128function get_safecss_post() { 
    129     $safecss_post = array(); 
    130     $a = array_shift( get_posts( array( 'posts_per_page' => 1, 'post_type' => 'safecss', 'post_status' => 'publish', 'orderby' => 'date', 'order' => 'DESC' ) ) ); 
    131     if ( $a ) 
    132         $safecss_post = get_object_vars( $a ); // needed for php 5.3 
    133     return $safecss_post; 
     129    $custom_css_post_id = custom_css_post_id(); 
     130 
     131    if ( $custom_css_post_id ) 
     132        return get_post( $custom_css_post_id, ARRAY_A ); 
     133 
     134    return array(); 
     135} 
     136 
     137/** 
     138 * Get the post ID of the published custom CSS post. 
     139 * 
     140 * @return int|bool The post ID if it exists; false otherwise. 
     141 */ 
     142function custom_css_post_id() { 
     143    $custom_css_post_id = wp_cache_get( 'custom_css_post_id' ); 
     144 
     145    if ( false === $custom_css_post_id ) { 
     146        $custom_css_post = array_shift( get_posts( array( 
     147            'posts_per_page' => 1, 
     148            'post_type' => 'safecss', 
     149            'post_status' => 'publish', 
     150            'orderby' => 'date', 
     151            'order' => 'DESC' 
     152        ) ) ); 
     153 
     154        if ( $custom_css_post ) 
     155            $custom_css_post_id = $custom_css_post->ID; 
     156        else 
     157            $custom_css_post_id = 0; 
     158 
     159        // Save post_id=0 to note that no safecss post exists. 
     160        wp_cache_set( 'custom_css_post_id', $custom_css_post_id ); 
     161    } 
     162 
     163    if ( ! $custom_css_post_id ) 
     164        return false; 
     165 
     166    return $custom_css_post_id; 
    134167} 
    135168 
     
    195228 
    196229        // Insert the CSS into wp_posts 
    197         return wp_insert_post( $post ); 
     230        $post_id = wp_insert_post( $post ); 
     231        wp_cache_set( 'custom_css_post_id', $post_id ); 
     232        return $post_id; 
    198233    } 
    199234 
     
    219254    // Do not update post if we are only saving a preview 
    220255    if ( false === $is_preview ) { 
    221         return wp_update_post( $safecss_post ); 
     256        $post_id = wp_update_post( $safecss_post ); 
     257        wp_cache_set( 'custom_css_post_id', $post_id ); 
     258        return $post_id; 
    222259    } 
    223260    else if ( !defined( 'DOING_MIGRATE' ) ) { 
     
    236273        } 
    237274        else { 
    238             $safecss_post = get_safecss_post(); 
    239  
    240             return (bool) ( get_option('safecss_add') == 'no' || get_post_meta( $safecss_post['ID'], 'custom_css_add', true ) == 'no' ); 
     275            $custom_css_post_id = custom_css_post_id(); 
     276 
     277            return (bool) ( get_option('safecss_add') == 'no' || ( $custom_css_post_id && get_post_meta( $custom_css_post_id, 'custom_css_add', true ) == 'no' ) ); 
    241278        } 
    242279    } 
Note: See TracChangeset for help on using the changeset viewer.