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