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

/wp-content/plugins/stops-core-theme-and-plugin-updates/includes/MPSUM_Logs.php

https://gitlab.com/memuller.web/wp_site
PHP | 436 lines | 328 code | 20 blank | 88 comment | 34 complexity | 25758bdb4d939028b63c67f806a7252e MD5 | raw file
  1. <?php
  2. /**
  3. * Easy Updates Manager log controller
  4. *
  5. * Initializes the log table and sets up actions/events
  6. *
  7. * @since 6.0.0
  8. *
  9. * @package WordPress
  10. */
  11. class MPSUM_Logs {
  12. /**
  13. * Holds the class instance.
  14. *
  15. * @since 6.0.0
  16. * @access static
  17. * @var MPSUM_Log $instance
  18. */
  19. private static $instance = null;
  20. /**
  21. * Holds version number of the table
  22. *
  23. * @since 6.0.0
  24. * @access static
  25. * @var string $slug
  26. */
  27. private $version = '1.0.0';
  28. /**
  29. * Set a class instance.
  30. *
  31. * Set a class instance.
  32. *
  33. * @since 5.0.0
  34. * @access static
  35. *
  36. */
  37. public static function run() {
  38. if ( null == self::$instance ) {
  39. self::$instance = new self;
  40. }
  41. } //end get_instance
  42. /**
  43. * Class constructor.
  44. *
  45. * Initialize the class
  46. *
  47. * @since 6.0.0
  48. * @access private
  49. *
  50. */
  51. private function __construct() {
  52. $table_version = get_site_option( 'mpsum_log_table_version', '0' );
  53. if ( version_compare( $table_version, $this->version ) < 0 ) {
  54. $this->build_table();
  55. update_site_option( 'mpsum_log_table_version', $this->version );
  56. }
  57. add_action( 'automatic_updates_complete', array( $this, 'automatic_updates' ) );
  58. add_action( 'upgrader_process_complete', array( $this, 'manual_updates' ), 5, 2 );
  59. } //end constructor
  60. /**
  61. * automatic_updates
  62. *
  63. * Log automatic updates
  64. *
  65. * @since 6.0.0
  66. * @access public
  67. *
  68. */
  69. public function automatic_updates( $update_results ) {
  70. global $wpdb;
  71. $tablename = $wpdb->base_prefix . 'eum_logs';
  72. if ( empty( $update_results ) ) return;
  73. foreach( $update_results as $type => $results ) {
  74. switch( $type ) {
  75. case 'core':
  76. $core = $results[ 0 ];
  77. $status = is_wp_error( $core->result ) ? 0: 1;
  78. $version = ( 1 == $status ) ? $core->result : '';
  79. $wpdb->insert(
  80. $tablename,
  81. array(
  82. 'name' => $core->name,
  83. 'type' => $type,
  84. 'version' => $version,
  85. 'action' => 'automatic',
  86. 'status' => $status,
  87. 'date' => current_time( 'mysql' ),
  88. ),
  89. array(
  90. '%s',
  91. '%s',
  92. '%s',
  93. '%s',
  94. '%s',
  95. '%s',
  96. )
  97. );
  98. break;
  99. case 'plugin':
  100. foreach( $results as $plugin ) {
  101. $status = is_wp_error( $plugin->result ) ? 0: 1;
  102. $version = isset( $plugin->item->new_version ) ? $plugin->item->new_version : '0.00';
  103. $name = ( isset( $plugin->name ) && !empty( $plugin->name ) ) ? $plugin->name : $plugin->item->slug;
  104. $wpdb->insert(
  105. $tablename,
  106. array(
  107. 'name' => $name,
  108. 'type' => $type,
  109. 'version' => $version,
  110. 'action' => 'automatic',
  111. 'status' => $status,
  112. 'date' => current_time( 'mysql' ),
  113. ),
  114. array(
  115. '%s',
  116. '%s',
  117. '%s',
  118. '%s',
  119. '%s',
  120. '%s',
  121. )
  122. );
  123. }
  124. break;
  125. case 'theme':
  126. foreach( $results as $theme ) {
  127. $status = ( is_wp_error( $theme->result ) || empty( $theme->result ) ) ? 0: 1;
  128. if ( 0 == $status ) {
  129. $theme_data_from_cache = wp_get_themes();
  130. $theme_data = $theme_data_from_cache[ $theme->item->theme ];
  131. $version = $theme_data->get( 'Version' );
  132. } else {
  133. $version = $theme->item->new_version;
  134. }
  135. $wpdb->insert(
  136. $tablename,
  137. array(
  138. 'name' => $theme->name,
  139. 'type' => $type,
  140. 'version' => $version,
  141. 'action' => 'automatic',
  142. 'status' => $status,
  143. 'date' => current_time( 'mysql' ),
  144. ),
  145. array(
  146. '%s',
  147. '%s',
  148. '%s',
  149. '%s',
  150. '%s',
  151. '%s',
  152. )
  153. );
  154. }
  155. break;
  156. case 'translation':
  157. foreach( $results as $translation ) {
  158. $status = is_wp_error( $translation->result ) ? 0: 1;
  159. $version = ( 1 == $status ) ? $translation->item->version : '';
  160. $slug = $translation->item->slug;
  161. $name = $this->get_name_for_update( $translation->item->type, $translation->item->slug );
  162. $wpdb->insert(
  163. $tablename,
  164. array(
  165. 'name' => $name . ' (' . $translation->item->language . ')',
  166. 'type' => $type,
  167. 'version' => $version,
  168. 'action' => 'automatic',
  169. 'status' => $status,
  170. 'date' => current_time( 'mysql' ),
  171. ),
  172. array(
  173. '%s',
  174. '%s',
  175. '%s',
  176. '%s',
  177. '%s',
  178. '%s',
  179. )
  180. );
  181. }
  182. break;
  183. }
  184. }
  185. }
  186. /**
  187. * Get the name of an translation item being updated.
  188. *
  189. * @since 6.0.3
  190. * @access private
  191. *
  192. * @param string type of translation update
  193. * @param string $slug of item
  194. * @return string The name of the item being updated.
  195. */
  196. private function get_name_for_update( $type, $slug ) {
  197. if ( ! function_exists( 'get_plugins' ) ) {
  198. require_once ABSPATH . 'wp-admin/includes/plugin.php';
  199. }
  200. switch ( $type ) {
  201. case 'core':
  202. return 'WordPress'; // Not translated
  203. case 'theme':
  204. $theme = wp_get_theme( $slug );
  205. if ( $theme->exists() )
  206. return $theme->Get( 'Name' );
  207. break;
  208. case 'plugin':
  209. $plugin_data = get_plugins( '/' . $slug );
  210. $plugin_data = reset( $plugin_data );
  211. if ( $plugin_data )
  212. return $plugin_data['Name'];
  213. break;
  214. }
  215. return '';
  216. }
  217. public function manual_updates( $upgrader_object, $options ) {
  218. if ( !isset( $options[ 'action' ] ) || 'update' !== $options[ 'action' ] ) return;
  219. global $wpdb;
  220. $tablename = $wpdb->base_prefix . 'eum_logs';
  221. $user_id = get_current_user_id();
  222. if ( 0 == $user_id ) return; // If there is no user, this is not a manual update
  223. switch( $options[ 'type' ] ) {
  224. case 'core':
  225. include( ABSPATH . WPINC . '/version.php' );
  226. $wpdb->insert(
  227. $tablename,
  228. array(
  229. 'user_id' => $user_id,
  230. 'name' => 'WordPress ' . $wp_version,
  231. 'type' => $options[ 'type' ],
  232. 'version' => $wp_version,
  233. 'action' => 'manual',
  234. 'status' => 1,
  235. 'date' => current_time( 'mysql' ),
  236. ),
  237. array(
  238. '%d',
  239. '%s',
  240. '%s',
  241. '%s',
  242. '%s',
  243. '%s',
  244. '%s',
  245. )
  246. );
  247. break;
  248. case 'plugin':
  249. $plugins = array();
  250. if ( ! function_exists( 'get_plugins' ) ) {
  251. require_once ABSPATH . 'wp-admin/includes/plugin.php';
  252. }
  253. $plugins_from_cache = get_site_transient( 'update_plugins' );
  254. wp_clean_plugins_cache();
  255. $plugins = get_plugins();
  256. if ( !empty( $plugins ) && isset( $options[ 'plugins' ] ) && !empty( $options[ 'plugins' ] ) ) {
  257. foreach( $options[ 'plugins' ] as $plugin ) {
  258. $plugin_data = isset( $plugins[ $plugin ] ) ? $plugins[ $plugin ] : false;
  259. $plugins_from_cache = isset( $plugins_from_cache->checked[ $plugin ] ) ? $plugins_from_cache->checked[ $plugin ] : false;
  260. if ( false !== $plugin_data && false !== $plugins_from_cache ) {
  261. $status = ( $plugin_data[ 'Version' ] == $plugins_from_cache ) ? 0 : 1;
  262. $wpdb->insert(
  263. $tablename,
  264. array(
  265. 'user_id' => $user_id,
  266. 'name' => $plugin_data[ 'Name' ],
  267. 'type' => $options[ 'type' ],
  268. 'version' => $plugin_data[ 'Version' ],
  269. 'action' => 'manual',
  270. 'status' => $status,
  271. 'date' => current_time( 'mysql' ),
  272. ),
  273. array(
  274. '%d',
  275. '%s',
  276. '%s',
  277. '%s',
  278. '%s',
  279. '%s',
  280. '%s',
  281. )
  282. );
  283. }
  284. }
  285. }
  286. break;
  287. case 'theme':
  288. if ( isset( $options[ 'themes' ] ) && !empty( $options[ 'themes' ] ) ) {
  289. $theme_data_from_cache = get_site_transient( 'update_themes' );
  290. wp_clean_themes_cache();
  291. foreach( $options[ 'themes' ] as $theme ) {
  292. $theme_data = wp_get_theme( $theme );
  293. $theme_from_cache_version = isset( $theme_data_from_cache->checked[ $theme ] ) ? $theme_data_from_cache->checked[ $theme ] : false;
  294. error_log( $theme_from_cache_version );
  295. error_log( $theme_data->get( 'Version' ) );
  296. if ( $theme_data->exists() && false !== $theme_from_cache_version ) {
  297. $status = ( $theme_from_cache_version == $theme_data->get( 'Version' ) ) ? 0 : 1;
  298. $wpdb->insert(
  299. $tablename,
  300. array(
  301. 'user_id' => $user_id,
  302. 'name' => $theme_data->get( 'Name' ),
  303. 'type' => $options[ 'type' ],
  304. 'version' => $theme_data->get( 'Version' ),
  305. 'action' => 'manual',
  306. 'status' => $status,
  307. 'date' => current_time( 'mysql' ),
  308. ),
  309. array(
  310. '%d',
  311. '%s',
  312. '%s',
  313. '%s',
  314. '%s',
  315. '%s',
  316. '%s',
  317. )
  318. );
  319. }
  320. }
  321. }
  322. break;
  323. case 'translation':
  324. foreach( $options[ 'translations' ] as $translation ) {
  325. $status = 1;
  326. $version = $translation[ 'version' ];
  327. $slug = $translation[ 'slug' ];
  328. $name = $this->get_name_for_update( $translation[ 'type' ], $slug );
  329. $wpdb->insert(
  330. $tablename,
  331. array(
  332. 'user_id' => $user_id,
  333. 'name' => $name . ' (' . $translation[ 'language' ] . ')',
  334. 'type' => $translation[ 'type' ],
  335. 'version' => $version,
  336. 'action' => 'manual',
  337. 'status' => $status,
  338. 'date' => current_time( 'mysql' ),
  339. ),
  340. array(
  341. '%d',
  342. '%s',
  343. '%s',
  344. '%s',
  345. '%s',
  346. '%s',
  347. '%s',
  348. )
  349. );
  350. }
  351. break;
  352. }
  353. }
  354. /**
  355. * Creates the log table
  356. *
  357. * Creates the log table
  358. *
  359. * @since 6.0.0
  360. * @access private
  361. *
  362. */
  363. private function build_table() {
  364. global $wpdb;
  365. $tablename = $wpdb->base_prefix . 'eum_logs';
  366. // Get collation - From /wp-admin/includes/schema.php
  367. $charset_collate = '';
  368. if ( ! empty($wpdb->charset) )
  369. $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
  370. if ( ! empty($wpdb->collate) )
  371. $charset_collate .= " COLLATE $wpdb->collate";
  372. $sql = "CREATE TABLE {$tablename} (
  373. log_id BIGINT(20) NOT NULL AUTO_INCREMENT,
  374. user_id BIGINT(20) NOT NULL DEFAULT 0,
  375. name VARCHAR(255) NOT NULL,
  376. type VARCHAR(255) NOT NULL,
  377. version VARCHAR(255) NOT NULL,
  378. action VARCHAR(255) NOT NULL,
  379. status VARCHAR(255) NOT NULL,
  380. date DATETIME NOT NULL,
  381. PRIMARY KEY (log_id)
  382. ) {$charset_collate};";
  383. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  384. dbDelta($sql);
  385. }
  386. /**
  387. * Clears the log table
  388. *
  389. * Clears the log table
  390. *
  391. * @since 6.0.0
  392. * @access static
  393. *
  394. */
  395. public static function clear() {
  396. global $wpdb;
  397. $tablename = $wpdb->base_prefix . 'eum_logs';
  398. $sql = "delete from $tablename";
  399. $wpdb->query( $sql );
  400. }
  401. /**
  402. * Drops the log table
  403. *
  404. * Drops the log table
  405. *
  406. * @since 6.0.0
  407. * @access static
  408. *
  409. */
  410. public static function drop() {
  411. global $wpdb;
  412. $tablename = $wpdb->base_prefix . 'eum_logs';
  413. $sql = "drop table if exists $tablename";
  414. $wpdb->query( $sql );
  415. }
  416. }