W3TCCdn_Environment

Class Cdn_Environment.

Defined (1)

The class is defined in the following location(s).

/Cdn_Environment.php  
  1. /* class Cdn_Environment 
  2. */ 
  3. class Cdn_Environment { 
  4.  
  5. /** 
  6. * Fixes environment in each wp-admin request 
  7. * @param Config $config 
  8. * @param bool $force_all_checks 
  9. * @throws Util_Environment_Exceptions 
  10. */ 
  11. public function fix_on_wpadmin_request( $config, $force_all_checks ) { 
  12. $exs = new Util_Environment_Exceptions(); 
  13.  
  14. if ( $config->get_boolean( 'config.check' ) || $force_all_checks ) { 
  15. if ( $config->get_boolean( 'cdn.enabled' ) ) { 
  16. $this->rules_add( $config, $exs ); 
  17. } else { 
  18. $this->rules_remove( $exs ); 
  19.  
  20. if ( count( $exs->exceptions() ) > 0 ) 
  21. throw $exs; 
  22.  
  23. /** 
  24. * Fixes environment once event occurs 
  25. * @param Config $config 
  26. * @param string $event 
  27. * @param Config|null $old_config 
  28. * @throws Util_Environment_Exceptions 
  29. */ 
  30. public function fix_on_event( $config, $event, $old_config = null ) { 
  31. if ( $config->get_boolean( 'cdn.enabled' ) && 
  32. !Cdn_Util::is_engine_mirror( $config->get_string( 'cdn.engine' ) ) ) { 
  33. if ( $old_config != null && 
  34. $config->get_integer( 'cdn.queue.interval' ) != 
  35. $old_config->get_integer( 'cdn.queue.interval' ) ) { 
  36. $this->unschedule_queue_process(); 
  37.  
  38. if ( !wp_next_scheduled( 'w3_cdn_cron_queue_process' ) ) { 
  39. wp_schedule_event( time(),  
  40. 'w3_cdn_cron_queue_process', 'w3_cdn_cron_queue_process' ); 
  41. } else { 
  42. $this->unschedule_queue_process(); 
  43.  
  44. if ( $config->get_boolean( 'cdn.enabled' ) && 
  45. $config->get_boolean( 'cdn.autoupload.enabled' ) && 
  46. !Cdn_Util::is_engine_mirror( $config->get_string( 'cdn.engine' ) ) ) { 
  47. if ( $old_config != null && 
  48. $config->get_integer( 'cdn.autoupload.interval' ) != 
  49. $old_config->get_integer( 'cdn.autoupload.interval' ) ) { 
  50. $this->unschedule_upload(); 
  51.  
  52. if ( !wp_next_scheduled( 'w3_cdn_cron_upload' ) ) { 
  53. wp_schedule_event( time(),  
  54. 'w3_cdn_cron_upload', 'w3_cdn_cron_upload' ); 
  55. } else { 
  56. $this->unschedule_upload(); 
  57.  
  58. $exs = new Util_Environment_Exceptions(); 
  59.  
  60. if ( $config->get_boolean( 'cdn.enabled' ) ) { 
  61. try { 
  62. $this->table_create( $event == 'activate' ); 
  63. } catch ( \Exception $ex ) { 
  64. $exs->push( $ex ); 
  65.  
  66. if ( count( $exs->exceptions() ) > 0 ) 
  67. throw $exs; 
  68.  
  69. /** 
  70. * Fixes environment after plugin deactivation 
  71. */ 
  72. public function fix_after_deactivation() { 
  73. $exs = new Util_Environment_Exceptions(); 
  74.  
  75. $this->rules_remove( $exs ); 
  76. $this->table_delete(); 
  77.  
  78. if ( count( $exs->exceptions() ) > 0 ) 
  79. throw $exs; 
  80.  
  81. /** 
  82. * Returns required rules for module 
  83. * @param Config $config 
  84. * @return array|null 
  85. */ 
  86. function get_required_rules( $config ) { 
  87. if ( !$config->get_boolean( 'cdn.enabled' ) ) 
  88. return null; 
  89.  
  90. $rewrite_rules = array(); 
  91. $rules = $this->rules_generate( $config ); 
  92.  
  93. if ( strlen( $rules ) > 0 ) { 
  94. if ( $config->get_string( 'cdn.engine' ) == 'ftp' ) { 
  95. $common = Dispatcher::component( 'Cdn_Core' ); 
  96. $domain = $common->get_cdn()->get_domain(); 
  97. $cdn_rules_path = sprintf( 'ftp://%s/%s', $domain,  
  98. Util_Rule::get_cdn_rules_path() ); 
  99. $rewrite_rules[] = array( 
  100. 'filename' => $cdn_rules_path,  
  101. 'content' => $rules 
  102. ); 
  103.  
  104. $path = Util_Rule::get_browsercache_rules_cache_path(); 
  105. $rewrite_rules[] = array( 
  106. 'filename' => $path,  
  107. 'content' => $rules 
  108. ); 
  109. return $rewrite_rules; 
  110.  
  111. /** 
  112. * @param Config $config 
  113. * @return array|null 
  114. */ 
  115. function get_instructions( $config ) { 
  116. if ( !$config->get_boolean( 'cdn.enabled' ) ) 
  117. return null; 
  118.  
  119. $instructions = array(); 
  120. $instructions[] = array( 'title'=>__( 'CDN module: Required Database SQL', 'w3-total-cache' ),  
  121. 'content' => $this->generate_table_sql(), 'area' => 'database' ); 
  122.  
  123. return $instructions; 
  124.  
  125. /** 
  126. * Generate rules for FTP 
  127. */ 
  128. public function rules_generate_for_ftp( $config ) { 
  129. return $this->rules_generate( $config, true ); 
  130.  
  131.  
  132.  
  133. /** 
  134. * table operations 
  135. */ 
  136.  
  137. /** 
  138. * Create queue table 
  139. * @param bool $drop 
  140. * @throws Util_Environment_Exception 
  141. */ 
  142. private function table_create( $drop = false ) { 
  143. global $wpdb; 
  144.  
  145. if ( $drop ) { 
  146. $sql = sprintf( 'DROP TABLE IF EXISTS `%s%s`;', $wpdb->base_prefix, W3TC_CDN_TABLE_QUEUE ); 
  147.  
  148. $wpdb->query( $sql ); 
  149.  
  150. $charset_collate = ''; 
  151.  
  152. if ( ! empty( $wpdb->charset ) ) 
  153. $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset"; 
  154. if ( ! empty( $wpdb->collate ) ) 
  155. $charset_collate .= " COLLATE $wpdb->collate"; 
  156.  
  157. $sql = sprintf( "CREATE TABLE IF NOT EXISTS `%s%s` ( 
  158. `id` int(11) unsigned NOT NULL AUTO_INCREMENT,  
  159. `local_path` varchar(500) NOT NULL DEFAULT '',  
  160. `remote_path` varchar(500) NOT NULL DEFAULT '',  
  161. `command` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '1 - Upload, 2 - Delete, 3 - Purge',  
  162. `last_error` varchar(150) NOT NULL DEFAULT '',  
  163. `date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',  
  164. PRIMARY KEY (`id`),  
  165. KEY `date` (`date`) 
  166. ) $charset_collate;", $wpdb->base_prefix, W3TC_CDN_TABLE_QUEUE ); 
  167.  
  168. $wpdb->query( $sql ); 
  169.  
  170. if ( !$wpdb->result ) 
  171. throw new Util_Environment_Exception( 'Can\'t create table ' . 
  172. $wpdb->base_prefix . W3TC_CDN_TABLE_QUEUE ); 
  173.  
  174. /** 
  175. * Delete queue table 
  176. * @return void 
  177. */ 
  178. private function table_delete() { 
  179. global $wpdb; 
  180.  
  181. $sql = sprintf( 'DROP TABLE IF EXISTS `%s%s`', $wpdb->base_prefix, W3TC_CDN_TABLE_QUEUE ); 
  182. $wpdb->query( $sql ); 
  183.  
  184. private function generate_table_sql() { 
  185. global $wpdb; 
  186. $charset_collate = ''; 
  187.  
  188. if ( ! empty( $wpdb->charset ) ) 
  189. $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset"; 
  190. if ( ! empty( $wpdb->collate ) ) 
  191. $charset_collate .= " COLLATE $wpdb->collate"; 
  192.  
  193. $sql = sprintf( 'DROP TABLE IF EXISTS `%s%s`;', $wpdb->base_prefix, W3TC_CDN_TABLE_QUEUE ); 
  194. $sql .= "\n" . sprintf( "CREATE TABLE IF NOT EXISTS `%s%s` ( 
  195. `id` int(11) unsigned NOT NULL AUTO_INCREMENT,  
  196. `local_path` varchar(500) NOT NULL DEFAULT '',  
  197. `remote_path` varchar(500) NOT NULL DEFAULT '',  
  198. `command` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '1 - Upload, 2 - Delete, 3 - Purge',  
  199. `last_error` varchar(150) NOT NULL DEFAULT '',  
  200. `date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',  
  201. PRIMARY KEY (`id`),  
  202. KEY `date` (`date`) 
  203. ) $charset_collate;", $wpdb->base_prefix, W3TC_CDN_TABLE_QUEUE ); 
  204.  
  205. return $sql; 
  206.  
  207. /** 
  208. * schedules 
  209. */ 
  210.  
  211. /** 
  212. * Unschedules cron events 
  213. */ 
  214. private function unschedule_queue_process() { 
  215. if ( wp_next_scheduled( 'w3_cdn_cron_queue_process' ) ) { 
  216. wp_clear_scheduled_hook( 'w3_cdn_cron_queue_process' ); 
  217.  
  218. /** 
  219. * Unschedule upload event 
  220. */ 
  221. private function unschedule_upload() { 
  222. if ( wp_next_scheduled( 'w3_cdn_cron_upload' ) ) { 
  223. wp_clear_scheduled_hook( 'w3_cdn_cron_upload' ); 
  224.  
  225.  
  226.  
  227. /** 
  228. * rules core modification 
  229. */ 
  230.  
  231. /** 
  232. * Writes directives to WP .htaccess 
  233. * @param Config $config 
  234. * @param Util_Environment_Exceptions $exs 
  235. * @throws Util_WpFile_FilesystemOperationException with S/FTP form if it can't get the required filesystem credentials 
  236. */ 
  237. private function rules_add( $config, $exs ) { 
  238. Util_Rule::add_rules( $exs, Util_Rule::get_browsercache_rules_cache_path(),  
  239. $this->rules_generate( $config ),  
  240. W3TC_MARKER_BEGIN_CDN,  
  241. W3TC_MARKER_END_CDN,  
  242. array( 
  243. W3TC_MARKER_BEGIN_MINIFY_CORE => 0,  
  244. W3TC_MARKER_BEGIN_PGCACHE_CORE => 0,  
  245. W3TC_MARKER_BEGIN_BROWSERCACHE_NO404WP => 0,  
  246. W3TC_MARKER_BEGIN_BROWSERCACHE_CACHE => 0,  
  247. W3TC_MARKER_BEGIN_WORDPRESS => 0,  
  248. W3TC_MARKER_END_PGCACHE_CACHE => strlen( W3TC_MARKER_END_PGCACHE_CACHE ) + 1,  
  249. W3TC_MARKER_END_MINIFY_CACHE => strlen( W3TC_MARKER_END_MINIFY_CACHE ) + 1 
  250. ); 
  251.  
  252. /** 
  253. * Removes Page Cache core directives 
  254. * @param Util_Environment_Exceptions $exs 
  255. * @throws Util_WpFile_FilesystemOperationException with S/FTP form if it can't get the required filesystem credentials 
  256. */ 
  257. private function rules_remove( $exs ) { 
  258. Util_Rule::remove_rules( $exs,  
  259. Util_Rule::get_browsercache_rules_cache_path(),  
  260. W3TC_MARKER_BEGIN_CDN,  
  261. W3TC_MARKER_END_CDN ); 
  262.  
  263. /** 
  264. * Generates rules for WP dir 
  265. * @param Config $config 
  266. * @param bool $cdnftp 
  267. * @return string 
  268. */ 
  269. private function rules_generate( $config, $cdnftp = false ) { 
  270. $rules = ''; 
  271. if ( Dispatcher::canonical_generated_by( $config, $cdnftp ) == 'cdn' ) 
  272. $rules .= Util_RuleSnippet::canonical( $config, $cdnftp ); 
  273. if ( Dispatcher::allow_origin_generated_by( $config ) == 'cdn' ) 
  274. $rules .= Util_RuleSnippet::allow_origin( $config, $cdnftp ); 
  275.  
  276. if ( strlen( $rules ) > 0 ) 
  277. $rules = 
  278. W3TC_MARKER_BEGIN_CDN . "\n" . 
  279. $rules . 
  280. W3TC_MARKER_END_CDN . "\n"; 
  281.  
  282. return $rules;