PageRenderTime 55ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/broken-link-checker/core/init.php

https://bitbucket.org/lgorence/quickpress
PHP | 326 lines | 166 code | 61 blank | 99 comment | 19 complexity | 84fecee70c1d02529593bfe8f3230bb9 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. //To prevent conflicts, only one version of the plugin can be activated at any given time.
  3. if ( defined('BLC_ACTIVE') ){
  4. trigger_error(
  5. 'Another version of Broken Link Checker is already active. Please deactivate it before activating this one.',
  6. E_USER_ERROR
  7. );
  8. } else {
  9. define('BLC_ACTIVE', true);
  10. //Fail fast if the WP version is unsupported. The $wp_version variable may be obfuscated by other
  11. //plugins, so use function detection to determine the version. get_post_stati was introduced in WP 3.0.0
  12. if ( !function_exists('get_post_stati') ){
  13. trigger_error(
  14. 'This version of Broken Link Checker requires WordPress 3.0 or later!',
  15. E_USER_ERROR
  16. );
  17. }
  18. /***********************************************
  19. Debugging stuff
  20. ************************************************/
  21. //define('BLC_DEBUG', true);
  22. /***********************************************
  23. Constants
  24. ************************************************/
  25. /*
  26. For performance, some internal APIs used for retrieving multiple links, instances or containers
  27. can take an optional "$purpose" argument. Those APIs will try to use this argument to pre-load
  28. any DB data required for the specified purpose ahead of time.
  29. For example, if you're loading a bunch of link containers for the purposes of parsing them and
  30. thus set $purpose to BLC_FOR_PARSING, the relevant container managers will (if applicable) precache
  31. the parse-able fields in each returned container object. Still, setting $purpose to any particular
  32. value does not *guarantee* any data will be preloaded - it's only a suggestion that it should.
  33. The currently supported values for the $purpose argument are :
  34. */
  35. define('BLC_FOR_EDITING', 'edit');
  36. define('BLC_FOR_PARSING', 'parse');
  37. define('BLC_FOR_DISPLAY', 'display');
  38. define('BLC_DATABASE_VERSION', 6);
  39. /***********************************************
  40. Configuration
  41. ************************************************/
  42. //Load and initialize the plugin's configuration
  43. require BLC_DIRECTORY . '/includes/config-manager.php';
  44. global $blc_config_manager;
  45. $blc_config_manager = new blcConfigurationManager(
  46. //Save the plugin's configuration into this DB option
  47. 'wsblc_options',
  48. //Initialize default settings
  49. array(
  50. 'max_execution_time' => 5*60, //(in seconds) How long the worker instance may run, at most.
  51. 'check_threshold' => 72, //(in hours) Check each link every 72 hours.
  52. 'recheck_count' => 3, //How many times a broken link should be re-checked.
  53. 'recheck_threshold' => 30*60, //(in seconds) Re-check broken links after 30 minutes.
  54. 'run_in_dashboard' => true, //Run the link checker algo. continuously while the Dashboard is open.
  55. 'run_via_cron' => true, //Run it hourly via WordPress pseudo-cron.
  56. 'mark_broken_links' => true, //Whether to add the broken_link class to broken links in posts.
  57. 'broken_link_css' => ".broken_link, a.broken_link {\n\ttext-decoration: line-through;\n}",
  58. 'nofollow_broken_links' => false, //Whether to add rel="nofollow" to broken links in posts.
  59. 'mark_removed_links' => false, //Whether to add the removed_link class when un-linking a link.
  60. 'removed_link_css' => ".removed_link, a.removed_link {\n\ttext-decoration: line-through;\n}",
  61. 'exclusion_list' => array(), //Links that contain a substring listed in this array won't be checked.
  62. 'send_email_notifications' => true, //Whether to send the admin email notifications about broken links
  63. 'send_authors_email_notifications' => false, //Whether to send post authors notifications about broken links in their posts.
  64. 'notification_schedule' => 'daily', //How often (at most) notifications will be sent. Possible values : 'daily', 'weekly'
  65. 'last_notification_sent' => 0, //When the last email notification was sent (Unix timestamp)
  66. 'server_load_limit' => 4, //Stop parsing stuff & checking links if the 1-minute load average
  67. //goes over this value. Only works on Linux servers. 0 = no limit.
  68. 'enable_load_limit' => true, //Enable/disable load monitoring.
  69. 'custom_fields' => array(), //List of custom fields that can contain URLs and should be checked.
  70. 'enabled_post_statuses' => array('publish'), //Only check posts that match one of these statuses
  71. 'autoexpand_widget' => true, //Autoexpand the Dashboard widget if broken links are detected
  72. 'show_link_count_bubble' => true, //Display a notification bubble in the menu when broken links are found
  73. 'table_layout' => 'flexible', //The layout of the link table. Possible values : 'classic', 'flexible'
  74. 'table_compact' => true, //Compact table mode on/off
  75. 'table_visible_columns' => array('new-url', 'status', 'used-in', 'new-link-text',),
  76. 'table_links_per_page' => 30,
  77. 'table_color_code_status' => true, //Color-code link status text
  78. 'need_resynch' => false, //[Internal flag] True if there are unparsed items.
  79. 'current_db_version' => 0, //The currently set-up version of the plugin's tables
  80. 'timeout' => 30, //(in seconds) Links that take longer than this to respond will be treated as broken.
  81. 'highlight_permanent_failures' => false,//Highlight links that have appear to be permanently broken (in Tools -> Broken Links).
  82. 'failure_duration_threshold' => 3, //(days) Assume a link is permanently broken if it still hasn't
  83. //recovered after this many days.
  84. 'installation_complete' => false,
  85. 'user_has_donated' => false, //Whether the user has donated to the plugin.
  86. 'donation_flag_fixed' => false,
  87. )
  88. );
  89. /***********************************************
  90. Logging
  91. ************************************************/
  92. include BLC_DIRECTORY . '/includes/logger.php';
  93. global $blclog;
  94. $blclog = new blcDummyLogger;
  95. /*
  96. if ( defined('BLC_DEBUG') && constant('BLC_DEBUG') ){
  97. //Load FirePHP for debug logging
  98. if ( !class_exists('FB') && file_exists(BLC_DIRECTORY . '/FirePHPCore/fb.php4') ) {
  99. require_once BLC_DIRECTORY . '/FirePHPCore/fb.php4';
  100. }
  101. //FB::setEnabled(false);
  102. }
  103. //to comment out all calls : (^[^\/]*)(FB::) -> $1\/\/$2
  104. //to uncomment : \/\/(\s*FB::) -> $1
  105. //*/
  106. /***********************************************
  107. Global functions
  108. ************************************************/
  109. /**
  110. * Get the configuration object used by Broken Link Checker.
  111. *
  112. * @return blcConfigurationManager
  113. */
  114. function blc_get_configuration(){
  115. return $GLOBALS['blc_config_manager'];
  116. }
  117. /**
  118. * Notify the link checker that there are unsynched items
  119. * that might contain links (e.g. a new or edited post).
  120. *
  121. * @return void
  122. */
  123. function blc_got_unsynched_items(){
  124. $conf = blc_get_configuration();
  125. if ( !$conf->options['need_resynch'] ){
  126. $conf->options['need_resynch'] = true;
  127. $conf->save_options();
  128. }
  129. }
  130. /**
  131. * (Re)create synchronization records for all containers and mark them all as unparsed.
  132. *
  133. * @param bool $forced If true, the plugin will recreate all synch. records from scratch.
  134. * @return void
  135. */
  136. function blc_resynch( $forced = false ){
  137. global $wpdb, $blclog; /* @var wpdb $wpdb */
  138. if ( $forced ){
  139. $blclog->info('... Forced resynchronization initiated');
  140. //Drop all synchronization records
  141. $wpdb->query("TRUNCATE {$wpdb->prefix}blc_synch");
  142. } else {
  143. $blclog->info('... Resynchronization initiated');
  144. }
  145. //Remove invalid DB entries
  146. blc_cleanup_database();
  147. //(Re)create and update synch. records for all container types.
  148. $blclog->info('... (Re)creating container records');
  149. blcContainerHelper::resynch($forced);
  150. $blclog->info('... Setting resync. flags');
  151. blc_got_unsynched_items();
  152. //All done.
  153. $blclog->info('Database resynchronization complete.');
  154. }
  155. /**
  156. * Delete synch. records, instances and links that refer to missing or invalid items.
  157. *
  158. * @return void
  159. */
  160. function blc_cleanup_database(){
  161. global $blclog;
  162. //Delete synch. records for container types that don't exist
  163. $blclog->info('... Deleting invalid container records');
  164. blcContainerHelper::cleanup_containers();
  165. //Delete invalid instances
  166. $blclog->info('... Deleting invalid link instances');
  167. blc_cleanup_instances();
  168. //Delete orphaned links
  169. $blclog->info('... Deleting orphaned links');
  170. blc_cleanup_links();
  171. }
  172. /***********************************************
  173. Utility hooks
  174. ************************************************/
  175. /**
  176. * Add a weekly Cron schedule for email notifications
  177. * and a bimonthly schedule for database maintenance.
  178. *
  179. * @param array $schedules Existing Cron schedules.
  180. * @return array
  181. */
  182. function blc_cron_schedules($schedules){
  183. if ( !isset($schedules['weekly']) ){
  184. $schedules['weekly'] = array(
  185. 'interval' => 604800, //7 days
  186. 'display' => __('Once Weekly')
  187. );
  188. }
  189. if ( !isset($schedules['bimonthly']) ){
  190. $schedules['bimonthly'] = array(
  191. 'interval' => 15*24*2600, //15 days
  192. 'display' => __('Twice a Month')
  193. );
  194. }
  195. return $schedules;
  196. }
  197. add_filter('cron_schedules', 'blc_cron_schedules');
  198. /***********************************************
  199. Main functionality
  200. ************************************************/
  201. //Execute the installation/upgrade script when the plugin is activated.
  202. function blc_activation_hook(){
  203. require BLC_DIRECTORY . '/includes/activation.php';
  204. }
  205. register_activation_hook(BLC_PLUGIN_FILE, 'blc_activation_hook');
  206. //Load the plugin if installed successfully
  207. if ( $blc_config_manager->options['installation_complete'] ){
  208. function blc_init(){
  209. global $blc_module_manager, $blc_config_manager, $ws_link_checker;
  210. static $init_done = false;
  211. if ( $init_done ){
  212. return;
  213. }
  214. $init_done = true;
  215. //Ensure the database is up to date
  216. if ($blc_config_manager->options['current_db_version'] != BLC_DATABASE_VERSION) {
  217. require_once BLC_DIRECTORY . '/includes/admin/db-upgrade.php';
  218. blcDatabaseUpgrader::upgrade_database(); //Also updates the DB ver. in options['current_db_version'].
  219. }
  220. //Load the base classes and utilities
  221. require_once BLC_DIRECTORY . '/includes/links.php';
  222. require_once BLC_DIRECTORY . '/includes/link-query.php';
  223. require_once BLC_DIRECTORY . '/includes/instances.php';
  224. require_once BLC_DIRECTORY . '/includes/utility-class.php';
  225. //Load the module subsystem
  226. require_once BLC_DIRECTORY . '/includes/modules.php';
  227. //Load the modules that want to be executed in all contexts
  228. $blc_module_manager->load_modules();
  229. if ( is_admin() || defined('DOING_CRON') ){
  230. //It's an admin-side or Cron request. Load the core.
  231. require_once BLC_DIRECTORY . '/core/core.php';
  232. $ws_link_checker = new wsBrokenLinkChecker( BLC_PLUGIN_FILE, $blc_config_manager );
  233. } else {
  234. //This is user-side request, so we don't need to load the core.
  235. //We might need to inject the CSS for removed links, though.
  236. if ( $blc_config_manager->options['mark_removed_links'] && !empty($blc_config_manager->options['removed_link_css']) ){
  237. function blc_print_removed_link_css(){
  238. global $blc_config_manager;
  239. echo '<style type="text/css">',$blc_config_manager->options['removed_link_css'],'</style>';
  240. }
  241. add_action('wp_head', 'blc_print_removed_link_css');
  242. }
  243. }
  244. }
  245. add_action('init', 'blc_init', 2000);
  246. } else {
  247. //Display installation errors (if any) on the Dashboard.
  248. function blc_print_installation_errors(){
  249. global $blc_config_manager;
  250. if ( $blc_config_manager->options['installation_complete'] ) {
  251. return;
  252. }
  253. $logger = new blcCachedOptionLogger('blc_installation_log');
  254. $messages = array_merge(
  255. array('<strong>' . __('Broken Link Checker installation failed. Try deactivating and then reactivating the plugin.', 'broken-link-checker') . '</strong>', '', '<em>Installation log follows :</em>'),
  256. $logger->get_messages()
  257. );
  258. echo "<div class='error'><p>", implode("<br>\n", $messages), "</p></div>";
  259. }
  260. add_action('admin_notices', 'blc_print_installation_errors');
  261. }
  262. }
  263. ?>