| 1 |
<?php |
| 2 |
namespace Templately\Core; |
| 3 |
|
| 4 |
use Exception; |
| 5 |
use PriyoMukul\WPNotice\Notices; |
| 6 |
use PriyoMukul\WPNotice\Utils\CacheBank; |
| 7 |
use PriyoMukul\WPNotice\Utils\NoticeRemover; |
| 8 |
use Templately\API\Login; |
| 9 |
use Templately\Utils\Base; |
| 10 |
use Templately\Utils\Helper; |
| 11 |
use Templately\Utils\Options; |
| 12 |
use Templately\Core\Platform\Elementor; |
| 13 |
use Templately\Core\Platform\Gutenberg; |
| 14 |
|
| 15 |
class Admin extends Base { |
| 16 |
|
| 17 |
/** |
| 18 |
* @var CacheBank |
| 19 |
*/ |
| 20 |
private static $cache_bank; |
| 21 |
|
| 22 |
/** |
| 23 |
* Initially invoked function. |
| 24 |
* Menu, Assets and maybe redirect on plugin activation is initialized. |
| 25 |
*/ |
| 26 |
public function __construct() { |
| 27 |
add_action( 'admin_enqueue_scripts', [ $this, 'scripts' ] ); |
| 28 |
add_action( 'admin_menu', [ $this, 'admin_menu' ] ); |
| 29 |
|
| 30 |
self::$cache_bank = CacheBank::get_instance(); |
| 31 |
|
| 32 |
try { |
| 33 |
add_action( 'admin_init', [ $this, 'notices' ], 11 ); |
| 34 |
} catch ( Exception $e ) { |
| 35 |
unset( $e ); |
| 36 |
} |
| 37 |
|
| 38 |
add_filter( 'plugin_action_links_' . TEMPLATELY_PLUGIN_BASENAME, [$this, 'handleActionLinks'], 10, 2 ); |
| 39 |
|
| 40 |
// Remove OLD notice from 1.0.0 (if other WPDeveloper plugin has notice) |
| 41 |
NoticeRemover::get_instance( '1.0.0' ); |
| 42 |
} |
| 43 |
|
| 44 |
public function register_post_type() { |
| 45 |
$labels = [ |
| 46 |
'name' => _x( 'Theme Builders', 'Post type general name', 'templately' ), |
| 47 |
'singular_name' => _x( 'Theme Builder', 'Post type singular name', 'templately' ), |
| 48 |
'menu_name' => _x( 'Theme Builder', 'Admin Menu text', 'templately' ), |
| 49 |
'name_admin_bar' => _x( 'Book', 'Add New on Toolbar', 'templately' ), |
| 50 |
'add_new' => __( 'Add New', 'templately' ), |
| 51 |
'add_new_item' => __( 'Add New Template', 'templately' ), |
| 52 |
'new_item' => __( 'New Template', 'templately' ), |
| 53 |
'edit_item' => __( 'Edit Template', 'templately' ), |
| 54 |
'view_item' => __( 'View Template', 'templately' ), |
| 55 |
'all_items' => __( 'All Templates', 'templately' ), |
| 56 |
'search_items' => __( 'Search Templates', 'templately' ), |
| 57 |
'parent_item_colon' => __( 'Parent Templates:', 'templately' ), |
| 58 |
'not_found' => __( 'No templates found.', 'templately' ), |
| 59 |
'not_found_in_trash' => __( 'No templates found in Trash.', 'templately' ), |
| 60 |
'featured_image' => _x( 'Template Cover Image', 'Overrides the “Featured Image” phrase for this post type. Added in 4.3', 'templately' ), |
| 61 |
'set_featured_image' => _x( 'Set cover image', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3', 'templately' ), |
| 62 |
'remove_featured_image' => _x( 'Remove cover image', 'Overrides the “Remove featured image” phrase for this post type. Added in 4.3', 'templately' ), |
| 63 |
'use_featured_image' => _x( 'Use as cover image', 'Overrides the “Use as featured image” phrase for this post type. Added in 4.3', 'templately' ), |
| 64 |
'archives' => _x( 'Template archives', 'The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4', 'templately' ), |
| 65 |
'insert_into_item' => _x( /** @lang text */ "Insert Into Theme Builder", 'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'templately' ), |
| 66 |
'uploaded_to_this_item' => _x( 'Uploaded to this template', 'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'templately' ), |
| 67 |
'filter_items_list' => _x( 'Filter templates list', 'Screen reader text for the filter links heading on the post type listing screen. Default “Filter posts list”/”Filter pages list”. Added in 4.4', 'templately' ), |
| 68 |
'items_list_navigation' => _x( 'Templates list navigation', 'Screen reader text for the pagination heading on the post type listing screen. Default “Posts list navigation”/”Pages list navigation”. Added in 4.4', 'templately' ), |
| 69 |
'items_list' => _x( 'Templates list', 'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4', 'templately' ), |
| 70 |
]; |
| 71 |
|
| 72 |
$args = [ |
| 73 |
'labels' => $labels, |
| 74 |
'public' => true, |
| 75 |
'publicly_queryable' => true, |
| 76 |
'show_ui' => true, |
| 77 |
'show_in_menu' => false, |
| 78 |
'query_var' => true, |
| 79 |
'rewrite' => [ 'slug' => 'templately-library' ], |
| 80 |
'capability_type' => 'post', |
| 81 |
'has_archive' => true, |
| 82 |
'hierarchical' => false, |
| 83 |
'menu_position' => 10, |
| 84 |
'supports' => [ 'title', 'editor', 'author', 'thumbnail', 'custom-field' ], |
| 85 |
]; |
| 86 |
|
| 87 |
register_post_type( 'templately_library', $args ); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Enqueuing Assets |
| 92 |
* |
| 93 |
* @param string $hook |
| 94 |
* @return void |
| 95 |
*/ |
| 96 |
public function scripts( string $hook ) { |
| 97 |
if ( ! in_array( $hook, [ 'edit.php', 'toplevel_page_templately', 'elementor', 'gutenberg' ], true ) ) { |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
$templately = []; |
| 102 |
|
| 103 |
if ( 'edit.php' === $hook ) { |
| 104 |
global $current_screen; |
| 105 |
if ( $current_screen->post_type !== 'templately_library' ) { |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
$types = templately()->theme_builder::$templates_manager->get_template_types(); |
| 110 |
$types = array_filter( $types, function ( $item ) { |
| 111 |
return $item::get_property( 'builder' ) || $item::get_property( 'builder' ) === null; |
| 112 |
} ); |
| 113 |
|
| 114 |
$types = array_reduce( $types, function ( $carry, $item ) { |
| 115 |
return array_merge( $carry, [ |
| 116 |
[ |
| 117 |
'value' => call_user_func( [ $item, 'get_type' ] ), |
| 118 |
'label' => call_user_func( [ $item, 'get_title' ] ) |
| 119 |
] |
| 120 |
] ); |
| 121 |
}, [] ); |
| 122 |
|
| 123 |
$templately = [ |
| 124 |
'types' => $types |
| 125 |
]; |
| 126 |
} |
| 127 |
|
| 128 |
$script_dependencies = ['regenerator-runtime']; |
| 129 |
$_localize_handle = 'templately'; |
| 130 |
$_current_screen = 'templately'; |
| 131 |
|
| 132 |
if ( $hook === 'elementor' || $hook === 'gutenberg' ) { |
| 133 |
$_current_screen = $hook; |
| 134 |
$_localize_handle = 'templately-' . $hook; |
| 135 |
$script_dependencies[] = $_localize_handle; |
| 136 |
} |
| 137 |
|
| 138 |
if ( $hook === 'toplevel_page_templately' || $hook == 'edit.php' ) { |
| 139 |
templately()->assets->enqueue( 'templately-admin', 'css/admin.css', [ 'templately' ] ); |
| 140 |
} |
| 141 |
|
| 142 |
// Google Font Enqueueing |
| 143 |
templately()->assets->enqueue( |
| 144 |
'templately-dmsans', |
| 145 |
set_url_scheme( '//fonts.googleapis.com/css2?family=DM+Sans:ital,wght@0,400;0,500;0,700;1,400;1,500;1,700&display=swap' ) |
| 146 |
); |
| 147 |
|
| 148 |
wp_enqueue_media(); |
| 149 |
templately()->assets->enqueue( 'templately', 'js/templately.js', $script_dependencies, true ); |
| 150 |
templately()->assets->enqueue( 'templately', 'css/templately.css', ['templately-dmsans'] ); |
| 151 |
|
| 152 |
/** |
| 153 |
* @var Elementor|Gutenberg $platform |
| 154 |
*/ |
| 155 |
$platform = $this->platform( $_current_screen ); |
| 156 |
|
| 157 |
$templately = array_merge( [ |
| 158 |
'url' => home_url(), |
| 159 |
'site_url' => site_url(), |
| 160 |
'nonce' => wp_create_nonce( 'templately_nonce' ), |
| 161 |
'rest_args' => [ |
| 162 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 163 |
'endpoint' => get_rest_url( null, 'templately/v1/' ) |
| 164 |
], |
| 165 |
'log' => defined( 'TEMPLATELY_DEBUG_LOG' ) && TEMPLATELY_DEBUG_LOG, |
| 166 |
'dev_mode' => defined( 'TEMPLATELY_DEV' ) && TEMPLATELY_DEV, |
| 167 |
"icons" => [ |
| 168 |
'construction' => templately()->assets->icon( 'icons/construction.gif' ), |
| 169 |
'profile' => templately()->assets->icon( 'icons/profile.svg' ), |
| 170 |
'warning' => templately()->assets->icon( 'icons/warning.png' ) |
| 171 |
], |
| 172 |
'promo_image' => templately()->assets->icon( 'single-page-promo.png' ), |
| 173 |
'default_image' => templately()->assets->icon( 'clouds/cloud-item.svg' ), |
| 174 |
'not_found' => templately()->assets->icon( 'no-item-found.png' ), |
| 175 |
'no_items' => templately()->assets->icon( 'no-items.png' ), |
| 176 |
'loadingImage' => templately()->assets->icon( 'logos/loading-logo.gif' ), |
| 177 |
'current_url' => admin_url( 'admin.php?page=templately' ), |
| 178 |
'is_signed' => Login::is_signed(), |
| 179 |
'is_globally_signed' => Login::is_globally_signed(), |
| 180 |
'signed_as_global' => Login::signed_as_global(), |
| 181 |
'current_screen' => $_current_screen, |
| 182 |
'post_type' => get_post_type(), |
| 183 |
'has_elementor' => rest_sanitize_boolean( is_plugin_active( 'elementor/elementor.php' ) ), |
| 184 |
'has_elementor_pro' => rest_sanitize_boolean( is_plugin_active( 'elementor-pro/elementor-pro.php' ) ), |
| 185 |
'theme' => $_current_screen == 'templately' ? 'light' : $platform->ui_theme(), |
| 186 |
'is_wp_support_gutenberg' => version_compare( get_bloginfo( 'version' ), '5.0.0', '>=' ), |
| 187 |
], $templately ); |
| 188 |
|
| 189 |
templately()->assets->localize( $_localize_handle, 'templately', $templately ); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Admin notices for Review and others. |
| 194 |
* |
| 195 |
* @since 2.0.0 |
| 196 |
* @return void |
| 197 |
* @throws Exception |
| 198 |
* @since 2.0.0 |
| 199 |
*/ |
| 200 |
public function notices() { |
| 201 |
$notices = new Notices( [ |
| 202 |
'id' => 'templately', |
| 203 |
'storage_key' => 'notices', |
| 204 |
'lifetime' => 3, |
| 205 |
'stylesheet_url' => TEMPLATELY_ASSETS . 'css/notices.css', |
| 206 |
'styles' => TEMPLATELY_ASSETS . 'css/notices.css', |
| 207 |
'priority' => 2, |
| 208 |
] ); |
| 209 |
|
| 210 |
$global_user = Options::get_instance()->get( 'user', false, get_current_user_id() ); |
| 211 |
$download_counts = Options::get_instance()->get( 'total_download_counts', 0, get_current_user_id() ); |
| 212 |
$cloud_items = 0; |
| 213 |
if ( isset( $global_user['my_cloud']['usages'] ) ) { |
| 214 |
$cloud_items = intval( $global_user['my_cloud']['usages'] ); |
| 215 |
} |
| 216 |
|
| 217 |
if ( $cloud_items >= 5 || $download_counts >= 4 ) { |
| 218 |
$message = sprintf( __( "We hope you're enjoying %s! Could you please do us a favor and give us a review on %s to help us spread the word and boost our motivation?", 'templately' ), '<strong>Templately</strong>', '<strong>WordPress</strong>' ); |
| 219 |
|
| 220 |
$_review_notice = [ |
| 221 |
'thumbnail' => templately()->assets->icon( 'logos/logo.svg' ), |
| 222 |
'html' => '<p>' . $message . '</p>', |
| 223 |
'links' => [ |
| 224 |
'later' => [ |
| 225 |
'link' => 'https://wordpress.org/support/plugin/templately/reviews/#new-post', |
| 226 |
'target' => '_blank', |
| 227 |
'label' => __( 'Sure, you deserve it!', 'templately' ), |
| 228 |
'icon_class' => 'dashicons dashicons-external' |
| 229 |
], |
| 230 |
'allready' => [ |
| 231 |
'label' => __( 'I already did', 'templately' ), |
| 232 |
'icon_class' => 'dashicons dashicons-smiley', |
| 233 |
'attributes' => [ |
| 234 |
'data-dismiss' => true |
| 235 |
] |
| 236 |
], |
| 237 |
'maybe_later' => [ |
| 238 |
'label' => __( 'Maybe Later', 'templately' ), |
| 239 |
'icon_class' => 'dashicons dashicons-calendar-alt', |
| 240 |
'attributes' => [ |
| 241 |
'data-later' => true, |
| 242 |
'class' => 'dismiss-btn' |
| 243 |
] |
| 244 |
], |
| 245 |
'support' => [ |
| 246 |
'link' => 'https://wpdeveloper.com/support', |
| 247 |
'attributes' => [ |
| 248 |
'target' => '_blank' |
| 249 |
], |
| 250 |
'label' => __( 'I need help', 'templately' ), |
| 251 |
'icon_class' => 'dashicons dashicons-sos' |
| 252 |
], |
| 253 |
'never_show_again' => [ |
| 254 |
'label' => __( 'Never show again', 'templately' ), |
| 255 |
'icon_class' => 'dashicons dashicons-dismiss', |
| 256 |
'attributes' => [ |
| 257 |
'data-dismiss' => true |
| 258 |
] |
| 259 |
] |
| 260 |
] |
| 261 |
]; |
| 262 |
|
| 263 |
$notices->add( 'review', $_review_notice, [ |
| 264 |
'start' => $notices->strtotime(), |
| 265 |
'recurrence' => 20, |
| 266 |
'dismissible' => true, |
| 267 |
'refresh' => TEMPLATELY_VERSION, |
| 268 |
'screens' => [ |
| 269 |
'dashboard', |
| 270 |
'plugins', |
| 271 |
'themes', |
| 272 |
'edit-page', |
| 273 |
'edit-post', |
| 274 |
'users', |
| 275 |
'tools', |
| 276 |
'options-general', |
| 277 |
'nav-menus' |
| 278 |
] |
| 279 |
] ); |
| 280 |
} |
| 281 |
|
| 282 |
if ( $global_user === false || isset( $global_user['plan'] ) && $global_user['plan'] == 'free' ) { |
| 283 |
$notices->add( 'upsale', wp_sprintf( '<p>%1$s <a target="_blank" href="%3$s">%2$s</a>.</p>', __( '🔥 Get access to 5,000+ Ready Templates & save up to 65% OFF now', 'templately' ), __( 'Upgrade to Pro', 'templately' ), 'https://templately.com/#pricing' ), [ |
| 284 |
'start' => $notices->strtotime( '+10 day' ), |
| 285 |
'dismissible' => true, |
| 286 |
'refresh' => TEMPLATELY_VERSION, |
| 287 |
'screens' => [ |
| 288 |
'dashboard', |
| 289 |
'plugins', |
| 290 |
'themes', |
| 291 |
'edit-page', |
| 292 |
'edit-post', |
| 293 |
'users', |
| 294 |
'tools', |
| 295 |
'options-general', |
| 296 |
'nav-menus' |
| 297 |
] |
| 298 |
] ); |
| 299 |
|
| 300 |
$notice_text = '<p style="margin-top: 0; margin-bottom: 10px;">Black Friday Sale: Save up to 70% and <strong>get access to 5000+ ready templates</strong> to design amazing websites ✨</p> |
| 301 |
<a class="button button-primary" href="https://wpdeveloper.com/upgrade/templately-bfcm" target="_blank">Upgrade to pro</a> <button data-dismiss="true" class="dismiss-btn button button-link">I don’t want to save money</button>'; |
| 302 |
|
| 303 |
$_black_friday = [ |
| 304 |
'thumbnail' => templately()->assets->icon( 'logos/logo-full.svg' ), |
| 305 |
'html' => $notice_text, |
| 306 |
]; |
| 307 |
|
| 308 |
$notices->add( 'black_friday', $_black_friday, [ |
| 309 |
'start' => $notices->time(), |
| 310 |
'recurrence' => false, |
| 311 |
'dismissible' => true, |
| 312 |
'refresh' => TEMPLATELY_VERSION, |
| 313 |
"expire" => strtotime( '11:59:59pm 2nd December, 2023' ), |
| 314 |
] ); |
| 315 |
} |
| 316 |
|
| 317 |
self::$cache_bank->create_account( $notices ); |
| 318 |
self::$cache_bank->calculate_deposits( $notices ); |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* Adding Menu In Sidebar ( WordPress Left-side Dashboard Menu ) |
| 323 |
* |
| 324 |
* @return void |
| 325 |
*/ |
| 326 |
public function admin_menu() { |
| 327 |
// TODO: Role Management |
| 328 |
|
| 329 |
add_menu_page( 'Templately', 'Templately', 'delete_posts', 'templately', '', templately()->assets->icon( 'logos/logo-icon.svg' ), '58.7' ); |
| 330 |
|
| 331 |
add_submenu_page( 'templately', 'Templately', 'Template Library', 'delete_posts', 'templately', [ |
| 332 |
$this, |
| 333 |
'display' |
| 334 |
], '58.7' ); |
| 335 |
|
| 336 |
add_submenu_page( 'templately', 'Theme Builder', 'Theme Builder', 'delete_posts', 'edit.php?post_type=templately_library', '', '58.7' ); |
| 337 |
|
| 338 |
// add_submenu_page( 'templately', 'Settings', 'Settings', 'administrator', 'templately-settings', [ |
| 339 |
// Settings::get_instance(), |
| 340 |
// 'display' |
| 341 |
// ], '58.7' ); |
| 342 |
} |
| 343 |
|
| 344 |
public function display() { |
| 345 |
Helper::views( 'settings' ); |
| 346 |
} |
| 347 |
|
| 348 |
/** |
| 349 |
* If Elementor doesn't exists. |
| 350 |
* |
| 351 |
* @return void |
| 352 |
*/ |
| 353 |
public static function has_no_elementor() { |
| 354 |
$plugin_url = \wp_nonce_url( \self_admin_url( 'update.php?action=install-plugin&plugin=elementor' ), 'install-plugin_elementor' ); |
| 355 |
$button_text = 'Install Elementor'; |
| 356 |
if ( isset( Helper::get_plugins()['elementor/elementor.php'] ) ) { |
| 357 |
$plugin_url = \wp_nonce_url( 'plugins.php?action=activate&plugin=elementor/elementor.php', 'activate-plugin_elementor/elementor.php' ); |
| 358 |
$button_text = 'Activate Elementor'; |
| 359 |
} |
| 360 |
$output = '<div class="notice notice-error">'; |
| 361 |
$output .= sprintf( |
| 362 |
"<p><strong>%s</strong> %s <strong>%s</strong> %s <a class='button-primary' href='%s'>%s</a></p>", |
| 363 |
__( 'Templately', 'templately' ), |
| 364 |
__( 'requires', 'templately' ), |
| 365 |
__( 'Elementor', 'templately' ), |
| 366 |
__( 'plugin to be installed and activated. Please install Elementor to continue.', 'templately' ), |
| 367 |
esc_url( $plugin_url ), |
| 368 |
__( $button_text, 'templately' ) |
| 369 |
); |
| 370 |
$output .= '</div>'; |
| 371 |
echo $output; |
| 372 |
} |
| 373 |
|
| 374 |
public function header() { |
| 375 |
Helper::views( 'header' ); |
| 376 |
} |
| 377 |
|
| 378 |
/** |
| 379 |
* Handle links displayed below the plugin name in the WordPress Installed Plugins page. |
| 380 |
* |
| 381 |
* @return array |
| 382 |
* @since 3.1.2 |
| 383 |
* @static |
| 384 |
* |
| 385 |
*/ |
| 386 |
public static function handleActionLinks($links, $file) |
| 387 |
{ |
| 388 |
$settingsLink = '<a href="' . admin_url('admin.php?page=templately') . '" aria-label="' . __('Open settings page', |
| 389 |
'templately') . '">' . __('Templates Library', 'templately') . '</a>'; |
| 390 |
|
| 391 |
$links[] = $settingsLink; |
| 392 |
return $links; |
| 393 |
} |
| 394 |
} |
| 395 |
|