| 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 |
'ip_address' => $this->get_ip(), |
| 93 |
'nonce' => \wp_create_nonce( 'templately_nonce' ), |
| 94 |
'platform' => $this->editor_platform(), |
| 95 |
'is_block_editor' => $current_screen->is_block_editor(), |
| 96 |
'is_elementor' => $this->is_elementor(), |
| 97 |
'is_editor' => $this->is_editor(), |
| 98 |
'is_admin' => is_admin(), |
| 99 |
'admin' => DB::get_user_specific_login_meta( '_templately_connect_data', [] ), |
| 100 |
'post_id' => get_the_ID(), |
| 101 |
'default_image' => \TEMPLATELY_ASSETS . 'images/cloud-item.svg', |
| 102 |
'not_found' => \TEMPLATELY_ASSETS . 'images/no-item-found.png', |
| 103 |
'no_items' => \TEMPLATELY_ASSETS . 'images/no-items.png', |
| 104 |
'loadingImage' => \TEMPLATELY_ASSETS . 'images/loading-logo.gif', |
| 105 |
'templates' => self::templates(), |
| 106 |
'reusable_blocks' => $this->reusable_blocks(), |
| 107 |
'is_logged_in' => DB::get_user_specific_login_meta( '_templately_connected' ), |
| 108 |
'user_can_logout' => Login::user_can_logout(), |
| 109 |
'is_global_login' => Login::already_signedin_globally(), |
| 110 |
'is_linked_login' => Login::get_link_my_account(), |
| 111 |
'cloud_map' => DB::get_option( '_templately_cloud_map', [] ), |
| 112 |
'categories' => array ( |
| 113 |
'blocks' => isset( $categories['item_categories'] ) ? $categories['item_categories'] : [], |
| 114 |
'packs' => isset( $categories['pack_categories'] ) ? $categories['pack_categories'] : [], |
| 115 |
'pages' => isset( $categories['page_categories'] ) ? $categories['page_categories'] : [] |
| 116 |
), |
| 117 |
'cloud_activity' => DB::get_user_specific_login_meta( '_templately_cloud_last_activity', array ( |
| 118 |
'labels' => 'My Clouds', |
| 119 |
'value' => 'clouds' |
| 120 |
) ), |
| 121 |
'platform_exists' => array ( |
| 122 |
'gutenberg' => $this->platform_exists( 'gutenberg' ), |
| 123 |
'elementor' => $this->platform_exists( 'elementor' ), |
| 124 |
) |
| 125 |
) ); |
| 126 |
|
| 127 |
if ( $hook !== 'admin' ) { |
| 128 |
\wp_enqueue_script( |
| 129 |
'templately-frontend-editor', |
| 130 |
TEMPLATELY_URL . "assets/js/editor{$suffix}.js", |
| 131 |
[ 'jquery', 'wp-i18n' ], |
| 132 |
\filemtime( TEMPLATELY_PATH . "assets/js/editor{$suffix}.js" ), |
| 133 |
true |
| 134 |
); |
| 135 |
\wp_set_script_translations( 'templately-frontend-editor', 'templately', TEMPLATELY_PATH . 'languages' ); |
| 136 |
} |
| 137 |
|
| 138 |
\wp_enqueue_style( 'templately-poppins', |
| 139 |
'//fonts.googleapis.com/css?family=Poppins:300,400,500,600,700', [], TEMPLATELY_VERSION |
| 140 |
); |
| 141 |
if ( ! defined( 'TEMPLATELY_SCRIPT_DEBUG' ) || ( defined( 'TEMPLATELY_SCRIPT_DEBUG' ) && ! TEMPLATELY_SCRIPT_DEBUG ) ) { |
| 142 |
\wp_enqueue_style( 'templately-style', |
| 143 |
TEMPLATELY_URL . 'assets/css/templately.css', [ 'templately-poppins' ], TEMPLATELY_VERSION |
| 144 |
); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Platform Exists or Not |
| 150 |
* |
| 151 |
* @return void |
| 152 |
*/ |
| 153 |
protected function platform_exists( $platform = 'elementor' ) { |
| 154 |
// $platform = isset( $_POST['platform'] ) ? trim( sanitize_text_field( $_POST['platform'] ) ) : 'elementor'; |
| 155 |
if ( $platform === 'gutenberg' ) { |
| 156 |
$wp_version = get_bloginfo( 'version' ); |
| 157 |
if ( version_compare( $wp_version, '5.0.0', '>=' ) ) { |
| 158 |
return true; |
| 159 |
} else { |
| 160 |
if ( \is_plugin_active( 'gutenberg/gutenberg.php' ) ) { |
| 161 |
return true; |
| 162 |
} else { |
| 163 |
return false; |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
if ( $platform === 'elementor' ) { |
| 169 |
if ( \is_plugin_active( 'elementor/elementor.php' ) ) { |
| 170 |
return true; |
| 171 |
} else { |
| 172 |
return false; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
return false; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Get all saved templates from elementor library |
| 181 |
* @return void |
| 182 |
*/ |
| 183 |
public static function templates( $initial = true, $params = [] ) { |
| 184 |
$args = array ( |
| 185 |
'post_type' => 'elementor_library', |
| 186 |
'numberposts' => 10, |
| 187 |
'paged' => 1, |
| 188 |
); |
| 189 |
if ( ! $initial ) { |
| 190 |
$args['paged'] = isset( $params['page'] ) ? $params['page'] : 1; |
| 191 |
} |
| 192 |
$templates = new \WP_Query( $args ); |
| 193 |
$templates_list = $templates->posts; |
| 194 |
$templateList = array (); |
| 195 |
|
| 196 |
if ( count( $templates_list ) > 0 ) : |
| 197 |
$date_format = get_option( 'date_format', 'F j, Y' ); |
| 198 |
foreach ( $templates_list as $post ) { |
| 199 |
$templateList[] = array ( |
| 200 |
'id' => $post->ID, |
| 201 |
'title' => $post->post_title, |
| 202 |
'date' => date( $date_format, strtotime( $post->post_date ) ), |
| 203 |
'type' => ucwords( \get_post_meta( $post->ID, '_elementor_template_type', true ) ), |
| 204 |
'created_by' => \get_the_author_meta( 'user_nicename', $post->post_author ), |
| 205 |
'preview_url' => \get_permalink( $post->ID ), |
| 206 |
'export_url' => self::get_export_link( $post->ID ), |
| 207 |
); |
| 208 |
} |
| 209 |
endif; |
| 210 |
wp_reset_postdata(); |
| 211 |
wp_reset_query(); |
| 212 |
|
| 213 |
return array ( |
| 214 |
'current_page' => $templates->query_vars['paged'], |
| 215 |
'total_page' => $templates->max_num_pages, |
| 216 |
'items' => $templateList, |
| 217 |
); |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Get all saved templates from elementor library |
| 222 |
* @return void |
| 223 |
*/ |
| 224 |
public function reusable_blocks() { |
| 225 |
$templates = new \WP_Query( array ( |
| 226 |
'post_type' => 'wp_block' |
| 227 |
) ); |
| 228 |
$templateList = array (); |
| 229 |
if ( $templates->have_posts() ) : |
| 230 |
$date_format = get_option( 'date_format', 'F j, Y' ); |
| 231 |
foreach ( $templates->posts as $post ) { |
| 232 |
$templateList[] = array ( |
| 233 |
'id' => $post->ID, |
| 234 |
'title' => $post->post_title, |
| 235 |
'date' => date( $date_format, strtotime( $post->post_date ) ), |
| 236 |
'type' => '', |
| 237 |
'created_by' => \get_the_author_meta( 'user_nicename', $post->post_author ), |
| 238 |
'preview_url' => '', |
| 239 |
'export_url' => '', |
| 240 |
); |
| 241 |
} |
| 242 |
endif; |
| 243 |
wp_reset_postdata(); |
| 244 |
wp_reset_query(); |
| 245 |
|
| 246 |
return $templateList; |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* From Elementor It Self! |
| 251 |
* |
| 252 |
* @param template id $template_id |
| 253 |
* |
| 254 |
* @return void |
| 255 |
*/ |
| 256 |
private static function get_export_link( $template_id ) { |
| 257 |
return \add_query_arg( |
| 258 |
[ |
| 259 |
'action' => 'elementor_library_direct_actions', |
| 260 |
'library_action' => 'export_template', |
| 261 |
'source' => 'local', |
| 262 |
'_nonce' => \wp_create_nonce( 'elementor_ajax' ), |
| 263 |
'template_id' => $template_id, |
| 264 |
], |
| 265 |
\admin_url( 'admin-ajax.php' ) |
| 266 |
); |
| 267 |
} |
| 268 |
} |