Admin
1 month ago
Api
1 month ago
Blocks
2 months ago
Hooks
1 month ago
AjaxController.php
1 month ago
BlocksController.php
1 month ago
DiviController.php
1 year ago
ElementorController.php
1 year ago
GutenBergController.php
1 month ago
PageTemplateController.php
2 years ago
ScriptController.php
1 month ago
ShortcodeController.php
5 months ago
WidgetController.php
2 years ago
ElementorController.php
358 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Elementor Controller class. |
| 4 | * |
| 5 | * @package RT_TPG |
| 6 | */ |
| 7 | |
| 8 | namespace RT\ThePostGrid\Controllers; |
| 9 | |
| 10 | // Do not allow directly accessing this file. |
| 11 | use RT\ThePostGrid\Helpers\Fns; |
| 12 | |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit( 'This script cannot be accessed directly.' ); |
| 15 | } |
| 16 | |
| 17 | if ( ! class_exists( 'ElementorController' ) ) : |
| 18 | /** |
| 19 | * Elementor Controller class. |
| 20 | */ |
| 21 | class ElementorController { |
| 22 | /** |
| 23 | * Category ID |
| 24 | * |
| 25 | * @var string |
| 26 | */ |
| 27 | public $el_cat_id; |
| 28 | |
| 29 | /** |
| 30 | * Version |
| 31 | * |
| 32 | * @var string |
| 33 | */ |
| 34 | private $version; |
| 35 | |
| 36 | /** |
| 37 | * Class constructor |
| 38 | */ |
| 39 | public function __construct() { |
| 40 | $this->version = defined( 'WP_DEBUG' ) && WP_DEBUG ? time() : RT_THE_POST_GRID_VERSION; |
| 41 | $this->el_cat_id = RT_THE_POST_GRID_PLUGIN_SLUG . '-elements'; |
| 42 | |
| 43 | if ( did_action( 'elementor/loaded' ) ) { |
| 44 | add_action( 'elementor/widgets/register', [ $this, 'init_widgets' ] ); |
| 45 | add_action( 'elementor/elements/categories_registered', [ $this, 'widget_category' ] ); |
| 46 | add_action( 'elementor/editor/after_enqueue_scripts', [ $this, 'el_editor_script' ] ); |
| 47 | add_action( 'wp_footer', [ $this, 'tpg_el_scripts' ] ); |
| 48 | add_action( 'wp_enqueue_scripts', [ $this, 'tpg_el_style' ] ); |
| 49 | add_filter( 'elementor/editor/localize_settings', [ $this, 'promotePremiumWidgets' ] ); |
| 50 | } |
| 51 | add_action( 'elementor/editor/after_enqueue_scripts', [ $this, 'editor_el_enqueue' ] ); |
| 52 | add_action( 'wp_ajax_rttpg_get_el_layouts', [ $this, 'rttpg_get_el_layouts' ] ); |
| 53 | add_action( 'wp_ajax_rttpg_el_layout_count', [ $this, 'rttpg_el_layout_count' ] ); |
| 54 | } |
| 55 | |
| 56 | // TODO Import |
| 57 | |
| 58 | public function editor_el_enqueue() { |
| 59 | |
| 60 | //phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion |
| 61 | wp_enqueue_style( |
| 62 | 'rttpg-elementor-edition', |
| 63 | rtTPG()->get_assets_uri( 'elementor/main.css' ) . |
| 64 | null, |
| 65 | $this->version |
| 66 | ); |
| 67 | |
| 68 | wp_enqueue_script( |
| 69 | 'rttpg-elementor-import', |
| 70 | rtTPG()->get_assets_uri( 'elementor/main.js' ), |
| 71 | [ |
| 72 | 'wp-block-editor', |
| 73 | 'wp-blocks', |
| 74 | 'wp-i18n', |
| 75 | 'wp-element', |
| 76 | 'wp-hooks', |
| 77 | 'wp-util', |
| 78 | 'wp-components', |
| 79 | 'elementor-editor', |
| 80 | 'jquery', |
| 81 | ], |
| 82 | $this->version, |
| 83 | true |
| 84 | ); |
| 85 | |
| 86 | wp_localize_script( |
| 87 | 'rttpg-elementor-import', |
| 88 | 'rttpgParams', |
| 89 | [ |
| 90 | 'nonce' => wp_create_nonce( 'rttpg_nonce' ), |
| 91 | 'hasPro' => rtTPG()->hasPro(), |
| 92 | 'current_user_id' => get_current_user_id(), |
| 93 | 'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 94 | 'site_url' => site_url(), |
| 95 | 'plugin_url' => RT_THE_POST_GRID_PLUGIN_URL, |
| 96 | 'admin_url' => admin_url(), |
| 97 | 'plugin_pro_url' => rtTPG()->getProPath(), |
| 98 | 'post_type' => Fns::get_post_types(), |
| 99 | 'all_term_list' => Fns::get_all_taxonomy_guten(), |
| 100 | ] |
| 101 | ); |
| 102 | } |
| 103 | |
| 104 | public function rttpg_el_layout_count() { |
| 105 | |
| 106 | $BASE_URL = 'https://www.radiustheme.com/demo/plugins/the-post-grid-elementor/wp-json/rttpgapi/v1/layoutinfo/'; |
| 107 | // Verify the request. |
| 108 | check_ajax_referer( 'rttpg_nonce', 'nonce' ); |
| 109 | |
| 110 | // It's good let's do some capability check. |
| 111 | $user = wp_get_current_user(); |
| 112 | $allowed_roles = [ 'editor', 'administrator', 'author' ]; |
| 113 | |
| 114 | if ( ! array_intersect( $allowed_roles, $user->roles ) ) { |
| 115 | wp_die( esc_html__( 'You don\'t have permission to perform this action', 'the-post-grid' ) ); |
| 116 | } |
| 117 | |
| 118 | // Cool, we're almost there, let's check the user authenticity a little bit, shall we! |
| 119 | if ( ! is_user_logged_in() && $user->ID !== sanitize_text_field( $_REQUEST['user_id'] ) ) { |
| 120 | wp_die( esc_html__( 'You don\'t have proper authorization to perform this action', 'the-post-grid' ) ); |
| 121 | } |
| 122 | |
| 123 | $status = $_REQUEST['status'] ?? ''; |
| 124 | $layout_id = $_REQUEST['layout_id'] ?? ''; |
| 125 | |
| 126 | $post_args = [ 'timeout' => 120 ]; |
| 127 | $post_args['body'] = [ |
| 128 | 'status' => $status, |
| 129 | 'layout_id' => $layout_id, |
| 130 | ]; |
| 131 | $layoutRequest = wp_remote_get( $BASE_URL, $post_args ); |
| 132 | if ( is_wp_error( $layoutRequest ) ) { |
| 133 | wp_send_json_error( [ 'messages' => $layoutRequest->get_error_messages() ] ); |
| 134 | } |
| 135 | $layoutData = json_decode( $layoutRequest['body'], true ); |
| 136 | |
| 137 | wp_send_json_success( $layoutData ); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | /** |
| 142 | * @return void |
| 143 | */ |
| 144 | public function rttpg_get_el_layouts() { |
| 145 | |
| 146 | $BASE_URL = 'https://www.radiustheme.com/demo/plugins/the-post-grid-elementor/wp-json/rttpgelapi/v1/layouts/'; |
| 147 | |
| 148 | // Verify the request. |
| 149 | check_ajax_referer( 'rttpg_nonce', 'nonce' ); |
| 150 | |
| 151 | // It's good let's do some capability check. |
| 152 | $user = wp_get_current_user(); |
| 153 | $allowed_roles = [ 'editor', 'administrator', 'author' ]; |
| 154 | |
| 155 | if ( ! array_intersect( $allowed_roles, $user->roles ) ) { |
| 156 | wp_die( esc_html__( 'You don\'t have permission to perform this action', 'the-post-grid' ) ); |
| 157 | } |
| 158 | |
| 159 | // Cool, we're almost there, let's check the user authenticity a little bit, shall we! |
| 160 | if ( ! is_user_logged_in() && $user->ID !== sanitize_text_field( $_REQUEST['user_id'] ) ) { |
| 161 | wp_die( esc_html__( 'You don\'t have proper authorization to perform this action', 'the-post-grid' ) ); |
| 162 | } |
| 163 | |
| 164 | $status = isset( $_REQUEST['status'] ) ? $_REQUEST['status'] : ''; |
| 165 | $post_args = [ 'timeout' => 120 ]; |
| 166 | $post_args['body'] = [ 'status' => $status ]; |
| 167 | $layoutRequest = wp_remote_get( $BASE_URL, $post_args ); |
| 168 | if ( is_wp_error( $layoutRequest ) ) { |
| 169 | wp_send_json_error( [ 'messages' => $layoutRequest->get_error_messages() ] ); |
| 170 | } |
| 171 | $layoutData = json_decode( $layoutRequest['body'], true ); |
| 172 | |
| 173 | wp_send_json_success( $layoutData ); |
| 174 | } |
| 175 | |
| 176 | |
| 177 | /** |
| 178 | * Style |
| 179 | * |
| 180 | * @return void |
| 181 | */ |
| 182 | public function tpg_el_style() { |
| 183 | // Custom CSS From Settings. |
| 184 | $css = isset( $settings['custom_css'] ) ? stripslashes( $settings['custom_css'] ) : null; |
| 185 | if ( $css ) { |
| 186 | wp_add_inline_style( 'rt-tpg-block', $css ); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Scripts |
| 192 | * |
| 193 | * @return void |
| 194 | */ |
| 195 | public function tpg_el_scripts() { |
| 196 | $ajaxurl = ''; |
| 197 | |
| 198 | if ( in_array( 'sitepress-multilingual-cms/sitepress.php', get_option( 'active_plugins' ) ) ) { |
| 199 | $ajaxurl .= admin_url( 'admin-ajax.php?lang=' . ICL_LANGUAGE_CODE ); |
| 200 | } else { |
| 201 | $ajaxurl .= admin_url( 'admin-ajax.php' ); |
| 202 | } |
| 203 | |
| 204 | $variables = [ |
| 205 | 'nonceID' => esc_attr( rtTPG()->nonceId() ), |
| 206 | 'nonce' => esc_attr( wp_create_nonce( rtTPG()->nonceText() ) ), |
| 207 | 'ajaxurl' => esc_url( $ajaxurl ), |
| 208 | 'uid' => get_current_user_id(), |
| 209 | ]; |
| 210 | |
| 211 | wp_localize_script( 'rt-tpg', 'rttpg', $variables ); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Editor Scripts |
| 216 | * |
| 217 | * @return void |
| 218 | */ |
| 219 | public function el_editor_script() { |
| 220 | wp_enqueue_script( 'tgp-el-editor-scripts', rtTPG()->get_assets_uri( 'js/tpg-el-editor.js' ), [ 'jquery' ], $this->version, true ); |
| 221 | wp_enqueue_style( 'tgp-el-editor-style', rtTPG()->get_assets_uri( 'css/admin/tpg-el-editor.css' ), [], $this->version ); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Elementor widgets |
| 226 | * |
| 227 | * @param object $widgets_manager Manager. |
| 228 | * |
| 229 | * @return void |
| 230 | */ |
| 231 | public function init_widgets( $widgets_manager ) { |
| 232 | require_once RT_THE_POST_GRID_PLUGIN_PATH . '/app/Widgets/elementor/base.php'; |
| 233 | require_once RT_THE_POST_GRID_PLUGIN_PATH . '/app/Widgets/elementor/rtTPGElementorHelper.php'; |
| 234 | |
| 235 | // dir_name => class_name. |
| 236 | $widgets = [ |
| 237 | 'grid-layout' => '\TPGGridLayout', |
| 238 | 'list-layout' => '\TPGListLayout', |
| 239 | 'grid-hover-layout' => '\TPGGridHoverLayout', |
| 240 | 'slider-layout' => '\TPGSliderLayout', |
| 241 | 'category-block' => '\TPGCategoryBlock', |
| 242 | 'section-title' => '\SectionTitle', |
| 243 | 'news-ticker' => '\NewsTicker', |
| 244 | 'post-timeline' => '\TPGPostTimeline', |
| 245 | ]; |
| 246 | |
| 247 | if ( rtTPG()->hasPro() && defined( 'RT_THE_POST_GRID_PRO_PLUGIN_PATH' ) ) { |
| 248 | $proFileCheck = RT_THE_POST_GRID_PRO_PLUGIN_PATH . '/app/Widgets/elementor/category-block.php'; |
| 249 | if ( file_exists( $proFileCheck ) ) { |
| 250 | unset( $widgets['category-block'] ); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | $widgets = apply_filters( 'tpg_el_widget_register', $widgets ); |
| 255 | |
| 256 | foreach ( $widgets as $file_name => $class ) { |
| 257 | |
| 258 | if ( ! rtTPG()->hasPro() && in_array( $file_name, [ 'slider-layout', 'category-block', 'post-timeline', 'news-ticker' ] ) ) { |
| 259 | continue; |
| 260 | } |
| 261 | |
| 262 | $template_name = 'the-post-grid/elementor/' . $file_name . '.php'; |
| 263 | |
| 264 | if ( file_exists( get_stylesheet_directory() . $template_name ) ) { |
| 265 | $file = get_stylesheet_directory() . $template_name; |
| 266 | } elseif ( file_exists( get_template_directory() . $template_name ) ) { |
| 267 | $file = get_template_directory() . $template_name; |
| 268 | } else { |
| 269 | $file = RT_THE_POST_GRID_PLUGIN_PATH . '/app/Widgets/elementor/widgets/' . $file_name . '.php'; |
| 270 | } |
| 271 | |
| 272 | require_once $file; |
| 273 | |
| 274 | $widgets_manager->register( new $class() ); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Widget category |
| 280 | * |
| 281 | * @param object $elements_manager Manager. |
| 282 | * |
| 283 | * @return void |
| 284 | */ |
| 285 | public function widget_category( $elements_manager ) { |
| 286 | $categories['tpg-block-builder-widgets'] = [ |
| 287 | 'title' => esc_html__( 'TPG Template Builder Element', 'the-post-grid' ), |
| 288 | 'icon' => 'fa fa-plug', |
| 289 | ]; |
| 290 | |
| 291 | $categories[ RT_THE_POST_GRID_PLUGIN_SLUG . '-elements' ] = [ |
| 292 | 'title' => esc_html__( 'The Post Grid', 'the-post-grid' ), |
| 293 | 'icon' => 'fa fa-plug', |
| 294 | ]; |
| 295 | |
| 296 | $get_all_categories = $elements_manager->get_categories(); |
| 297 | $categories = array_merge( $categories, $get_all_categories ); |
| 298 | $set_categories = function ( $categories ) { |
| 299 | $this->categories = $categories; |
| 300 | }; |
| 301 | |
| 302 | $set_categories->call( $elements_manager, $categories ); |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Promotion |
| 307 | * |
| 308 | * @param array $config Config. |
| 309 | * |
| 310 | * @return array |
| 311 | */ |
| 312 | public function promotePremiumWidgets( $config ) { |
| 313 | if ( rtTPG()->hasPro() ) { |
| 314 | return $config; |
| 315 | } |
| 316 | |
| 317 | if ( ! isset( $config['promotionWidgets'] ) || ! is_array( $config['promotionWidgets'] ) ) { |
| 318 | $config['promotionWidgets'] = []; |
| 319 | } |
| 320 | |
| 321 | $pro_widgets = [ |
| 322 | [ |
| 323 | 'name' => 'tpg-slider-layout', |
| 324 | 'title' => esc_html__( 'TPG - Slider Layout', 'the-post-grid' ), |
| 325 | 'description' => esc_html__( 'TPG - Slider Layout', 'the-post-grid' ), |
| 326 | 'icon' => 'eicon-post-slider tpg-grid-icon tss-promotional-element', |
| 327 | 'categories' => '[ "the-post-grid-elements" ]', |
| 328 | ], |
| 329 | [ |
| 330 | 'name' => 'tpg-category-block', |
| 331 | 'title' => esc_html__( 'TPG - Category Block', 'the-post-grid' ), |
| 332 | 'description' => esc_html__( 'TPG - Category Block', 'the-post-grid' ), |
| 333 | 'icon' => 'eicon-folder-o tpg-grid-icon tss-promotional-element', |
| 334 | 'categories' => '[ "the-post-grid-elements" ]', |
| 335 | ], |
| 336 | [ |
| 337 | 'name' => 'tpg-news-ticker', |
| 338 | 'title' => esc_html__( 'TPG - News Ticker', 'the-post-grid' ), |
| 339 | 'description' => esc_html__( 'TPG - News Ticker', 'the-post-grid' ), |
| 340 | 'icon' => 'eicon-carousel tpg-grid-icon tss-promotional-element', |
| 341 | 'categories' => '[ "the-post-grid-elements" ]', |
| 342 | ], |
| 343 | [ |
| 344 | 'name' => 'tpg-post-timeline', |
| 345 | 'title' => esc_html__( 'TPG - Post Timeline', 'the-post-grid' ), |
| 346 | 'description' => esc_html__( 'TPG - Post Timeline', 'the-post-grid' ), |
| 347 | 'icon' => 'eicon-time-line tpg-grid-icon tss-promotional-element', |
| 348 | 'categories' => '[ "the-post-grid-elements" ]', |
| 349 | ], |
| 350 | ]; |
| 351 | |
| 352 | $config['promotionWidgets'] = array_merge( $config['promotionWidgets'], $pro_widgets ); |
| 353 | |
| 354 | return $config; |
| 355 | } |
| 356 | } |
| 357 | endif; |
| 358 |