PageRenderTime 35ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/jetpack/class.jetpack-autoupdate.php

https://gitlab.com/chernushov881/charity-fund
PHP | 369 lines | 213 code | 46 blank | 110 comment | 39 complexity | 33eecf781c95a9b54a0092a6c990b2b6 MD5 | raw file
  1. <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
  2. /**
  3. * Handles items that have been selected for automatic updates.
  4. * Hooks into WP_Automatic_Updater
  5. *
  6. * @package automattic/jetpack
  7. */
  8. /**
  9. * Handles items that have been selected for automatic updates.
  10. * Hooks into WP_Automatic_Updater
  11. */
  12. class Jetpack_Autoupdate {
  13. /**
  14. * Results.
  15. *
  16. * @var array
  17. */
  18. private $results = array();
  19. /**
  20. * Expected updates.
  21. *
  22. * @var array
  23. */
  24. private $expected = array();
  25. /**
  26. * Successful updates.
  27. *
  28. * @var array
  29. */
  30. private $success = array(
  31. 'plugin' => array(),
  32. 'theme' => array(),
  33. );
  34. /**
  35. * Failed updates.
  36. *
  37. * @var array
  38. */
  39. private $failed = array(
  40. 'plugin' => array(),
  41. 'theme' => array(),
  42. );
  43. /**
  44. * Static instance.
  45. *
  46. * @var self
  47. */
  48. private static $instance = null;
  49. /**
  50. * Initialize and fetch the static instance.
  51. *
  52. * @return self
  53. */
  54. public static function init() {
  55. if ( is_null( self::$instance ) ) {
  56. self::$instance = new Jetpack_Autoupdate();
  57. }
  58. return self::$instance;
  59. }
  60. /** Constructor. */
  61. private function __construct() {
  62. if (
  63. /** This filter is documented in class.jetpack-json-api-endpoint.php */
  64. apply_filters( 'jetpack_json_manage_api_enabled', true )
  65. ) {
  66. add_filter( 'auto_update_theme', array( $this, 'autoupdate_theme' ), 10, 2 );
  67. add_filter( 'auto_update_core', array( $this, 'autoupdate_core' ), 10, 2 );
  68. add_filter( 'auto_update_translation', array( $this, 'autoupdate_translation' ), 10, 2 );
  69. add_action( 'automatic_updates_complete', array( $this, 'automatic_updates_complete' ), 999, 1 );
  70. }
  71. }
  72. /**
  73. * Filter function for `auto_update_translation`.
  74. *
  75. * @param bool|null $update Whether to update.
  76. * @param object $item The update offer.
  77. * @return bool|null Whether to update.
  78. */
  79. public function autoupdate_translation( $update, $item ) {
  80. // Autoupdate all translations.
  81. if ( Jetpack_Options::get_option( 'autoupdate_translations', false ) ) {
  82. return true;
  83. }
  84. // Themes.
  85. $autoupdate_themes_translations = Jetpack_Options::get_option( 'autoupdate_themes_translations', array() );
  86. $autoupdate_theme_list = Jetpack_Options::get_option( 'autoupdate_themes', array() );
  87. if ( ( in_array( $item->slug, $autoupdate_themes_translations, true ) || in_array( $item->slug, $autoupdate_theme_list, true ) )
  88. && 'theme' === $item->type
  89. ) {
  90. $this->expect( $item->type . ':' . $item->slug, 'translation' );
  91. return true;
  92. }
  93. // Plugins.
  94. $autoupdate_plugin_translations = Jetpack_Options::get_option( 'autoupdate_plugins_translations', array() );
  95. $autoupdate_plugin_list = (array) get_site_option( 'auto_update_plugins', array() );
  96. $plugin_files = array_unique( array_merge( $autoupdate_plugin_list, $autoupdate_plugin_translations ) );
  97. $plugin_slugs = array_map( array( __CLASS__, 'get_plugin_slug' ), $plugin_files );
  98. if ( in_array( $item->slug, $plugin_slugs, true )
  99. && 'plugin' === $item->type
  100. ) {
  101. $this->expect( $item->type . ':' . $item->slug, 'translation' );
  102. return true;
  103. }
  104. return $update;
  105. }
  106. /**
  107. * Filter function for `auto_update_theme`.
  108. *
  109. * @param bool|null $update Whether to update.
  110. * @param object $item The update offer.
  111. * @return bool|null Whether to update.
  112. */
  113. public function autoupdate_theme( $update, $item ) {
  114. $autoupdate_theme_list = Jetpack_Options::get_option( 'autoupdate_themes', array() );
  115. if ( in_array( $item->theme, $autoupdate_theme_list, true ) ) {
  116. $this->expect( $item->theme, 'theme' );
  117. return true;
  118. }
  119. return $update;
  120. }
  121. /**
  122. * Filter function for `auto_update_core`.
  123. *
  124. * @param bool|null $update Whether to update.
  125. * @return bool|null Whether to update.
  126. */
  127. public function autoupdate_core( $update ) {
  128. $autoupdate_core = Jetpack_Options::get_option( 'autoupdate_core', false );
  129. if ( $autoupdate_core ) {
  130. return $autoupdate_core;
  131. }
  132. return $update;
  133. }
  134. /**
  135. * Stores the an item identifier to the expected array.
  136. *
  137. * @param string $item Example: 'jetpack/jetpack.php' for type 'plugin' or 'twentyfifteen' for type 'theme'.
  138. * @param string $type 'plugin' or 'theme'.
  139. */
  140. private function expect( $item, $type ) {
  141. if ( ! isset( $this->expected[ $type ] ) ) {
  142. $this->expected[ $type ] = array();
  143. }
  144. $this->expected[ $type ][] = $item;
  145. }
  146. /**
  147. * On completion of an automatic update, let's store the results.
  148. *
  149. * @param mixed $results - Sent by WP_Automatic_Updater after it completes an autoupdate action. Results may be empty.
  150. */
  151. public function automatic_updates_complete( $results ) {
  152. if ( empty( $this->expected ) ) {
  153. return;
  154. }
  155. $this->results = empty( $results ) ? self::get_possible_failures() : $results;
  156. add_action( 'shutdown', array( $this, 'bump_stats' ) );
  157. Jetpack::init();
  158. $items_to_log = array( 'plugin', 'theme', 'translation' );
  159. foreach ( $items_to_log as $items ) {
  160. $this->log_items( $items );
  161. }
  162. Jetpack::log( 'autoupdates', $this->get_log() );
  163. }
  164. /**
  165. * Get log data.
  166. *
  167. * @return array Data.
  168. */
  169. public function get_log() {
  170. return array(
  171. 'results' => $this->results,
  172. 'failed' => $this->failed,
  173. 'success' => $this->success,
  174. );
  175. }
  176. /**
  177. * Iterates through expected items ( plugins or themes ) and compares them to actual results.
  178. *
  179. * @param string $items 'plugin' or 'theme'.
  180. */
  181. private function log_items( $items ) {
  182. if ( ! isset( $this->expected[ $items ] ) ) {
  183. return;
  184. }
  185. $item_results = $this->get_successful_updates( $items );
  186. if ( is_array( $this->expected[ $items ] ) ) {
  187. foreach ( $this->expected[ $items ] as $item ) {
  188. if ( in_array( $item, $item_results, true ) ) {
  189. $this->success[ $items ][] = $item;
  190. } else {
  191. $this->failed[ $items ][] = $item;
  192. }
  193. }
  194. }
  195. }
  196. /**
  197. * Bump stats.
  198. */
  199. public function bump_stats() {
  200. $instance = Jetpack::init();
  201. $log = array();
  202. // Bump numbers.
  203. if ( ! empty( $this->success['theme'] ) ) {
  204. $instance->stat( 'autoupdates/theme-success', count( $this->success['theme'] ) );
  205. $log['themes_success'] = $this->success['theme'];
  206. }
  207. if ( ! empty( $this->failed['theme'] ) ) {
  208. $instance->stat( 'autoupdates/theme-fail', count( $this->failed['theme'] ) );
  209. $log['themes_failed'] = $this->failed['theme'];
  210. }
  211. $instance->do_stats( 'server_side' );
  212. // Send a more detailed log to logstash.
  213. if ( ! empty( $log ) ) {
  214. $xml = new Jetpack_IXR_Client(
  215. array(
  216. 'user_id' => get_current_user_id(),
  217. )
  218. );
  219. $log['blog_id'] = Jetpack_Options::get_option( 'id' );
  220. $xml->query( 'jetpack.debug_autoupdate', $log );
  221. }
  222. }
  223. /**
  224. * Parses the autoupdate results generated by WP_Automatic_Updater and returns a simple array of successful items.
  225. *
  226. * @param string $type 'plugin' or 'theme'.
  227. * @return array
  228. */
  229. private function get_successful_updates( $type ) {
  230. $successful_updates = array();
  231. if ( ! isset( $this->results[ $type ] ) ) {
  232. return $successful_updates;
  233. }
  234. foreach ( $this->results[ $type ] as $result ) {
  235. if ( $result->result ) {
  236. switch ( $type ) {
  237. case 'theme':
  238. $successful_updates[] = $result->item->theme;
  239. break;
  240. case 'translation':
  241. $successful_updates[] = $result->item->type . ':' . $result->item->slug;
  242. break;
  243. }
  244. }
  245. }
  246. return $successful_updates;
  247. }
  248. /**
  249. * Get possible failure codes.
  250. *
  251. * @return string[] Failure codes.
  252. */
  253. public static function get_possible_failures() {
  254. $result = array();
  255. // Lets check some reasons why it might not be working as expected.
  256. include_once ABSPATH . '/wp-admin/includes/admin.php';
  257. include_once ABSPATH . '/wp-admin/includes/class-wp-upgrader.php';
  258. $upgrader = new WP_Automatic_Updater();
  259. if ( $upgrader->is_disabled() ) {
  260. $result[] = 'autoupdates-disabled';
  261. }
  262. if ( ! is_main_site() ) {
  263. $result[] = 'is-not-main-site';
  264. }
  265. if ( ! is_main_network() ) {
  266. $result[] = 'is-not-main-network';
  267. }
  268. if ( $upgrader->is_vcs_checkout( ABSPATH ) ) {
  269. $result[] = 'site-on-vcs';
  270. }
  271. if ( $upgrader->is_vcs_checkout( WP_PLUGIN_DIR ) ) {
  272. $result[] = 'plugin-directory-on-vcs';
  273. }
  274. if ( $upgrader->is_vcs_checkout( WP_CONTENT_DIR ) ) {
  275. $result[] = 'content-directory-on-vcs';
  276. }
  277. $lock = get_option( 'auto_updater.lock' );
  278. if ( $lock > ( time() - HOUR_IN_SECONDS ) ) {
  279. $result[] = 'lock-is-set';
  280. }
  281. $skin = new Automatic_Upgrader_Skin();
  282. include_once ABSPATH . 'wp-admin/includes/file.php';
  283. include_once ABSPATH . 'wp-admin/includes/template.php';
  284. if ( ! $skin->request_filesystem_credentials( false, ABSPATH, false ) ) {
  285. $result[] = 'no-system-write-access';
  286. }
  287. if ( ! $skin->request_filesystem_credentials( false, WP_PLUGIN_DIR, false ) ) {
  288. $result[] = 'no-plugin-directory-write-access';
  289. }
  290. if ( ! $skin->request_filesystem_credentials( false, WP_CONTENT_DIR, false ) ) {
  291. $result[] = 'no-wp-content-directory-write-access';
  292. }
  293. return $result;
  294. }
  295. /**
  296. * Get the plugin slug.
  297. *
  298. * @param string $plugin_file Plugin file.
  299. * @return string Slug.
  300. */
  301. public static function get_plugin_slug( $plugin_file ) {
  302. $update_plugins = get_site_transient( 'update_plugins' );
  303. if ( isset( $update_plugins->no_update ) ) {
  304. if ( isset( $update_plugins->no_update[ $plugin_file ] ) ) {
  305. $slug = $update_plugins->no_update[ $plugin_file ]->slug;
  306. }
  307. }
  308. if ( empty( $slug ) && isset( $update_plugins->response ) ) {
  309. if ( isset( $update_plugins->response[ $plugin_file ] ) ) {
  310. $slug = $update_plugins->response[ $plugin_file ]->slug;
  311. }
  312. }
  313. // Try to infer from the plugin file if not cached.
  314. if ( empty( $slug ) ) {
  315. $slug = dirname( $plugin_file );
  316. if ( '.' === $slug ) {
  317. $slug = preg_replace( '/(.+)\.php$/', '$1', $plugin_file );
  318. }
  319. }
  320. return $slug;
  321. }
  322. }
  323. Jetpack_Autoupdate::init();