diff --git a/readme.txt b/readme.txt index 9b9f447..f3a9e1d 100644 --- a/readme.txt +++ b/readme.txt @@ -85,9 +85,10 @@ Please post feature requests to [WP-FFPC feature request topic](http://wordpress == Changelog == = 1.1 = -*2013.03.25* +*under development* * added validate caching "noob" method: HTML comment option for cache engine & generation before closing "body" tag +* pre-cache possibility * bugfix for status check ( there were situations where the status was not updated correctly ) = 1.0 = diff --git a/wp-ffpc-class.php b/wp-ffpc-class.php index 47a4ecc..0fc4884 100644 --- a/wp-ffpc-class.php +++ b/wp-ffpc-class.php @@ -34,7 +34,16 @@ if ( ! class_exists( 'WP_FFPC' ) ) { const port_separator = ':'; const donation_id_key = 'hosted_button_id='; const global_config_var = '$wp_ffpc_config'; + const key_save = 'saved'; + const key_delete = 'deleted'; + const key_flush = 'flushed'; const slug_flush = '&flushed=true'; + const key_precache = 'precached'; + const slug_precache = '&precached=true'; + const key_precache_disabled = 'precache_disabled'; + const slug_precache_disabled = '&precache_disabled=true'; + const precache_log = 'wp-ffpc-precache.log'; + private $precache_message = ''; private $global_option = ''; private $global_config_key = ''; private $global_config = array(); @@ -44,9 +53,13 @@ if ( ! class_exists( 'WP_FFPC' ) ) { private $nginx_sample = ''; private $acache_backend = ''; private $button_flush; + private $button_precache; protected $select_cache_type = array (); protected $select_invalidation_method = array (); protected $valid_cache_type = array (); + private $precache_logfile = ''; + private $shell_function = false; + private $shell_possibilities = array (); /** @@ -63,8 +76,23 @@ if ( ! class_exists( 'WP_FFPC' ) ) { $this->acache_backend = $this->plugin_dir . $this->plugin_constant . '-backend.php'; /* flush button identifier */ $this->button_flush = $this->plugin_constant . '-flush'; + /* precache button identifier */ + $this->button_precache = $this->plugin_constant . '-precache'; /* global options identifier */ $this->global_option = $this->plugin_constant . '-global'; + /* precache log */ + $this->precache_logfile = sys_get_temp_dir() . '/' . self::precache_log; + /* search for a system function */ + $this->shell_possibilities = array ( 'shell_exec', 'exec', 'system', 'passthru' ); + $disabled_functions = array_map('trim', explode(',', ini_get('disable_functions') ) ); + + foreach ( $this->shell_possibilities as $possible ) { + if ( function_exists ($possible) && ! ( ini_get('safe_mode') || in_array( $possible, $disabled_functions ) ) ) { + /* set shell function */ + $this->shell_function = $possible; + break; + } + } /* set global config key; here, because it's needed for migration */ if ( $this->network ) @@ -152,6 +180,33 @@ if ( ! class_exists( 'WP_FFPC' ) ) { } } + /** + * extending admin init + * + */ + public function plugin_hook_admin_init () { + /* save parameter updates, if there are any */ + if ( isset( $_POST[ $this->button_flush ] ) ) { + $this->backend->clear(); + $this->status = 3; + header( "Location: ". $this->settings_link . self::slug_flush ); + } + + /* save parameter updates, if there are any */ + if ( isset( $_POST[ $this->button_precache ] ) ) { + + if ( $this->shell_function == false ) { + $this->status = 5; + header( "Location: ". $this->settings_link . self::slug_precache_disabled ); + } + else { + $this->precache_message = $this->precache(); + $this->status = 4; + header( "Location: ". $this->settings_link . self::slug_precache ); + } + } + } + /** * admin panel, the admin page displayed for plugin settings */ @@ -175,6 +230,7 @@ if ( ! class_exists( 'WP_FFPC' ) ) { plugin_donation_form(); + /** * if options were saved, display saved message */ @@ -185,24 +241,38 @@ if ( ! class_exists( 'WP_FFPC' ) ) { /** * if options were saved, display saved message */ - if ($_GET['saved']=='true' || $this->status == 1) : ?> + if ($_GET[ self::key_save ]=='true' || $this->status == 1) : ?>
plugin_constant ) ?>
plugin_constant ) ?>
plugin_constant ); ?>
plugin_constant ) ?>
Since precaching may take a very long time, it's done through a background CLI process in order not to run out of max execution time of PHP. Please enable one of the following functions if you whish to use precaching: " , $this->plugin_constant ) ?>shell_possibilities ); ?>
- - + shell_function == false ) ? 'disabled="disabled"' : '' ; ?> + class="button-primary" type="submit" name="button_precache ?>" id="button_precache ?>" value="plugin_constant ) ?>" /> + + + +
@@ -474,19 +584,6 @@ if ( ! class_exists( 'WP_FFPC' ) ) { button_flush ] ) ) { - $this->backend->clear(); - $this->status = 3; - header( "Location: ". $this->settings_link . self::slug_flush ); - } - } - /** * extending options_save * @@ -674,6 +771,105 @@ if ( ! class_exists( 'WP_FFPC' ) ) { update_site_option( $this->global_option , $this->global_config ); } + /** + * generate cache entry for every available permalink, might be very-very slow + * + */ + private function precache () { + /* container for links to precache */ + $links = array(); + + if ( $this->network ) { + $blog_list = get_blog_list( 0, 'all' ); + foreach ($blog_list as $blog) { + $this->precache_list_permalinks ( $links, $blog['blog_id'] ); + } + } + else { + $links = $this->precache_list_permalinks ( $links, false ); + } + + $tmpfile = tempnam(sys_get_temp_dir(), 'wp-ffpc'); + + $out .= ''; + + file_put_contents ( $tmpfile, $out ); + $shellfunction = $this->shell_function; + $shellfunction( 'php '. $tmpfile .' >'. $this->precache_logfile .' 2>&1 &' ); + } + + + /** + * gets all post-like entry permalinks for a site, returns values in passed-by-reference array + * + */ + private function precache_list_permalinks ( &$links, $site = false ) { + global $post; + $current_blog = get_current_blog_id(); + include_once ( ABSPATH . "wp-load.php" ); + + if ( $site !== false ) { + switch_to_blog( $site ); + } + + $args = array ( + 'post_type' => 'any', + 'posts_per_page' => -1, + 'post_status' => 'publish', + ); + + $posts = new WP_Query( $args ); + + while ( $posts->have_posts() ) { + $posts->the_post(); + + switch ($post->post_type) { + case 'revision': + case 'nav_menu_item': + break; + case 'page': + $permalink = get_page_link( $post->ID ); + break; + case 'post': + $permalink = get_permalink( $post->ID ); + break; + case 'attachment': + $permalink = get_attachment_link( $post->ID ); + break; + default: + $permalink = get_post_permalink( $post->ID ); + break; + } + + $links[] = $permalink; + + } + + wp_reset_postdata(); + + if ( $site !== false ) { + switch_to_blog( $current_blog ); + } + } } }