PluginProbe
Master Addons for Elementor – Elementor Addons, Widgets, Mega Menu Builder, Popup Builder, Widget Builder & Template Kits / trunk
Master Addons for Elementor – Elementor Addons, Widgets, Mega Menu Builder, Popup Builder, Widget Builder & Template Kits vtrunk
3.2.2 3.2.3 3.2.1 3.2.0 3.1.9 3.1.8 3.1.7 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.9 trunk 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.3 1.1.4 1.1.5 All 174 releases
master-addons / inc / classes / freemius-hooks.php

freemius-hooks.php in Master Addons for Elementor – Elementor Addons, Widgets, Mega Menu Builder, Popup Builder, Widget Builder & Template Kits trunk, at inc/classes/freemius-hooks.php

498 lines 18.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace MasterAddons\Inc\Classes;
4
5 if (!defined('ABSPATH')) {
6 exit; // Exit if accessed directly
7 }
8
9 use MasterAddons\Inc\Classes\Helper;
10
11 /**
12 * Freemius_Hooks
13 *
14 * @author Jewel Theme <support@jeweltheme.com>
15 */
16 if (!class_exists('MasterAddons\Inc\Classes\Freemius_Hooks')) {
17 class Freemius_Hooks
18 {
19
20 private static $instance = null;
21
22 public static function get_instance()
23 {
24 if (!self::$instance) {
25 self::$instance = new self;
26 }
27 return self::$instance;
28 }
29
30 public function __construct() {
31
32 // Reorder admin submenu items to place Freemius menus at the end
33 add_action( 'admin_menu', [$this, 'jltma_reorder_admin_submenu'], 999 );
34
35 add_action( 'wp_head', [$this, 'jltma_add_licensing_helper'] );
36 ma_el_fs()->add_filter('connect_message_on_update', [$this, 'jltma_custom_connect_message_on_update'], 10, 6);
37
38 // Not like register_uninstall_hook(), you do NOT have to use a static function.
39 ma_el_fs()->add_action('after_uninstall', [$this, 'jltma_uninstall_cleanup']);
40
41 ma_el_fs()->add_filter( 'permissions_list', [$this, 'jltma_add_helpscount_permission'] );
42
43 //Controlling the visibility of admin notices added by the Freemius SDK
44 ma_el_fs()->add_filter( 'show_admin_notice', [$this, 'jltma_custom_show_admin_notice'], 10, 2 );
45
46 // Freemius Purchase Completion JavaScript Callback Filter
47 ma_el_fs()->add_filter('checkout/purchaseCompleted', [$this, 'jltma_after_purchase_js'] );
48
49 // Freemius submenu items visibility filter
50 ma_el_fs()->add_filter( 'is_submenu_visible', [$this, 'jltma_is_submenu_visible'], 10, 2 );
51
52 ma_el_fs()->add_filter( 'show_affiliate_program_notice', '__return_false' );
53
54 ma_el_fs()->add_filter( 'is_submenu_visible', [$this, 'jltma_disable_contact_for_free_users'], 10, 2 );
55
56 ma_el_fs()->add_filter( 'show_deactivation_subscription_cancellation', '__return_false' );
57
58 // Disable Freemius deactivation feedback popup (we have our own custom feedback)
59 ma_el_fs()->add_filter( 'show_deactivation_feedback_form', '__return_false' );
60
61 // Support menu and URL customization
62 ma_el_fs()->add_filter( 'support_forum_submenu', [$this, 'jltma_override_support_menu_text'] );
63 ma_el_fs()->add_filter( 'support_forum_url', [$this, 'jltma_support_forum_url'] );
64 ma_el_fs()->add_filter( 'plugin_icon', [$this, 'jltma_freemius_logo_icon'] );
65
66 // Premium version activation hook
67 ma_el_fs()->add_action( 'after_premium_version_activation', [$this, 'jltma_after_premium_activation'] );
68
69 // Remove Freemius links from free plugin when Pro is active
70 if ( Helper::jltma_premium() ) {
71 add_filter( 'plugin_action_links_' . JLTMA_BASE, [$this, 'jltma_remove_freemius_action_links'], 999 );
72 }
73
74 // Add plugin row meta links + deduplicate with Freemius's own
75 // Support / Documentation entries (always, regardless of Pro).
76 // Our branded entries with dashicons ship first via jltma_plugin_row_meta;
77 // jltma_remove_freemius_row_meta runs at priority 999 to strip
78 // the duplicate plain-text links Freemius injects afterwards.
79 add_filter( 'plugin_row_meta', [$this, 'jltma_plugin_row_meta'], 10, 2 );
80 add_filter( 'plugin_row_meta', [$this, 'jltma_remove_freemius_row_meta'], 999, 2 );
81
82 // Trial
83 // ma_el_fs()->override_i18n( array(
84 // 'hey' => 'Hey',
85 // 'trial-x-promotion-message' => 'Thank you so much for using %s!',
86 // 'already-opted-in-to-product-usage-tracking' => 'How do you like %s so far? Test all our %s premium features with a %d-day free trial.',
87 // 'start-free-trial' => 'Start free trial',
88 // // Trial with a payment method required.
89 // 'no-commitment-for-x-days' => 'No commitment for %s days - cancel anytime!',
90 // // Trial without a payment method.
91 // 'no-cc-required' => 'No credit card required',
92 // ) );
93
94
95 // Show the 1st trial promotion after 7 days instead of 24 hours.
96 // ma_el_fs()->add_filter( 'show_first_trial_after_n_sec', [$this, 'jltma_show_first_trial_after_7_days'] );
97
98 // Re-show the trial promotional offer after every 60 days instead of 30 days.
99 // ma_el_fs()->add_filter( 'reshow_trial_after_every_n_sec', [$this, 'jltma_reshow_trial_after_every_60_days'] );
100 }
101
102
103 public function jltma_disable_contact_for_free_users( $is_visible, $menu_id ) {
104 if ( 'contact' != $menu_id ) {
105 return $is_visible;
106 }
107 return false;
108 }
109
110 public function jltma_add_licensing_helper(){
111 ?>
112 <script type="text/javascript">
113 (function () {
114 window.ma_el_fs = { can_use_premium_code__premium_only: <?php
115 echo json_encode( function_exists('ma_el_fs') && ma_el_fs()->can_use_premium_code() ) ;
116 ?>};
117 })();
118 </script>
119 <?php
120 }
121
122 // Customize Opt-in Message for Existing Users
123 public function jltma_custom_connect_message_on_update( $message, $user_first_name, $plugin_title, $user_login, $site_link, $freemius_link ){
124
125 return sprintf(
126 /* translators: 1: First Name, 2: Plugin Title, 3: Freemius Link. */
127 __( 'Hey %1$s,<br> Please help us improve %2$s! If you opt-in, some data about your usage of %2$s will be sent to %3$s. If you skip this, that\'s okay! %2$s will still work just fine.', 'master-addons' ),
128 $user_first_name,
129 '<b>' . $plugin_title . '</b>',
130 $freemius_link
131 );
132 }
133
134 /**
135 * Cleanup plugin data on uninstall
136 * Called by Freemius after_uninstall hook
137 */
138 public function jltma_uninstall_cleanup(){
139 // global $wpdb;
140
141 // // Delete all plugin options
142 // delete_option('maad_el_save_settings');
143 // delete_option('ma_el_extensions_save_settings');
144 // delete_option('ma_el_third_party_plugins_save_settings');
145 // delete_option('jltma_icons_library_save_settings');
146 // delete_option('jltma_white_label_settings');
147 // delete_option('_master_addons_version');
148 // delete_option('jltma_sheet_promo_data');
149 // delete_option('jltma_sheet_promo_data_hash');
150 // delete_option('jltma_activation_time');
151
152 // // Delete custom post types (Theme Builder, Popup Builder, Widget Builder, etc.)
153 // $custom_post_types = array(
154 // 'jltma_template', // Theme Builder templates
155 // 'jltma-popup', // Popup Builder
156 // 'jltma-widget' // Widget Builder
157 // );
158
159 // foreach ($custom_post_types as $post_type) {
160 // $posts = get_posts(array(
161 // 'post_type' => $post_type,
162 // 'posts_per_page' => -1,
163 // 'post_status' => 'any'
164 // ));
165
166 // foreach ($posts as $post) {
167 // wp_delete_post($post->ID, true); // true = force delete, bypass trash
168 // }
169 // }
170
171 // // Delete all transients
172 // $wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_master-addons%'");
173 // $wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_timeout_master-addons%'");
174 // $wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_jltma%'");
175 // $wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_timeout_jltma%'");
176
177 // // Clear scheduled cron jobs
178 // wp_clear_scheduled_hook('jltma_sheet_promo_data_remote_sync');
179
180 // // Drop popup builder custom table if it exists
181 // $table_name = $wpdb->prefix . 'jltma_popup_builder';
182 // $wpdb->query("DROP TABLE IF EXISTS {$table_name}");
183
184 // // Delete post meta related to plugin
185 // $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE 'jltma%'");
186 // $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE '_jltma%'");
187 // $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE 'ma_el%'");
188
189 // // Delete user meta related to plugin
190 // $wpdb->query("DELETE FROM {$wpdb->usermeta} WHERE meta_key LIKE 'jltma%'");
191 // $wpdb->query("DELETE FROM {$wpdb->usermeta} WHERE meta_key LIKE 'ma_el%'");
192
193 // // Clear any rewrite rules
194 // flush_rewrite_rules();
195 }
196
197 public function jltma_add_helpscount_permission( $permissions ){
198 $permissions['helpscout'] = array(
199 'icon-class' => 'dashicons dashicons-email-alt',
200 'label' => ma_el_fs()->get_text_inline( 'Help Scout', 'helpscout' ),
201 'desc' => ma_el_fs()->get_text_inline( 'Rendering Help Scout\'s beacon for easy support access', 'permissions-helpscout' ),
202 'priority' => 16,
203 );
204
205 $permissions['newsletter'] = array(
206 'icon-class' => 'dashicons dashicons-email-alt',
207 'label' => ma_el_fs()->get_text_inline( 'Newsletter', 'permissions-newsletter' ),
208 'desc' => ma_el_fs()->get_text_inline( 'Updates, announcements, marketing, no spam', 'permissions-newsletter_desc' ),
209 'priority' => 15,
210 );
211 }
212
213
214 public function jltma_custom_show_admin_notice( $show, $msg ) {
215 if ('trial_promotion' == $msg['id']) {
216 // Don't show the trial promotional admin notice.
217 return false;
218 }
219 return $show;
220 }
221
222 public function jltma_after_purchase_js( $js_function ) {
223 return 'function (data) {
224 console.log("checkout", "purchaseCompleted");
225 }';
226 }
227
228 public function jltma_is_submenu_visible($is_visible, $submenu_id){
229 // Hide account page if user is not registered with Freemius
230 if ('account' === $submenu_id && !ma_el_fs()->is_registered()) {
231 return false;
232 }
233 // Hide pricing page for premium users
234 if ('pricing' === $submenu_id && Helper::jltma_premium()) {
235 return false;
236 }
237 return $is_visible;
238 }
239
240 /**
241 * Remove account page when not premium - show for licensed users
242 */
243 public function jltma_remove_account_page_if_unregistered() {
244 if (!Helper::jltma_premium()) {
245 remove_submenu_page( 'master-addons-settings', 'master-addons-account' );
246 }
247 }
248
249
250 public function jltma_show_first_trial_after_7_days( $day_in_sec ) {
251 // 7 days in sec.
252 return 7 * 24 * 60 * 60;
253 }
254
255
256 public function jltma_reshow_trial_after_every_60_days( $thirty_days_in_sec ) {
257 // 60 days in sec.
258 return 60 * 24 * 60 * 60;
259 }
260
261 /**
262 * Override support menu text
263 *
264 * @return string
265 */
266 public function jltma_override_support_menu_text() {
267 return __( 'Support', 'master-addons' );
268 }
269
270 /**
271 * Support Forum URL
272 *
273 * @param string $support_url Default support URL
274 * @return string Modified support URL
275 */
276 public function jltma_support_forum_url( $support_url ) {
277 if (Helper::jltma_premium()) {
278 $support_url = 'https://master-addons.com/contact-us/';
279 } else {
280 $support_url = 'https://wordpress.org/support/plugin/master-addons/#new-topic-0';
281 }
282 return $support_url;
283 }
284
285 /**
286 * Freemius plugin icon
287 *
288 * @return string Path to plugin icon
289 */
290 public function jltma_freemius_logo_icon() {
291 return JLTMA_PATH . 'assets/images/master-addons.png';
292 }
293
294 /**
295 * After premium version activation
296 * Redirect to welcome page on single site
297 *
298 * @param bool $network_wide Whether activated network-wide
299 */
300 public function jltma_after_premium_activation( $network_wide ) {
301 if ( function_exists( 'is_multisite' ) && is_multisite() ) {
302 // Do nothing for multisite
303 } else {
304 // Redirect to welcome page
305 set_transient( '_master_addons_activation_redirect', true, 30 );
306 }
307 }
308
309 /**
310 * Add plugin row meta links
311 *
312 * @param array $links Existing row meta links
313 * @param string $file Plugin file
314 * @return array Modified links
315 */
316 public function jltma_plugin_row_meta( $links, $file ) {
317 if ( JLTMA_BASE === $file ) {
318 // Array keys are prefixed with jltma_ so they never collide
319 // with Freemius's own 'support' / 'documentation' / 'pricing'
320 // keys that jltma_remove_freemius_row_meta drops.
321 //
322 // Inline styles brand-color the links on the Plugins list
323 // (blue for Docs/Support, purple for Premium) because this
324 // runs on the core plugins.php screen where our admin CSS
325 // isn't enqueued — inline is the safe, self-contained path.
326 $link_style = 'style="color:#2563eb;font-weight:600;"';
327 $premium_style = 'style="color:#6814cd;font-weight:700;"';
328 $icon_style = 'style="vertical-align:middle;margin-right:3px;font-size:16px;width:16px;height:16px;"';
329
330 $new_links = array(
331 'jltma_doc' => '<a ' . $link_style . ' href="' . esc_url( 'https://master-addons.com/docs/' ) . '" target="_blank"><span class="dashicons dashicons-media-document" ' . $icon_style . '></span>Docs</a>',
332 'jltma_support' => '<a ' . $link_style . ' href="' . esc_url( 'https://master-addons.com/contact-us' ) . '" target="_blank"><span class="dashicons dashicons-admin-users" ' . $icon_style . '></span>Support</a>',
333 'jltma_pro' => '<a ' . $premium_style . ' href="' . esc_url( 'https://master-addons.com/pricing' ) . '" target="_blank"><span class="dashicons dashicons-star-filled" ' . $icon_style . '></span>Upgrade to Pro</a>',
334 );
335 $links = array_merge( $links, $new_links );
336 }
337 return $links;
338 }
339
340 /**
341 * Remove Freemius action links from free plugin when Pro is active
342 *
343 * @param array $links Plugin action links
344 * @return array Modified links
345 */
346 public function jltma_remove_freemius_action_links( $links ) {
347 $freemius_keys = array(
348 'upgrade',
349 'pricing',
350 'fs_upgrade',
351 'opt-in',
352 'opt-out',
353 'change-license',
354 'activate-license',
355 'deactivate-license',
356 'sync-license',
357 'account',
358 'contact',
359 'support',
360 'affiliation'
361 );
362
363 foreach ( $freemius_keys as $key ) {
364 if ( isset( $links[ $key ] ) ) {
365 unset( $links[ $key ] );
366 }
367 }
368 return $links;
369 }
370
371 /**
372 * Remove Freemius row meta links from free plugin when Pro is active
373 *
374 * @param array $links Plugin row meta links
375 * @param string $file Plugin file
376 * @return array Modified links
377 */
378 public function jltma_remove_freemius_row_meta( $links, $file ) {
379 if ( $file === JLTMA_BASE ) {
380 // Known Freemius row-meta keys. 'support' and 'documentation'
381 // are included because we already ship our own branded versions
382 // via jltma_plugin_row_meta and Freemius would otherwise add
383 // plain-text duplicates next to ours.
384 $freemius_keys = array(
385 'upgrade',
386 'pricing',
387 'change-license',
388 'opt-in',
389 'opt-out',
390 'activate-license',
391 'deactivate-license',
392 'support',
393 'documentation',
394 'docs',
395 );
396
397 foreach ( $freemius_keys as $key ) {
398 if ( isset( $links[ $key ] ) ) {
399 unset( $links[ $key ] );
400 }
401 }
402
403 // Belt-and-suspenders: Freemius sometimes emits the links
404 // without a stable array key. Scan by anchor text content and
405 // drop any plain-text 'Support' / 'Documentation' entry that
406 // isn't our own (ours contain the dashicons-* span class).
407 foreach ( $links as $key => $html ) {
408 if ( ! is_string( $html ) ) continue;
409 if ( strpos( $html, 'dashicons-' ) !== false ) continue; // ours
410 if ( preg_match( '~>(Support|Documentation)\s*<~i', $html ) ) {
411 unset( $links[ $key ] );
412 }
413 }
414 }
415 return $links;
416 }
417
418
419 /**
420 * Reorder admin submenu items
421 * Places Freemius menus (Account, Support) at the end
422 *
423 * Desired order:
424 * 1. Settings
425 * 2. Template Library
426 * 3. Theme Builder
427 * 4. Account (Freemius)
428 * 5. Support (Freemius)
429 */
430 public function jltma_reorder_admin_submenu() {
431 global $submenu;
432
433 $parent_slug = 'master-addons-settings';
434
435 if ( !isset( $submenu[ $parent_slug ] ) ) {
436 return;
437 }
438
439 // Define the desired order of menu slugs
440 $desired_order = [
441 'master-addons-settings', // 0 - Settings
442 'jltma-template-library', // 1 - Template Library
443 'jltma-template-kits', // 2 - Template Kits
444 'edit.php?post_type=master_template', // 3 - Theme Builder
445 'edit.php?post_type=jltma_popup', // 4 - Popup Builder
446 'edit.php?post_type=jltma_widget', // 5 - Widget Builder
447 'master-addons-account', // 6 - Account (Freemius)
448 'master-addons-wp-support-forum', // 7 - Support (Freemius)
449 ];
450
451 // Separate known items and unknown items
452 $ordered_items = [];
453 $remaining_items = [];
454
455 foreach ( $submenu[ $parent_slug ] as $item ) {
456 $slug = $item[2] ?? '';
457 $position = array_search( $slug, $desired_order );
458
459 if ( $position !== false ) {
460 $ordered_items[ $position ] = $item;
461 } else {
462 // Unknown items go between Theme Builder and Account
463 $remaining_items[] = $item;
464 }
465 }
466
467 // Sort ordered items by their position
468 ksort( $ordered_items );
469
470 // Build final submenu array
471 $final_submenu = [];
472
473 // Add items in order, inserting remaining items after Widget Builder (position 5)
474 foreach ( $ordered_items as $position => $item ) {
475 $final_submenu[] = $item;
476
477 // After Widget Builder, add remaining items (Recommended, Setup Wizard, etc.)
478 if ( $position === 5 && !empty( $remaining_items ) ) {
479 foreach ( $remaining_items as $remaining_item ) {
480 $final_submenu[] = $remaining_item;
481 }
482 $remaining_items = []; // Clear to avoid duplicates
483 }
484 }
485
486 // Add any remaining items at the end (if Theme Builder wasn't found)
487 foreach ( $remaining_items as $remaining_item ) {
488 $final_submenu[] = $remaining_item;
489 }
490
491 // Update the global submenu
492 $submenu[ $parent_slug ] = $final_submenu;
493 }
494
495
496 }
497 }
498 Freemius_Hooks::get_instance();