PluginProbe
TablePress – Tables in WordPress made easy / 3.0.3
TablePress – Tables in WordPress made easy v3.0.3
3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 2.4.4 All 44 releases
tablepress / classes / class-controller.php

class-controller.php in TablePress – Tables in WordPress made easy 3.0.3, at classes/class-controller.php

149 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * TablePress Base Controller with members and methods for all controllers
4 *
5 * @package TablePress
6 * @subpackage Controllers
7 * @author Tobias Bäthge
8 * @since 1.0.0
9 */
10
11 // Prohibit direct script loading.
12 defined( 'ABSPATH' ) || die( 'No direct script access allowed!' );
13
14 /**
15 * Base Controller class
16 *
17 * @package TablePress
18 * @subpackage Controllers
19 * @author Tobias Bäthge
20 * @since 1.0.0
21 */
22 abstract class TablePress_Controller {
23
24 /**
25 * Initializes all controllers.
26 *
27 * @since 1.0.0
28 */
29 public function __construct() {
30 // Update check, in all controllers (frontend and admin), to make sure we always have up-to-date options, should be done very early.
31 $this->plugin_update_check();
32 }
33
34 /**
35 * Check if the plugin was updated and perform necessary actions, like updating the options.
36 *
37 * @since 1.0.0
38 */
39 protected function plugin_update_check(): void {
40 // First activation or plugin update.
41 $current_plugin_options_db_version = TablePress::$model_options->get( 'plugin_options_db_version' );
42 if ( $current_plugin_options_db_version < TablePress::db_version ) {
43 // Allow more PHP execution time for update process.
44 if ( function_exists( 'set_time_limit' ) ) {
45 @set_time_limit( 300 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
46 }
47
48 // Add TablePress capabilities to the WP_Roles objects, for new installations and all versions below 12.
49 if ( $current_plugin_options_db_version < 12 ) {
50 TablePress::$model_options->add_access_capabilities();
51 }
52
53 if ( 0 === TablePress::$model_options->get( 'first_activation' ) ) {
54 // Save initial set of plugin options, and time of first activation of the plugin, on first activation.
55 TablePress::$model_options->update( array(
56 'first_activation' => time(),
57 'plugin_options_db_version' => TablePress::db_version,
58 ) );
59 } else {
60 // Update Plugin Options Options, if necessary.
61 TablePress::$model_options->merge_plugin_options_defaults();
62 $updated_options = array(
63 'plugin_options_db_version' => TablePress::db_version,
64 'prev_tablepress_version' => TablePress::$model_options->get( 'tablepress_version' ),
65 'tablepress_version' => TablePress::version,
66 'message_plugin_update' => true,
67 );
68
69 // If used, re-save "Custom CSS" to re-create all files (as TablePress Default CSS might have changed).
70 $custom_css = TablePress::$model_options->get( 'custom_css' );
71 if ( TablePress::$model_options->get( 'use_custom_css' ) && '' !== $custom_css ) {
72 /**
73 * Load WP file functions to provide filesystem access functions early.
74 */
75 require_once ABSPATH . 'wp-admin/includes/file.php'; // @phpstan-ignore requireOnce.fileNotFound (This is a WordPress core file that always exists.)
76 /**
77 * Load WP admin template functions to provide `submit_button()` which is necessary for `request_filesystem_credentials()`.
78 */
79 require_once ABSPATH . 'wp-admin/includes/template.php'; // @phpstan-ignore requireOnce.fileNotFound (This is a WordPress core file that always exists.)
80 $tablepress_css = TablePress::load_class( 'TablePress_CSS', 'class-css.php', 'classes' );
81
82 $custom_css_minified = TablePress::$model_options->get( 'custom_css_minified' );
83
84 // Update "Custom CSS" to be compatible with DataTables 2, introduced in TablePress 3.0.
85 if ( $current_plugin_options_db_version < 96 ) {
86 $old_custom_css = $custom_css;
87 $custom_css = TablePress::convert_datatables_api_data( $custom_css );
88 if ( $old_custom_css !== $custom_css ) {
89 $custom_css = $tablepress_css->sanitize_css( $custom_css );
90 $custom_css_minified = $tablepress_css->minify_css( $custom_css );
91 $updated_options['custom_css'] = $custom_css;
92 $updated_options['custom_css_minified'] = $custom_css_minified;
93 }
94 unset( $old_custom_css );
95 }
96
97 $result = $tablepress_css->save_custom_css_to_file( $custom_css, $custom_css_minified );
98 // If saving was successful, use "Custom CSS" file.
99 $updated_options['use_custom_css_file'] = $result;
100 // Increase the "Custom CSS" version number for cache busting.
101 if ( $result ) {
102 $updated_options['custom_css_version'] = TablePress::$model_options->get( 'custom_css_version' ) + 1;
103 }
104 }
105
106 TablePress::$model_options->update( $updated_options );
107
108 // Clear table caches.
109 TablePress::$model_table->invalidate_table_output_caches();
110
111 // Add mime type field to existing posts with the TablePress Custom Post Type, in TablePress 1.5.
112 if ( $current_plugin_options_db_version < 25 ) {
113 TablePress::$model_table->add_mime_type_to_posts();
114 }
115
116 // Add new access capabilities that were introduced in TablePress 2.3.2.
117 if ( $current_plugin_options_db_version < 77 ) {
118 TablePress::$model_options->add_access_capabilities_tp232();
119 }
120
121 // Update all tables' "Custom Commands" to be compatible with DataTables 2, introduced in TablePress 3.0.
122 if ( $current_plugin_options_db_version < 96 ) {
123 TablePress::$model_table->update_custom_commands_datatables_tp30();
124 }
125 }
126 }
127
128 // Maybe update the table scheme in each existing table, independently from updating the plugin options.
129 if ( TablePress::$model_options->get( 'table_scheme_db_version' ) < TablePress::table_scheme_version ) {
130 TablePress::$model_table->merge_table_options_defaults();
131 TablePress::$model_options->update( 'table_scheme_db_version', TablePress::table_scheme_version );
132 }
133
134 /*
135 * Update User Options, if necessary.
136 * User Options are not saved in DB until first change occurs.
137 */
138 if ( is_user_logged_in() && TablePress::$model_options->get( 'user_options_db_version' ) < TablePress::db_version ) {
139 TablePress::$model_options->merge_user_options_defaults();
140 $updated_options = array(
141 'user_options_db_version' => TablePress::db_version,
142 'message_superseded_extensions' => true,
143 );
144 TablePress::$model_options->update( $updated_options );
145 }
146 }
147
148 } // class TablePress_Controller
149