| 1 |
<?php |
| 2 |
/** |
| 3 |
* Automation UI Recommendations |
| 4 |
* |
| 5 |
* @package AutomatorWP\Automation_UI_Recommendations |
| 6 |
* @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Automation UI add-ons recommendations |
| 14 |
* |
| 15 |
* @since 1.1.2 |
| 16 |
* |
| 17 |
* @param stdClass $automation The automation object |
| 18 |
* @param string $item_type The item type (trigger|action) |
| 19 |
*/ |
| 20 |
function automatorwp_automation_ui_integrations_recommendations( $automation, $item_type ) { |
| 21 |
|
| 22 |
if( class_exists( 'AutomatorWP_Pro' ) ) { |
| 23 |
return; |
| 24 |
} |
| 25 |
|
| 26 |
if( (bool) automatorwp_get_option( 'disable_pro_recommendations' ) ) { |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
?> |
| 31 |
|
| 32 |
<div class="automatorwp-more-integrations"> |
| 33 |
<span><?php if ( $item_type === 'trigger' ) : _e( 'Looking for more triggers?', 'automatorwp' ); elseif ( $item_type === 'action' ) : _e( 'Looking for more actions?', 'automatorwp' ); endif; ?></span> |
| 34 |
<a href="https://automatorwp.com/add-ons/" target="_blank"><?php _e( 'View all add-ons', 'automatorwp' ); ?></a> |
| 35 |
</div> |
| 36 |
|
| 37 |
<?php |
| 38 |
|
| 39 |
} |
| 40 |
add_action( 'automatorwp_automation_ui_add_item_content_bottom', 'automatorwp_automation_ui_integrations_recommendations', 10, 2 ); |
| 41 |
|
| 42 |
/** |
| 43 |
* Get recommended integrations |
| 44 |
* |
| 45 |
* @since 1.1.2 |
| 46 |
* |
| 47 |
* @return array|WP_Error Object with recommended integrations |
| 48 |
*/ |
| 49 |
function automatorwp_get_recommended_integrations() { |
| 50 |
|
| 51 |
if( (bool) automatorwp_get_option( 'disable_pro_recommendations' ) ) { |
| 52 |
return array(); |
| 53 |
} |
| 54 |
|
| 55 |
$integrations = automatorwp_integrations_api(); |
| 56 |
|
| 57 |
if( is_wp_error( $integrations ) ) { |
| 58 |
return $integrations; |
| 59 |
} |
| 60 |
|
| 61 |
$recommended_integrations = array(); |
| 62 |
|
| 63 |
foreach ( $integrations as $integration ) { |
| 64 |
|
| 65 |
if( $integration === null ) { |
| 66 |
continue; |
| 67 |
} |
| 68 |
|
| 69 |
// Skip integration if can't determine its class |
| 70 |
if( empty( $integration->integration_class ) ) { |
| 71 |
continue; |
| 72 |
} |
| 73 |
|
| 74 |
// Skip integration if already installed |
| 75 |
if( class_exists( $integration->integration_class ) ) { |
| 76 |
continue; |
| 77 |
} |
| 78 |
|
| 79 |
// Skip integration if free version already installed |
| 80 |
if( class_exists( $integration->integration_class . '_Integration' ) ) { |
| 81 |
continue; |
| 82 |
} |
| 83 |
|
| 84 |
// Bail if integration plugin is not installed |
| 85 |
if( ! automatorwp_automation_ui_integration_plugin_installed( $integration ) ) { |
| 86 |
continue; |
| 87 |
} |
| 88 |
|
| 89 |
$recommended_integrations[] = $integration; |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
return $recommended_integrations; |
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Function to contact with the AutomatorWP integrations API |
| 99 |
* |
| 100 |
* @since 1.1.2 |
| 101 |
* |
| 102 |
* @return object|WP_Error Object with AutomatorWP integrations |
| 103 |
*/ |
| 104 |
function automatorwp_integrations_api() { |
| 105 |
|
| 106 |
$cache = automatorwp_get_cache( 'automatorwp_integrations_api', false, false ); |
| 107 |
|
| 108 |
// If result already cached, return it |
| 109 |
if( $cache !== false ) { |
| 110 |
return $cache; |
| 111 |
} |
| 112 |
|
| 113 |
// If a integrations api request has been cached already, then use cached integrations |
| 114 |
if ( false !== ( $res = get_transient( 'automatorwp_integrations_api' ) ) ) { |
| 115 |
return $res; |
| 116 |
} |
| 117 |
|
| 118 |
$url = $http_url = 'http://automatorwp.com/wp-json/automatorwp/integrations'; |
| 119 |
|
| 120 |
if ( $ssl = wp_http_supports( array( 'ssl' ) ) ) { |
| 121 |
$url = set_url_scheme( $url, 'https' ); |
| 122 |
} |
| 123 |
|
| 124 |
$http_args = array( |
| 125 |
'timeout' => 15, |
| 126 |
); |
| 127 |
|
| 128 |
$request = wp_remote_get( $url, $http_args ); |
| 129 |
|
| 130 |
if ( $ssl && is_wp_error( $request ) ) { |
| 131 |
// Try again without SSL |
| 132 |
$request = wp_remote_get( $http_url, $http_args ); |
| 133 |
} |
| 134 |
|
| 135 |
if ( is_wp_error( $request ) ) { |
| 136 |
// No way to contact server |
| 137 |
$res = array(); |
| 138 |
|
| 139 |
// Set a transient for 1 day |
| 140 |
set_transient( 'automatorwp_integrations_api', $res, ( 24 * 1 ) * HOUR_IN_SECONDS ); |
| 141 |
} else { |
| 142 |
$res = json_decode( $request['body'] ); |
| 143 |
|
| 144 |
$res = (array) $res; |
| 145 |
|
| 146 |
// Set a transient for 1 week with api integrations |
| 147 |
set_transient( 'automatorwp_integrations_api', $res, ( 24 * 7 ) * HOUR_IN_SECONDS ); |
| 148 |
} |
| 149 |
|
| 150 |
// Update cache |
| 151 |
automatorwp_set_cache( 'automatorwp_integrations_api', $res ); |
| 152 |
|
| 153 |
return $res; |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Inform about integration pro choices |
| 159 |
* |
| 160 |
* @since 1.2.4 |
| 161 |
* |
| 162 |
* @param string $integration_name The integration name |
| 163 |
* @param array $args Integration arguments |
| 164 |
* @param stdClass $automation The automation object |
| 165 |
* @param string $item_type The item type |
| 166 |
*/ |
| 167 |
function automatorwp_automation_ui_integration_pro_choice( $integration_name, $args, $automation, $item_type ) { |
| 168 |
|
| 169 |
if( (bool) automatorwp_get_option( 'disable_pro_recommendations' ) ) { |
| 170 |
return; |
| 171 |
} |
| 172 |
|
| 173 |
$integrations = automatorwp_integrations_api(); |
| 174 |
|
| 175 |
if ( is_array( $integrations ) && empty( $integrations) ) { |
| 176 |
return; |
| 177 |
} |
| 178 |
|
| 179 |
if( is_wp_error( $integrations ) ) { |
| 180 |
return; |
| 181 |
} |
| 182 |
|
| 183 |
foreach ( $integrations as $integration ) { |
| 184 |
if( ! isset( $integration ) ) { |
| 185 |
continue; |
| 186 |
} |
| 187 |
|
| 188 |
if( ! is_object( $integration ) ) { |
| 189 |
continue; |
| 190 |
} |
| 191 |
|
| 192 |
// Break if found the integration |
| 193 |
if( $integration->code === $integration_name ) { |
| 194 |
break; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
if( $integration === null ) { |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
// Bail if integration if already installed |
| 203 |
if( class_exists( $integration->integration_class ) ) { |
| 204 |
return; |
| 205 |
} |
| 206 |
|
| 207 |
// Bail if integration plugin is not installed |
| 208 |
if( ! automatorwp_automation_ui_integration_plugin_installed( $integration ) ) { |
| 209 |
return; |
| 210 |
} |
| 211 |
|
| 212 |
// Get integration items |
| 213 |
$items = array(); |
| 214 |
|
| 215 |
if( $item_type === 'trigger' ) { |
| 216 |
|
| 217 |
$choices = automatorwp_get_integration_triggers( $integration_name ); |
| 218 |
|
| 219 |
// Don't list if already listed |
| 220 |
if( count( $choices ) ) { |
| 221 |
return; |
| 222 |
} |
| 223 |
|
| 224 |
$items = $integration->triggers; |
| 225 |
|
| 226 |
} else if( $item_type === 'action' ) { |
| 227 |
|
| 228 |
$choices = automatorwp_get_integration_actions( $integration_name ); |
| 229 |
|
| 230 |
// Don't list if already listed |
| 231 |
if( count( $choices ) ) { |
| 232 |
return; |
| 233 |
} |
| 234 |
|
| 235 |
$items = $integration->actions; |
| 236 |
} |
| 237 |
|
| 238 |
// Check the free and pro elements |
| 239 |
$has_free = false; |
| 240 |
$has_pro = false; |
| 241 |
|
| 242 |
foreach( $items as $item ) { |
| 243 |
|
| 244 |
// For triggers, only get anonymous choices |
| 245 |
if( $item_type === 'trigger' && $automation->type === 'anonymous' && ! $item->anonymous ) { |
| 246 |
continue; |
| 247 |
} |
| 248 |
|
| 249 |
if( $item->free ) { |
| 250 |
$has_free = true; |
| 251 |
} else { |
| 252 |
$has_pro = true; |
| 253 |
} |
| 254 |
|
| 255 |
} |
| 256 |
|
| 257 |
if( ! $has_free && $has_pro ) : ?> |
| 258 |
|
| 259 |
<div class="automatorwp-integration automatorwp-integration-pro" |
| 260 |
data-integration="<?php echo esc_attr( $integration_name ); ?>" |
| 261 |
data-label="<?php echo esc_attr( $args['label'] ); ?>" |
| 262 |
data-icon="<?php echo esc_attr( $args['icon'] ); ?>"> |
| 263 |
<span class="automatorwp-integration-pro-badge">PRO</span> |
| 264 |
<div class="automatorwp-integration-icon"> |
| 265 |
<img src="<?php echo esc_attr( $args['icon'] ); ?>" alt="<?php echo esc_attr( $args['label'] ); ?>"> |
| 266 |
</div> |
| 267 |
<div class="automatorwp-integration-label"><?php echo $args['label']; ?></div> |
| 268 |
</div> |
| 269 |
|
| 270 |
<?php endif; |
| 271 |
|
| 272 |
} |
| 273 |
add_action( 'automatorwp_automation_ui_after_integration_choice', 'automatorwp_automation_ui_integration_pro_choice', 10, 4 ); |
| 274 |
|
| 275 |
/** |
| 276 |
* Inform about integration triggers pro choices |
| 277 |
* |
| 278 |
* @since 1.2.4 |
| 279 |
* |
| 280 |
* @param string $integration_name The integration name |
| 281 |
* @param array $args Integration arguments |
| 282 |
* @param stdClass $automation The automation object |
| 283 |
* @param string $item_type The item type |
| 284 |
*/ |
| 285 |
function automatorwp_automation_ui_integration_triggers_pro_choices( $integration_name, $args, $automation, $item_type ) { |
| 286 |
|
| 287 |
if( (bool) automatorwp_get_option( 'disable_pro_recommendations' ) ) { |
| 288 |
return; |
| 289 |
} |
| 290 |
|
| 291 |
$integrations = automatorwp_integrations_api(); |
| 292 |
|
| 293 |
if ( is_array( $integrations ) && empty( $integrations) ) { |
| 294 |
return; |
| 295 |
} |
| 296 |
|
| 297 |
if( is_wp_error( $integrations ) ) { |
| 298 |
return; |
| 299 |
} |
| 300 |
|
| 301 |
foreach ( $integrations as $integration ) { |
| 302 |
if( ! isset( $integration ) ) { |
| 303 |
continue; |
| 304 |
} |
| 305 |
|
| 306 |
// Break if found the integration |
| 307 |
if( $integration->code === $integration_name ) { |
| 308 |
break; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
if( $integration === null ) { |
| 313 |
return; |
| 314 |
} |
| 315 |
|
| 316 |
// Bail if integration is already installed |
| 317 |
if( class_exists( $integration->integration_class ) ) { |
| 318 |
return; |
| 319 |
} |
| 320 |
|
| 321 |
// Bail if integration plugin is not installed |
| 322 |
if( ! automatorwp_automation_ui_integration_plugin_installed( $integration ) ) { |
| 323 |
return; |
| 324 |
} |
| 325 |
|
| 326 |
// Get the list of already listed triggers |
| 327 |
$choices = automatorwp_get_integration_triggers( $integration_name ); |
| 328 |
|
| 329 |
foreach( $integration->triggers as $i => $trigger ) { |
| 330 |
|
| 331 |
// For triggers, only get anonymous choices |
| 332 |
if( $automation->type === 'anonymous' && ! $trigger->anonymous ) { |
| 333 |
continue; |
| 334 |
} |
| 335 |
|
| 336 |
// Skip free triggers |
| 337 |
if( $trigger->free ) { |
| 338 |
continue; |
| 339 |
} |
| 340 |
|
| 341 |
// Skip if trigger label is empty |
| 342 |
if( empty( $trigger->label ) ) { |
| 343 |
continue; |
| 344 |
} |
| 345 |
|
| 346 |
// Skip if integration is already installed |
| 347 |
if( property_exists( $trigger, 'required_class' ) && ! empty( $trigger->required_class ) ) { |
| 348 |
if( class_exists( $trigger->required_class ) ) { |
| 349 |
continue; |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
$already_listed = false; |
| 354 |
|
| 355 |
foreach( $choices as $choice ) { |
| 356 |
// Check if trigger has been already listed |
| 357 |
if( $choice['label'] === $trigger->label ) { |
| 358 |
$already_listed = true; |
| 359 |
break; |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
// Skip triggers already listed |
| 364 |
if( $already_listed ) { |
| 365 |
continue; |
| 366 |
} ?> |
| 367 |
<option value="<?php echo esc_attr( $integration_name ) . '_' . $i; ?>" disabled="disabled"><?php echo $trigger->label; ?></option> |
| 368 |
<?php |
| 369 |
} |
| 370 |
|
| 371 |
} |
| 372 |
add_action( 'automatorwp_automation_ui_after_integration_triggers_choices', 'automatorwp_automation_ui_integration_triggers_pro_choices', 10, 4 ); |
| 373 |
|
| 374 |
/** |
| 375 |
* Inform about integration actions pro choices |
| 376 |
* |
| 377 |
* @since 1.2.4 |
| 378 |
* |
| 379 |
* @param string $integration_name The integration name |
| 380 |
* @param array $args Integration arguments |
| 381 |
* @param stdClass $automation The automation object |
| 382 |
* @param string $item_type The item type |
| 383 |
*/ |
| 384 |
function automatorwp_automation_ui_integration_actions_pro_choices( $integration_name, $args, $automation, $item_type ) { |
| 385 |
|
| 386 |
if( (bool) automatorwp_get_option( 'disable_pro_recommendations' ) ) { |
| 387 |
return; |
| 388 |
} |
| 389 |
|
| 390 |
$integrations = automatorwp_integrations_api(); |
| 391 |
|
| 392 |
if ( is_array( $integrations ) && empty( $integrations) ) { |
| 393 |
return; |
| 394 |
} |
| 395 |
|
| 396 |
if( is_wp_error( $integrations ) ) { |
| 397 |
return; |
| 398 |
} |
| 399 |
|
| 400 |
foreach ( $integrations as $integration ) { |
| 401 |
if( ! isset( $integration ) ) { |
| 402 |
continue; |
| 403 |
} |
| 404 |
// Break if found the integration |
| 405 |
if( $integration->code === $integration_name ) { |
| 406 |
break; |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
if( $integration === null ) { |
| 411 |
return; |
| 412 |
} |
| 413 |
|
| 414 |
// Bail if integration is already installed |
| 415 |
if( class_exists( $integration->integration_class ) ) { |
| 416 |
return; |
| 417 |
} |
| 418 |
|
| 419 |
// Bail if integration plugin is not installed |
| 420 |
if( ! automatorwp_automation_ui_integration_plugin_installed( $integration ) ) { |
| 421 |
return; |
| 422 |
} |
| 423 |
|
| 424 |
// Get the list of already listed actions |
| 425 |
$choices = automatorwp_get_integration_actions( $integration_name ); |
| 426 |
|
| 427 |
foreach( $integration->actions as $i => $action ) { |
| 428 |
// Skip free actions |
| 429 |
if( $action->free ) { |
| 430 |
continue; |
| 431 |
} |
| 432 |
|
| 433 |
// Skip if action label is empty |
| 434 |
if( empty( $action->label ) ) { |
| 435 |
continue; |
| 436 |
} |
| 437 |
|
| 438 |
// Skip if integration is already installed |
| 439 |
if( property_exists( $action, 'required_class' ) && ! empty( $action->required_class ) ) { |
| 440 |
if( class_exists( $action->required_class ) ) { |
| 441 |
continue; |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
$already_listed = false; |
| 446 |
|
| 447 |
foreach( $choices as $choice ) { |
| 448 |
// Check if action has been already listed |
| 449 |
if( $choice['label'] === $action->label ) { |
| 450 |
$already_listed = true; |
| 451 |
break; |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
// Skip actions already listed |
| 456 |
if( $already_listed ) { |
| 457 |
continue; |
| 458 |
} ?> |
| 459 |
<option value="<?php echo esc_attr( $integration_name ) . '_' . $i; ?>" disabled="disabled"><?php echo $action->label; ?></option> |
| 460 |
|
| 461 |
<?php |
| 462 |
} |
| 463 |
|
| 464 |
} |
| 465 |
add_action( 'automatorwp_automation_ui_after_integration_actions_choices', 'automatorwp_automation_ui_integration_actions_pro_choices', 10, 4 ); |
| 466 |
|
| 467 |
/** |
| 468 |
* Check if integration plugin is installed |
| 469 |
* |
| 470 |
* @since 1.5.1 |
| 471 |
* |
| 472 |
* @param stdClass $integration |
| 473 |
*/ |
| 474 |
function automatorwp_automation_ui_integration_plugin_installed( $integration ) { |
| 475 |
|
| 476 |
if( $integration === null || ! is_object( $integration ) ) { |
| 477 |
return false; |
| 478 |
} |
| 479 |
|
| 480 |
if( $integration->code === 'automatorwp' || $integration->code === 'wordpress' ) { |
| 481 |
return true; |
| 482 |
} |
| 483 |
|
| 484 |
// Check if required class exists |
| 485 |
if( property_exists( $integration, 'required_class' ) && ! empty( $integration->required_class ) ) { |
| 486 |
return class_exists( $integration->required_class ); |
| 487 |
} |
| 488 |
|
| 489 |
// Check if required function exists |
| 490 |
if( property_exists( $integration, 'required_function' ) && ! empty( $integration->required_function ) ) { |
| 491 |
return function_exists( $integration->required_function ); |
| 492 |
} |
| 493 |
|
| 494 |
// Check if required constant is defined |
| 495 |
if( property_exists( $integration, 'required_constant' ) && ! empty( $integration->required_constant ) ) { |
| 496 |
return defined( $integration->required_constant ); |
| 497 |
} |
| 498 |
|
| 499 |
return false; |
| 500 |
|
| 501 |
} |