Plugin Directory

Changeset 326617


Ignore:
Timestamp:
12/28/2010 10:35:13 AM (15 years ago)
Author:
rukbat
Message:

Upgrade to version 1.2

File:
1 edited

Legend:

Unmodified
Added
Removed
  • nginx-manager/trunk/nginxmNginx.php

    r323094 r326617  
    5959        function purgePost($_ID) {
    6060           
    61             global $nginxm, $post, $blog_id;
     61            global $nginxm, $blog_id;
    6262           
    6363            $this->log( "Function purgePost BEGIN ===" );
     
    6565            switch ( current_filter() ) {
    6666                case 'comment_post':
    67                     $this->log( "New comment published/edited (id $_ID) on post '".addslashes($post->post_title)."' ($post->ID) on blog '".addslashes(get_bloginfo('name'))."' ($blog_id)" );
     67                    $_comment_ID = $_ID;
     68                    $_comment = get_comment($_ID);
     69                    $_ID = $_comment->comment_post_ID;
     70                    $this->log( "New comment published/edited (id $_comment_ID) on post ($_ID) on blog '".addslashes(get_bloginfo('name'))."' ($blog_id)" );
    6871                    break;
    6972                case 'publish_post':
     
    102105            if ( current_filter() == 'comment_post' ) {
    103106               
    104                 if ($post) {
     107                /* When a new comment is published */
     108                $this->_purge_by_options($_ID, $blog_id, $nginxm->options['automatically_purge_comment_page'], $nginxm->options['automatically_purge_comment_archive'], $nginxm->options['automatically_purge_comment_custom_taxa']);
     109               
     110            } else {
     111               
     112                /* When a post/page is modified */
     113                $this->_purge_by_options($_ID, $blog_id, $nginxm->options['automatically_purge_page'], $nginxm->options['automatically_purge_page_archive'], $nginxm->options['automatically_purge_page_custom_taxa']);
     114            }
     115           
     116            $this->log( "Function purgePost END ^^^" );
     117           
     118        }
     119       
     120       
     121        private function _purge_by_options($_post_ID, $blog_id, $_purge_page, $_purge_archive, $_purge_custom_taxa) {
     122           
     123            global $nginxm;
     124           
     125            $_post_type = get_post_type( $_post_ID );
     126           
     127            /* Purge cache for the related page/post */
     128            if ($_purge_page) {
     129               
     130                if ( $_post_type == 'post' || $_post_type == 'page' ) {
     131                    $this->log( "Purging $_post_type (id $_post_ID, blog id $blog_id)" );
     132                } else {
     133                    $this->log( "Purging custom post type '$_post_type' (id $_post_ID, blog id $blog_id)" );
     134                }
     135               
     136                $this->purgeUrl( get_permalink( $_post_ID ) );
     137            }
     138           
     139            /* Purge cache for archive related to the page (author, category, date) */
     140            if ($_purge_archive) {
     141               
     142                /* date */
     143                if ( $_post_type == 'post' ) {
    105144                   
    106                     /* When a new comment is published */
    107                     $this->_purge_by_options($post->ID, $blog_id, $nginxm->options['automatically_purge_comment_page'], $nginxm->options['automatically_purge_comment_archive']);
    108                 } else {
    109                     $this->log( "No post information for this comment", "ERROR" );
    110                 }
    111             } else {
    112                
    113                 /* When a post/page is modified */
    114                 $this->_purge_by_options($_ID, $blog_id, $nginxm->options['automatically_purge_page'], $nginxm->options['automatically_purge_page_archive']);
    115             }
    116            
    117             $this->log( "Function purgePost END ^^^" );
    118            
    119         }
    120        
    121        
    122         private function _purge_by_options($_post_ID, $blog_id, $_purge_page, $_purge_archive) {
    123 
    124             global $nginxm;
    125            
    126             $permalink = get_permalink($_post_ID);
    127                        
    128             /* Purge cache for the related page/post */
    129             if ($_purge_page) {
    130                 $this->log( "Purging page/post (id $_post_ID, blog id $blog_id)" );
    131                 $this->purgeUrl($permalink);
    132             }
    133            
    134             /* Purge cache for archive related to the page (author, category, date) */
    135             if ($_purge_archive) {
    136 
    137                 /* date */
    138                 $this->log( "Purging date" );
    139                 $day    = get_the_time('d', $_post_ID);
    140                 $month  = get_the_time('m', $_post_ID);
    141                 $year   = get_the_time('Y', $_post_ID);
    142                
    143                 if (isset($nginxm->options['purgeable_url']['date'][$year.$month.$day]))
    144                     $this->purgeUrl($nginxm->options['purgeable_url']['date'][$year.$month.$day]);
    145 
    146                 if (isset($nginxm->options['purgeable_url']['date'][$year.$month]))
    147                     $this->purgeUrl($nginxm->options['purgeable_url']['date'][$year.$month]);
    148                    
    149                 if (isset($nginxm->options['purgeable_url']['date'][$year]))
    150                     $this->purgeUrl($nginxm->options['purgeable_url']['date'][$year]);
    151                
    152                 /* category */
    153                 $this->log( "Purging category archive" );
    154                
    155                 $categories = wp_get_post_categories($_post_ID);
    156                
    157                 foreach ($categories as $category_id) {
    158                     $this->log( "Purging category ".$category_id );
    159                     if (isset($nginxm->options['purgeable_url']['category'][$category_id]))
    160                         $this->purgeUrl($nginxm->options['purgeable_url']['category'][$category_id]);
    161                 }
    162                
    163                 /* author */
    164                 $this->log( "Purging author" );
    165                 $author_id = get_post($_post_ID)->post_author;
    166                
    167                 if (isset($nginxm->options['purgeable_url']['author'][$author_id]))
    168                     $this->purgeUrl($nginxm->options['purgeable_url']['author'][$author_id]);                   
    169                    
    170             }
    171            
     145                    $this->log( "Purging date" );
     146                   
     147                    $day    = get_the_time('d', $_post_ID);
     148                    $month  = get_the_time('m', $_post_ID);
     149                    $year   = get_the_time('Y', $_post_ID);
     150                   
     151                    if ( $year ) {
     152                        $this->purgeUrl( get_year_link( $year ) );
     153                        if ( $month ) {
     154                            $this->purgeUrl( get_month_link( $year, $month ) );
     155                            if ( $day )
     156                                $this->purgeUrl( get_day_link( $year, $month, $day ) );
     157                        }
     158                    }
     159                }
     160               
     161                /* category */
     162                $this->log( "Purging category archives" );
     163               
     164                if ( $categories = wp_get_post_categories( $_post_ID ) ) {
     165                    foreach ( $categories as $category_id ) {
     166                        $this->log( "Purging category ".$category_id );
     167                        $this->purgeUrl( get_category_link( $category_id ) );
     168                    }
     169                }
     170               
     171                /* tags, @since Nginx Manager 1.2 */
     172                $this->log( "Purging tag archives" );
     173               
     174                if ( $tags = get_the_tags( $_post_ID ) ) {
     175                    foreach ( $tags as $tag ) {
     176                        $this->log( "Purging tag ".$tag->term_id );
     177                        $this->purgeUrl( get_tag_link( $tag->term_id ) );
     178                    }
     179                }
     180               
     181                /* author */
     182                $this->log( "Purging author archive" );
     183               
     184                if ( $author_id = get_post($_post_ID)->post_author )
     185                    $this->purgeUrl( get_author_posts_url( $author_id ) );
     186               
     187            }
     188           
     189            /* Purge related custom terms, @since Nginx Manager 1.2 */
     190            if ( $_purge_custom_taxa ) {
     191                $this->log( "Purging custom taxonomies related" );
     192                if ( $custom_taxonomies = get_taxonomies( array( 'public' => true, '_builtin' => false ) ) ) {
     193                    foreach ( $custom_taxonomies as $taxon ) {
     194                        // Extra check to get rid of the standard taxonomies
     195                        if ( !in_array( $taxon, array( 'category', 'post_tag', 'link_category' ) ) ) {
     196                            if ( $terms = get_the_terms( $_post_ID, $taxon ) ) {
     197                                foreach ( $terms as $term ) {
     198                                    $this->purgeUrl( get_term_link( $term, $taxon ) );
     199                                }
     200                            }
     201                        } else {
     202                            $this->log( "Your built-in taxonomy '".$taxon."' has param '_builtin' set to false.", "WARNING" );
     203                        }
     204                    }
     205                }
     206            }
     207           
    172208        }
    173209       
     
    177213            $this->log( "Purging URL | ".$url );
    178214           
    179             $parse      = parse_url($url);
     215            $parse      = parse_url($url);
    180216           
    181217            $this->_do_remote_get($parse['scheme'].'://'.$parse['host'].'/purge'.$parse['path']);
     
    185221                $this->_do_remote_get($parse['scheme'].'://'.$parse['host'].'/purge'.$parse['path'].$feed_string);
    186222            }
    187            
     223           
    188224        }
    189225       
     
    191227           
    192228            $response = wp_remote_get($url);
    193                
    194             if( is_wp_error( $response ) ) {
    195                 $_errors_str = implode(" - ",$response->get_error_messages());
     229           
     230            if( is_wp_error( $response ) ) {
     231                $_errors_str = implode(" - ",$response->get_error_messages());
    196232                $this->log( "Error while purging URL. ".$_errors_str, "ERROR" );
    197             } else {
    198                 if ( $response['response']['code'] ) {
    199                     switch ( $response['response']['code'] ) {
    200                         case 200:
    201                             $this->log( $url." *** PURGED ***" );
    202                             break;
    203                         case 404:
    204                             $this->log( $url." not found", "WARNING" );
    205                             break;
    206                     }
    207                 }
    208                
     233            } else {
     234                if ( $response['response']['code'] ) {
     235                    switch ( $response['response']['code'] ) {
     236                        case 200:
     237                            $this->log( $url." *** PURGED ***" );
     238                            break;
     239                        case 404:
     240                            $this->log( $url." is currently not cached" );
     241                            break;
     242                        default:
     243                            $this->log( $url." not found (".$response['response']['code'].")", "WARNING" );
     244                    }
     245                }
    209246            }           
    210247           
     
    213250        function checkHttpConnection() {
    214251           
    215             $purgeURL = plugins_url("nginx-manager/check-proxy.php");
     252            $purgeURL = plugins_url("nginx-manager/check-proxy.php");
    216253            $response = wp_remote_get($purgeURL);
    217254           
    218255            if( !is_wp_error( $response ) && ($response['body'] == 'HTTP Connection OK')) {
    219256                return "OK";
    220             }
    221 
    222             return "KO";
    223            
    224         }
    225        
    226         function captureCategoryLink($link, $category_id) {
    227            
    228             global $nginxm;
    229            
    230             $nginxm->options['purgeable_url']['category'][$category_id] = $link;
    231             update_option( "nginxm_options", $nginxm->options );
    232            
    233             return $link;
    234            
    235         }
    236        
    237         function captureAuthorLink($link, $author_id) {
    238            
    239             global $nginxm;
    240            
    241             $nginxm->options['purgeable_url']['author'][$author_id] = $link;
    242             update_option( "nginxm_options", $nginxm->options );
    243            
    244             return $link;
    245            
    246         }
    247 
    248         function captureDateLink($link, $year, $month = null, $day = null) {
    249            
    250             global $nginxm;
    251            
    252             if (!is_null($day)) {
    253                 $nginxm->options['purgeable_url']['date'][$year.$month.$day] = $link;
    254             } elseif (!is_null($month)) {
    255                 $nginxm->options['purgeable_url']['date'][$year.$month] = $link;
    256             } else {
    257                 $nginxm->options['purgeable_url']['date'][$year] = $link;
    258             }
    259             update_option( "nginxm_options", $nginxm->options );
    260            
    261             return $link;
     257            }
     258           
     259            return "KO";
    262260           
    263261        }
     
    273271                    fwrite($fp, "\n".gmdate("Y-m-d H:i:s ")." | ".$level." | ".$msg);
    274272                    fclose($fp);
    275                 } 
     273                }
    276274            }
    277275           
    278276            return true;
    279        
     277           
    280278        }
    281279       
     
    305303        }
    306304       
    307         function ngg_purge_url_on_saving_images($thumb_path) {
    308            
    309             global $current_blog;
    310            
    311             $this->log( "NGG image saved BEGIN ===" );
    312            
    313             $thumb_path = 'http://'.$current_blog->domain.$current_blog->path.substr(strrchr($thumb_path, "wp-content"), 0);
    314             $this->purgeUrl($thumb_path, false);
    315            
    316             $this->log( "NGG image saved END ^^^" );
    317            
    318         }
    319        
    320         function ngg_purge_post_on_editing_galleries($gallery_id) {
    321            
    322             global $wpdb, $blog_id;
    323            
    324             $this->log( "NGG gallery $gallery_id edited (blog $blog_id) BEGIN ===" );
    325            
    326             $post_ids = $wpdb->get_col(
    327                 $wpdb->prepare(
    328                     "SELECT ID
    329                     FROM $wpdb->posts
    330                     WHERE post_type = 'post'
    331                         AND post_status = 'publish'
    332                         AND post_content LIKE '%%[nggallery id=%d]%%'", $gallery_id) );
    333            
    334             if ($post_ids) {
    335                 foreach ($post_ids as $post_id) {
    336                     $this->log( "NGG gallery $gallery_id edited (blog $blog_id): purge post $post_id" );
    337                     $this->purgePost($post_id);
    338                 }
    339             } else {
    340                 $this->log( "NGG gallery $gallery_id edited (blog $blog_id): no posts with this gallery" );
    341             }
    342            
    343             $this->log( "NGG gallery $gallery_id edited (blog $blog_id) END ^^^" );
    344            
    345         }
    346        
    347305        function purgeImageOnEdit($attachment_id) {
    348306           
    349             global $blog_id;
    350            
    351307            $this->log( "Purging media on edit BEGIN ===" );
    352308           
    353309            // check if is an image
    354             if ( wp_attachment_is_image($attachment_id) ) {
    355                
    356                 $attachment = wp_get_attachment_metadata($attachment_id);
    357                
    358                 $main_image_link        = get_blog_details($blog_id)->siteurl."/files/".$attachment['file'];
    359                 $needle                 = strrchr( $main_image_link, '/' );
    360                 $image_url_permalink    = str_replace( $needle, "/", $main_image_link );
     310            if ( wp_attachment_is_image( $attachment_id ) ) {
    361311               
    362312                // 1. purge image "main" URL
    363                 $this->purgeUrl( $main_image_link, false );
     313                $this->purgeUrl( wp_get_attachment_url( $attachment_id ), false );
     314               
     315                $attachment = wp_get_attachment_metadata( $attachment_id );
    364316               
    365317                // 2. purge image other sizes
    366318                if ( $attachment['sizes'] ) {
    367319                    foreach ( $attachment['sizes'] as $size_name => $size ) {
    368                         $this->purgeUrl( $image_url_permalink.$size['file'], false );
     320                       
     321                        $resize_image = wp_get_attachment_image_src( $attachment_id, $size_name );
     322                        if ( $resize_image )
     323                            $this->purgeUrl( $resize_image[0], false );
    369324                    }
    370325                }
Note: See TracChangeset for help on using the changeset viewer.