PluginProbe
Swatchly – Product Variation Swatches for WooCommerce / trunk
Swatchly – Product Variation Swatches for WooCommerce vtrunk
1.4.16 1.4.15 1.4.14 trunk 1.0.5 1.0.7 1.0.9 1.1.0 1.1.1 1.1.2 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 All 49 releases
swatchly / includes / Admin.php

Admin.php in Swatchly – Product Variation Swatches for WooCommerce trunk, at includes/Admin.php

264 lines 12.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Swatchly;
3
4 /**
5 * Admin class.
6 */
7 class Admin {
8 public $version = '';
9 /**
10 * Constructor.
11 */
12 public function __construct() {
13 // Set time as the version for development mode.
14 if( defined('WP_DEBUG') && WP_DEBUG ){
15 $this->version = time();
16 } else {
17 $this->version = SWATCHLY_VERSION;
18 }
19 new Admin\Woo_Config();
20 new Admin\Attribute_Taxonomy_Metabox();
21 new Admin\Product_Metabox();
22 new Admin\Install_Manager\Install_Manager();
23
24
25 // Add a top-level custom menu called "Swatchly" for the plugin
26 add_action( 'admin_menu', array( $this, 'add_top_level_menu' ), 10 );
27 add_action( 'admin_menu', array( $this, 'dashboard_menu_tweaks' ), 30 );
28 add_action( 'admin_menu', array( $this, 'upgrade_menu_tweaks' ), 1000 );
29 add_action( 'admin_footer', [ $this, 'enqueue_admin_head_scripts'], 11 );
30
31 // Bind admin page link to the plugin action link.
32 add_filter( 'plugin_action_links_swatchly/swatchly.php', array($this, 'action_links_add'), 10, 4 );
33
34 // Admin assets hook into action.
35 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_assets' ) );
36
37 add_action('admin_footer', array( $this, 'pro_version_notice' ));
38 add_action('admin_head', array( $this, 'admin_rating_notice' ));
39 add_action('admin_head', [$this, 'show_promo_notice']);
40 }
41
42 function show_promo_notice() {
43
44 // remove on next update
45 if(!get_option('force_update_swatchly_notice_info')) {
46 delete_transient('swatchly_notice_info');
47 update_option('force_update_swatchly_notice_info', true);
48 }
49
50 $noticeManager = \Swatchly_Notice_Manager::instance();
51 $notices = $noticeManager->get_notices_info();
52 if(!empty($notices)) {
53 foreach ($notices as $notice) {
54 if(empty($notice['disable'])) {
55 \Swatchly_Notices::set_notice($notice);
56 }
57 }
58 }
59 }
60
61 public function admin_rating_notice() {
62 if(get_option('swatchly_rating_already_rated', false)) {
63 return;
64 }
65 $message = '<div class="swatchly-review-notice-wrap">
66 <div class="swatchly-rating-notice-logo">
67 <img src="' . esc_url(SWATCHLY_URL . "/assets/images/logo.png") . '" alt="'.esc_attr__('Swatchly','swatchly').'" style="max-width:110px"/>
68 </div>
69 <div class="swatchly-review-notice-content">
70 <h3>'.esc_html__('Thank you for choosing Swatchly WooCommerce Variation Swatches for Products to design your website!','swatchly').'</h3>
71 <p>'.esc_html__('Would you mind doing us a huge favor by providing your feedback on WordPress? Your support helps us spread the word and greatly boosts our motivation.','swatchly').'</p>
72 <div class="swatchly-review-notice-action">
73 <a href="https://wordpress.org/support/plugin/swatchly/reviews/?filter=5#new-post" class="swatchly-review-notice button-primary" target="_blank">'.esc_html__('Ok, you deserve it!','swatchly').'</a>
74 <span class="dashicons dashicons-calendar"></span>
75 <a href="#" class="swatchly-notice-close swatchly-review-notice">'.esc_html__('Maybe Later','swatchly').'</a>
76 <span class="dashicons dashicons-smiley"></span>
77 <a href="#" data-already-did="yes" class="swatchly-notice-close swatchly-review-notice">'.esc_html__('I already did','swatchly').'</a>
78 </div>
79 </div>
80 </div>';
81
82 \Swatchly_Notices::set_notice(
83 [
84 'id' => 'swatchly-rating-notice',
85 'type' => 'info',
86 'dismissible' => true,
87 'message_type' => 'html',
88 'message' => $message,
89 'display_after' => ( 14 * DAY_IN_SECONDS ),
90 'expire_time' => ( 20 * DAY_IN_SECONDS ),
91 'close_by' => 'transient'
92 ]
93 );
94 }
95
96 /**
97 * Top level menu for the plugin
98 */
99 public function add_top_level_menu(){
100 add_menu_page(
101 __( 'Swatchly Settings', 'swatchly' ),
102 __( 'Swatchly', 'swatchly' ),
103 'manage_options',
104 'swatchly-admin',
105 '', // leave it empty to add custom submenus under this menu
106 'dashicons-ellipsis',
107 57
108 );
109 }
110
111 public function dashboard_menu_tweaks(){
112 add_submenu_page( 'swatchly-admin', esc_html__('Welcome', 'swatchly'), esc_html__( 'Welcome', 'swatchly' ), 'manage_options','swatchly-welcome', array( $this, 'quick_recommended_plugin'), 1);
113 }
114
115 public function upgrade_menu_tweaks(){
116 if(!is_plugin_active('swatchly-pro/swatchly-pro.php')) {
117 add_submenu_page( 'swatchly-admin', esc_html__('Upgrade to Pro', 'swatchly'), esc_html__( 'Upgrade to Pro', 'swatchly' ), 'manage_options','https://hasthemes.com/plugins/swatchly-product-variation-swatches-for-woocommerce-products/?utm_source=admin&utm_medium=mainmenu&utm_campaign=free#pricing');
118 }
119 }
120
121 function enqueue_admin_head_scripts() {
122 printf( '<style>%s</style>', '#adminmenu #toplevel_page_swatchly-admin a.swatchly-upgrade-pro { font-weight: 600; background-color: #ff6e30; color: #ffffff; text-align: center; margin-top: 8px;}' );
123 printf( '<script>%s</script>', '(function ($) {
124 $("#toplevel_page_swatchly-admin .wp-submenu a").each(function() {
125 if($(this)[0].href === "https://hasthemes.com/plugins/swatchly-product-variation-swatches-for-woocommerce-products/?utm_source=admin&utm_medium=mainmenu&utm_campaign=free#pricing") {
126 $(this).addClass("swatchly-upgrade-pro").attr("target", "_blank");
127 }
128 })
129 })(jQuery);' );
130 }
131
132 /**
133 * Action link add.
134 */
135 public function action_links_add( $actions, $plugin_file, $plugin_data, $context ){
136
137 $settings_page_link = sprintf(
138 /*
139 * translators:
140 * 1: Settings label
141 */
142 '<a href="'. esc_url( get_admin_url() . 'admin.php?page=swatchly-admin' ) .'">%1$s</a>',
143 esc_html__( 'Settings', 'swatchly' )
144 );
145
146 array_unshift( $actions, $settings_page_link );
147
148 return $actions;
149 }
150
151 /**
152 * Enqueue admin assets.
153 */
154 public function enqueue_admin_assets() {
155 $current_screen = get_current_screen();
156
157 if (
158 $current_screen->post_type == 'product' ||
159 $current_screen->base == 'toplevel_page_swatchly-admin' ||
160 $current_screen->base == 'swatchly_page_swatchly-welcome'
161 ) {
162 if( $current_screen->base == 'post' ){
163 wp_enqueue_style( 'wp-color-picker' );
164 wp_enqueue_script( 'wp-color-picker-alpha', SWATCHLY_ASSETS . '/js/wp-color-picker-alpha.min.js', array( 'wp-color-picker' ), $this->version, true );
165 }
166
167 add_thickbox();
168 wp_enqueue_style( 'swatchly-admin', SWATCHLY_ASSETS . '/css/admin.css', array(), $this->version );
169 wp_enqueue_script( 'swatchly-admin', SWATCHLY_ASSETS . '/js/admin.js', array('jquery'), $this->version, true );
170
171
172 // inline js for the settings submenu
173 $is_swatchly_setting = isset( $_GET['page'] ) ? sanitize_text_field($_GET['page']) : ''; // phpcs:ignore
174 $is_swatchly_setting = $is_swatchly_setting == 'swatchly-admin' ? 1 : 0;
175 wp_add_inline_script( 'swatchly-admin', 'var swatchly_is_settings_page = '. esc_js( $is_swatchly_setting ) .';');
176
177 $localize_vars = array();
178 if(get_post_type() == 'product'){
179 $localize_vars['product_id'] = get_the_id();
180 } else {
181 $localize_vars['product_id'] = '';
182 }
183
184 $localize_vars['nonce'] = wp_create_nonce('swatchly_product_metabox_save_nonce');
185 $localize_vars['i18n']['saving'] = esc_html__('Saving...', 'swatchly');
186 $localize_vars['i18n']['choose_an_image'] = esc_html__('Choose an image', 'swatchly');
187 $localize_vars['i18n']['use_image'] = esc_html__('Use image', 'swatchly');
188 $localize_vars['pl_override_global'] = swatchly_get_option('pl_override_global');
189 $localize_vars['sp_override_global'] = swatchly_get_option('sp_override_global');
190 wp_localize_script( 'swatchly-admin', 'swatchly_params', $localize_vars );
191 }
192
193 $css = '#adminmenu li a[href="admin.php?page=swatchly-welcome"]{display: none;}';
194 wp_add_inline_style('common', $css);
195 }
196
197 /**
198 * Pro version notice
199 */
200 public function pro_version_notice(){
201 ?>
202 <a href="#TB_inline?height=250&width=400&inlineId=swatchly_pro_notice" class="thickbox swatchly_trigger_pro_notice" style="display: none;"><?php echo esc_html__('Pro Notice', 'swatchly') ?></a>
203 <div id="swatchly_pro_notice" style="display: none;">
204 <div class="swatchly_pro_notice_wrapper">
205 <h3><?php echo esc_html__('Pro Version is Required!', 'swatchly') ?></h3>
206 <p><?php echo esc_html__('This feature is available in the pro version.', 'swatchly') ?></p>
207 <a target="_blank" href="https://hasthemes.com/plugins/swatchly-product-variation-swatches-for-woocommerce-products/"><?php echo esc_html__('More Details', 'swatchly') ?></a>
208 </div>
209 </div>
210 <?php
211 }
212
213 // Recommended plugin page after activating the plugin
214 public function quick_recommended_plugin(){
215 wp_enqueue_script('ht-install-manager');
216
217 $plugin_slug = 'woolentor-addons';
218 $plugin_file = 'woolentor-addons/woolentor_addons_elementor.php';
219
220 // Installed but Inactive.
221 if ( file_exists( WP_PLUGIN_DIR . '/' . $plugin_file ) && is_plugin_inactive( $plugin_file ) ) {
222
223 $button_classes = 'button ht-activate-now button-primary';
224 $button_text = esc_html__( 'Active Now', 'swatchly' );
225
226 // Not Installed.
227 } elseif ( ! file_exists( WP_PLUGIN_DIR . '/' . $plugin_file ) ) {
228
229 $button_classes = 'button ht-install-now button-primary';
230 $button_text = esc_html__( 'Active Now', 'swatchly' );
231
232 // Active.
233 } else {
234 $button_classes = 'button disabled';
235 $button_text = esc_html__( 'Activated', 'swatchly' );
236 }
237
238 $data_attr = array(
239 'slug' => $plugin_slug,
240 'location' => $plugin_file,
241 'name' => '',
242 );
243 ?>
244 <!-- ht-quick-recommended-plugin-area -->
245 <div class="ht-qrp-area">
246 <div class="ht-qrp">
247 <div class="ht-qrp-body">
248 <div class="ht-qrp-logo">
249 <img src="<?php echo esc_url(SWATCHLY_ASSETS . '/images/woolentor-logo.png') ?>" alt="">
250 </div>
251 <p><?php echo wp_kses_post(__('<span>Want to have complete control over the dull designs of all WooCommerce default pages and create an eye-catching WooCommerce store?</span> If you are interested, don\'t forget to try out the free version of the WooLentor today!', 'swatchly')) ?></p>
252 <button class="<?php echo esc_attr($button_classes); ?>"
253 data-slug='<?php echo esc_attr($data_attr['slug']); ?>'
254 data-location="<?php echo esc_attr($data_attr['location']); ?>"
255 data-progress_message="<?php echo esc_attr__('Activating..', 'swatchly') ?>"
256 data-redirect_after_activate="<?php echo esc_url(admin_url('admin.php?page=swatchly-admin')) ?>">
257 <?php echo esc_html($button_text); ?>
258 </button>
259 </div>
260 </div>
261 </div> <!-- .ht-quick-recommended-plugin-area -->
262 <?php
263 }
264 }