PluginProbe
Extendify / 3.0.4
Extendify v3.0.4
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
extendify / app / Assist / Controllers / DomainsSuggestionController.php

DomainsSuggestionController.php in Extendify 3.0.4, at app/Assist/Controllers/DomainsSuggestionController.php

154 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 'partnerId' => \esc_attr(constant('EXTENDIFY_PARTNER_ID')),
79 'wpLanguage' => \get_locale(),
80 'wpVersion' => \get_bloginfo('version'),
81 'businessDescription' => \esc_attr($businessDescription),
82 ];
83
84 $response = \wp_remote_post(
85 sprintf('%s/api/domains/suggest', static::$host),
86 [
87 'body' => \wp_json_encode($data),
88 'headers' => ['Content-Type' => 'application/json'],
89 ]
90 );
91
92 if (is_wp_error($response)) {
93 return new \WP_REST_Response([]);
94 }
95
96 $body = wp_remote_retrieve_body($response);
97 if (empty($body)) {
98 return new \WP_REST_Response([]);
99 }
100
101 return new \WP_REST_Response(json_decode($body, true), \wp_remote_retrieve_response_code($response));
102 }
103
104 /**
105 * Clean site title.
106 *
107 * @param string $siteTitle - The site title to clean.
108 * @return string
109 */
110 public static function cleanSiteTitle($siteTitle)
111 {
112 return preg_replace('/[^\p{L}\p{N}\s\-]+/u', '', html_entity_decode($siteTitle));
113 }
114
115 /**
116 * Check if the site Title is part of the blocked list.
117 *
118 * @param string $siteTitle - The site title to check.
119 * @return bool
120 */
121 public static function hasValidSiteTitle($siteTitle)
122 {
123 return empty(array_filter(self::$blockList, function ($item) use ($siteTitle) {
124 // in php 8.0 we can use str_contains.
125 return strpos(strtolower($siteTitle), strtolower($item)) !== false;
126 }));
127 }
128
129 /**
130 * Delete the cache for the domains suggestions.
131 *
132 * @return \WP_REST_Response
133 */
134 public static function deleteCache()
135 {
136 \delete_transient('extendify_domains');
137
138 return new \WP_REST_Response(['success' => true]);
139 }
140
141 /**
142 * Persist the tracking data
143 *
144 * @param \WP_REST_Request $request - The request.
145 * @return \WP_REST_Response
146 */
147 public static function tracking($request)
148 {
149 $data = json_decode($request->get_param('state'), true);
150 update_option('extendify_domains_recommendations_activities', Sanitizer::sanitizeArray($data));
151 return new \WP_REST_Response($data);
152 }
153 }
154