Controllers
3 years ago
Helpers
3 years ago
Models
3 years ago
Widgets
3 years ago
RtTpg.php
3 years ago
RtTpg.php
361 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Main initialization class. |
| 4 | * |
| 5 | * @package RT_TPG |
| 6 | */ |
| 7 | |
| 8 | // Do not allow directly accessing this file. |
| 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | exit( 'This script cannot be accessed directly.' ); |
| 11 | } |
| 12 | |
| 13 | require_once __DIR__ . './../vendor/autoload.php'; |
| 14 | |
| 15 | use RT\ThePostGrid\Controllers\Api\RestApi; |
| 16 | use RT\ThePostGrid\Controllers\Admin\AdminAjaxController; |
| 17 | use RT\ThePostGrid\Controllers\Admin\MetaController; |
| 18 | use RT\ThePostGrid\Controllers\Admin\NoticeController; |
| 19 | use RT\ThePostGrid\Controllers\Admin\PostTypeController; |
| 20 | use RT\ThePostGrid\Controllers\Admin\SettingsController; |
| 21 | use RT\ThePostGrid\Controllers\AjaxController; |
| 22 | use RT\ThePostGrid\Controllers\ElementorController; |
| 23 | use RT\ThePostGrid\Controllers\BlocksController; |
| 24 | use RT\ThePostGrid\Controllers\GutenBergController; |
| 25 | use RT\ThePostGrid\Controllers\ScriptController; |
| 26 | use RT\ThePostGrid\Controllers\ShortcodeController; |
| 27 | use RT\ThePostGrid\Controllers\PageTemplateController; |
| 28 | use RT\ThePostGrid\Controllers\Hooks\FilterHooks; |
| 29 | use RT\ThePostGrid\Controllers\Hooks\ActionHooks; |
| 30 | use RT\ThePostGrid\Controllers\WidgetController; |
| 31 | use RT\ThePostGrid\Helpers\Install; |
| 32 | use RT\ThePostGrid\Controllers\Admin\UpgradeController; |
| 33 | |
| 34 | |
| 35 | if ( ! class_exists( RtTpg::class ) ) { |
| 36 | /** |
| 37 | * Main initialization class. |
| 38 | */ |
| 39 | final class RtTpg { |
| 40 | /** |
| 41 | * Post Type |
| 42 | * |
| 43 | * @var string |
| 44 | */ |
| 45 | public $post_type = 'rttpg'; |
| 46 | |
| 47 | /** |
| 48 | * Options |
| 49 | * |
| 50 | * @var array |
| 51 | */ |
| 52 | public $options = [ |
| 53 | 'settings' => 'rt_the_post_grid_settings', |
| 54 | 'version' => RT_THE_POST_GRID_VERSION, |
| 55 | 'installed_version' => 'rt_the_post_grid_current_version', |
| 56 | 'slug' => RT_THE_POST_GRID_PLUGIN_SLUG, |
| 57 | ]; |
| 58 | |
| 59 | /** |
| 60 | * Defaut Settings |
| 61 | * |
| 62 | * @var array |
| 63 | */ |
| 64 | public $defaultSettings = [ |
| 65 | 'tpg_block_type' => 'default', |
| 66 | 'popup_fields' => [ |
| 67 | 'title', |
| 68 | 'feature_img', |
| 69 | 'content', |
| 70 | 'post_date', |
| 71 | 'author', |
| 72 | 'categories', |
| 73 | 'tags', |
| 74 | 'social_share', |
| 75 | ], |
| 76 | 'social_share_items' => [ |
| 77 | 'facebook', |
| 78 | 'twitter', |
| 79 | 'linkedin', |
| 80 | ], |
| 81 | ]; |
| 82 | |
| 83 | /** |
| 84 | * Store the singleton object. |
| 85 | * |
| 86 | * @var boolean |
| 87 | */ |
| 88 | private static $singleton = false; |
| 89 | |
| 90 | /** |
| 91 | * Create an inaccessible constructor. |
| 92 | */ |
| 93 | private function __construct() { |
| 94 | $this->__init(); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Fetch an instance of the class. |
| 99 | */ |
| 100 | public static function getInstance() { |
| 101 | if ( false === self::$singleton ) { |
| 102 | self::$singleton = new self(); |
| 103 | } |
| 104 | |
| 105 | return self::$singleton; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Class init |
| 110 | * |
| 111 | * @return void |
| 112 | */ |
| 113 | protected function __init() { |
| 114 | $settings = get_option( $this->options['settings'] ); |
| 115 | |
| 116 | new UpgradeController(); |
| 117 | new PostTypeController(); |
| 118 | new AjaxController(); |
| 119 | new ScriptController(); |
| 120 | new WidgetController(); |
| 121 | new PageTemplateController(); |
| 122 | |
| 123 | if ( is_admin() ) { |
| 124 | new AdminAjaxController(); |
| 125 | new NoticeController(); |
| 126 | new MetaController(); |
| 127 | } |
| 128 | |
| 129 | if ( ! isset( $settings['tpg_block_type'] ) || in_array( $settings['tpg_block_type'], [ 'default', 'shortcode' ], true ) ) { |
| 130 | new ShortcodeController(); |
| 131 | new GutenBergController(); |
| 132 | } |
| 133 | |
| 134 | new RestApi(); |
| 135 | |
| 136 | FilterHooks::init(); |
| 137 | ActionHooks::init(); |
| 138 | |
| 139 | ( new SettingsController() )->init(); |
| 140 | |
| 141 | if ( ! isset( $settings['tpg_block_type'] ) || in_array( $settings['tpg_block_type'], [ 'default', 'elementor' ], true ) ) { |
| 142 | new ElementorController(); |
| 143 | new BlocksController(); |
| 144 | } |
| 145 | |
| 146 | $this->load_hooks(); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Load hooks |
| 151 | * |
| 152 | * @return void |
| 153 | */ |
| 154 | private function load_hooks() { |
| 155 | register_activation_hook( RT_THE_POST_GRID_PLUGIN_FILE, [ Install::class, 'activate' ] ); |
| 156 | register_deactivation_hook( RT_THE_POST_GRID_PLUGIN_FILE, [ Install::class, 'deactivate' ] ); |
| 157 | |
| 158 | add_action( 'plugins_loaded', [ $this, 'on_plugins_loaded' ], - 1 ); |
| 159 | add_action( 'init', [ $this, 'init_hooks' ], 0 ); |
| 160 | add_filter( 'wp_calculate_image_srcset', [ $this, 'calculate_image_srcset' ] ); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Init hooks |
| 165 | * |
| 166 | * @return void |
| 167 | */ |
| 168 | public function init_hooks() { |
| 169 | do_action( 'rttpg_before_init', $this ); |
| 170 | |
| 171 | $this->load_language(); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Remove calculate image srcset |
| 176 | * @return array |
| 177 | */ |
| 178 | public function calculate_image_srcset() { |
| 179 | return []; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * I18n |
| 184 | * |
| 185 | * @return void |
| 186 | */ |
| 187 | public function load_language() { |
| 188 | do_action( 'rttpg_set_local', null ); |
| 189 | $locale = determine_locale(); |
| 190 | $locale = apply_filters( 'plugin_locale', $locale, 'the-post-grid' ); |
| 191 | unload_textdomain( 'the-post-grid' ); |
| 192 | load_textdomain( 'the-post-grid', WP_LANG_DIR . '/the-post-grid/the-post-grid-' . $locale . '.mo' ); |
| 193 | load_plugin_textdomain( 'the-post-grid', false, plugin_basename( dirname( RT_THE_POST_GRID_PLUGIN_FILE ) ) . '/languages' ); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Plugin loaded action |
| 198 | * |
| 199 | * @return void |
| 200 | */ |
| 201 | public function on_plugins_loaded() { |
| 202 | do_action( 'rttpg_loaded', $this ); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Get the plugin path. |
| 207 | * |
| 208 | * @return string |
| 209 | */ |
| 210 | public function plugin_path() { |
| 211 | return untrailingslashit( plugin_dir_path( RT_THE_POST_GRID_PLUGIN_FILE ) ); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Plugin template path |
| 216 | * |
| 217 | * @return string |
| 218 | */ |
| 219 | public function plugin_template_path() { |
| 220 | $plugin_template = $this->plugin_path() . '/templates/'; |
| 221 | |
| 222 | return apply_filters( 'tlp_tpg_template_path', $plugin_template ); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Default template path |
| 227 | * |
| 228 | * @return string |
| 229 | */ |
| 230 | public function default_template_path() { |
| 231 | return apply_filters( 'rttpg_default_template_path', untrailingslashit( plugin_dir_path( RT_THE_POST_GRID_PLUGIN_FILE ) ) ); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Nonce text |
| 236 | * |
| 237 | * @return string |
| 238 | */ |
| 239 | public static function nonceText() { |
| 240 | return 'rttpg_nonce_secret'; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Nonce ID |
| 245 | * |
| 246 | * @return string |
| 247 | */ |
| 248 | public static function nonceId() { |
| 249 | return 'rttpg_nonce'; |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Get assets URI |
| 254 | * |
| 255 | * @param string $file File. |
| 256 | * |
| 257 | * @return string |
| 258 | */ |
| 259 | public function get_assets_uri( $file ) { |
| 260 | $file = ltrim( $file, '/' ); |
| 261 | |
| 262 | return trailingslashit( RT_THE_POST_GRID_PLUGIN_URL . '/assets' ) . $file; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * RTL check. |
| 267 | * |
| 268 | * @param string $file File. |
| 269 | * |
| 270 | * @return string |
| 271 | */ |
| 272 | public function tpg_can_be_rtl( $file ) { |
| 273 | $file = ltrim( str_replace( '.css', '', $file ), '/' ); |
| 274 | |
| 275 | if ( is_rtl() ) { |
| 276 | $file .= '.rtl'; |
| 277 | } |
| 278 | |
| 279 | return trailingslashit( RT_THE_POST_GRID_PLUGIN_URL . '/assets' ) . $file . '.min.css'; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Get the template path. |
| 284 | * |
| 285 | * @return string |
| 286 | */ |
| 287 | public function get_template_path() { |
| 288 | return apply_filters( 'rttpg_template_path', 'the-post-grid/' ); |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Pro check. |
| 293 | * |
| 294 | * @return boolean |
| 295 | */ |
| 296 | public function hasPro() { |
| 297 | return class_exists( 'RtTpgPro' ) || class_exists( 'rtTPGP' ); |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Pro check. |
| 302 | * |
| 303 | * @return boolean |
| 304 | */ |
| 305 | public function getProPath() { |
| 306 | if ( ! $this->hasPro() ) { |
| 307 | return false; |
| 308 | } |
| 309 | if ( ! function_exists( 'get_plugin_data' ) ) { |
| 310 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 311 | } |
| 312 | |
| 313 | $path = WP_PLUGIN_DIR . '/the-post-grid-pro/'; |
| 314 | if ( file_exists( $path ) ) { |
| 315 | return plugins_url() . '/the-post-grid-pro/'; |
| 316 | } |
| 317 | |
| 318 | return false; |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Pro link. |
| 323 | * |
| 324 | * @return string |
| 325 | */ |
| 326 | public function proLink() { |
| 327 | return '//www.radiustheme.com/downloads/the-post-grid-pro-for-wordpress/'; |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Doc link. |
| 332 | * |
| 333 | * @return string |
| 334 | */ |
| 335 | public function docLink() { |
| 336 | return '//www.radiustheme.com/docs/the-post-grid/'; |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Demo link. |
| 341 | * |
| 342 | * @return string |
| 343 | */ |
| 344 | public function demoLink() { |
| 345 | return '//www.radiustheme.com/demo/plugins/the-post-grid/'; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Function for external use. |
| 351 | * |
| 352 | * @return rtTPG |
| 353 | */ |
| 354 | function rtTPG() { |
| 355 | return rtTPG::getInstance(); |
| 356 | } |
| 357 | |
| 358 | // Init app. |
| 359 | rtTPG(); |
| 360 | } |
| 361 |