| 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 |
* File name of the admin screens' parent page in the admin menu. |
| 26 |
* |
| 27 |
* @since 1.0.0 |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
public $parent_page = 'middle'; |
| 31 |
|
| 32 |
/** |
| 33 |
* Whether TablePress admin screens are a top-level menu item in the admin menu. |
| 34 |
* |
| 35 |
* @since 1.0.0 |
| 36 |
* @var bool |
| 37 |
*/ |
| 38 |
public $is_top_level_page = false; |
| 39 |
|
| 40 |
/** |
| 41 |
* Initialize all controllers, by loading Plugin and User Options, and by performing an update check. |
| 42 |
* |
| 43 |
* @since 1.0.0 |
| 44 |
*/ |
| 45 |
public function __construct() { |
| 46 |
// Update check, in all controllers (frontend and admin), to make sure we always have up-to-date options, should be done very early. |
| 47 |
$this->plugin_update_check(); |
| 48 |
|
| 49 |
/** |
| 50 |
* Filters the admin menu parent page, which is needed for the construction of plugin URLs. |
| 51 |
* |
| 52 |
* @since 1.0.0 |
| 53 |
* |
| 54 |
* @param string $parent_page Current admin menu parent page. |
| 55 |
*/ |
| 56 |
$this->parent_page = apply_filters( 'tablepress_admin_menu_parent_page', TablePress::$model_options->get( 'admin_menu_parent_page' ) ); |
| 57 |
$this->is_top_level_page = in_array( $this->parent_page, array( 'top', 'middle', 'bottom' ), true ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Check if the plugin was updated and perform necessary actions, like updating the options. |
| 62 |
* |
| 63 |
* @since 1.0.0 |
| 64 |
*/ |
| 65 |
protected function plugin_update_check() { |
| 66 |
// First activation or plugin update. |
| 67 |
$current_plugin_options_db_version = TablePress::$model_options->get( 'plugin_options_db_version' ); |
| 68 |
if ( $current_plugin_options_db_version < TablePress::db_version ) { |
| 69 |
// Allow more PHP execution time for update process. |
| 70 |
if ( function_exists( 'set_time_limit' ) ) { |
| 71 |
@set_time_limit( 300 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 72 |
} |
| 73 |
|
| 74 |
// Add TablePress capabilities to the WP_Roles objects, for new installations and all versions below 12. |
| 75 |
if ( $current_plugin_options_db_version < 12 ) { |
| 76 |
TablePress::$model_options->add_access_capabilities(); |
| 77 |
} |
| 78 |
|
| 79 |
if ( 0 === TablePress::$model_options->get( 'first_activation' ) ) { |
| 80 |
// Save initial set of plugin options, and time of first activation of the plugin, on first activation. |
| 81 |
TablePress::$model_options->update( array( |
| 82 |
'first_activation' => time(), |
| 83 |
'plugin_options_db_version' => TablePress::db_version, |
| 84 |
) ); |
| 85 |
} else { |
| 86 |
// Update Plugin Options Options, if necessary. |
| 87 |
TablePress::$model_options->merge_plugin_options_defaults(); |
| 88 |
$updated_options = array( |
| 89 |
'plugin_options_db_version' => TablePress::db_version, |
| 90 |
'prev_tablepress_version' => TablePress::$model_options->get( 'tablepress_version' ), |
| 91 |
'tablepress_version' => TablePress::version, |
| 92 |
'message_plugin_update' => true, |
| 93 |
); |
| 94 |
|
| 95 |
// Only write files, if "Custom CSS" is to be used, and if there is "Custom CSS". |
| 96 |
if ( TablePress::$model_options->get( 'use_custom_css' ) && '' !== TablePress::$model_options->get( 'custom_css' ) ) { |
| 97 |
// Re-save "Custom CSS" to re-create all files (as TablePress Default CSS might have changed). |
| 98 |
/** |
| 99 |
* Load WP file functions to provide filesystem access functions early. |
| 100 |
*/ |
| 101 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 102 |
/** |
| 103 |
* Load WP admin template functions to provide `submit_button()` which is necessary for `request_filesystem_credentials()`. |
| 104 |
*/ |
| 105 |
require_once ABSPATH . 'wp-admin/includes/template.php'; |
| 106 |
$tablepress_css = TablePress::load_class( 'TablePress_CSS', 'class-css.php', 'classes' ); |
| 107 |
$result = $tablepress_css->save_custom_css_to_file( TablePress::$model_options->get( 'custom_css' ), TablePress::$model_options->get( 'custom_css_minified' ) ); |
| 108 |
// If saving was successful, use "Custom CSS" file. |
| 109 |
$updated_options['use_custom_css_file'] = $result; |
| 110 |
// Increase the "Custom CSS" version number for cache busting. |
| 111 |
if ( $result ) { |
| 112 |
$updated_options['custom_css_version'] = TablePress::$model_options->get( 'custom_css_version' ) + 1; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
TablePress::$model_options->update( $updated_options ); |
| 117 |
|
| 118 |
// Clear table caches. |
| 119 |
TablePress::$model_table->invalidate_table_output_caches(); |
| 120 |
|
| 121 |
// Add mime type field to existing posts with the TablePress Custom Post Type, so that other plugins know that they are not dealing with plain text. |
| 122 |
if ( $current_plugin_options_db_version < 25 ) { |
| 123 |
TablePress::$model_table->add_mime_type_to_posts(); |
| 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 |
TablePress::$model_options->update( 'user_options_db_version', TablePress::db_version ); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
} // class TablePress_Controller |
| 145 |
|