| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Controls Suggest Domains |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace Extendify\Assist\Controllers; |
| 8 |
|
| 9 |
use Extendify\Shared\Services\Sanitizer; |
| 10 |
|
| 11 |
defined('ABSPATH') || die('No direct access.'); |
| 12 |
|
| 13 |
/** |
| 14 |
* The controller for fetching quick links |
| 15 |
*/ |
| 16 |
|
| 17 |
class DomainsSuggestionController |
| 18 |
{ |
| 19 |
/** |
| 20 |
* The url for the server. |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
public static $host = 'https://ai.extendify.com'; |
| 25 |
|
| 26 |
/** |
| 27 |
* The list of url strings in the site name to block from using the api. |
| 28 |
* |
| 29 |
* @var array |
| 30 |
*/ |
| 31 |
public static $blockList = ['instawp.xyz']; |
| 32 |
|
| 33 |
// phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh |
| 34 |
/** |
| 35 |
* Return domains recommendation. |
| 36 |
* |
| 37 |
* @return \WP_REST_Response |
| 38 |
*/ |
| 39 |
public static function fetchDomainSuggestions() |
| 40 |
{ |
| 41 |
if (!defined('EXTENDIFY_PARTNER_ID')) { |
| 42 |
return new \WP_REST_Response([], 200); |
| 43 |
} |
| 44 |
|
| 45 |
// Get the data directly from the database. |
| 46 |
$partnerData = \get_option('extendify_partner_data_v2', []); |
| 47 |
|
| 48 |
// Return early if neither of the banners are enabled. |
| 49 |
if ( |
| 50 |
!($partnerData['showDomainBanner'] ?? false) |
| 51 |
&& !($partnerData['showDomainTask'] ?? false) |
| 52 |
&& !($partnerData['showSecondaryDomainBanner'] ?? false) |
| 53 |
&& !($partnerData['showSecondaryDomainTask'] ?? false) |
| 54 |
) { |
| 55 |
return new \WP_REST_Response([]); |
| 56 |
} |
| 57 |
|
| 58 |
$siteName = \get_bloginfo('name'); |
| 59 |
|
| 60 |
// in case of an exact match we should not do a request. |
| 61 |
if (in_array(strtolower($siteName), ['wordpress', 'my blog'], true)) { |
| 62 |
return new \WP_REST_Response([]); |
| 63 |
} |
| 64 |
|
| 65 |
if (!self::hasValidSiteTitle($siteName)) { |
| 66 |
return new \WP_REST_Response([]); |
| 67 |
} |
| 68 |
|
| 69 |
$siteProfile = \get_option('extendify_site_profile', []); |
| 70 |
$businessDescription = ($siteProfile['description'] ?? ''); |
| 71 |
$data = [ |
| 72 |
'query' => self::cleanSiteTitle($siteName), |
| 73 |
'devbuild' => defined('EXTENDIFY_DEVMODE') |
| 74 |
? constant('EXTENDIFY_DEVMODE') |
| 75 |
: is_readable(EXTENDIFY_PATH . '.devbuild'), |
| 76 |
'siteId' => \get_option('extendify_site_id', ''), |
| 77 |
'tlds' => ($partnerData['domainTLDs'] ?? []), |
| 78 |
'priorityTlds' => ($partnerData['priorityDomainTLDs'] ?? []), |
| 79 |
'partnerId' => \esc_attr(constant('EXTENDIFY_PARTNER_ID')), |
| 80 |
'wpLanguage' => \get_locale(), |
| 81 |
'wpVersion' => \get_bloginfo('version'), |
| 82 |
'businessDescription' => \esc_attr($businessDescription), |
| 83 |
'siteProfile' => $siteProfile, |
| 84 |
]; |
| 85 |
|
| 86 |
$response = \wp_remote_post( |
| 87 |
sprintf('%s/api/domains/suggest', static::$host), |
| 88 |
[ |
| 89 |
'body' => \wp_json_encode($data), |
| 90 |
'headers' => ['Content-Type' => 'application/json'], |
| 91 |
] |
| 92 |
); |
| 93 |
|
| 94 |
if (is_wp_error($response)) { |
| 95 |
return new \WP_REST_Response([]); |
| 96 |
} |
| 97 |
|
| 98 |
$status = \wp_remote_retrieve_response_code($response); |
| 99 |
if ($status < 200 || $status >= 300) { |
| 100 |
return new \WP_REST_Response([]); |
| 101 |
} |
| 102 |
|
| 103 |
$body = wp_remote_retrieve_body($response); |
| 104 |
if (empty($body)) { |
| 105 |
return new \WP_REST_Response([]); |
| 106 |
} |
| 107 |
|
| 108 |
return new \WP_REST_Response(json_decode($body, true), $status); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Clean site title. |
| 113 |
* |
| 114 |
* @param string $siteTitle - The site title to clean. |
| 115 |
* @return string |
| 116 |
*/ |
| 117 |
public static function cleanSiteTitle($siteTitle) |
| 118 |
{ |
| 119 |
return preg_replace('/[^\p{L}\p{N}\s\-]+/u', '', html_entity_decode($siteTitle)); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Check if the site Title is part of the blocked list. |
| 124 |
* |
| 125 |
* @param string $siteTitle - The site title to check. |
| 126 |
* @return bool |
| 127 |
*/ |
| 128 |
public static function hasValidSiteTitle($siteTitle) |
| 129 |
{ |
| 130 |
return empty(array_filter(self::$blockList, function ($item) use ($siteTitle) { |
| 131 |
// in php 8.0 we can use str_contains. |
| 132 |
return strpos(strtolower($siteTitle), strtolower($item)) !== false; |
| 133 |
})); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Delete the cache for the domains suggestions. |
| 138 |
* |
| 139 |
* @return \WP_REST_Response |
| 140 |
*/ |
| 141 |
public static function deleteCache() |
| 142 |
{ |
| 143 |
\delete_transient('extendify_domains'); |
| 144 |
|
| 145 |
return new \WP_REST_Response(['success' => true]); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Persist the tracking data |
| 150 |
* |
| 151 |
* @param \WP_REST_Request $request - The request. |
| 152 |
* @return \WP_REST_Response |
| 153 |
*/ |
| 154 |
public static function tracking($request) |
| 155 |
{ |
| 156 |
$data = json_decode($request->get_param('state'), true); |
| 157 |
update_option('extendify_domains_recommendations_activities', Sanitizer::sanitizeArray($data)); |
| 158 |
return new \WP_REST_Response($data); |
| 159 |
} |
| 160 |
} |
| 161 |
|