| 1 |
<?php |
| 2 |
/** |
| 3 |
* The float widget model class for ThemeIsle SDK |
| 4 |
* |
| 5 |
* Here's how to hook it in your plugin: |
| 6 |
* |
| 7 |
* add_filter( <product_slug>_float_widget_metadata', 'add_float_widget_meta' ); |
| 8 |
* |
| 9 |
* function add_float_widget_meta($data) { |
| 10 |
* return [ |
| 11 |
* 'logo' => <logo url>, |
| 12 |
* 'nice_name' => <nice name>, // optional, will default to product name |
| 13 |
* 'primary_color' => <hex_color_value>, // optional |
| 14 |
* 'pages' => [ 'page-slugs' ], //pages where the float widget should be displayed |
| 15 |
* 'has_upgrade_menu' => <condition>, |
| 16 |
* 'upgrade_link' => <url>, |
| 17 |
* 'documentation_link' => <url>, |
| 18 |
* 'premium_support_link' => <url>, // optional, provide from pro version |
| 19 |
* 'feature_request_link' => <url>, // optional, provide from pro version |
| 20 |
* 'wizard_link' => <url>, // optional, provide if a user is available |
| 21 |
* ] |
| 22 |
* } |
| 23 |
* |
| 24 |
* @package ThemeIsleSDK |
| 25 |
* @subpackage Modules |
| 26 |
* @copyright Copyright (c) 2024, Bogdan Preda |
| 27 |
* @license http://opensource.org/licenses/gpl-3.0.php GNU Public License |
| 28 |
* @since 3.2.42 |
| 29 |
*/ |
| 30 |
|
| 31 |
namespace ThemeisleSDK\Modules; |
| 32 |
|
| 33 |
use ThemeisleSDK\Common\Abstract_Module; |
| 34 |
use ThemeisleSDK\Loader; |
| 35 |
use ThemeisleSDK\Product; |
| 36 |
|
| 37 |
// Exit if accessed directly. |
| 38 |
if ( ! defined( 'ABSPATH' ) ) { |
| 39 |
exit; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Float widget module for ThemeIsle SDK. |
| 44 |
*/ |
| 45 |
class Float_Widget extends Abstract_Module { |
| 46 |
/** |
| 47 |
* Float widget data. |
| 48 |
* |
| 49 |
* @var array $float_widget_data Float widget data, received from the filter. |
| 50 |
* |
| 51 |
* Shape of the $about_data property array: |
| 52 |
* [ |
| 53 |
* 'logo' => <logo url>, |
| 54 |
* 'nice_name' => <nice name>, // optional, will default to product name |
| 55 |
* 'primary_color' => <hex_color_value>, // optional |
| 56 |
* 'pages' => [ 'page-slugs' ], //pages where the float widget should be displayed |
| 57 |
* 'has_upgrade_menu' => <condition>, |
| 58 |
* 'upgrade_link' => <url>, |
| 59 |
* 'documentation_link' => <url>, |
| 60 |
* 'premium_support_link' => <url>, // optional, provide from pro version |
| 61 |
* 'feature_request_link' => <url>, // optional, provide from pro version |
| 62 |
* 'wizard_link' => <url>, // optional, provide if a user is available |
| 63 |
* ] |
| 64 |
*/ |
| 65 |
private $float_widget_data = array(); |
| 66 |
|
| 67 |
/** |
| 68 |
* Should we load this module. |
| 69 |
* |
| 70 |
* @param Product $product Product object. |
| 71 |
* |
| 72 |
* @return bool |
| 73 |
*/ |
| 74 |
public function can_load( $product ) { |
| 75 |
if ( $this->is_from_partner( $product ) ) { |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
$this->float_widget_data = apply_filters( $product->get_key() . '_float_widget_metadata', array() ); |
| 80 |
|
| 81 |
$can_load = ! empty( $this->float_widget_data ); |
| 82 |
|
| 83 |
$this->float_widget_data = array_merge( |
| 84 |
[ |
| 85 |
'logo' => '', |
| 86 |
'primary_color' => '#2271b1', // Default color. |
| 87 |
'nice_name' => $product->get_name(), |
| 88 |
'documentation_link' => '', |
| 89 |
'premium_support_link' => '', |
| 90 |
'feature_request_link' => '', |
| 91 |
'wizard_link' => '', |
| 92 |
], |
| 93 |
$this->float_widget_data |
| 94 |
); |
| 95 |
|
| 96 |
return $can_load; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Registers the hooks. |
| 101 |
* |
| 102 |
* @param Product $product Product to load. |
| 103 |
*/ |
| 104 |
public function load( $product ) { |
| 105 |
$this->product = $product; |
| 106 |
|
| 107 |
add_action( 'in_admin_footer', [ $this, 'render_float_placeholder' ] ); |
| 108 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_float_widget_script' ] ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Returns the allowed pages for the float widget. |
| 113 |
* |
| 114 |
* @return array |
| 115 |
*/ |
| 116 |
private function get_allowed_pages() { |
| 117 |
if ( ! isset( $this->float_widget_data['pages'] ) || ! is_array( $this->float_widget_data['pages'] ) ) { |
| 118 |
return []; |
| 119 |
} |
| 120 |
return $this->float_widget_data['pages']; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Checks if the current screen is allowed for the float widget. |
| 125 |
* |
| 126 |
* @return bool |
| 127 |
*/ |
| 128 |
private function is_current_screen_allowed() { |
| 129 |
$current_screen = get_current_screen(); |
| 130 |
|
| 131 |
if ( ! isset( $current_screen->id ) ) { |
| 132 |
return false; |
| 133 |
} |
| 134 |
|
| 135 |
if ( ! in_array( $current_screen->id, $this->get_allowed_pages(), true ) ) { |
| 136 |
return false; |
| 137 |
} |
| 138 |
|
| 139 |
return true; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Renders the float widget placeholder. |
| 144 |
* |
| 145 |
* @return void |
| 146 |
*/ |
| 147 |
public function render_float_placeholder() { |
| 148 |
if ( ! $this->is_current_screen_allowed() ) { |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
echo '<div id="ti-sdk-float-widget" style="--ti-float-primary-color: ' . esc_attr( $this->float_widget_data['primary_color'] ) . '"></div>'; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Enqueue scripts & styles. |
| 157 |
* |
| 158 |
* @return void |
| 159 |
*/ |
| 160 |
public function enqueue_float_widget_script() { |
| 161 |
|
| 162 |
if ( ! $this->is_current_screen_allowed() ) { |
| 163 |
return; |
| 164 |
} |
| 165 |
|
| 166 |
global $themeisle_sdk_max_path; |
| 167 |
$handle = 'ti-sdk-float-' . $this->product->get_key(); |
| 168 |
$asset_file = require $themeisle_sdk_max_path . '/assets/js/build/float_widget/float.asset.php'; |
| 169 |
$deps = array_merge( $asset_file['dependencies'], [ 'updates' ] ); |
| 170 |
|
| 171 |
wp_register_script( $handle, $this->get_sdk_uri() . 'assets/js/build/float_widget/float.js', $deps, $asset_file['version'], true ); |
| 172 |
wp_localize_script( $handle, 'tiSDKFloatData', $this->get_float_localization_data() ); |
| 173 |
|
| 174 |
wp_enqueue_script( $handle ); |
| 175 |
wp_enqueue_style( $handle, $this->get_sdk_uri() . 'assets/js/build/float_widget/float.css', [ 'wp-components' ], $asset_file['version'] ); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Get the float widget localization data. |
| 180 |
* |
| 181 |
* @return array |
| 182 |
*/ |
| 183 |
private function get_float_localization_data() { |
| 184 |
return [ |
| 185 |
'logoUrl' => $this->float_widget_data['logo'], |
| 186 |
'primaryColor' => esc_attr( $this->float_widget_data['primary_color'] ), |
| 187 |
'strings' => [ |
| 188 |
'toggleButton' => sprintf( Loader::$labels['float_widget']['button'], $this->float_widget_data['nice_name'] ), |
| 189 |
'panelGreet' => sprintf( Loader::$labels['float_widget']['panel']['greeting'], $this->float_widget_data['nice_name'] ), |
| 190 |
'panelTitle' => Loader::$labels['float_widget']['panel']['title'], |
| 191 |
'closeToggle' => Loader::$labels['float_widget']['panel']['close'], |
| 192 |
], |
| 193 |
'links' => $this->get_links(), |
| 194 |
]; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Generates the links for the float widget. |
| 199 |
* |
| 200 |
* For Free: |
| 201 |
* - Documentation (redirects to Themeisle doc page) |
| 202 |
* - Get Support (redirects to WP free support forum) |
| 203 |
* - Run Setup Wizard (this will trigger the setup wizard) if available |
| 204 |
* - Upgrade to Pro (redirects to Themeisle upgrade page) |
| 205 |
* - Rate Us (redirects to WP rating page) |
| 206 |
* |
| 207 |
* For Pro: |
| 208 |
* - Documentation (redirects to Themeisle doc page) |
| 209 |
* - Get Support (redirects to Themeisle support page to open a ticket) |
| 210 |
* - Run Setup Wizard (this will trigger the setup wizard) if available |
| 211 |
* - Feature Request (if available redirect to collect feedback requests) |
| 212 |
* - Rate Us (redirects to WP rating page) |
| 213 |
* |
| 214 |
* @return array |
| 215 |
*/ |
| 216 |
private function get_links() { |
| 217 |
$links = []; |
| 218 |
|
| 219 |
if ( ! empty( $this->float_widget_data['documentation_link'] ) ) { |
| 220 |
$links[] = [ |
| 221 |
'icon' => 'dashicons-book-alt', |
| 222 |
'title' => Loader::$labels['float_widget']['links']['documentation'], |
| 223 |
'link' => $this->float_widget_data['documentation_link'], |
| 224 |
]; |
| 225 |
} |
| 226 |
|
| 227 |
$support_link = [ |
| 228 |
'icon' => 'dashicons-format-status', |
| 229 |
'title' => Loader::$labels['float_widget']['links']['support'], |
| 230 |
'link' => 'https://wordpress.org/support/' . $this->product->get_type() . '/' . $this->product->get_slug() . '/', |
| 231 |
]; |
| 232 |
if ( ! $this->float_widget_data['has_upgrade_menu'] && ! empty( $this->float_widget_data['premium_support_link'] ) ) { |
| 233 |
$support_link['link'] = $this->float_widget_data['premium_support_link']; |
| 234 |
} |
| 235 |
$links[] = $support_link; |
| 236 |
|
| 237 |
if ( ! empty( $this->float_widget_data['wizard_link'] ) ) { |
| 238 |
$links[] = [ |
| 239 |
'icon' => 'dashicons-admin-tools', |
| 240 |
'title' => Loader::$labels['float_widget']['links']['wizard'], |
| 241 |
'link' => $this->float_widget_data['wizard_link'], |
| 242 |
'internal' => true, |
| 243 |
]; |
| 244 |
} |
| 245 |
|
| 246 |
$pro = [ |
| 247 |
'icon' => 'dashicons-superhero-alt', |
| 248 |
'title' => Loader::$labels['float_widget']['links']['upgrade'], |
| 249 |
'link' => $this->float_widget_data['upgrade_link'], |
| 250 |
]; |
| 251 |
$featured_or_pro = $pro; |
| 252 |
if ( ! $this->float_widget_data['has_upgrade_menu'] ) { |
| 253 |
$featured_or_pro = []; // we remove the upgrade link |
| 254 |
$featured = $pro; |
| 255 |
$featured['title'] = Loader::$labels['float_widget']['links']['feature_request']; |
| 256 |
$featured['link'] = $this->float_widget_data['feature_request_link']; |
| 257 |
if ( ! empty( $featured['link'] ) ) { |
| 258 |
$featured_or_pro = $featured; |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
if ( ! empty( $featured_or_pro ) ) { |
| 263 |
$links[] = $featured_or_pro; |
| 264 |
} |
| 265 |
|
| 266 |
$links[] = [ |
| 267 |
'icon' => 'dashicons-star-filled', |
| 268 |
'title' => Loader::$labels['float_widget']['links']['rate'], |
| 269 |
'link' => 'https://wordpress.org/support/' . $this->product->get_type() . '/' . $this->product->get_slug() . '/reviews/#new-post', |
| 270 |
]; |
| 271 |
|
| 272 |
return $links; |
| 273 |
} |
| 274 |
} |
| 275 |
|