PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.6.1
Defender Security – Malware Scanner, Login Security & Firewall v5.6.1
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / component / class-hub-connector.php

class-hub-connector.php in Defender Security – Malware Scanner, Login Security & Firewall 5.6.1, at src/component/class-hub-connector.php

251 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Hub Connector class.
4 *
5 * @package WP_Defender\Component
6 */
7
8 namespace WP_Defender\Component;
9
10 use WPMUDEV\Hub\Connector;
11 use Calotes\Base\Component;
12 use WP_Defender\Traits\Defender_Dashboard_Client;
13
14 /**
15 * Handles the functionality related to the Hub Connector module.
16 *
17 * @since 4.10.0
18 */
19 class Hub_Connector extends Component {
20 use Defender_Dashboard_Client;
21
22 /**
23 * The identifier for the WP Defender plugin in the Hub.
24 *
25 * @const string
26 */
27 public const PLUGIN_IDENTIFIER = 'defender';
28
29 /**
30 * The action name used for the Hub connection.
31 *
32 * @const string
33 */
34 public const CONNECTION_ACTION = 'hub_connection';
35
36 /**
37 * Checks if the Hub connector should render its UI.
38 *
39 * Verifies the nonce from the global-ip page to determine if the Hub connector should render its UI.
40 *
41 * @return bool The nonce value on success, false on failure.
42 */
43 public static function should_render() {
44 return ! self::get_status() && wp_verify_nonce( defender_get_data_from_request( '_def_nonce', 'g' ), self::CONNECTION_ACTION );
45 }
46
47 /**
48 * Initialize the Hub Connector module and set its options.
49 *
50 * The `extra/hub-connector/connector.php` file is required, and the options are set for the Hub Connector module.
51 *
52 * @return void
53 */
54 public function init() {
55 require_once defender_path( 'extra/hub-connector/connector.php' );
56 $options = array(
57 'screens' => array(
58 'toplevel_page_wp-defender',
59 'toplevel_page_wp-defender-network',
60 'defender_page_wdf-ip-lockout',
61 'defender_page_wdf-ip-lockout-network',
62 'defender-pro_page_wdf-ip-lockout',
63 'defender-pro_page_wdf-ip-lockout-network',
64 ),
65 'extra_args' => array(
66 'register' => array(
67 'connect_ref' => self::PLUGIN_IDENTIFIER,
68 'utm_medium' => 'plugin',
69 'utm_source' => self::PLUGIN_IDENTIFIER,
70 ),
71 ),
72 );
73 // Get advanced params.
74 $page = defender_get_data_from_request( 'page', 'g' );
75 $view = defender_get_data_from_request( 'view', 'g' );
76
77 $result = $this->maybe_summary_box_trigger( $view );
78 // Get advanced params.
79 $res = $this->get_utm_tags( $page, $result['view'], $result['is_summary'] );
80 if ( ! empty( $res['utm_campaign'] ) ) {
81 $options['extra_args']['register']['utm_campaign'] = $res['utm_campaign'];
82 }
83 if ( ! empty( $res['utm_content'] ) ) {
84 $options['extra_args']['register']['utm_content'] = $res['utm_content'];
85 }
86
87 Connector::get()->set_options( self::PLUGIN_IDENTIFIER, $options );
88 }
89
90 /**
91 * Gets the label for the button to connect the site to the Hub.
92 *
93 * @return string The label for the button.
94 */
95 public function get_button_label(): string {
96 if ( $this->is_dash_installed() && ! $this->is_dash_activated() ) {
97 return __( 'Activate WPMU DEV Dashboard', 'defender-security' );
98 }
99 if ( $this->is_dash_activated() ) {
100 return __( 'Log in to WPMU DEV', 'defender-security' );
101 }
102 return __( 'Connect site for Full protection', 'defender-security' );
103 }
104
105 /**
106 * Modify text string vars.
107 *
108 * @param array $texts Vars.
109 * @param string $plugin Plugin identifier.
110 *
111 * @return array
112 */
113 public static function customize_text_vars( $texts, $plugin ): array {
114 if ( self::PLUGIN_IDENTIFIER === $plugin ) {
115 $feature_name = self::get_feature_name();
116 $texts['create_account_desc'] = sprintf(
117 /* translators: 1. Feature name. 2. Opened tag. 3. Closed tag. */
118 esc_html__( 'Create a free account to connect your site to WPMU DEV and activate %1$s. %2$sIt`s fast, seamless, and free%3$s.', 'defender-security' ),
119 '<strong>' . __( 'Defender - ', 'defender-security' ) . $feature_name . '</strong>',
120 '<i>',
121 '</i>'
122 );
123 $texts['login_desc'] = sprintf(
124 /* translators: %s: Feature name. */
125 esc_html__( 'Log in with your WPMU DEV account credentials to activate %s.', 'defender-security' ),
126 $feature_name
127 );
128 }
129
130 return $texts;
131 }
132
133 /**
134 * Get feature's name.
135 *
136 * @return string
137 */
138 public static function get_feature_name(): string {
139 $feature_name = defender_get_data_from_request( 'module_name', 'g' );
140 if ( ! empty( $feature_name ) ) {
141 return $feature_name;
142 }
143
144 $view = defender_get_data_from_request( 'view', 'g' );
145 switch ( $view ) {
146 case 'blocklist':
147 return __( 'Custom IP Allow/Block list', 'defender-security' );
148 default:
149 return __( 'AntiBot Global Firewall', 'defender-security' );
150 }
151 }
152
153 /**
154 * Get UTM tags.
155 *
156 * @param string $page The page to load.
157 * @param string $view The view to load.
158 * @param bool $is_summary Is this from the Summary section? Default false.
159 *
160 * @return array
161 */
162 private function get_utm_tags( string $page = '', string $view = '', bool $is_summary = false ): array {
163 $utm_campaign = '';
164 $utm_content = '';
165 if ( ! empty( $page ) ) {
166 switch ( $page ) {
167 // There are buttons on notice or widget on the Dashboard page.
168 case 'wp-defender':
169 $utm_content = 'hub-connector';
170 $utm_campaign = ( 'dashboard' === $view )
171 ? 'defender_dashboard_firewall_antibot'
172 : 'defender_onboarding_antibot';
173 break;
174 case 'wdf-ip-lockout':
175 $utm_content = 'hub-connector';
176 if ( $is_summary ) {
177 $utm_campaign = 'defender_firewall_antibot_summary';
178 } else {
179 $utm_campaign = ( 'blocklist' === $view )
180 ? 'defender_firewall_centralip'
181 : 'defender_firewall_antibot';
182 }
183 break;
184 default:
185 break;
186 }
187 }
188
189 return array(
190 'utm_campaign' => $utm_campaign,
191 'utm_content' => $utm_content,
192 );
193 }
194
195 /**
196 * Update data if a trigger is the Summary section.
197 *
198 * @param string $view The view to load.
199 *
200 * @return array
201 */
202 private function maybe_summary_box_trigger( string $view ): array {
203 return array(
204 'view' => 'summary-box' === $view ? 'global-ip' : $view,
205 'is_summary' => 'summary-box' === $view,
206 );
207 }
208
209 /**
210 * Retrieve the Hub connector URL.
211 *
212 * @param string $page Optional. The page to load. Default is empty string.
213 * @param string $view Optional. The view to load. Default is empty string.
214 *
215 * @return string
216 */
217 public function get_url( string $page = '', string $view = '' ): string {
218 if ( $this->is_dash_activated() ) {
219 $args = array(
220 'page' => 'wpmudev',
221 'utm_source' => self::PLUGIN_IDENTIFIER,
222 'utm_medium' => 'plugin',
223 );
224
225 $result = $this->maybe_summary_box_trigger( $view );
226 // Get advanced params.
227 $res = $this->get_utm_tags( $page, $result['view'], $result['is_summary'] );
228 if ( ! empty( $res['utm_campaign'] ) ) {
229 $args['utm_campaign'] = $res['utm_campaign'];
230 }
231 if ( ! empty( $res['utm_content'] ) ) {
232 $args['utm_content'] = $res['utm_content'];
233 }
234
235 return add_query_arg( $args, network_admin_url( 'admin.php' ) );
236 }
237
238 $query = array(
239 '_def_nonce' => wp_create_nonce( self::CONNECTION_ACTION ),
240 );
241 if ( ! empty( $page ) ) {
242 $query['page'] = $page;
243 }
244 if ( ! empty( $view ) ) {
245 $query['view'] = $view;
246 }
247
248 return add_query_arg( $query, network_admin_url( 'admin.php' ) );
249 }
250 }
251