Completed
Pull Request — master (#1548)
by Naveen
52s
created
src/wordlift/configuration/class-config.php 2 patches
Indentation   +151 added lines, -151 removed lines patch added patch discarded remove patch
@@ -4,186 +4,186 @@
 block discarded – undo
4 4
 
5 5
 
6 6
 class Config {
7
-	/**
8
-	 * @var \Wordlift_Admin_Setup
9
-	 */
10
-	private $admin_setup;
11
-	/**
12
-	 * @var \Wordlift_Key_Validation_Service
13
-	 */
14
-	private $key_validation_service;
15
-
16
-	/**
17
-	 * Config constructor.
18
-	 *
19
-	 * @param $admin_setup \Wordlift_Admin_Setup
20
-	 * @param $key_validation_service \Wordlift_Key_Validation_Service
21
-	 */
22
-	public function __construct( $admin_setup, $key_validation_service ) {
23
-
24
-		$this->admin_setup            = $admin_setup;
25
-		$this->key_validation_service = $key_validation_service;
26
-		add_action( 'wp_ajax_nopriv_wl_config_plugin', array( $this, 'config' ) );
27
-
28
-	}
29
-
30
-	/**
31
-	 * Check if the key is valid and also not bound to any domain.
32
-	 *
33
-	 * @param $key string
34
-	 *
35
-	 * @return bool
36
-	 */
37
-	private function is_key_valid_and_not_bound_to_any_domain( $key ) {
38
-		$account_info = $this->key_validation_service->get_account_info( $key );
39
-
40
-		/**
41
-		 * we need to check if the key is not associated with any account
42
-		 * before setting it, we should check if the url is null.
43
-		 */
44
-		if ( is_wp_error( $account_info )
45
-		     || wp_remote_retrieve_response_code( $account_info ) !== 200 ) {
46
-			return false;
47
-		}
48
-
49
-		$account_info_json = $account_info['body'];
50
-
51
-		$account_info_data = json_decode( $account_info_json, true );
52
-
53
-		if ( ! $account_info_data ) {
54
-			// Invalid json returned by api.
55
-			return false;
56
-		}
57
-
58
-		$site_url = apply_filters( 'wl_production_site_url', untrailingslashit( get_option( 'home' ) ) );
59
-
60
-
61
-		if ( $account_info_data['url'] === null ) {
62
-			return true;
63
-		}
64
-
65
-		// Check if the key belongs to same site.
66
-		if ( $site_url !== untrailingslashit( $account_info_data['url'] ) ) {
67
-			// key already associated with another account.
68
-			return false;
69
-		}
70
-
71
-		// Return true if the key domain and site domain are the same.
72
-		return true;
73
-	}
74
-
75
-
76
-	public function config() {
77
-
78
-		// Perform validation check for all the parameters.
79
-		$required_fields = array(
80
-			'diagnostic',
81
-			'vocabulary',
7
+    /**
8
+     * @var \Wordlift_Admin_Setup
9
+     */
10
+    private $admin_setup;
11
+    /**
12
+     * @var \Wordlift_Key_Validation_Service
13
+     */
14
+    private $key_validation_service;
15
+
16
+    /**
17
+     * Config constructor.
18
+     *
19
+     * @param $admin_setup \Wordlift_Admin_Setup
20
+     * @param $key_validation_service \Wordlift_Key_Validation_Service
21
+     */
22
+    public function __construct( $admin_setup, $key_validation_service ) {
23
+
24
+        $this->admin_setup            = $admin_setup;
25
+        $this->key_validation_service = $key_validation_service;
26
+        add_action( 'wp_ajax_nopriv_wl_config_plugin', array( $this, 'config' ) );
27
+
28
+    }
29
+
30
+    /**
31
+     * Check if the key is valid and also not bound to any domain.
32
+     *
33
+     * @param $key string
34
+     *
35
+     * @return bool
36
+     */
37
+    private function is_key_valid_and_not_bound_to_any_domain( $key ) {
38
+        $account_info = $this->key_validation_service->get_account_info( $key );
39
+
40
+        /**
41
+         * we need to check if the key is not associated with any account
42
+         * before setting it, we should check if the url is null.
43
+         */
44
+        if ( is_wp_error( $account_info )
45
+             || wp_remote_retrieve_response_code( $account_info ) !== 200 ) {
46
+            return false;
47
+        }
48
+
49
+        $account_info_json = $account_info['body'];
50
+
51
+        $account_info_data = json_decode( $account_info_json, true );
52
+
53
+        if ( ! $account_info_data ) {
54
+            // Invalid json returned by api.
55
+            return false;
56
+        }
57
+
58
+        $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( get_option( 'home' ) ) );
59
+
60
+
61
+        if ( $account_info_data['url'] === null ) {
62
+            return true;
63
+        }
64
+
65
+        // Check if the key belongs to same site.
66
+        if ( $site_url !== untrailingslashit( $account_info_data['url'] ) ) {
67
+            // key already associated with another account.
68
+            return false;
69
+        }
70
+
71
+        // Return true if the key domain and site domain are the same.
72
+        return true;
73
+    }
74
+
75
+
76
+    public function config() {
77
+
78
+        // Perform validation check for all the parameters.
79
+        $required_fields = array(
80
+            'diagnostic',
81
+            'vocabulary',
82 82
 // Don't ask for language from webapp.
83 83
 //			'language',
84
-			'country',
85
-			'publisherName',
86
-			'publisher',
87
-			'license'
88
-		);
84
+            'country',
85
+            'publisherName',
86
+            'publisher',
87
+            'license'
88
+        );
89 89
 
90
-		header( 'Access-Control-Allow-Origin: *' );
90
+        header( 'Access-Control-Allow-Origin: *' );
91 91
 
92
-		// validate all the fields before processing
93
-		foreach ( $required_fields as $field ) {
94
-			if ( ! array_key_exists( $field, $_POST ) ) {
95
-				wp_send_json_error( sprintf( __( 'Field %s is required', 'wordlift' ), $field ), 422 );
92
+        // validate all the fields before processing
93
+        foreach ( $required_fields as $field ) {
94
+            if ( ! array_key_exists( $field, $_POST ) ) {
95
+                wp_send_json_error( sprintf( __( 'Field %s is required', 'wordlift' ), $field ), 422 );
96 96
 
97
-				return;
98
-			}
99
-		}
97
+                return;
98
+            }
99
+        }
100 100
 
101
-		$key = (string) $_POST['license'];
101
+        $key = (string) $_POST['license'];
102 102
 
103
-		if ( ! $this->is_key_valid_and_not_bound_to_any_domain( $key ) ) {
104
-			wp_send_json_error( __( 'Key is not valid or associated with other domain', 'wordlift' ), 403 );
103
+        if ( ! $this->is_key_valid_and_not_bound_to_any_domain( $key ) ) {
104
+            wp_send_json_error( __( 'Key is not valid or associated with other domain', 'wordlift' ), 403 );
105 105
 
106
-			// exit if not valid.
107
-			return;
108
-		}
106
+            // exit if not valid.
107
+            return;
108
+        }
109 109
 
110 110
 
111
-		// check if key is already configured, if yes then dont save settings.
112
-		if ( \Wordlift_Configuration_Service::get_instance()->get_key() ) {
113
-			wp_send_json_error( __( 'Key already configured.', 'wordlift' ), 403 );
111
+        // check if key is already configured, if yes then dont save settings.
112
+        if ( \Wordlift_Configuration_Service::get_instance()->get_key() ) {
113
+            wp_send_json_error( __( 'Key already configured.', 'wordlift' ), 403 );
114 114
 
115
-			// key already configured
116
-			return;
117
-		}
115
+            // key already configured
116
+            return;
117
+        }
118 118
 
119
-		$this->admin_setup->save_configuration( $this->get_params() );
119
+        $this->admin_setup->save_configuration( $this->get_params() );
120 120
 
121 121
 
122
-		wp_send_json_success( __( 'Configuration Saved', 'wordlift' ) );
123
-	}
122
+        wp_send_json_success( __( 'Configuration Saved', 'wordlift' ) );
123
+    }
124 124
 
125
-	/**
126
-	 *
127
-	 * @return array
128
-	 */
129
-	private function get_params() {
125
+    /**
126
+     *
127
+     * @return array
128
+     */
129
+    private function get_params() {
130 130
 
131
-		$attachment_id = $this->may_be_get_attachment_id();
131
+        $attachment_id = $this->may_be_get_attachment_id();
132 132
 
133
-		$params = array(
134
-			'key'              => isset( $_POST['license'] ) ? (string) $_POST['license'] : '',
135
-			'vocabulary'       => isset( $_POST['vocabulary'] ) ? (string) $_POST['vocabulary'] : '',
136
-			'wl-country-code'  => isset( $_POST['country'] ) ? (string) $_POST['country'] : '',
137
-			'name'             => isset( $_POST['publisherName'] ) ? (string) $_POST['publisherName'] : '',
138
-			'user_type'        => isset( $_POST['publisher'] ) ? (string) $_POST['publisher'] : '',
139
-			'logo'             => $attachment_id
140
-		);
133
+        $params = array(
134
+            'key'              => isset( $_POST['license'] ) ? (string) $_POST['license'] : '',
135
+            'vocabulary'       => isset( $_POST['vocabulary'] ) ? (string) $_POST['vocabulary'] : '',
136
+            'wl-country-code'  => isset( $_POST['country'] ) ? (string) $_POST['country'] : '',
137
+            'name'             => isset( $_POST['publisherName'] ) ? (string) $_POST['publisherName'] : '',
138
+            'user_type'        => isset( $_POST['publisher'] ) ? (string) $_POST['publisher'] : '',
139
+            'logo'             => $attachment_id
140
+        );
141 141
 
142
-		$diagnostic = isset( $_POST['diagnostic'] ) ? (bool) $_POST['diagnostic'] : false;
143
-		if ( $diagnostic ) {
144
-			$params['share-diagnostic'] = 'on';
145
-		}
142
+        $diagnostic = isset( $_POST['diagnostic'] ) ? (bool) $_POST['diagnostic'] : false;
143
+        if ( $diagnostic ) {
144
+            $params['share-diagnostic'] = 'on';
145
+        }
146 146
 
147
-		return $params;
148
-	}
147
+        return $params;
148
+    }
149 149
 
150
-	/**
151
-	 * @return int | bool
152
-	 */
153
-	private function may_be_get_attachment_id() {
150
+    /**
151
+     * @return int | bool
152
+     */
153
+    private function may_be_get_attachment_id() {
154 154
 
155
-		// if image or image extension not posted then return false.
156
-		if ( ! isset( $_POST['image'] ) || ! isset( $_POST['imageExtension'] ) ) {
157
-			return false;
158
-		}
155
+        // if image or image extension not posted then return false.
156
+        if ( ! isset( $_POST['image'] ) || ! isset( $_POST['imageExtension'] ) ) {
157
+            return false;
158
+        }
159 159
 
160
-		$allowed_extensions = array( 'png', 'jpeg', 'jpg' );
161
-		$image_string       = (string) $_POST['image'];
162
-		$image_ext          = (string) $_POST['imageExtension'];
160
+        $allowed_extensions = array( 'png', 'jpeg', 'jpg' );
161
+        $image_string       = (string) $_POST['image'];
162
+        $image_ext          = (string) $_POST['imageExtension'];
163 163
 
164
-		if ( ! in_array( $image_ext, $allowed_extensions ) ) {
165
-			return false;
166
-		}
164
+        if ( ! in_array( $image_ext, $allowed_extensions ) ) {
165
+            return false;
166
+        }
167 167
 
168
-		$image_decoded_string = base64_decode( $image_string );
168
+        $image_decoded_string = base64_decode( $image_string );
169 169
 
170
-		$upload_dir = wp_upload_dir();
170
+        $upload_dir = wp_upload_dir();
171 171
 
172
-		$file_path = $upload_dir['path'] . DIRECTORY_SEPARATOR . md5( $image_string ) . "." . $image_ext;
172
+        $file_path = $upload_dir['path'] . DIRECTORY_SEPARATOR . md5( $image_string ) . "." . $image_ext;
173 173
 
174
-		file_put_contents( $file_path, $image_decoded_string );
174
+        file_put_contents( $file_path, $image_decoded_string );
175 175
 
176
-		$attachment_id = wp_insert_attachment( array(
177
-			'post_status'    => 'inherit',
178
-			'post_mime_type' => "image/$image_ext"
179
-		), $file_path );
176
+        $attachment_id = wp_insert_attachment( array(
177
+            'post_status'    => 'inherit',
178
+            'post_mime_type' => "image/$image_ext"
179
+        ), $file_path );
180 180
 
181
-		// Generate the metadata for the attachment, and update the database record.
182
-		$attachment_data = wp_generate_attachment_metadata( $attachment_id, $file_path );
183
-		// Update the attachment metadata.
184
-		wp_update_attachment_metadata( $attachment_id, $attachment_data );
181
+        // Generate the metadata for the attachment, and update the database record.
182
+        $attachment_data = wp_generate_attachment_metadata( $attachment_id, $file_path );
183
+        // Update the attachment metadata.
184
+        wp_update_attachment_metadata( $attachment_id, $attachment_data );
185 185
 
186
-		return $attachment_id;
187
-	}
186
+        return $attachment_id;
187
+    }
188 188
 
189 189
 }
Please login to merge, or discard this patch.
Spacing   +38 added lines, -38 removed lines patch added patch discarded remove patch
@@ -19,11 +19,11 @@  discard block
 block discarded – undo
19 19
 	 * @param $admin_setup \Wordlift_Admin_Setup
20 20
 	 * @param $key_validation_service \Wordlift_Key_Validation_Service
21 21
 	 */
22
-	public function __construct( $admin_setup, $key_validation_service ) {
22
+	public function __construct($admin_setup, $key_validation_service) {
23 23
 
24 24
 		$this->admin_setup            = $admin_setup;
25 25
 		$this->key_validation_service = $key_validation_service;
26
-		add_action( 'wp_ajax_nopriv_wl_config_plugin', array( $this, 'config' ) );
26
+		add_action('wp_ajax_nopriv_wl_config_plugin', array($this, 'config'));
27 27
 
28 28
 	}
29 29
 
@@ -34,36 +34,36 @@  discard block
 block discarded – undo
34 34
 	 *
35 35
 	 * @return bool
36 36
 	 */
37
-	private function is_key_valid_and_not_bound_to_any_domain( $key ) {
38
-		$account_info = $this->key_validation_service->get_account_info( $key );
37
+	private function is_key_valid_and_not_bound_to_any_domain($key) {
38
+		$account_info = $this->key_validation_service->get_account_info($key);
39 39
 
40 40
 		/**
41 41
 		 * we need to check if the key is not associated with any account
42 42
 		 * before setting it, we should check if the url is null.
43 43
 		 */
44
-		if ( is_wp_error( $account_info )
45
-		     || wp_remote_retrieve_response_code( $account_info ) !== 200 ) {
44
+		if (is_wp_error($account_info)
45
+		     || wp_remote_retrieve_response_code($account_info) !== 200) {
46 46
 			return false;
47 47
 		}
48 48
 
49 49
 		$account_info_json = $account_info['body'];
50 50
 
51
-		$account_info_data = json_decode( $account_info_json, true );
51
+		$account_info_data = json_decode($account_info_json, true);
52 52
 
53
-		if ( ! $account_info_data ) {
53
+		if ( ! $account_info_data) {
54 54
 			// Invalid json returned by api.
55 55
 			return false;
56 56
 		}
57 57
 
58
-		$site_url = apply_filters( 'wl_production_site_url', untrailingslashit( get_option( 'home' ) ) );
58
+		$site_url = apply_filters('wl_production_site_url', untrailingslashit(get_option('home')));
59 59
 
60 60
 
61
-		if ( $account_info_data['url'] === null ) {
61
+		if ($account_info_data['url'] === null) {
62 62
 			return true;
63 63
 		}
64 64
 
65 65
 		// Check if the key belongs to same site.
66
-		if ( $site_url !== untrailingslashit( $account_info_data['url'] ) ) {
66
+		if ($site_url !== untrailingslashit($account_info_data['url'])) {
67 67
 			// key already associated with another account.
68 68
 			return false;
69 69
 		}
@@ -87,12 +87,12 @@  discard block
 block discarded – undo
87 87
 			'license'
88 88
 		);
89 89
 
90
-		header( 'Access-Control-Allow-Origin: *' );
90
+		header('Access-Control-Allow-Origin: *');
91 91
 
92 92
 		// validate all the fields before processing
93
-		foreach ( $required_fields as $field ) {
94
-			if ( ! array_key_exists( $field, $_POST ) ) {
95
-				wp_send_json_error( sprintf( __( 'Field %s is required', 'wordlift' ), $field ), 422 );
93
+		foreach ($required_fields as $field) {
94
+			if ( ! array_key_exists($field, $_POST)) {
95
+				wp_send_json_error(sprintf(__('Field %s is required', 'wordlift'), $field), 422);
96 96
 
97 97
 				return;
98 98
 			}
@@ -100,8 +100,8 @@  discard block
 block discarded – undo
100 100
 
101 101
 		$key = (string) $_POST['license'];
102 102
 
103
-		if ( ! $this->is_key_valid_and_not_bound_to_any_domain( $key ) ) {
104
-			wp_send_json_error( __( 'Key is not valid or associated with other domain', 'wordlift' ), 403 );
103
+		if ( ! $this->is_key_valid_and_not_bound_to_any_domain($key)) {
104
+			wp_send_json_error(__('Key is not valid or associated with other domain', 'wordlift'), 403);
105 105
 
106 106
 			// exit if not valid.
107 107
 			return;
@@ -109,17 +109,17 @@  discard block
 block discarded – undo
109 109
 
110 110
 
111 111
 		// check if key is already configured, if yes then dont save settings.
112
-		if ( \Wordlift_Configuration_Service::get_instance()->get_key() ) {
113
-			wp_send_json_error( __( 'Key already configured.', 'wordlift' ), 403 );
112
+		if (\Wordlift_Configuration_Service::get_instance()->get_key()) {
113
+			wp_send_json_error(__('Key already configured.', 'wordlift'), 403);
114 114
 
115 115
 			// key already configured
116 116
 			return;
117 117
 		}
118 118
 
119
-		$this->admin_setup->save_configuration( $this->get_params() );
119
+		$this->admin_setup->save_configuration($this->get_params());
120 120
 
121 121
 
122
-		wp_send_json_success( __( 'Configuration Saved', 'wordlift' ) );
122
+		wp_send_json_success(__('Configuration Saved', 'wordlift'));
123 123
 	}
124 124
 
125 125
 	/**
@@ -131,16 +131,16 @@  discard block
 block discarded – undo
131 131
 		$attachment_id = $this->may_be_get_attachment_id();
132 132
 
133 133
 		$params = array(
134
-			'key'              => isset( $_POST['license'] ) ? (string) $_POST['license'] : '',
135
-			'vocabulary'       => isset( $_POST['vocabulary'] ) ? (string) $_POST['vocabulary'] : '',
136
-			'wl-country-code'  => isset( $_POST['country'] ) ? (string) $_POST['country'] : '',
137
-			'name'             => isset( $_POST['publisherName'] ) ? (string) $_POST['publisherName'] : '',
138
-			'user_type'        => isset( $_POST['publisher'] ) ? (string) $_POST['publisher'] : '',
134
+			'key'              => isset($_POST['license']) ? (string) $_POST['license'] : '',
135
+			'vocabulary'       => isset($_POST['vocabulary']) ? (string) $_POST['vocabulary'] : '',
136
+			'wl-country-code'  => isset($_POST['country']) ? (string) $_POST['country'] : '',
137
+			'name'             => isset($_POST['publisherName']) ? (string) $_POST['publisherName'] : '',
138
+			'user_type'        => isset($_POST['publisher']) ? (string) $_POST['publisher'] : '',
139 139
 			'logo'             => $attachment_id
140 140
 		);
141 141
 
142
-		$diagnostic = isset( $_POST['diagnostic'] ) ? (bool) $_POST['diagnostic'] : false;
143
-		if ( $diagnostic ) {
142
+		$diagnostic = isset($_POST['diagnostic']) ? (bool) $_POST['diagnostic'] : false;
143
+		if ($diagnostic) {
144 144
 			$params['share-diagnostic'] = 'on';
145 145
 		}
146 146
 
@@ -153,35 +153,35 @@  discard block
 block discarded – undo
153 153
 	private function may_be_get_attachment_id() {
154 154
 
155 155
 		// if image or image extension not posted then return false.
156
-		if ( ! isset( $_POST['image'] ) || ! isset( $_POST['imageExtension'] ) ) {
156
+		if ( ! isset($_POST['image']) || ! isset($_POST['imageExtension'])) {
157 157
 			return false;
158 158
 		}
159 159
 
160
-		$allowed_extensions = array( 'png', 'jpeg', 'jpg' );
160
+		$allowed_extensions = array('png', 'jpeg', 'jpg');
161 161
 		$image_string       = (string) $_POST['image'];
162 162
 		$image_ext          = (string) $_POST['imageExtension'];
163 163
 
164
-		if ( ! in_array( $image_ext, $allowed_extensions ) ) {
164
+		if ( ! in_array($image_ext, $allowed_extensions)) {
165 165
 			return false;
166 166
 		}
167 167
 
168
-		$image_decoded_string = base64_decode( $image_string );
168
+		$image_decoded_string = base64_decode($image_string);
169 169
 
170 170
 		$upload_dir = wp_upload_dir();
171 171
 
172
-		$file_path = $upload_dir['path'] . DIRECTORY_SEPARATOR . md5( $image_string ) . "." . $image_ext;
172
+		$file_path = $upload_dir['path'].DIRECTORY_SEPARATOR.md5($image_string).".".$image_ext;
173 173
 
174
-		file_put_contents( $file_path, $image_decoded_string );
174
+		file_put_contents($file_path, $image_decoded_string);
175 175
 
176
-		$attachment_id = wp_insert_attachment( array(
176
+		$attachment_id = wp_insert_attachment(array(
177 177
 			'post_status'    => 'inherit',
178 178
 			'post_mime_type' => "image/$image_ext"
179
-		), $file_path );
179
+		), $file_path);
180 180
 
181 181
 		// Generate the metadata for the attachment, and update the database record.
182
-		$attachment_data = wp_generate_attachment_metadata( $attachment_id, $file_path );
182
+		$attachment_data = wp_generate_attachment_metadata($attachment_id, $file_path);
183 183
 		// Update the attachment metadata.
184
-		wp_update_attachment_metadata( $attachment_id, $attachment_data );
184
+		wp_update_attachment_metadata($attachment_id, $attachment_data);
185 185
 
186 186
 		return $attachment_id;
187 187
 	}
Please login to merge, or discard this patch.