| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Tag Groups |
| 5 |
* |
| 6 |
* @package Tag Groups |
| 7 |
* |
| 8 |
* @author Christoph Amthor |
| 9 |
* @copyright 2019 Christoph Amthor (@ Chatty Mango, chattymango.com) |
| 10 |
* @license GPL-3.0+ |
| 11 |
*/ |
| 12 |
|
| 13 |
// phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace, Squiz.Classes.ValidClassName.NotCamelCaps, PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 14 |
if (!class_exists('TagGroups_Loader')) { |
| 15 |
class TagGroups_Loader |
| 16 |
{ |
| 17 |
/** |
| 18 |
* absolute path to the plugin main file |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
public $plugin_path ; |
| 23 |
public function __construct($plugin_path) |
| 24 |
{ |
| 25 |
$this->plugin_path = $plugin_path; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Provide objects that we'll need frequently |
| 30 |
* |
| 31 |
* @param void |
| 32 |
* @return object $this |
| 33 |
*/ |
| 34 |
public function provide_globals() |
| 35 |
{ |
| 36 |
global $tag_group_groups, $tag_group_terms ; |
| 37 |
$tag_group_groups = new TagGroups_Groups(); |
| 38 |
$tag_group_terms = new TagGroups_Terms(); |
| 39 |
return $this; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Makes all required classes available through an autoloader |
| 44 |
* |
| 45 |
* @param void |
| 46 |
* @return object $this |
| 47 |
*/ |
| 48 |
public function require_classes() |
| 49 |
{ |
| 50 |
spl_autoload_register(function ($class_name) { |
| 51 |
|
| 52 |
if (strpos($class_name, 'TagGroups_') !== 0) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
if (class_exists($class_name)) { |
| 57 |
return; |
| 58 |
} |
| 59 |
/** |
| 60 |
* Directories are ordered according to priority |
| 61 |
*/ |
| 62 |
$dirs = array( |
| 63 |
'/include/entities/', |
| 64 |
'/include/helpers/', |
| 65 |
'/include/helpers/cache/', |
| 66 |
'/include/admin/', |
| 67 |
'/include/admin/runners/', |
| 68 |
'/include/admin/handlers/', |
| 69 |
'/include/shortcodes/' |
| 70 |
); |
| 71 |
$class_name = str_replace('TagGroups_', '', $class_name); |
| 72 |
foreach ($dirs as $dir) { |
| 73 |
/** |
| 74 |
* We need to make all strings lower-case for different filesystems |
| 75 |
*/ |
| 76 |
|
| 77 |
if (file_exists($this->plugin_path . $dir . 'class.' . strtolower($class_name) . '.php')) { |
| 78 |
include_once $this->plugin_path . $dir . 'class.' . strtolower($class_name) . '.php'; |
| 79 |
return; |
| 80 |
} |
| 81 |
} |
| 82 |
}); |
| 83 |
return $this; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* whether the installed version is newer than the latest recorded in the database |
| 88 |
* |
| 89 |
* @return boolean |
| 90 |
*/ |
| 91 |
public function is_version_update() |
| 92 |
{ |
| 93 |
return version_compare($this->get_version(), $this->get_saved_version(), '>'); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Gets the version number of the installed files |
| 98 |
* |
| 99 |
* @return string |
| 100 |
*/ |
| 101 |
public function get_version() |
| 102 |
{ |
| 103 |
$this->set_version(); |
| 104 |
return TAG_GROUPS_VERSION; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Gets the latest recorded version number |
| 109 |
* |
| 110 |
* @return string |
| 111 |
*/ |
| 112 |
public function get_saved_version() |
| 113 |
{ |
| 114 |
return TagGroups_Options::get_option('tag_group_base_version', '1.0'); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Sets the version from the plugin main file |
| 119 |
* |
| 120 |
* @param void |
| 121 |
* @return object $this; |
| 122 |
*/ |
| 123 |
public function set_version() |
| 124 |
{ |
| 125 |
if (defined('TAG_GROUPS_VERSION')) { |
| 126 |
return; |
| 127 |
} |
| 128 |
if (!function_exists('get_plugin_data')) { |
| 129 |
include_once ABSPATH . '/wp-admin/includes/plugin.php'; |
| 130 |
} |
| 131 |
$plugin_header = get_plugin_data(WP_PLUGIN_DIR . '/' . TAG_GROUPS_PLUGIN_BASENAME, false, false); |
| 132 |
if (isset($plugin_header['Version'])) { |
| 133 |
$version = $plugin_header['Version']; |
| 134 |
} else { |
| 135 |
$version = '1.0'; |
| 136 |
} |
| 137 |
|
| 138 |
define('TAG_GROUPS_VERSION', $version); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Check if WordPress meets the minimum version |
| 143 |
* |
| 144 |
* @param void |
| 145 |
* @return void |
| 146 |
*/ |
| 147 |
public function check_preconditions() |
| 148 |
{ |
| 149 |
if (!defined('TAG_GROUPS_MINIMUM_VERSION_WP')) { |
| 150 |
return; |
| 151 |
} |
| 152 |
global $wp_version ; |
| 153 |
/** |
| 154 |
* Check the minimum WP version |
| 155 |
*/ |
| 156 |
|
| 157 |
if (version_compare($wp_version, TAG_GROUPS_MINIMUM_VERSION_WP, '<')) { |
| 158 |
TagGroups_Error::log('[Tag Groups] Insufficient WordPress version for Tag Groups plugin.'); |
| 159 |
/* translators: %1$s is the plugin name, %2$s is the minimum WordPress version */ |
| 160 |
TagGroups_Admin_Notice::add('error', sprintf(__('The plugin %1$s requires WordPress %2$s to function properly.', 'tag-groups'), '<b>Tag Groups</b>', TAG_GROUPS_MINIMUM_VERSION_WP) . __('Please upgrade WordPress and then try again.', 'tag-groups')); |
| 161 |
return; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* adds all hooks |
| 167 |
* |
| 168 |
* @param void |
| 169 |
* @return object $this |
| 170 |
*/ |
| 171 |
public function add_hooks() |
| 172 |
{ |
| 173 |
global $tag_groups_hooks ; |
| 174 |
$tag_groups_hooks = new TagGroups_Hooks($this); |
| 175 |
$tag_groups_hooks->root_all(); |
| 176 |
if (is_admin()) { |
| 177 |
$tag_groups_hooks->is_admin(); |
| 178 |
} else { |
| 179 |
$tag_groups_hooks->not_is_admin(); |
| 180 |
} |
| 181 |
|
| 182 |
return $this; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* registers the shortcodes with Gutenberg blocks |
| 187 |
* |
| 188 |
* @param void |
| 189 |
* @return object $this |
| 190 |
*/ |
| 191 |
public function register_shortcodes_and_blocks() |
| 192 |
{ |
| 193 |
if (defined('CM_UNIT_TESTING') && CM_UNIT_TESTING) { |
| 194 |
return; |
| 195 |
} |
| 196 |
/** |
| 197 |
* add Gutenberg functionality |
| 198 |
*/ |
| 199 |
include_once $this->plugin_path . '/src/init.php'; |
| 200 |
// Register shortcodes also for admin so that we can remove them with strip_shortcodes in Ajax call |
| 201 |
TagGroups_Shortcode_Statics::register(); |
| 202 |
return $this; |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* registers the REST API |
| 207 |
* |
| 208 |
* @param void |
| 209 |
* @return object $this |
| 210 |
*/ |
| 211 |
public function register_REST_API() |
| 212 |
{ |
| 213 |
TagGroups_REST_API::register_hook(); |
| 214 |
return $this; |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Loads text domain for internationalization |
| 219 |
*/ |
| 220 |
public function register_textdomain() |
| 221 |
{ |
| 222 |
$domain = 'tag-groups'; |
| 223 |
$mofile_custom = sprintf('%s-%s.mo', $domain, get_user_locale()); |
| 224 |
$locations = [ |
| 225 |
trailingslashit(WP_LANG_DIR . '/' . $domain), |
| 226 |
trailingslashit(WP_LANG_DIR . '/loco/plugins/'), |
| 227 |
trailingslashit(WP_LANG_DIR), |
| 228 |
trailingslashit(plugin_dir_path(TAG_GROUPS_FILE) . 'languages'), |
| 229 |
]; |
| 230 |
// Try custom locations in WP_LANG_DIR. |
| 231 |
foreach ($locations as $location) { |
| 232 |
if (load_textdomain($domain, $location . $mofile_custom)) { |
| 233 |
return true; |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* registers the CRON routines |
| 240 |
* |
| 241 |
* @param void |
| 242 |
* @return object $this |
| 243 |
*/ |
| 244 |
public function register_CRON() |
| 245 |
{ |
| 246 |
// CRON independent from admin or frontend |
| 247 |
TagGroups_Cron::register_cron_handlers(); |
| 248 |
TagGroups_Cron::schedule_regular('hourly', 'tag_groups_check_tag_migration'); |
| 249 |
// schedule purging of expired transients |
| 250 |
TagGroups_Cron::schedule_regular('daily', 'tag_groups_purge_expired_transients'); |
| 251 |
return $this; |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
} |
| 256 |
|