| @@ -1,0 +1,285 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace Templately; | |
| 4 | + | |
| 5 | +use Elementor\User; | |
| 6 | +use Templately\Helper; | |
| 7 | + | |
| 8 | +class Elementor { | |
| 9 | + protected static $_instance = null; | |
| 10 | + | |
| 11 | + public static function get_instance() { | |
| 12 | + if ( static::$_instance === null ) { | |
| 13 | + static::$_instance = new static; | |
| 14 | + } | |
| 15 | + | |
| 16 | + return static::$_instance; | |
| 17 | + } | |
| 18 | + | |
| 19 | + public function __construct() { | |
| 20 | + API::get_instance(); | |
| 21 | + if ( class_exists( '\Elementor\Plugin' ) ) { | |
| 22 | + Loader::add_action( 'wp_enqueue_scripts', $this, 'enqueue_styles' ); | |
| 23 | + Loader::add_action( 'elementor/editor/before_enqueue_scripts', $this, 'enqueue_scripts' ); | |
| 24 | + } | |
| 25 | + Loader::add_action( 'admin_enqueue_scripts', $this, 'admin_scripts', 99 ); | |
| 26 | + Loader::add_action( 'enqueue_block_editor_assets', $this, 'admin_scripts', 99 ); | |
| 27 | + Loader::add_action( 'enqueue_block_editor_assets', $this, 'enqueue_styles', 99 ); | |
| 28 | + } | |
| 29 | + | |
| 30 | + public function enqueue_styles() { | |
| 31 | + \wp_enqueue_style( 'templately-editor', TEMPLATELY_URL . 'assets/css/editor.css', [], TEMPLATELY_VERSION ); | |
| 32 | + } | |
| 33 | + | |
| 34 | + public function is_editor() { | |
| 35 | + return is_admin() && isset( $_GET['action'] ) && 'elementor' === $_GET['action']; | |
| 36 | + } | |
| 37 | + | |
| 38 | + public function is_elementor() { | |
| 39 | + return isset( $_GET['action'] ) && 'elementor' === $_GET['action']; | |
| 40 | + } | |
| 41 | + | |
| 42 | + private function get_ip() { | |
| 43 | + if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) { | |
| 44 | + $ip = $_SERVER['HTTP_CLIENT_IP']; | |
| 45 | + } elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { | |
| 46 | + $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; | |
| 47 | + } else { | |
| 48 | + $ip = $_SERVER['REMOTE_ADDR']; | |
| 49 | + } | |
| 50 | + | |
| 51 | + return $ip; | |
| 52 | + } | |
| 53 | + | |
| 54 | + public function admin_scripts( $hook ) { | |
| 55 | + if ( ! in_array( $hook, array ( 'toplevel_page_templately', 'post-new.php', 'post.php' ) ) ) { | |
| 56 | + return; | |
| 57 | + } | |
| 58 | + $this->enqueue_scripts( 'admin' ); | |
| 59 | + } | |
| 60 | + | |
| 61 | + protected function editor_platform() { | |
| 62 | + global $current_screen; | |
| 63 | + $editor = 'elementor'; | |
| 64 | + if ( isset( $_GET['action'] ) && 'elementor' === $_GET['action'] ) { | |
| 65 | + $editor = 'elementor'; | |
| 66 | + } | |
| 67 | + if ( $current_screen->is_block_editor() && ! ( isset( $_GET['action'] ) && 'elementor' === $_GET['action'] ) ) { | |
| 68 | + $editor = 'gutenberg'; | |
| 69 | + } | |
| 70 | + if ( $current_screen->base === 'toplevel_page_templately' ) { | |
| 71 | + $editor = 'templately'; | |
| 72 | + } | |
| 73 | + | |
| 74 | + return $editor; | |
| 75 | + } | |
| 76 | + | |
| 77 | + public function enqueue_scripts( $hook = '' ) { | |
| 78 | + $categories = API::get_categories(); | |
| 79 | + global $current_screen; | |
| 80 | + $suffix = ! ( defined( 'TEMPLATELY_SCRIPT_DEBUG' ) && TEMPLATELY_SCRIPT_DEBUG ) ? '.min' : ''; | |
| 81 | + \wp_enqueue_script( | |
| 82 | + 'templately-editor', | |
| 83 | + TEMPLATELY_URL . "assets/js/templately{$suffix}.js", | |
| 84 | + [ 'wp-i18n', 'wp-element', 'wp-components' ], | |
| 85 | + \filemtime( TEMPLATELY_PATH . "assets/js/templately{$suffix}.js" ), $hook === 'admin' | |
| 86 | + ); | |
| 87 | + | |
| 88 | + \wp_set_script_translations( 'templately-editor', 'templately', TEMPLATELY_PATH . 'languages' ); | |
| 89 | + \wp_localize_script( 'templately-editor', 'TemplatelyAdmin', array ( | |
| 90 | + 'url' => \home_url( '/' ), | |
| 91 | + 'site_url' => \home_url( '/' ), | |
| 92 | + 'api_nonce' => \wp_create_nonce( 'wp_rest' ), | |
| 93 | + 'rest_url' => get_rest_url() . 'templately/v1/', | |
| 94 | + 'ip_address' => $this->get_ip(), | |
| 95 | + 'nonce' => \wp_create_nonce( 'templately_nonce' ), | |
| 96 | + 'platform' => $this->editor_platform(), | |
| 97 | + 'is_block_editor' => $current_screen->is_block_editor(), | |
| 98 | + 'is_elementor' => $this->is_elementor(), | |
| 99 | + 'is_editor' => $this->is_editor(), | |
| 100 | + 'is_admin' => is_admin(), | |
| 101 | + 'admin' => DB::get_user_specific_login_meta( '_templately_connect_data', [] ), | |
| 102 | + 'post_id' => get_the_ID(), | |
| 103 | + 'default_image' => \TEMPLATELY_ASSETS . 'images/cloud-item.svg', | |
| 104 | + 'not_found' => \TEMPLATELY_ASSETS . 'images/no-item-found.png', | |
| 105 | + 'no_items' => \TEMPLATELY_ASSETS . 'images/no-items.png', | |
| 106 | + 'loadingImage' => \TEMPLATELY_ASSETS . 'images/loading-logo.gif', | |
| 107 | + 'templates' => self::templates(), | |
| 108 | + 'reusable_blocks' => $this->reusable_blocks(), | |
| 109 | + 'is_logged_in' => DB::get_user_specific_login_meta( '_templately_connected' ), | |
| 110 | + 'user_can_logout' => Login::user_can_logout(), | |
| 111 | + 'is_global_login' => Login::already_signedin_globally(), | |
| 112 | + 'is_linked_login' => Login::get_link_my_account(), | |
| 113 | + 'cloud_map' => DB::get_option( '_templately_cloud_map', [] ), | |
| 114 | + 'categories' => array ( | |
| 115 | + 'blocks' => isset( $categories['item_categories'] ) ? $categories['item_categories'] : [], | |
| 116 | + 'packs' => isset( $categories['pack_categories'] ) ? $categories['pack_categories'] : [], | |
| 117 | + 'pages' => isset( $categories['page_categories'] ) ? $categories['page_categories'] : [] | |
| 118 | + ), | |
| 119 | + 'cloud_activity' => DB::get_user_specific_login_meta( '_templately_cloud_last_activity', array ( | |
| 120 | + 'labels' => 'My Clouds', | |
| 121 | + 'value' => 'clouds' | |
| 122 | + ) ), | |
| 123 | + 'platform_exists' => array ( | |
| 124 | + 'gutenberg' => $this->platform_exists( 'gutenberg' ), | |
| 125 | + 'elementor' => $this->platform_exists( 'elementor' ), | |
| 126 | + ), | |
| 127 | + 'current_url' => admin_url('admin.php?page=templately'), | |
| 128 | + 'prettyurl' => $this->prettyurl(), | |
| 129 | + 'items_count' => API::get_items_count() | |
| 130 | + ) ); | |
| 131 | + | |
| 132 | + if ( $hook !== 'admin' ) { | |
| 133 | + \wp_enqueue_script( | |
| 134 | + 'templately-frontend-editor', | |
| 135 | + TEMPLATELY_URL . "assets/js/editor{$suffix}.js", | |
| 136 | + [ 'jquery', 'wp-i18n' ], | |
| 137 | + \filemtime( TEMPLATELY_PATH . "assets/js/editor{$suffix}.js" ), | |
| 138 | + true | |
| 139 | + ); | |
| 140 | + \wp_set_script_translations( 'templately-frontend-editor', 'templately', TEMPLATELY_PATH . 'languages' ); | |
| 141 | + } | |
| 142 | + | |
| 143 | + \wp_enqueue_style( 'templately-poppins', | |
| 144 | + '//fonts.googleapis.com/css?family=Poppins:300,400,500,600,700', [], TEMPLATELY_VERSION | |
| 145 | + ); | |
| 146 | + if ( ! defined( 'TEMPLATELY_SCRIPT_DEBUG' ) || ( defined( 'TEMPLATELY_SCRIPT_DEBUG' ) && ! TEMPLATELY_SCRIPT_DEBUG ) ) { | |
| 147 | + \wp_enqueue_style( 'templately-style', | |
| 148 | + TEMPLATELY_URL . 'assets/css/templately.css', [ 'templately-poppins' ], TEMPLATELY_VERSION | |
| 149 | + ); | |
| 150 | + } | |
| 151 | + } | |
| 152 | + | |
| 153 | + public function prettyurl(){ | |
| 154 | + if( isset( $_GET['t_route'] ) && ! empty( $_GET['t_route'] ) ) { | |
| 155 | + $t_route = trim( $_GET['t_route'] ); | |
| 156 | + if( isset( $_GET['preview'] ) && $_GET['preview'] == true ) { | |
| 157 | + $t_route .= '?preview=true'; | |
| 158 | + } | |
| 159 | + | |
| 160 | + return $t_route; | |
| 161 | + } | |
| 162 | + return ''; | |
| 163 | + } | |
| 164 | + | |
| 165 | + /** | |
| 166 | + * Platform Exists or Not | |
| 167 | + * | |
| 168 | + * @return void | |
| 169 | + */ | |
| 170 | + protected function platform_exists( $platform = 'elementor' ) { | |
| 171 | + // $platform = isset( $_POST['platform'] ) ? trim( sanitize_text_field( $_POST['platform'] ) ) : 'elementor'; | |
| 172 | + if ( $platform === 'gutenberg' ) { | |
| 173 | + $wp_version = get_bloginfo( 'version' ); | |
| 174 | + if ( version_compare( $wp_version, '5.0.0', '>=' ) ) { | |
| 175 | + return true; | |
| 176 | + } else { | |
| 177 | + if ( \is_plugin_active( 'gutenberg/gutenberg.php' ) ) { | |
| 178 | + return true; | |
| 179 | + } else { | |
| 180 | + return false; | |
| 181 | + } | |
| 182 | + } | |
| 183 | + } | |
| 184 | + | |
| 185 | + if ( $platform === 'elementor' ) { | |
| 186 | + if ( \is_plugin_active( 'elementor/elementor.php' ) ) { | |
| 187 | + return true; | |
| 188 | + } else { | |
| 189 | + return false; | |
| 190 | + } | |
| 191 | + } | |
| 192 | + | |
| 193 | + return false; | |
| 194 | + } | |
| 195 | + | |
| 196 | + /** | |
| 197 | + * Get all saved templates from elementor library | |
| 198 | + * @return void | |
| 199 | + */ | |
| 200 | + public static function templates( $initial = true, $params = [] ) { | |
| 201 | + $args = array ( | |
| 202 | + 'post_type' => 'elementor_library', | |
| 203 | + 'numberposts' => 10, | |
| 204 | + 'paged' => 1, | |
| 205 | + ); | |
| 206 | + if ( ! $initial ) { | |
| 207 | + $args['paged'] = isset( $params['page'] ) ? $params['page'] : 1; | |
| 208 | + } | |
| 209 | + $templates = new \WP_Query( $args ); | |
| 210 | + $templates_list = $templates->posts; | |
| 211 | + $templateList = array (); | |
| 212 | + | |
| 213 | + if ( count( $templates_list ) > 0 ) : | |
| 214 | + $date_format = get_option( 'date_format', 'F j, Y' ); | |
| 215 | + foreach ( $templates_list as $post ) { | |
| 216 | + $templateList[] = array ( | |
| 217 | + 'id' => $post->ID, | |
| 218 | + 'title' => $post->post_title, | |
| 219 | + 'date' => date( $date_format, strtotime( $post->post_date ) ), | |
| 220 | + 'type' => ucwords( \get_post_meta( $post->ID, '_elementor_template_type', true ) ), | |
| 221 | + 'created_by' => \get_the_author_meta( 'user_nicename', $post->post_author ), | |
| 222 | + 'preview_url' => \get_permalink( $post->ID ), | |
| 223 | + 'export_url' => self::get_export_link( $post->ID ), | |
| 224 | + ); | |
| 225 | + } | |
| 226 | + endif; | |
| 227 | + wp_reset_postdata(); | |
| 228 | + wp_reset_query(); | |
| 229 | + | |
| 230 | + return array ( | |
| 231 | + 'current_page' => $templates->query_vars['paged'], | |
| 232 | + 'total_page' => $templates->max_num_pages, | |
| 233 | + 'items' => $templateList, | |
| 234 | + ); | |
| 235 | + } | |
| 236 | + | |
| 237 | + /** | |
| 238 | + * Get all saved templates from elementor library | |
| 239 | + * @return void | |
| 240 | + */ | |
| 241 | + public function reusable_blocks() { | |
| 242 | + $templates = new \WP_Query( array ( | |
| 243 | + 'post_type' => 'wp_block' | |
| 244 | + ) ); | |
| 245 | + $templateList = array (); | |
| 246 | + if ( $templates->have_posts() ) : | |
| 247 | + $date_format = get_option( 'date_format', 'F j, Y' ); | |
| 248 | + foreach ( $templates->posts as $post ) { | |
| 249 | + $templateList[] = array ( | |
| 250 | + 'id' => $post->ID, | |
| 251 | + 'title' => $post->post_title, | |
| 252 | + 'date' => date( $date_format, strtotime( $post->post_date ) ), | |
| 253 | + 'type' => '', | |
| 254 | + 'created_by' => \get_the_author_meta( 'user_nicename', $post->post_author ), | |
| 255 | + 'preview_url' => '', | |
| 256 | + 'export_url' => '', | |
| 257 | + ); | |
| 258 | + } | |
| 259 | + endif; | |
| 260 | + wp_reset_postdata(); | |
| 261 | + wp_reset_query(); | |
| 262 | + | |
| 263 | + return $templateList; | |
| 264 | + } | |
| 265 | + | |
| 266 | + /** | |
| 267 | + * From Elementor It Self! | |
| 268 | + * | |
| 269 | + * @param template id $template_id | |
| 270 | + * | |
| 271 | + * @return void | |
| 272 | + */ | |
| 273 | + private static function get_export_link( $template_id ) { | |
| 274 | + return \add_query_arg( | |
| 275 | + [ | |
| 276 | + 'action' => 'elementor_library_direct_actions', | |
| 277 | + 'library_action' => 'export_template', | |
| 278 | + 'source' => 'local', | |
| 279 | + '_nonce' => \wp_create_nonce( 'elementor_ajax' ), | |
| 280 | + 'template_id' => $template_id, | |
| 281 | + ], | |
| 282 | + \admin_url( 'admin-ajax.php' ) | |
| 283 | + ); | |
| 284 | + } | |
| 285 | +} | |