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