Completed
Push — develop ( bd9722...bf0832 )
by Naveen
01:23 queued 17s
created
src/modules/food-kg/includes/admin/Main_Ingredient_List_Table.php 2 patches
Indentation   +109 added lines, -109 removed lines patch added patch discarded remove patch
@@ -3,43 +3,43 @@  discard block
 block discarded – undo
3 3
 namespace Wordlift\Modules\Food_Kg\Admin;
4 4
 
5 5
 if ( ! class_exists( 'WP_List_Table' ) ) {
6
-	require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
6
+    require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
7 7
 }
8 8
 
9 9
 use WP_List_Table;
10 10
 
11 11
 class Main_Ingredient_List_Table extends WP_List_Table {
12 12
 
13
-	public function prepare_items() {
14
-		global $wpdb; // This is used only if making any database queries
15
-
16
-		/**
17
-		 * REQUIRED. Now we need to define our column headers. This includes a complete
18
-		 * array of columns to be displayed (slugs & titles), a list of columns
19
-		 * to keep hidden, and a list of columns that are sortable. Each of these
20
-		 * can be defined in another method (as we've done here) before being
21
-		 * used to build the value for our _column_headers property.
22
-		 */
23
-		$columns  = $this->get_columns();
24
-		$hidden   = array();
25
-		$sortable = $this->get_sortable_columns();
26
-
27
-		/**
28
-		 * REQUIRED. Finally, we build an array to be used by the class for column
29
-		 * headers. The $this->_column_headers property takes an array which contains
30
-		 * 3 other arrays. One for all columns, one for hidden columns, and one
31
-		 * for sortable columns.
32
-		 */
33
-		$this->_column_headers = array( $columns, $hidden, $sortable );
34
-
35
-		// Pagination.
36
-		$per_page     = 20;
37
-		$current_page = $this->get_pagenum();
38
-		$total_items  = $this->count();
39
-
40
-		$this->items = $wpdb->get_results(
41
-			$wpdb->prepare(
42
-				"SELECT p1.ID AS recipe_ID,
13
+    public function prepare_items() {
14
+        global $wpdb; // This is used only if making any database queries
15
+
16
+        /**
17
+         * REQUIRED. Now we need to define our column headers. This includes a complete
18
+         * array of columns to be displayed (slugs & titles), a list of columns
19
+         * to keep hidden, and a list of columns that are sortable. Each of these
20
+         * can be defined in another method (as we've done here) before being
21
+         * used to build the value for our _column_headers property.
22
+         */
23
+        $columns  = $this->get_columns();
24
+        $hidden   = array();
25
+        $sortable = $this->get_sortable_columns();
26
+
27
+        /**
28
+         * REQUIRED. Finally, we build an array to be used by the class for column
29
+         * headers. The $this->_column_headers property takes an array which contains
30
+         * 3 other arrays. One for all columns, one for hidden columns, and one
31
+         * for sortable columns.
32
+         */
33
+        $this->_column_headers = array( $columns, $hidden, $sortable );
34
+
35
+        // Pagination.
36
+        $per_page     = 20;
37
+        $current_page = $this->get_pagenum();
38
+        $total_items  = $this->count();
39
+
40
+        $this->items = $wpdb->get_results(
41
+            $wpdb->prepare(
42
+                "SELECT p1.ID AS recipe_ID,
43 43
 					    p1.post_title AS recipe_name,
44 44
 					    p2.ID AS post_ID,
45 45
 					    p2.post_title
@@ -49,99 +49,99 @@  discard block
 block discarded – undo
49 49
 							INNER JOIN $wpdb->postmeta pm2
50 50
 								ON pm2.post_ID = pm1.post_ID AND pm2.meta_key = 'wprm_parent_post_id'
51 51
 						    INNER JOIN $wpdb->posts p2"
52
-				// The following ignore rule is used against the `LIKE CONCAT`. We only have const values.
53
-				// phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.LikeWildcardsInQuery
54
-				. " ON p2.post_status = 'publish' AND p2.ID = pm2.post_ID
52
+                // The following ignore rule is used against the `LIKE CONCAT`. We only have const values.
53
+                // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.LikeWildcardsInQuery
54
+                . " ON p2.post_status = 'publish' AND p2.ID = pm2.post_ID
55 55
 							WHERE pm1.meta_key = '_wl_main_ingredient_jsonld'
56 56
 					LIMIT %d
57 57
 					OFFSET %d",
58
-				$per_page,
59
-				( $current_page - 1 ) * $per_page
60
-			)
61
-		);
58
+                $per_page,
59
+                ( $current_page - 1 ) * $per_page
60
+            )
61
+        );
62 62
 
63
-		$this->set_pagination_args(
64
-			array(
65
-				'total_items' => $total_items,
66
-				'per_page'    => $per_page,
67
-				'total_pages' => ceil( $total_items / $per_page ),
68
-			)
69
-		);
70
-	}
63
+        $this->set_pagination_args(
64
+            array(
65
+                'total_items' => $total_items,
66
+                'per_page'    => $per_page,
67
+                'total_pages' => ceil( $total_items / $per_page ),
68
+            )
69
+        );
70
+    }
71 71
 
72
-	private function count() {
73
-		global $wpdb;
72
+    private function count() {
73
+        global $wpdb;
74 74
 
75
-		$count = get_transient( '_wl_main_ingredient_list_table__count' );
75
+        $count = get_transient( '_wl_main_ingredient_list_table__count' );
76 76
 
77
-		if ( ! $count ) {
77
+        if ( ! $count ) {
78 78
 
79
-			$count = $wpdb->get_var(
80
-				"SELECT COUNT(1)
79
+            $count = $wpdb->get_var(
80
+                "SELECT COUNT(1)
81 81
 						FROM $wpdb->postmeta pm1
82 82
 						    INNER JOIN $wpdb->posts p1
83 83
 						        ON p1.ID = pm1.post_ID AND p1.post_type = 'wprm_recipe'
84 84
 							INNER JOIN $wpdb->postmeta pm2
85 85
 								ON pm2.post_ID = pm1.post_ID AND pm2.meta_key = 'wprm_parent_post_id'
86 86
 						    INNER JOIN $wpdb->posts p2"
87
-				// The following ignore rule is used against the `LIKE CONCAT`. We only have const values.
88
-				// phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.LikeWildcardsInQuery
89
-				. " ON p2.post_status = 'publish' AND p2.ID = pm2.post_ID
87
+                // The following ignore rule is used against the `LIKE CONCAT`. We only have const values.
88
+                // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.LikeWildcardsInQuery
89
+                . " ON p2.post_status = 'publish' AND p2.ID = pm2.post_ID
90 90
 							WHERE pm1.meta_key = '_wl_main_ingredient_jsonld'"
91
-			);
92
-
93
-			set_transient( '_wl_main_ingredient_list_table__count', $count, 60 );
94
-		}
95
-
96
-		return $count;
97
-	}
98
-
99
-	public function no_items() {
100
-		esc_html_e( 'No main ingredients found.', 'wordlift' );
101
-	}
102
-
103
-	public function get_columns() {
104
-		return array(
105
-			'ingredient_name' => __( 'Ingredient Name', 'wordlift' ),
106
-			'recipe_name'     => __( 'Recipe Name', 'wordlift' ),
107
-			'post_title'      => __( 'Post Title', 'wordlift' ),
108
-			'url'             => __( 'Post URL', 'wordlift' ),
109
-			'actions'         => '',
110
-		);
111
-	}
112
-
113
-	public function column_ingredient_name( $item ) {
114
-		$recipe_json_ld = get_post_meta( $item->recipe_ID, '_wl_main_ingredient_jsonld', true ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
115
-		$recipe         = json_decode( $recipe_json_ld, true );
116
-
117
-		return $recipe ? $recipe['name'] : 'null';
118
-	}
119
-
120
-	public function column_recipe_name( $item ) {
121
-		return $item->recipe_name;
122
-	}
123
-
124
-	public function column_post_title( $item ) {
125
-		return sprintf( '<a href="%s">%s</a>', get_edit_post_link( $item->post_ID ), $item->post_title );
126
-	}
127
-
128
-	public function column_url( $item ) {
129
-		return get_permalink( $item->post_ID );
130
-	}
131
-
132
-	public function column_actions( $item ) {
133
-
134
-		$url = admin_url(
135
-			sprintf( 'admin.php?page=wl_ingredients&modal_window=true&id=%d&TB_iframe=true', $item->recipe_ID )  // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
136
-		);
137
-
138
-		return sprintf(
139
-			'<a href="%s" class="button alignright thickbox open-plugin-details-modal" data-title="%s" type="button">%s</a>',
140
-			$url,
141
-			esc_attr( $item->post_title ),
142
-			esc_html__( 'JSON-LD', 'wordlift' )
143
-		);
144
-	}
91
+            );
92
+
93
+            set_transient( '_wl_main_ingredient_list_table__count', $count, 60 );
94
+        }
95
+
96
+        return $count;
97
+    }
98
+
99
+    public function no_items() {
100
+        esc_html_e( 'No main ingredients found.', 'wordlift' );
101
+    }
102
+
103
+    public function get_columns() {
104
+        return array(
105
+            'ingredient_name' => __( 'Ingredient Name', 'wordlift' ),
106
+            'recipe_name'     => __( 'Recipe Name', 'wordlift' ),
107
+            'post_title'      => __( 'Post Title', 'wordlift' ),
108
+            'url'             => __( 'Post URL', 'wordlift' ),
109
+            'actions'         => '',
110
+        );
111
+    }
112
+
113
+    public function column_ingredient_name( $item ) {
114
+        $recipe_json_ld = get_post_meta( $item->recipe_ID, '_wl_main_ingredient_jsonld', true ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
115
+        $recipe         = json_decode( $recipe_json_ld, true );
116
+
117
+        return $recipe ? $recipe['name'] : 'null';
118
+    }
119
+
120
+    public function column_recipe_name( $item ) {
121
+        return $item->recipe_name;
122
+    }
123
+
124
+    public function column_post_title( $item ) {
125
+        return sprintf( '<a href="%s">%s</a>', get_edit_post_link( $item->post_ID ), $item->post_title );
126
+    }
127
+
128
+    public function column_url( $item ) {
129
+        return get_permalink( $item->post_ID );
130
+    }
131
+
132
+    public function column_actions( $item ) {
133
+
134
+        $url = admin_url(
135
+            sprintf( 'admin.php?page=wl_ingredients&modal_window=true&id=%d&TB_iframe=true', $item->recipe_ID )  // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
136
+        );
137
+
138
+        return sprintf(
139
+            '<a href="%s" class="button alignright thickbox open-plugin-details-modal" data-title="%s" type="button">%s</a>',
140
+            $url,
141
+            esc_attr( $item->post_title ),
142
+            esc_html__( 'JSON-LD', 'wordlift' )
143
+        );
144
+    }
145 145
 
146 146
 }
147 147
 
Please login to merge, or discard this patch.
Spacing   +25 added lines, -25 removed lines patch added patch discarded remove patch
@@ -2,8 +2,8 @@  discard block
 block discarded – undo
2 2
 
3 3
 namespace Wordlift\Modules\Food_Kg\Admin;
4 4
 
5
-if ( ! class_exists( 'WP_List_Table' ) ) {
6
-	require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
5
+if ( ! class_exists('WP_List_Table')) {
6
+	require_once ABSPATH.'wp-admin/includes/class-wp-list-table.php';
7 7
 }
8 8
 
9 9
 use WP_List_Table;
@@ -30,7 +30,7 @@  discard block
 block discarded – undo
30 30
 		 * 3 other arrays. One for all columns, one for hidden columns, and one
31 31
 		 * for sortable columns.
32 32
 		 */
33
-		$this->_column_headers = array( $columns, $hidden, $sortable );
33
+		$this->_column_headers = array($columns, $hidden, $sortable);
34 34
 
35 35
 		// Pagination.
36 36
 		$per_page     = 20;
@@ -56,7 +56,7 @@  discard block
 block discarded – undo
56 56
 					LIMIT %d
57 57
 					OFFSET %d",
58 58
 				$per_page,
59
-				( $current_page - 1 ) * $per_page
59
+				($current_page - 1) * $per_page
60 60
 			)
61 61
 		);
62 62
 
@@ -64,7 +64,7 @@  discard block
 block discarded – undo
64 64
 			array(
65 65
 				'total_items' => $total_items,
66 66
 				'per_page'    => $per_page,
67
-				'total_pages' => ceil( $total_items / $per_page ),
67
+				'total_pages' => ceil($total_items / $per_page),
68 68
 			)
69 69
 		);
70 70
 	}
@@ -72,9 +72,9 @@  discard block
 block discarded – undo
72 72
 	private function count() {
73 73
 		global $wpdb;
74 74
 
75
-		$count = get_transient( '_wl_main_ingredient_list_table__count' );
75
+		$count = get_transient('_wl_main_ingredient_list_table__count');
76 76
 
77
-		if ( ! $count ) {
77
+		if ( ! $count) {
78 78
 
79 79
 			$count = $wpdb->get_var(
80 80
 				"SELECT COUNT(1)
@@ -90,56 +90,56 @@  discard block
 block discarded – undo
90 90
 							WHERE pm1.meta_key = '_wl_main_ingredient_jsonld'"
91 91
 			);
92 92
 
93
-			set_transient( '_wl_main_ingredient_list_table__count', $count, 60 );
93
+			set_transient('_wl_main_ingredient_list_table__count', $count, 60);
94 94
 		}
95 95
 
96 96
 		return $count;
97 97
 	}
98 98
 
99 99
 	public function no_items() {
100
-		esc_html_e( 'No main ingredients found.', 'wordlift' );
100
+		esc_html_e('No main ingredients found.', 'wordlift');
101 101
 	}
102 102
 
103 103
 	public function get_columns() {
104 104
 		return array(
105
-			'ingredient_name' => __( 'Ingredient Name', 'wordlift' ),
106
-			'recipe_name'     => __( 'Recipe Name', 'wordlift' ),
107
-			'post_title'      => __( 'Post Title', 'wordlift' ),
108
-			'url'             => __( 'Post URL', 'wordlift' ),
105
+			'ingredient_name' => __('Ingredient Name', 'wordlift'),
106
+			'recipe_name'     => __('Recipe Name', 'wordlift'),
107
+			'post_title'      => __('Post Title', 'wordlift'),
108
+			'url'             => __('Post URL', 'wordlift'),
109 109
 			'actions'         => '',
110 110
 		);
111 111
 	}
112 112
 
113
-	public function column_ingredient_name( $item ) {
114
-		$recipe_json_ld = get_post_meta( $item->recipe_ID, '_wl_main_ingredient_jsonld', true ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
115
-		$recipe         = json_decode( $recipe_json_ld, true );
113
+	public function column_ingredient_name($item) {
114
+		$recipe_json_ld = get_post_meta($item->recipe_ID, '_wl_main_ingredient_jsonld', true); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
115
+		$recipe         = json_decode($recipe_json_ld, true);
116 116
 
117 117
 		return $recipe ? $recipe['name'] : 'null';
118 118
 	}
119 119
 
120
-	public function column_recipe_name( $item ) {
120
+	public function column_recipe_name($item) {
121 121
 		return $item->recipe_name;
122 122
 	}
123 123
 
124
-	public function column_post_title( $item ) {
125
-		return sprintf( '<a href="%s">%s</a>', get_edit_post_link( $item->post_ID ), $item->post_title );
124
+	public function column_post_title($item) {
125
+		return sprintf('<a href="%s">%s</a>', get_edit_post_link($item->post_ID), $item->post_title);
126 126
 	}
127 127
 
128
-	public function column_url( $item ) {
129
-		return get_permalink( $item->post_ID );
128
+	public function column_url($item) {
129
+		return get_permalink($item->post_ID);
130 130
 	}
131 131
 
132
-	public function column_actions( $item ) {
132
+	public function column_actions($item) {
133 133
 
134 134
 		$url = admin_url(
135
-			sprintf( 'admin.php?page=wl_ingredients&modal_window=true&id=%d&TB_iframe=true', $item->recipe_ID )  // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
135
+			sprintf('admin.php?page=wl_ingredients&modal_window=true&id=%d&TB_iframe=true', $item->recipe_ID)  // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
136 136
 		);
137 137
 
138 138
 		return sprintf(
139 139
 			'<a href="%s" class="button alignright thickbox open-plugin-details-modal" data-title="%s" type="button">%s</a>',
140 140
 			$url,
141
-			esc_attr( $item->post_title ),
142
-			esc_html__( 'JSON-LD', 'wordlift' )
141
+			esc_attr($item->post_title),
142
+			esc_html__('JSON-LD', 'wordlift')
143 143
 		);
144 144
 	}
145 145
 
Please login to merge, or discard this patch.
src/wordlift/shipping-data/class-flat-rate-shipping-method.php 2 patches
Spacing   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -6,40 +6,40 @@
 block discarded – undo
6 6
 
7 7
 class Flat_Rate_Shipping_Method extends Shipping_Method {
8 8
 
9
-	public function add_available_delivery_method( &$jsonld ) {
9
+	public function add_available_delivery_method(&$jsonld) {
10 10
 
11
-		if ( ! isset( $jsonld['availableDeliveryMethod'] ) ) {
11
+		if ( ! isset($jsonld['availableDeliveryMethod'])) {
12 12
 			$jsonld['availableDeliveryMethod'] = array();
13 13
 		}
14 14
 
15
-		if ( ! is_array( $jsonld['availableDeliveryMethod'] ) ) {
16
-			$jsonld['availableDeliveryMethod'] = array( $jsonld['availableDeliveryMethod'] );
15
+		if ( ! is_array($jsonld['availableDeliveryMethod'])) {
16
+			$jsonld['availableDeliveryMethod'] = array($jsonld['availableDeliveryMethod']);
17 17
 		}
18 18
 
19
-		if ( ! in_array( 'ParcelDelivery', $jsonld['availableDeliveryMethod'], true ) ) {
20
-			array_push( $jsonld['availableDeliveryMethod'], 'ParcelDelivery' );
19
+		if ( ! in_array('ParcelDelivery', $jsonld['availableDeliveryMethod'], true)) {
20
+			array_push($jsonld['availableDeliveryMethod'], 'ParcelDelivery');
21 21
 		}
22 22
 
23 23
 	}
24 24
 
25
-	public function add_shipping_rate( &$offer_shipping_details ) {
25
+	public function add_shipping_rate(&$offer_shipping_details) {
26 26
 
27
-		if ( ! isset( $offer_shipping_details['shippingRate'] ) ) {
27
+		if ( ! isset($offer_shipping_details['shippingRate'])) {
28 28
 			$offer_shipping_details['shippingRate'] = array();
29 29
 		}
30 30
 
31
-		$description = isset( $this->wc_shipping_method->instance_settings['description'] )
32
-			? wp_strip_all_tags( $this->wc_shipping_method->instance_settings['description'] ) : '';
31
+		$description = isset($this->wc_shipping_method->instance_settings['description'])
32
+			? wp_strip_all_tags($this->wc_shipping_method->instance_settings['description']) : '';
33 33
 
34 34
 		$shipping_rate = array(
35 35
 			'@type'       => 'MonetaryAmount',
36 36
 			'name'        => $this->wc_shipping_method->get_title(),
37 37
 			'description' => $description,
38
-			'value'       => $this->wc_shipping_method->get_option( 'cost' ),
38
+			'value'       => $this->wc_shipping_method->get_option('cost'),
39 39
 			'currency'    => get_woocommerce_currency(),
40 40
 		);
41 41
 
42
-		$this->change_to_manual_currency( $shipping_rate );
42
+		$this->change_to_manual_currency($shipping_rate);
43 43
 
44 44
 		$offer_shipping_details['shippingRate'][] = $shipping_rate;
45 45
 
Please login to merge, or discard this patch.
Indentation   +27 added lines, -27 removed lines patch added patch discarded remove patch
@@ -4,43 +4,43 @@
 block discarded – undo
4 4
 
5 5
 class Flat_Rate_Shipping_Method extends Shipping_Method {
6 6
 
7
-	public function add_available_delivery_method( &$jsonld ) {
7
+    public function add_available_delivery_method( &$jsonld ) {
8 8
 
9
-		if ( ! isset( $jsonld['availableDeliveryMethod'] ) ) {
10
-			$jsonld['availableDeliveryMethod'] = array();
11
-		}
9
+        if ( ! isset( $jsonld['availableDeliveryMethod'] ) ) {
10
+            $jsonld['availableDeliveryMethod'] = array();
11
+        }
12 12
 
13
-		if ( ! is_array( $jsonld['availableDeliveryMethod'] ) ) {
14
-			$jsonld['availableDeliveryMethod'] = array( $jsonld['availableDeliveryMethod'] );
15
-		}
13
+        if ( ! is_array( $jsonld['availableDeliveryMethod'] ) ) {
14
+            $jsonld['availableDeliveryMethod'] = array( $jsonld['availableDeliveryMethod'] );
15
+        }
16 16
 
17
-		if ( ! in_array( 'ParcelDelivery', $jsonld['availableDeliveryMethod'], true ) ) {
18
-			array_push( $jsonld['availableDeliveryMethod'], 'ParcelDelivery' );
19
-		}
17
+        if ( ! in_array( 'ParcelDelivery', $jsonld['availableDeliveryMethod'], true ) ) {
18
+            array_push( $jsonld['availableDeliveryMethod'], 'ParcelDelivery' );
19
+        }
20 20
 
21
-	}
21
+    }
22 22
 
23
-	public function add_shipping_rate( &$offer_shipping_details ) {
23
+    public function add_shipping_rate( &$offer_shipping_details ) {
24 24
 
25
-		if ( ! isset( $offer_shipping_details['shippingRate'] ) ) {
26
-			$offer_shipping_details['shippingRate'] = array();
27
-		}
25
+        if ( ! isset( $offer_shipping_details['shippingRate'] ) ) {
26
+            $offer_shipping_details['shippingRate'] = array();
27
+        }
28 28
 
29
-		$description = isset( $this->wc_shipping_method->instance_settings['description'] )
30
-			? wp_strip_all_tags( $this->wc_shipping_method->instance_settings['description'] ) : '';
29
+        $description = isset( $this->wc_shipping_method->instance_settings['description'] )
30
+            ? wp_strip_all_tags( $this->wc_shipping_method->instance_settings['description'] ) : '';
31 31
 
32
-		$shipping_rate = array(
33
-			'@type'       => 'MonetaryAmount',
34
-			'name'        => $this->wc_shipping_method->get_title(),
35
-			'description' => $description,
36
-			'value'       => $this->wc_shipping_method->get_option( 'cost' ),
37
-			'currency'    => get_woocommerce_currency(),
38
-		);
32
+        $shipping_rate = array(
33
+            '@type'       => 'MonetaryAmount',
34
+            'name'        => $this->wc_shipping_method->get_title(),
35
+            'description' => $description,
36
+            'value'       => $this->wc_shipping_method->get_option( 'cost' ),
37
+            'currency'    => get_woocommerce_currency(),
38
+        );
39 39
 
40
-		$this->change_to_manual_currency( $shipping_rate );
40
+        $this->change_to_manual_currency( $shipping_rate );
41 41
 
42
-		$offer_shipping_details['shippingRate'][] = $shipping_rate;
42
+        $offer_shipping_details['shippingRate'][] = $shipping_rate;
43 43
 
44
-	}
44
+    }
45 45
 
46 46
 }
Please login to merge, or discard this patch.
src/wordlift/shipping-data/class-free-shipping-shipping-method.php 2 patches
Indentation   +27 added lines, -27 removed lines patch added patch discarded remove patch
@@ -4,43 +4,43 @@
 block discarded – undo
4 4
 
5 5
 class Free_Shipping_Shipping_Method extends Shipping_Method {
6 6
 
7
-	public function add_available_delivery_method( &$jsonld ) {
7
+    public function add_available_delivery_method( &$jsonld ) {
8 8
 
9
-		if ( ! isset( $jsonld['availableDeliveryMethod'] ) ) {
10
-			$jsonld['availableDeliveryMethod'] = array();
11
-		}
9
+        if ( ! isset( $jsonld['availableDeliveryMethod'] ) ) {
10
+            $jsonld['availableDeliveryMethod'] = array();
11
+        }
12 12
 
13
-		if ( ! is_array( $jsonld['availableDeliveryMethod'] ) ) {
14
-			$jsonld['availableDeliveryMethod'] = array( $jsonld['availableDeliveryMethod'] );
15
-		}
13
+        if ( ! is_array( $jsonld['availableDeliveryMethod'] ) ) {
14
+            $jsonld['availableDeliveryMethod'] = array( $jsonld['availableDeliveryMethod'] );
15
+        }
16 16
 
17
-		if ( ! in_array( 'ParcelDelivery', $jsonld['availableDeliveryMethod'], true ) ) {
18
-			array_push( $jsonld['availableDeliveryMethod'], 'ParcelDelivery' );
19
-		}
17
+        if ( ! in_array( 'ParcelDelivery', $jsonld['availableDeliveryMethod'], true ) ) {
18
+            array_push( $jsonld['availableDeliveryMethod'], 'ParcelDelivery' );
19
+        }
20 20
 
21
-	}
21
+    }
22 22
 
23
-	public function add_shipping_rate( &$offer_shipping_details ) {
23
+    public function add_shipping_rate( &$offer_shipping_details ) {
24 24
 
25
-		if ( ! isset( $offer_shipping_details['shippingRate'] ) ) {
26
-			$offer_shipping_details['shippingRate'] = array();
27
-		}
25
+        if ( ! isset( $offer_shipping_details['shippingRate'] ) ) {
26
+            $offer_shipping_details['shippingRate'] = array();
27
+        }
28 28
 
29
-		$description = isset( $this->wc_shipping_method->instance_settings['description'] )
30
-			? wp_strip_all_tags( $this->wc_shipping_method->instance_settings['description'] ) : '';
29
+        $description = isset( $this->wc_shipping_method->instance_settings['description'] )
30
+            ? wp_strip_all_tags( $this->wc_shipping_method->instance_settings['description'] ) : '';
31 31
 
32
-		$shipping_rate = array(
33
-			'@type'       => 'MonetaryAmount',
34
-			'name'        => $this->wc_shipping_method->get_title(),
35
-			'description' => $description,
36
-			'value'       => '0',
37
-			'currency'    => get_woocommerce_currency(),
38
-		);
32
+        $shipping_rate = array(
33
+            '@type'       => 'MonetaryAmount',
34
+            'name'        => $this->wc_shipping_method->get_title(),
35
+            'description' => $description,
36
+            'value'       => '0',
37
+            'currency'    => get_woocommerce_currency(),
38
+        );
39 39
 
40
-		$this->change_to_manual_currency( $shipping_rate );
40
+        $this->change_to_manual_currency( $shipping_rate );
41 41
 
42
-		$offer_shipping_details['shippingRate'][] = $shipping_rate;
42
+        $offer_shipping_details['shippingRate'][] = $shipping_rate;
43 43
 
44
-	}
44
+    }
45 45
 
46 46
 }
Please login to merge, or discard this patch.
Spacing   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -4,30 +4,30 @@  discard block
 block discarded – undo
4 4
 
5 5
 class Free_Shipping_Shipping_Method extends Shipping_Method {
6 6
 
7
-	public function add_available_delivery_method( &$jsonld ) {
7
+	public function add_available_delivery_method(&$jsonld) {
8 8
 
9
-		if ( ! isset( $jsonld['availableDeliveryMethod'] ) ) {
9
+		if ( ! isset($jsonld['availableDeliveryMethod'])) {
10 10
 			$jsonld['availableDeliveryMethod'] = array();
11 11
 		}
12 12
 
13
-		if ( ! is_array( $jsonld['availableDeliveryMethod'] ) ) {
14
-			$jsonld['availableDeliveryMethod'] = array( $jsonld['availableDeliveryMethod'] );
13
+		if ( ! is_array($jsonld['availableDeliveryMethod'])) {
14
+			$jsonld['availableDeliveryMethod'] = array($jsonld['availableDeliveryMethod']);
15 15
 		}
16 16
 
17
-		if ( ! in_array( 'ParcelDelivery', $jsonld['availableDeliveryMethod'], true ) ) {
18
-			array_push( $jsonld['availableDeliveryMethod'], 'ParcelDelivery' );
17
+		if ( ! in_array('ParcelDelivery', $jsonld['availableDeliveryMethod'], true)) {
18
+			array_push($jsonld['availableDeliveryMethod'], 'ParcelDelivery');
19 19
 		}
20 20
 
21 21
 	}
22 22
 
23
-	public function add_shipping_rate( &$offer_shipping_details ) {
23
+	public function add_shipping_rate(&$offer_shipping_details) {
24 24
 
25
-		if ( ! isset( $offer_shipping_details['shippingRate'] ) ) {
25
+		if ( ! isset($offer_shipping_details['shippingRate'])) {
26 26
 			$offer_shipping_details['shippingRate'] = array();
27 27
 		}
28 28
 
29
-		$description = isset( $this->wc_shipping_method->instance_settings['description'] )
30
-			? wp_strip_all_tags( $this->wc_shipping_method->instance_settings['description'] ) : '';
29
+		$description = isset($this->wc_shipping_method->instance_settings['description'])
30
+			? wp_strip_all_tags($this->wc_shipping_method->instance_settings['description']) : '';
31 31
 
32 32
 		$shipping_rate = array(
33 33
 			'@type'       => 'MonetaryAmount',
@@ -37,7 +37,7 @@  discard block
 block discarded – undo
37 37
 			'currency'    => get_woocommerce_currency(),
38 38
 		);
39 39
 
40
-		$this->change_to_manual_currency( $shipping_rate );
40
+		$this->change_to_manual_currency($shipping_rate);
41 41
 
42 42
 		$offer_shipping_details['shippingRate'][] = $shipping_rate;
43 43
 
Please login to merge, or discard this patch.
src/wordlift/shipping-data/class-local-pickup-shipping-method.php 2 patches
Indentation   +28 added lines, -28 removed lines patch added patch discarded remove patch
@@ -6,45 +6,45 @@
 block discarded – undo
6 6
 
7 7
 class Local_Pickup_Shipping_Method extends Shipping_Method {
8 8
 
9
-	public function add_available_delivery_method( &$jsonld ) {
9
+    public function add_available_delivery_method( &$jsonld ) {
10 10
 
11
-		if ( ! isset( $jsonld['availableDeliveryMethod'] ) ) {
12
-			$jsonld['availableDeliveryMethod'] = array();
13
-		}
11
+        if ( ! isset( $jsonld['availableDeliveryMethod'] ) ) {
12
+            $jsonld['availableDeliveryMethod'] = array();
13
+        }
14 14
 
15
-		if ( ! is_array( $jsonld['availableDeliveryMethod'] ) ) {
16
-			$jsonld['availableDeliveryMethod'] = array( $jsonld['availableDeliveryMethod'] );
17
-		}
15
+        if ( ! is_array( $jsonld['availableDeliveryMethod'] ) ) {
16
+            $jsonld['availableDeliveryMethod'] = array( $jsonld['availableDeliveryMethod'] );
17
+        }
18 18
 
19
-		if ( ! in_array( 'OnSitePickup', $jsonld['availableDeliveryMethod'], true ) ) {
20
-			array_push( $jsonld['availableDeliveryMethod'], 'OnSitePickup' );
21
-		}
19
+        if ( ! in_array( 'OnSitePickup', $jsonld['availableDeliveryMethod'], true ) ) {
20
+            array_push( $jsonld['availableDeliveryMethod'], 'OnSitePickup' );
21
+        }
22 22
 
23
-	}
23
+    }
24 24
 
25
-	public function add_shipping_rate( &$offer_shipping_details ) {
25
+    public function add_shipping_rate( &$offer_shipping_details ) {
26 26
 
27
-		if ( ! isset( $offer_shipping_details['shippingRate'] ) ) {
28
-			$offer_shipping_details['shippingRate'] = array();
29
-		}
27
+        if ( ! isset( $offer_shipping_details['shippingRate'] ) ) {
28
+            $offer_shipping_details['shippingRate'] = array();
29
+        }
30 30
 
31
-		$description = isset( $this->wc_shipping_method->instance_settings['description'] )
32
-			? wp_strip_all_tags( $this->wc_shipping_method->instance_settings['description'] ) : '';
31
+        $description = isset( $this->wc_shipping_method->instance_settings['description'] )
32
+            ? wp_strip_all_tags( $this->wc_shipping_method->instance_settings['description'] ) : '';
33 33
 
34
-		$cost = $this->wc_shipping_method->get_option( 'cost' );
34
+        $cost = $this->wc_shipping_method->get_option( 'cost' );
35 35
 
36
-		$shipping_rate = array(
37
-			'@type'       => 'MonetaryAmount',
38
-			'name'        => $this->wc_shipping_method->get_title(),
39
-			'description' => $description,
40
-			'value'       => $cost ? $cost : '0',
41
-			'currency'    => get_woocommerce_currency(),
42
-		);
36
+        $shipping_rate = array(
37
+            '@type'       => 'MonetaryAmount',
38
+            'name'        => $this->wc_shipping_method->get_title(),
39
+            'description' => $description,
40
+            'value'       => $cost ? $cost : '0',
41
+            'currency'    => get_woocommerce_currency(),
42
+        );
43 43
 
44
-		$this->change_to_manual_currency( $shipping_rate );
44
+        $this->change_to_manual_currency( $shipping_rate );
45 45
 
46
-		$offer_shipping_details['shippingRate'][] = $shipping_rate;
46
+        $offer_shipping_details['shippingRate'][] = $shipping_rate;
47 47
 
48
-	}
48
+    }
49 49
 
50 50
 }
Please login to merge, or discard this patch.
Spacing   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -6,32 +6,32 @@  discard block
 block discarded – undo
6 6
 
7 7
 class Local_Pickup_Shipping_Method extends Shipping_Method {
8 8
 
9
-	public function add_available_delivery_method( &$jsonld ) {
9
+	public function add_available_delivery_method(&$jsonld) {
10 10
 
11
-		if ( ! isset( $jsonld['availableDeliveryMethod'] ) ) {
11
+		if ( ! isset($jsonld['availableDeliveryMethod'])) {
12 12
 			$jsonld['availableDeliveryMethod'] = array();
13 13
 		}
14 14
 
15
-		if ( ! is_array( $jsonld['availableDeliveryMethod'] ) ) {
16
-			$jsonld['availableDeliveryMethod'] = array( $jsonld['availableDeliveryMethod'] );
15
+		if ( ! is_array($jsonld['availableDeliveryMethod'])) {
16
+			$jsonld['availableDeliveryMethod'] = array($jsonld['availableDeliveryMethod']);
17 17
 		}
18 18
 
19
-		if ( ! in_array( 'OnSitePickup', $jsonld['availableDeliveryMethod'], true ) ) {
20
-			array_push( $jsonld['availableDeliveryMethod'], 'OnSitePickup' );
19
+		if ( ! in_array('OnSitePickup', $jsonld['availableDeliveryMethod'], true)) {
20
+			array_push($jsonld['availableDeliveryMethod'], 'OnSitePickup');
21 21
 		}
22 22
 
23 23
 	}
24 24
 
25
-	public function add_shipping_rate( &$offer_shipping_details ) {
25
+	public function add_shipping_rate(&$offer_shipping_details) {
26 26
 
27
-		if ( ! isset( $offer_shipping_details['shippingRate'] ) ) {
27
+		if ( ! isset($offer_shipping_details['shippingRate'])) {
28 28
 			$offer_shipping_details['shippingRate'] = array();
29 29
 		}
30 30
 
31
-		$description = isset( $this->wc_shipping_method->instance_settings['description'] )
32
-			? wp_strip_all_tags( $this->wc_shipping_method->instance_settings['description'] ) : '';
31
+		$description = isset($this->wc_shipping_method->instance_settings['description'])
32
+			? wp_strip_all_tags($this->wc_shipping_method->instance_settings['description']) : '';
33 33
 
34
-		$cost = $this->wc_shipping_method->get_option( 'cost' );
34
+		$cost = $this->wc_shipping_method->get_option('cost');
35 35
 
36 36
 		$shipping_rate = array(
37 37
 			'@type'       => 'MonetaryAmount',
@@ -41,7 +41,7 @@  discard block
 block discarded – undo
41 41
 			'currency'    => get_woocommerce_currency(),
42 42
 		);
43 43
 
44
-		$this->change_to_manual_currency( $shipping_rate );
44
+		$this->change_to_manual_currency($shipping_rate);
45 45
 
46 46
 		$offer_shipping_details['shippingRate'][] = $shipping_rate;
47 47
 
Please login to merge, or discard this patch.
src/wordlift/shipping-data/class-shipping-method.php 2 patches
Indentation   +100 added lines, -100 removed lines patch added patch discarded remove patch
@@ -7,105 +7,105 @@
 block discarded – undo
7 7
 
8 8
 class Shipping_Method {
9 9
 
10
-	/**
11
-	 * @var WC_Shipping_Method $wc_shipping_method
12
-	 */
13
-	protected $wc_shipping_method;
14
-
15
-	/**
16
-	 * Shipping_Method constructor.
17
-	 *
18
-	 * @param WC_Shipping_Method $wc_shipping_method
19
-	 */
20
-	public function __construct( $wc_shipping_method ) {
21
-		$this->wc_shipping_method = $wc_shipping_method;
22
-	}
23
-
24
-	/**
25
-	 * @param WC_Shipping_Method $wc_shipping_method
26
-	 *
27
-	 * @return Shipping_Method
28
-	 */
29
-	public static function from_wc_shipping_method( $wc_shipping_method ) {
30
-
31
-		switch ( get_class( $wc_shipping_method ) ) {
32
-			case 'WC_Shipping_Local_Pickup':
33
-				return new Local_Pickup_Shipping_Method( $wc_shipping_method );
34
-
35
-			case 'WC_Shipping_Flat_Rate':
36
-				return new Flat_Rate_Shipping_Method( $wc_shipping_method );
37
-
38
-			case 'WC_Shipping_Free_Shipping':
39
-				return new Free_Shipping_Shipping_Method( $wc_shipping_method );
40
-
41
-			default:
42
-				return new self( $wc_shipping_method );
43
-		}
44
-
45
-	}
46
-
47
-	// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
48
-	public function add_available_delivery_method( &$jsonld ) {
49
-
50
-	}
51
-
52
-	// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
53
-	public function add_shipping_rate( &$offer_shipping_details ) {
54
-
55
-	}
56
-
57
-	public function add_transit_time( &$shipping_delivery_time ) {
58
-
59
-		$prefix   = "wcsdt_transit_m{$this->wc_shipping_method->get_instance_id()}";
60
-		$property = 'transitTime';
61
-
62
-		$option = get_option( 'wpsso_options' );
63
-
64
-		if ( empty( $option[ "{$prefix}_unit_code" ] )
65
-			 || ! isset( $option[ "{$prefix}_minimum" ] )
66
-			 || ! isset( $option[ "{$prefix}_maximum" ] )
67
-			 || ! is_numeric( $option[ "{$prefix}_minimum" ] )
68
-			 || ! is_numeric( $option[ "{$prefix}_maximum" ] ) ) {
69
-			return;
70
-		}
71
-
72
-		$unit_code = $option[ "{$prefix}_unit_code" ];
73
-		$minimum   = $option[ "{$prefix}_minimum" ];
74
-		$maximum   = $option[ "{$prefix}_maximum" ];
75
-
76
-		if ( 'HUR' === $unit_code ) {
77
-			$minimum = floor( $minimum / 24.0 );
78
-			$maximum = ceil( $maximum / 24.0 );
79
-		}
80
-
81
-		$shipping_delivery_time[ $property ] = array(
82
-			'@type'    => 'QuantitativeValue',
83
-			'minValue' => intval( $minimum ),
84
-			'maxValue' => intval( $maximum ),
85
-		);
86
-
87
-	}
88
-
89
-	protected function change_to_manual_currency( &$shipping_rate ) {
90
-		// WCML not available.
91
-		if ( ! class_exists( 'WCML\Multicurrency\Shipping\ShippingModeProvider' ) ) {
92
-			return;
93
-		}
94
-
95
-		// Manual pricing not enabled.
96
-		if ( ! ShippingModeProvider::get( $this->wc_shipping_method->id )->isManualPricingEnabled( $this->wc_shipping_method ) ) {
97
-			return;
98
-		}
99
-
100
-		// Get the first manual price.
101
-		foreach ( $this->wc_shipping_method->instance_settings as $key => $value ) {
102
-			if ( preg_match( '@^cost_(\w{3})$@', $key, $matches ) ) {
103
-				$shipping_rate['value']    = $value;
104
-				$shipping_rate['currency'] = $matches[1];
105
-
106
-				return;
107
-			}
108
-		}
109
-	}
10
+    /**
11
+     * @var WC_Shipping_Method $wc_shipping_method
12
+     */
13
+    protected $wc_shipping_method;
14
+
15
+    /**
16
+     * Shipping_Method constructor.
17
+     *
18
+     * @param WC_Shipping_Method $wc_shipping_method
19
+     */
20
+    public function __construct( $wc_shipping_method ) {
21
+        $this->wc_shipping_method = $wc_shipping_method;
22
+    }
23
+
24
+    /**
25
+     * @param WC_Shipping_Method $wc_shipping_method
26
+     *
27
+     * @return Shipping_Method
28
+     */
29
+    public static function from_wc_shipping_method( $wc_shipping_method ) {
30
+
31
+        switch ( get_class( $wc_shipping_method ) ) {
32
+            case 'WC_Shipping_Local_Pickup':
33
+                return new Local_Pickup_Shipping_Method( $wc_shipping_method );
34
+
35
+            case 'WC_Shipping_Flat_Rate':
36
+                return new Flat_Rate_Shipping_Method( $wc_shipping_method );
37
+
38
+            case 'WC_Shipping_Free_Shipping':
39
+                return new Free_Shipping_Shipping_Method( $wc_shipping_method );
40
+
41
+            default:
42
+                return new self( $wc_shipping_method );
43
+        }
44
+
45
+    }
46
+
47
+    // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
48
+    public function add_available_delivery_method( &$jsonld ) {
49
+
50
+    }
51
+
52
+    // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
53
+    public function add_shipping_rate( &$offer_shipping_details ) {
54
+
55
+    }
56
+
57
+    public function add_transit_time( &$shipping_delivery_time ) {
58
+
59
+        $prefix   = "wcsdt_transit_m{$this->wc_shipping_method->get_instance_id()}";
60
+        $property = 'transitTime';
61
+
62
+        $option = get_option( 'wpsso_options' );
63
+
64
+        if ( empty( $option[ "{$prefix}_unit_code" ] )
65
+             || ! isset( $option[ "{$prefix}_minimum" ] )
66
+             || ! isset( $option[ "{$prefix}_maximum" ] )
67
+             || ! is_numeric( $option[ "{$prefix}_minimum" ] )
68
+             || ! is_numeric( $option[ "{$prefix}_maximum" ] ) ) {
69
+            return;
70
+        }
71
+
72
+        $unit_code = $option[ "{$prefix}_unit_code" ];
73
+        $minimum   = $option[ "{$prefix}_minimum" ];
74
+        $maximum   = $option[ "{$prefix}_maximum" ];
75
+
76
+        if ( 'HUR' === $unit_code ) {
77
+            $minimum = floor( $minimum / 24.0 );
78
+            $maximum = ceil( $maximum / 24.0 );
79
+        }
80
+
81
+        $shipping_delivery_time[ $property ] = array(
82
+            '@type'    => 'QuantitativeValue',
83
+            'minValue' => intval( $minimum ),
84
+            'maxValue' => intval( $maximum ),
85
+        );
86
+
87
+    }
88
+
89
+    protected function change_to_manual_currency( &$shipping_rate ) {
90
+        // WCML not available.
91
+        if ( ! class_exists( 'WCML\Multicurrency\Shipping\ShippingModeProvider' ) ) {
92
+            return;
93
+        }
94
+
95
+        // Manual pricing not enabled.
96
+        if ( ! ShippingModeProvider::get( $this->wc_shipping_method->id )->isManualPricingEnabled( $this->wc_shipping_method ) ) {
97
+            return;
98
+        }
99
+
100
+        // Get the first manual price.
101
+        foreach ( $this->wc_shipping_method->instance_settings as $key => $value ) {
102
+            if ( preg_match( '@^cost_(\w{3})$@', $key, $matches ) ) {
103
+                $shipping_rate['value']    = $value;
104
+                $shipping_rate['currency'] = $matches[1];
105
+
106
+                return;
107
+            }
108
+        }
109
+    }
110 110
 
111 111
 }
Please login to merge, or discard this patch.
Spacing   +30 added lines, -30 removed lines patch added patch discarded remove patch
@@ -17,7 +17,7 @@  discard block
 block discarded – undo
17 17
 	 *
18 18
 	 * @param WC_Shipping_Method $wc_shipping_method
19 19
 	 */
20
-	public function __construct( $wc_shipping_method ) {
20
+	public function __construct($wc_shipping_method) {
21 21
 		$this->wc_shipping_method = $wc_shipping_method;
22 22
 	}
23 23
 
@@ -26,80 +26,80 @@  discard block
 block discarded – undo
26 26
 	 *
27 27
 	 * @return Shipping_Method
28 28
 	 */
29
-	public static function from_wc_shipping_method( $wc_shipping_method ) {
29
+	public static function from_wc_shipping_method($wc_shipping_method) {
30 30
 
31
-		switch ( get_class( $wc_shipping_method ) ) {
31
+		switch (get_class($wc_shipping_method)) {
32 32
 			case 'WC_Shipping_Local_Pickup':
33
-				return new Local_Pickup_Shipping_Method( $wc_shipping_method );
33
+				return new Local_Pickup_Shipping_Method($wc_shipping_method);
34 34
 
35 35
 			case 'WC_Shipping_Flat_Rate':
36
-				return new Flat_Rate_Shipping_Method( $wc_shipping_method );
36
+				return new Flat_Rate_Shipping_Method($wc_shipping_method);
37 37
 
38 38
 			case 'WC_Shipping_Free_Shipping':
39
-				return new Free_Shipping_Shipping_Method( $wc_shipping_method );
39
+				return new Free_Shipping_Shipping_Method($wc_shipping_method);
40 40
 
41 41
 			default:
42
-				return new self( $wc_shipping_method );
42
+				return new self($wc_shipping_method);
43 43
 		}
44 44
 
45 45
 	}
46 46
 
47 47
 	// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
48
-	public function add_available_delivery_method( &$jsonld ) {
48
+	public function add_available_delivery_method(&$jsonld) {
49 49
 
50 50
 	}
51 51
 
52 52
 	// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
53
-	public function add_shipping_rate( &$offer_shipping_details ) {
53
+	public function add_shipping_rate(&$offer_shipping_details) {
54 54
 
55 55
 	}
56 56
 
57
-	public function add_transit_time( &$shipping_delivery_time ) {
57
+	public function add_transit_time(&$shipping_delivery_time) {
58 58
 
59 59
 		$prefix   = "wcsdt_transit_m{$this->wc_shipping_method->get_instance_id()}";
60 60
 		$property = 'transitTime';
61 61
 
62
-		$option = get_option( 'wpsso_options' );
62
+		$option = get_option('wpsso_options');
63 63
 
64
-		if ( empty( $option[ "{$prefix}_unit_code" ] )
65
-			 || ! isset( $option[ "{$prefix}_minimum" ] )
66
-			 || ! isset( $option[ "{$prefix}_maximum" ] )
67
-			 || ! is_numeric( $option[ "{$prefix}_minimum" ] )
68
-			 || ! is_numeric( $option[ "{$prefix}_maximum" ] ) ) {
64
+		if (empty($option["{$prefix}_unit_code"])
65
+			 || ! isset($option["{$prefix}_minimum"])
66
+			 || ! isset($option["{$prefix}_maximum"])
67
+			 || ! is_numeric($option["{$prefix}_minimum"])
68
+			 || ! is_numeric($option["{$prefix}_maximum"])) {
69 69
 			return;
70 70
 		}
71 71
 
72
-		$unit_code = $option[ "{$prefix}_unit_code" ];
73
-		$minimum   = $option[ "{$prefix}_minimum" ];
74
-		$maximum   = $option[ "{$prefix}_maximum" ];
72
+		$unit_code = $option["{$prefix}_unit_code"];
73
+		$minimum   = $option["{$prefix}_minimum"];
74
+		$maximum   = $option["{$prefix}_maximum"];
75 75
 
76
-		if ( 'HUR' === $unit_code ) {
77
-			$minimum = floor( $minimum / 24.0 );
78
-			$maximum = ceil( $maximum / 24.0 );
76
+		if ('HUR' === $unit_code) {
77
+			$minimum = floor($minimum / 24.0);
78
+			$maximum = ceil($maximum / 24.0);
79 79
 		}
80 80
 
81
-		$shipping_delivery_time[ $property ] = array(
81
+		$shipping_delivery_time[$property] = array(
82 82
 			'@type'    => 'QuantitativeValue',
83
-			'minValue' => intval( $minimum ),
84
-			'maxValue' => intval( $maximum ),
83
+			'minValue' => intval($minimum),
84
+			'maxValue' => intval($maximum),
85 85
 		);
86 86
 
87 87
 	}
88 88
 
89
-	protected function change_to_manual_currency( &$shipping_rate ) {
89
+	protected function change_to_manual_currency(&$shipping_rate) {
90 90
 		// WCML not available.
91
-		if ( ! class_exists( 'WCML\Multicurrency\Shipping\ShippingModeProvider' ) ) {
91
+		if ( ! class_exists('WCML\Multicurrency\Shipping\ShippingModeProvider')) {
92 92
 			return;
93 93
 		}
94 94
 
95 95
 		// Manual pricing not enabled.
96
-		if ( ! ShippingModeProvider::get( $this->wc_shipping_method->id )->isManualPricingEnabled( $this->wc_shipping_method ) ) {
96
+		if ( ! ShippingModeProvider::get($this->wc_shipping_method->id)->isManualPricingEnabled($this->wc_shipping_method)) {
97 97
 			return;
98 98
 		}
99 99
 
100 100
 		// Get the first manual price.
101
-		foreach ( $this->wc_shipping_method->instance_settings as $key => $value ) {
102
-			if ( preg_match( '@^cost_(\w{3})$@', $key, $matches ) ) {
101
+		foreach ($this->wc_shipping_method->instance_settings as $key => $value) {
102
+			if (preg_match('@^cost_(\w{3})$@', $key, $matches)) {
103 103
 				$shipping_rate['value']    = $value;
104 104
 				$shipping_rate['currency'] = $matches[1];
105 105
 
Please login to merge, or discard this patch.
src/modules/food-kg/includes/admin/Download_Ingredients_Data.php 2 patches
Indentation   +68 added lines, -68 removed lines patch added patch discarded remove patch
@@ -4,22 +4,22 @@  discard block
 block discarded – undo
4 4
 
5 5
 class Download_Ingredients_Data {
6 6
 
7
-	public function register_hooks() {
8
-		add_action( 'wp_ajax_wl_download_ingredients_data', array( $this, 'wl_download_ingredients_data' ) );
9
-	}
7
+    public function register_hooks() {
8
+        add_action( 'wp_ajax_wl_download_ingredients_data', array( $this, 'wl_download_ingredients_data' ) );
9
+    }
10 10
 
11
-	public function wl_download_ingredients_data() {
11
+    public function wl_download_ingredients_data() {
12 12
 
13
-		check_ajax_referer( 'wl-dl-ingredients-data-nonce' );
13
+        check_ajax_referer( 'wl-dl-ingredients-data-nonce' );
14 14
 
15
-		if ( ! current_user_can( 'manage_options' ) ) {
16
-			wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'wordlift' ) );
17
-		}
15
+        if ( ! current_user_can( 'manage_options' ) ) {
16
+            wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'wordlift' ) );
17
+        }
18 18
 
19
-		global $wpdb;
19
+        global $wpdb;
20 20
 
21
-		$items = $wpdb->get_results(
22
-			"SELECT p1.ID AS recipe_ID,
21
+        $items = $wpdb->get_results(
22
+            "SELECT p1.ID AS recipe_ID,
23 23
 					    p1.post_title AS recipe_name,
24 24
 					    p2.ID AS post_ID,
25 25
 					    p2.post_title
@@ -29,62 +29,62 @@  discard block
 block discarded – undo
29 29
 							INNER JOIN $wpdb->postmeta pm2
30 30
 								ON pm2.post_ID = pm1.post_ID AND pm2.meta_key = 'wprm_parent_post_id'
31 31
 						    INNER JOIN $wpdb->posts p2"
32
-			// The following ignore rule is used against the `LIKE CONCAT`. We only have const values.
33
-			// phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.LikeWildcardsInQuery
34
-			. " ON p2.post_status = 'publish' AND p2.ID = pm2.post_ID
32
+            // The following ignore rule is used against the `LIKE CONCAT`. We only have const values.
33
+            // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.LikeWildcardsInQuery
34
+            . " ON p2.post_status = 'publish' AND p2.ID = pm2.post_ID
35 35
 							WHERE pm1.meta_key = '_wl_main_ingredient_jsonld'"
36
-		);
37
-
38
-		if ( ! $items ) {
39
-			wp_send_json_error( __( 'No main ingredients found.', 'wordlift' ) );
40
-		}
41
-
42
-		// Generate unique filename using current timestamp.
43
-		$filename = 'wl-main-ingredients-data-' . gmdate( 'Y-m-d-H-i-s' ) . '.tsv';
44
-
45
-		header( 'Content-Disposition: attachment; filename=' . $filename );
46
-		header( 'Content-Type: text/text/tab-separated-values; charset=' . get_bloginfo( 'charset' ) );
47
-
48
-		// Do not cache the file.
49
-		header( 'Pragma: no-cache' );
50
-		header( 'Expires: 0' );
51
-
52
-		$output = fopen( 'php://output', 'w' );
53
-
54
-		// Insert Header.
55
-		fputcsv(
56
-			$output,
57
-			array(
58
-				__( 'Ingredient Name', 'wordlift' ),
59
-				__( 'Recipe Name', 'wordlift' ),
60
-				__( 'Recipe ID', 'wordlift' ),
61
-				__( 'Post Name', 'wordlift' ),
62
-				__( 'Post ID', 'wordlift' ),
63
-				__( 'Post URL', 'wordlift' ),
64
-			),
65
-			"\t"
66
-		);
67
-
68
-		// Insert Data.
69
-		foreach ( $items as $item ) {
70
-			$recipe_json_ld = get_post_meta( $item->recipe_ID, '_wl_main_ingredient_jsonld', true ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
71
-			$recipe         = json_decode( $recipe_json_ld, true );
72
-			fputcsv(
73
-				$output,
74
-				array(
75
-					$recipe ? $recipe['name'] : 'null',
76
-					$item->recipe_name,
77
-					// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
78
-					$item->recipe_ID,
79
-					$item->post_title,
80
-					$item->post_ID,
81
-					esc_url( get_the_permalink( $item->post_ID ) ),
82
-				),
83
-				"\t"
84
-			);
85
-			ob_flush();
86
-		}
87
-
88
-		wp_die();
89
-	}
36
+        );
37
+
38
+        if ( ! $items ) {
39
+            wp_send_json_error( __( 'No main ingredients found.', 'wordlift' ) );
40
+        }
41
+
42
+        // Generate unique filename using current timestamp.
43
+        $filename = 'wl-main-ingredients-data-' . gmdate( 'Y-m-d-H-i-s' ) . '.tsv';
44
+
45
+        header( 'Content-Disposition: attachment; filename=' . $filename );
46
+        header( 'Content-Type: text/text/tab-separated-values; charset=' . get_bloginfo( 'charset' ) );
47
+
48
+        // Do not cache the file.
49
+        header( 'Pragma: no-cache' );
50
+        header( 'Expires: 0' );
51
+
52
+        $output = fopen( 'php://output', 'w' );
53
+
54
+        // Insert Header.
55
+        fputcsv(
56
+            $output,
57
+            array(
58
+                __( 'Ingredient Name', 'wordlift' ),
59
+                __( 'Recipe Name', 'wordlift' ),
60
+                __( 'Recipe ID', 'wordlift' ),
61
+                __( 'Post Name', 'wordlift' ),
62
+                __( 'Post ID', 'wordlift' ),
63
+                __( 'Post URL', 'wordlift' ),
64
+            ),
65
+            "\t"
66
+        );
67
+
68
+        // Insert Data.
69
+        foreach ( $items as $item ) {
70
+            $recipe_json_ld = get_post_meta( $item->recipe_ID, '_wl_main_ingredient_jsonld', true ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
71
+            $recipe         = json_decode( $recipe_json_ld, true );
72
+            fputcsv(
73
+                $output,
74
+                array(
75
+                    $recipe ? $recipe['name'] : 'null',
76
+                    $item->recipe_name,
77
+                    // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
78
+                    $item->recipe_ID,
79
+                    $item->post_title,
80
+                    $item->post_ID,
81
+                    esc_url( get_the_permalink( $item->post_ID ) ),
82
+                ),
83
+                "\t"
84
+            );
85
+            ob_flush();
86
+        }
87
+
88
+        wp_die();
89
+    }
90 90
 }
Please login to merge, or discard this patch.
Spacing   +22 added lines, -22 removed lines patch added patch discarded remove patch
@@ -5,15 +5,15 @@  discard block
 block discarded – undo
5 5
 class Download_Ingredients_Data {
6 6
 
7 7
 	public function register_hooks() {
8
-		add_action( 'wp_ajax_wl_download_ingredients_data', array( $this, 'wl_download_ingredients_data' ) );
8
+		add_action('wp_ajax_wl_download_ingredients_data', array($this, 'wl_download_ingredients_data'));
9 9
 	}
10 10
 
11 11
 	public function wl_download_ingredients_data() {
12 12
 
13
-		check_ajax_referer( 'wl-dl-ingredients-data-nonce' );
13
+		check_ajax_referer('wl-dl-ingredients-data-nonce');
14 14
 
15
-		if ( ! current_user_can( 'manage_options' ) ) {
16
-			wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'wordlift' ) );
15
+		if ( ! current_user_can('manage_options')) {
16
+			wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'wordlift'));
17 17
 		}
18 18
 
19 19
 		global $wpdb;
@@ -35,40 +35,40 @@  discard block
 block discarded – undo
35 35
 							WHERE pm1.meta_key = '_wl_main_ingredient_jsonld'"
36 36
 		);
37 37
 
38
-		if ( ! $items ) {
39
-			wp_send_json_error( __( 'No main ingredients found.', 'wordlift' ) );
38
+		if ( ! $items) {
39
+			wp_send_json_error(__('No main ingredients found.', 'wordlift'));
40 40
 		}
41 41
 
42 42
 		// Generate unique filename using current timestamp.
43
-		$filename = 'wl-main-ingredients-data-' . gmdate( 'Y-m-d-H-i-s' ) . '.tsv';
43
+		$filename = 'wl-main-ingredients-data-'.gmdate('Y-m-d-H-i-s').'.tsv';
44 44
 
45
-		header( 'Content-Disposition: attachment; filename=' . $filename );
46
-		header( 'Content-Type: text/text/tab-separated-values; charset=' . get_bloginfo( 'charset' ) );
45
+		header('Content-Disposition: attachment; filename='.$filename);
46
+		header('Content-Type: text/text/tab-separated-values; charset='.get_bloginfo('charset'));
47 47
 
48 48
 		// Do not cache the file.
49
-		header( 'Pragma: no-cache' );
50
-		header( 'Expires: 0' );
49
+		header('Pragma: no-cache');
50
+		header('Expires: 0');
51 51
 
52
-		$output = fopen( 'php://output', 'w' );
52
+		$output = fopen('php://output', 'w');
53 53
 
54 54
 		// Insert Header.
55 55
 		fputcsv(
56 56
 			$output,
57 57
 			array(
58
-				__( 'Ingredient Name', 'wordlift' ),
59
-				__( 'Recipe Name', 'wordlift' ),
60
-				__( 'Recipe ID', 'wordlift' ),
61
-				__( 'Post Name', 'wordlift' ),
62
-				__( 'Post ID', 'wordlift' ),
63
-				__( 'Post URL', 'wordlift' ),
58
+				__('Ingredient Name', 'wordlift'),
59
+				__('Recipe Name', 'wordlift'),
60
+				__('Recipe ID', 'wordlift'),
61
+				__('Post Name', 'wordlift'),
62
+				__('Post ID', 'wordlift'),
63
+				__('Post URL', 'wordlift'),
64 64
 			),
65 65
 			"\t"
66 66
 		);
67 67
 
68 68
 		// Insert Data.
69
-		foreach ( $items as $item ) {
70
-			$recipe_json_ld = get_post_meta( $item->recipe_ID, '_wl_main_ingredient_jsonld', true ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
71
-			$recipe         = json_decode( $recipe_json_ld, true );
69
+		foreach ($items as $item) {
70
+			$recipe_json_ld = get_post_meta($item->recipe_ID, '_wl_main_ingredient_jsonld', true); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
71
+			$recipe         = json_decode($recipe_json_ld, true);
72 72
 			fputcsv(
73 73
 				$output,
74 74
 				array(
@@ -78,7 +78,7 @@  discard block
 block discarded – undo
78 78
 					$item->recipe_ID,
79 79
 					$item->post_title,
80 80
 					$item->post_ID,
81
-					esc_url( get_the_permalink( $item->post_ID ) ),
81
+					esc_url(get_the_permalink($item->post_ID)),
82 82
 				),
83 83
 				"\t"
84 84
 			);
Please login to merge, or discard this patch.
src/includes/class-wordlift-key-validation-service.php 2 patches
Spacing   +38 added lines, -38 removed lines patch added patch discarded remove patch
@@ -41,9 +41,9 @@  discard block
 block discarded – undo
41 41
 	 */
42 42
 	public function __construct() {
43 43
 
44
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Key_Validation_Service' );
44
+		$this->log = Wordlift_Log_Service::get_logger('Wordlift_Key_Validation_Service');
45 45
 
46
-		add_action( 'admin_init', array( $this, 'wl_load_plugin' ) );
46
+		add_action('admin_init', array($this, 'wl_load_plugin'));
47 47
 		/**
48 48
 		 * Filter: wl_feature__enable__notices.
49 49
 		 *
@@ -52,11 +52,11 @@  discard block
 block discarded – undo
52 52
 		 * @return bool
53 53
 		 * @since 3.27.6
54 54
 		 */
55
-		if ( apply_filters( 'wl_feature__enable__notices', true ) ) {
56
-			add_action( 'admin_notices', array( $this, 'wl_key_update_notice' ) );
55
+		if (apply_filters('wl_feature__enable__notices', true)) {
56
+			add_action('admin_notices', array($this, 'wl_key_update_notice'));
57 57
 		}
58 58
 
59
-		$this->ttl_cache_service = new Ttl_Cache( 'key-validation-notification' );
59
+		$this->ttl_cache_service = new Ttl_Cache('key-validation-notification');
60 60
 
61 61
 	}
62 62
 
@@ -68,9 +68,9 @@  discard block
 block discarded – undo
68 68
 	 * @return WP_Error|array The response or WP_Error on failure.
69 69
 	 * @since 3.9.0
70 70
 	 */
71
-	public function get_account_info( $key ) {
71
+	public function get_account_info($key) {
72 72
 
73
-		$this->log->debug( 'Validating key...' );
73
+		$this->log->debug('Validating key...');
74 74
 
75 75
 		$response = Default_Api_Service::get_instance()->get(
76 76
 			'/accounts/info',
@@ -84,7 +84,7 @@  discard block
 block discarded – undo
84 84
 		 * This action is fired when the key is validated.
85 85
 		 * @param $response \Wordlift\Api\Response
86 86
 		 */
87
-		do_action( 'wl_key_validation_response', $response );
87
+		do_action('wl_key_validation_response', $response);
88 88
 
89 89
 		return $response->get_response();
90 90
 	}
@@ -96,21 +96,21 @@  discard block
 block discarded – undo
96 96
 	 *
97 97
 	 * @return bool
98 98
 	 */
99
-	public function is_key_valid( $key ) {
99
+	public function is_key_valid($key) {
100 100
 
101
-		$response = $this->get_account_info( $key );
101
+		$response = $this->get_account_info($key);
102 102
 
103
-		if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) {
103
+		if (is_wp_error($response) || 2 !== (int) $response['response']['code'] / 100) {
104 104
 			return false;
105 105
 		}
106
-		$res_body = json_decode( wp_remote_retrieve_body( $response ), true );
106
+		$res_body = json_decode(wp_remote_retrieve_body($response), true);
107 107
 
108 108
 		$url = $res_body['url'];
109 109
 
110 110
 		// Considering that production URL may be filtered.
111
-		$home_url = get_option( 'home' );
112
-		$site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
113
-		if ( $url === null || $url === $site_url ) {
111
+		$home_url = get_option('home');
112
+		$site_url = apply_filters('wl_production_site_url', untrailingslashit($home_url));
113
+		if ($url === null || $url === $site_url) {
114 114
 			return true;
115 115
 		}
116 116
 
@@ -128,14 +128,14 @@  discard block
 block discarded – undo
128 128
 		ob_clean();
129 129
 
130 130
 		// Check if we have a key.
131
-		if ( ! isset( $_POST['key'] ) ) {  //phpcs:ignore WordPress.Security.NonceVerification.Missing
132
-			wp_send_json_error( 'The key parameter is required.' );
131
+		if ( ! isset($_POST['key'])) {  //phpcs:ignore WordPress.Security.NonceVerification.Missing
132
+			wp_send_json_error('The key parameter is required.');
133 133
 		}
134 134
 
135
-		$response = $this->get_account_info( sanitize_text_field( wp_unslash( (string) $_POST['key'] ) ) );  //phpcs:ignore WordPress.Security.NonceVerification.Missing
135
+		$response = $this->get_account_info(sanitize_text_field(wp_unslash((string) $_POST['key']))); //phpcs:ignore WordPress.Security.NonceVerification.Missing
136 136
 
137 137
 		// If we got an error, return invalid.
138
-		if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) {
138
+		if (is_wp_error($response) || 2 !== (int) $response['response']['code'] / 100) {
139 139
 			wp_send_json_success(
140 140
 				array(
141 141
 					'valid'    => false,
@@ -146,19 +146,19 @@  discard block
 block discarded – undo
146 146
 			);
147 147
 		}
148 148
 
149
-		$res_body = json_decode( wp_remote_retrieve_body( $response ), true );
149
+		$res_body = json_decode(wp_remote_retrieve_body($response), true);
150 150
 
151 151
 		// The URL stored in WLS. If this is the initial install the URL may be null.
152 152
 		$url = $res_body['url'];
153 153
 
154 154
 		// Considering that production URL may be filtered.
155
-		$home_url = get_option( 'home' );
156
-		$site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
155
+		$home_url = get_option('home');
156
+		$site_url = apply_filters('wl_production_site_url', untrailingslashit($home_url));
157 157
 
158 158
 		// If the URL isn't set or matches, then it's valid.
159
-		if ( $url === null || $url === $site_url ) {
159
+		if ($url === null || $url === $site_url) {
160 160
 			// Invalidate the cache key
161
-			$this->ttl_cache_service->delete( 'is_key_valid' );
161
+			$this->ttl_cache_service->delete('is_key_valid');
162 162
 			wp_send_json_success(
163 163
 				array(
164 164
 					'valid'   => true,
@@ -168,12 +168,12 @@  discard block
 block discarded – undo
168 168
 		}
169 169
 
170 170
 		// If the URL doesn't match it means that this key has been configured elsewhere already.
171
-		if ( $url !== $site_url ) {
172
-			Wordlift_Configuration_Service::get_instance()->set_key( '' );
171
+		if ($url !== $site_url) {
172
+			Wordlift_Configuration_Service::get_instance()->set_key('');
173 173
 			wp_send_json_success(
174 174
 				array(
175 175
 					'valid'   => false,
176
-					'message' => __( 'The key is already used on another site, please contact us at [email protected] to move the key to another site.', 'wordlift' ),
176
+					'message' => __('The key is already used on another site, please contact us at [email protected] to move the key to another site.', 'wordlift'),
177 177
 				)
178 178
 			);
179 179
 		}
@@ -182,7 +182,7 @@  discard block
 block discarded – undo
182 182
 		wp_send_json_success(
183 183
 			array(
184 184
 				'valid'   => false,
185
-				'message' => __( 'An error occurred, please contact us at [email protected]', 'wordlift' ),
185
+				'message' => __('An error occurred, please contact us at [email protected]', 'wordlift'),
186 186
 			)
187 187
 		);
188 188
 	}
@@ -192,15 +192,15 @@  discard block
 block discarded – undo
192 192
 	 */
193 193
 	public function wl_load_plugin() {
194 194
 
195
-		$wl_blog_url = get_option( '_wl_blog_url' );
196
-		$home_url    = get_option( 'home' );
195
+		$wl_blog_url = get_option('_wl_blog_url');
196
+		$home_url    = get_option('home');
197 197
 
198
-		if ( ! $wl_blog_url ) {
199
-			update_option( '_wl_blog_url', $home_url, true );
200
-		} elseif ( $wl_blog_url !== $home_url ) {
201
-			update_option( '_wl_blog_url', $home_url, true );
202
-			Wordlift_Configuration_Service::get_instance()->set_key( '' );
203
-			set_transient( 'wl-key-error-msg', __( "Your web site URL has changed. To avoid data corruption, WordLift's key has been removed. Please provide a new key in WordLift Settings. If you believe this to be an error, please contact us at [email protected]", 'wordlift' ), 10 );
198
+		if ( ! $wl_blog_url) {
199
+			update_option('_wl_blog_url', $home_url, true);
200
+		} elseif ($wl_blog_url !== $home_url) {
201
+			update_option('_wl_blog_url', $home_url, true);
202
+			Wordlift_Configuration_Service::get_instance()->set_key('');
203
+			set_transient('wl-key-error-msg', __("Your web site URL has changed. To avoid data corruption, WordLift's key has been removed. Please provide a new key in WordLift Settings. If you believe this to be an error, please contact us at [email protected]", 'wordlift'), 10);
204 204
 		}
205 205
 
206 206
 	}
@@ -209,10 +209,10 @@  discard block
 block discarded – undo
209 209
 	 * This function is hooked to the `admin_notices` to show admin notification.
210 210
 	 */
211 211
 	public function wl_key_update_notice() {
212
-		if ( get_transient( 'wl-key-error-msg' ) ) {
212
+		if (get_transient('wl-key-error-msg')) {
213 213
 			?>
214 214
 			<div class="updated notice is-dismissible error">
215
-				<p><?php esc_html( get_transient( 'wl-key-error-msg' ) ); ?></p>
215
+				<p><?php esc_html(get_transient('wl-key-error-msg')); ?></p>
216 216
 			</div>
217 217
 			<?php
218 218
 		}
Please login to merge, or discard this patch.
Indentation   +193 added lines, -193 removed lines patch added patch discarded remove patch
@@ -20,201 +20,201 @@
 block discarded – undo
20 20
  */
21 21
 class Wordlift_Key_Validation_Service {
22 22
 
23
-	/**
24
-	 * A {@link Wordlift_Log_Service} instance.
25
-	 *
26
-	 * @since  3.14.0
27
-	 * @access private
28
-	 * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
29
-	 */
30
-	private $log;
31
-
32
-	/**
33
-	 * @var Ttl_Cache
34
-	 */
35
-	private $ttl_cache_service;
36
-
37
-	/**
38
-	 * Create a {@link Wordlift_Key_Validation_Service} instance.
39
-	 *
40
-	 * @since 3.14.0
41
-	 */
42
-	public function __construct() {
43
-
44
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Key_Validation_Service' );
45
-
46
-		add_action( 'admin_init', array( $this, 'wl_load_plugin' ) );
47
-		/**
48
-		 * Filter: wl_feature__enable__notices.
49
-		 *
50
-		 * @param bool whether notices need to be enabled or not.
51
-		 *
52
-		 * @return bool
53
-		 * @since 3.27.6
54
-		 */
55
-		if ( apply_filters( 'wl_feature__enable__notices', true ) ) {
56
-			add_action( 'admin_notices', array( $this, 'wl_key_update_notice' ) );
57
-		}
58
-
59
-		$this->ttl_cache_service = new Ttl_Cache( 'key-validation-notification' );
60
-
61
-	}
62
-
63
-	/**
64
-	 * Validate the provided key.
65
-	 *
66
-	 * @param string $key WordLift's key to validate.
67
-	 *
68
-	 * @return WP_Error|array The response or WP_Error on failure.
69
-	 * @since 3.9.0
70
-	 */
71
-	public function get_account_info( $key ) {
72
-
73
-		$this->log->debug( 'Validating key...' );
74
-
75
-		$response = Default_Api_Service::get_instance()->get(
76
-			'/accounts/info',
77
-			array(
78
-				'Authorization' => "Key $key",
79
-			)
80
-		);
81
-
82
-		/**
83
-		 * @since 3.38.5
84
-		 * This action is fired when the key is validated.
85
-		 * @param $response \Wordlift\Api\Response
86
-		 */
87
-		do_action( 'wl_key_validation_response', $response );
88
-
89
-		return $response->get_response();
90
-	}
91
-
92
-	/**
93
-	 * Check if key is valid
94
-	 *
95
-	 * @param $key string
96
-	 *
97
-	 * @return bool
98
-	 */
99
-	public function is_key_valid( $key ) {
100
-
101
-		$response = $this->get_account_info( $key );
102
-
103
-		if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) {
104
-			return false;
105
-		}
106
-		$res_body = json_decode( wp_remote_retrieve_body( $response ), true );
107
-
108
-		$url = $res_body['url'];
109
-
110
-		// Considering that production URL may be filtered.
111
-		$home_url = get_option( 'home' );
112
-		$site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
113
-		if ( $url === null || $url === $site_url ) {
114
-			return true;
115
-		}
116
-
117
-		return false;
118
-	}
119
-
120
-	/**
121
-	 * This function is hooked to the `wl_validate_key` AJAX call.
122
-	 *
123
-	 * @since 3.9.0
124
-	 */
125
-	public function validate_key() {
126
-
127
-		// Ensure we don't have garbage before us.
128
-		ob_clean();
129
-
130
-		// Check if we have a key.
131
-		if ( ! isset( $_POST['key'] ) ) {  //phpcs:ignore WordPress.Security.NonceVerification.Missing
132
-			wp_send_json_error( 'The key parameter is required.' );
133
-		}
134
-
135
-		$response = $this->get_account_info( sanitize_text_field( wp_unslash( (string) $_POST['key'] ) ) );  //phpcs:ignore WordPress.Security.NonceVerification.Missing
136
-
137
-		// If we got an error, return invalid.
138
-		if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) {
139
-			wp_send_json_success(
140
-				array(
141
-					'valid'    => false,
142
-					'message'  => '',
143
-					'response' => $response,
144
-					'api_url'  => Default_Api_Service::get_instance()->get_base_url(),
145
-				)
146
-			);
147
-		}
148
-
149
-		$res_body = json_decode( wp_remote_retrieve_body( $response ), true );
150
-
151
-		// The URL stored in WLS. If this is the initial install the URL may be null.
152
-		$url = $res_body['url'];
153
-
154
-		// Considering that production URL may be filtered.
155
-		$home_url = get_option( 'home' );
156
-		$site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
157
-
158
-		// If the URL isn't set or matches, then it's valid.
159
-		if ( $url === null || $url === $site_url ) {
160
-			// Invalidate the cache key
161
-			$this->ttl_cache_service->delete( 'is_key_valid' );
162
-			wp_send_json_success(
163
-				array(
164
-					'valid'   => true,
165
-					'message' => '',
166
-				)
167
-			);
168
-		}
169
-
170
-		// If the URL doesn't match it means that this key has been configured elsewhere already.
171
-		if ( $url !== $site_url ) {
172
-			Wordlift_Configuration_Service::get_instance()->set_key( '' );
173
-			wp_send_json_success(
174
-				array(
175
-					'valid'   => false,
176
-					'message' => __( 'The key is already used on another site, please contact us at [email protected] to move the key to another site.', 'wordlift' ),
177
-				)
178
-			);
179
-		}
180
-
181
-		// Set a response with valid set to true or false according to the key validity with message.
182
-		wp_send_json_success(
183
-			array(
184
-				'valid'   => false,
185
-				'message' => __( 'An error occurred, please contact us at [email protected]', 'wordlift' ),
186
-			)
187
-		);
188
-	}
189
-
190
-	/**
191
-	 * This function is hooked `admin_init` to check _wl_blog_url.
192
-	 */
193
-	public function wl_load_plugin() {
194
-
195
-		$wl_blog_url = get_option( '_wl_blog_url' );
196
-		$home_url    = get_option( 'home' );
197
-
198
-		if ( ! $wl_blog_url ) {
199
-			update_option( '_wl_blog_url', $home_url, true );
200
-		} elseif ( $wl_blog_url !== $home_url ) {
201
-			update_option( '_wl_blog_url', $home_url, true );
202
-			Wordlift_Configuration_Service::get_instance()->set_key( '' );
203
-			set_transient( 'wl-key-error-msg', __( "Your web site URL has changed. To avoid data corruption, WordLift's key has been removed. Please provide a new key in WordLift Settings. If you believe this to be an error, please contact us at [email protected]", 'wordlift' ), 10 );
204
-		}
205
-
206
-	}
207
-
208
-	/**
209
-	 * This function is hooked to the `admin_notices` to show admin notification.
210
-	 */
211
-	public function wl_key_update_notice() {
212
-		if ( get_transient( 'wl-key-error-msg' ) ) {
213
-			?>
23
+    /**
24
+     * A {@link Wordlift_Log_Service} instance.
25
+     *
26
+     * @since  3.14.0
27
+     * @access private
28
+     * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
29
+     */
30
+    private $log;
31
+
32
+    /**
33
+     * @var Ttl_Cache
34
+     */
35
+    private $ttl_cache_service;
36
+
37
+    /**
38
+     * Create a {@link Wordlift_Key_Validation_Service} instance.
39
+     *
40
+     * @since 3.14.0
41
+     */
42
+    public function __construct() {
43
+
44
+        $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Key_Validation_Service' );
45
+
46
+        add_action( 'admin_init', array( $this, 'wl_load_plugin' ) );
47
+        /**
48
+         * Filter: wl_feature__enable__notices.
49
+         *
50
+         * @param bool whether notices need to be enabled or not.
51
+         *
52
+         * @return bool
53
+         * @since 3.27.6
54
+         */
55
+        if ( apply_filters( 'wl_feature__enable__notices', true ) ) {
56
+            add_action( 'admin_notices', array( $this, 'wl_key_update_notice' ) );
57
+        }
58
+
59
+        $this->ttl_cache_service = new Ttl_Cache( 'key-validation-notification' );
60
+
61
+    }
62
+
63
+    /**
64
+     * Validate the provided key.
65
+     *
66
+     * @param string $key WordLift's key to validate.
67
+     *
68
+     * @return WP_Error|array The response or WP_Error on failure.
69
+     * @since 3.9.0
70
+     */
71
+    public function get_account_info( $key ) {
72
+
73
+        $this->log->debug( 'Validating key...' );
74
+
75
+        $response = Default_Api_Service::get_instance()->get(
76
+            '/accounts/info',
77
+            array(
78
+                'Authorization' => "Key $key",
79
+            )
80
+        );
81
+
82
+        /**
83
+         * @since 3.38.5
84
+         * This action is fired when the key is validated.
85
+         * @param $response \Wordlift\Api\Response
86
+         */
87
+        do_action( 'wl_key_validation_response', $response );
88
+
89
+        return $response->get_response();
90
+    }
91
+
92
+    /**
93
+     * Check if key is valid
94
+     *
95
+     * @param $key string
96
+     *
97
+     * @return bool
98
+     */
99
+    public function is_key_valid( $key ) {
100
+
101
+        $response = $this->get_account_info( $key );
102
+
103
+        if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) {
104
+            return false;
105
+        }
106
+        $res_body = json_decode( wp_remote_retrieve_body( $response ), true );
107
+
108
+        $url = $res_body['url'];
109
+
110
+        // Considering that production URL may be filtered.
111
+        $home_url = get_option( 'home' );
112
+        $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
113
+        if ( $url === null || $url === $site_url ) {
114
+            return true;
115
+        }
116
+
117
+        return false;
118
+    }
119
+
120
+    /**
121
+     * This function is hooked to the `wl_validate_key` AJAX call.
122
+     *
123
+     * @since 3.9.0
124
+     */
125
+    public function validate_key() {
126
+
127
+        // Ensure we don't have garbage before us.
128
+        ob_clean();
129
+
130
+        // Check if we have a key.
131
+        if ( ! isset( $_POST['key'] ) ) {  //phpcs:ignore WordPress.Security.NonceVerification.Missing
132
+            wp_send_json_error( 'The key parameter is required.' );
133
+        }
134
+
135
+        $response = $this->get_account_info( sanitize_text_field( wp_unslash( (string) $_POST['key'] ) ) );  //phpcs:ignore WordPress.Security.NonceVerification.Missing
136
+
137
+        // If we got an error, return invalid.
138
+        if ( is_wp_error( $response ) || 2 !== (int) $response['response']['code'] / 100 ) {
139
+            wp_send_json_success(
140
+                array(
141
+                    'valid'    => false,
142
+                    'message'  => '',
143
+                    'response' => $response,
144
+                    'api_url'  => Default_Api_Service::get_instance()->get_base_url(),
145
+                )
146
+            );
147
+        }
148
+
149
+        $res_body = json_decode( wp_remote_retrieve_body( $response ), true );
150
+
151
+        // The URL stored in WLS. If this is the initial install the URL may be null.
152
+        $url = $res_body['url'];
153
+
154
+        // Considering that production URL may be filtered.
155
+        $home_url = get_option( 'home' );
156
+        $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
157
+
158
+        // If the URL isn't set or matches, then it's valid.
159
+        if ( $url === null || $url === $site_url ) {
160
+            // Invalidate the cache key
161
+            $this->ttl_cache_service->delete( 'is_key_valid' );
162
+            wp_send_json_success(
163
+                array(
164
+                    'valid'   => true,
165
+                    'message' => '',
166
+                )
167
+            );
168
+        }
169
+
170
+        // If the URL doesn't match it means that this key has been configured elsewhere already.
171
+        if ( $url !== $site_url ) {
172
+            Wordlift_Configuration_Service::get_instance()->set_key( '' );
173
+            wp_send_json_success(
174
+                array(
175
+                    'valid'   => false,
176
+                    'message' => __( 'The key is already used on another site, please contact us at [email protected] to move the key to another site.', 'wordlift' ),
177
+                )
178
+            );
179
+        }
180
+
181
+        // Set a response with valid set to true or false according to the key validity with message.
182
+        wp_send_json_success(
183
+            array(
184
+                'valid'   => false,
185
+                'message' => __( 'An error occurred, please contact us at [email protected]', 'wordlift' ),
186
+            )
187
+        );
188
+    }
189
+
190
+    /**
191
+     * This function is hooked `admin_init` to check _wl_blog_url.
192
+     */
193
+    public function wl_load_plugin() {
194
+
195
+        $wl_blog_url = get_option( '_wl_blog_url' );
196
+        $home_url    = get_option( 'home' );
197
+
198
+        if ( ! $wl_blog_url ) {
199
+            update_option( '_wl_blog_url', $home_url, true );
200
+        } elseif ( $wl_blog_url !== $home_url ) {
201
+            update_option( '_wl_blog_url', $home_url, true );
202
+            Wordlift_Configuration_Service::get_instance()->set_key( '' );
203
+            set_transient( 'wl-key-error-msg', __( "Your web site URL has changed. To avoid data corruption, WordLift's key has been removed. Please provide a new key in WordLift Settings. If you believe this to be an error, please contact us at [email protected]", 'wordlift' ), 10 );
204
+        }
205
+
206
+    }
207
+
208
+    /**
209
+     * This function is hooked to the `admin_notices` to show admin notification.
210
+     */
211
+    public function wl_key_update_notice() {
212
+        if ( get_transient( 'wl-key-error-msg' ) ) {
213
+            ?>
214 214
 			<div class="updated notice is-dismissible error">
215 215
 				<p><?php esc_html( get_transient( 'wl-key-error-msg' ) ); ?></p>
216 216
 			</div>
217 217
 			<?php
218
-		}
219
-	}
218
+        }
219
+    }
220 220
 }
Please login to merge, or discard this patch.
src/includes/class-wordlift-configuration-service.php 2 patches
Spacing   +102 added lines, -102 removed lines patch added patch discarded remove patch
@@ -12,7 +12,7 @@  discard block
 block discarded – undo
12 12
 
13 13
 use Wordlift\Api\Default_Api_Service;
14 14
 
15
-if ( ! defined( 'ABSPATH' ) ) {
15
+if ( ! defined('ABSPATH')) {
16 16
 	exit;
17 17
 }
18 18
 
@@ -136,10 +136,10 @@  discard block
 block discarded – undo
136 136
 	 */
137 137
 	protected function __construct() {
138 138
 
139
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
139
+		$this->log = Wordlift_Log_Service::get_logger(get_class());
140 140
 
141 141
 		// Sync some configuration properties when key is validated.
142
-		add_action( 'wl_key_validation_response', array( $this, 'sync' ) );
142
+		add_action('wl_key_validation_response', array($this, 'sync'));
143 143
 
144 144
 	}
145 145
 
@@ -148,15 +148,15 @@  discard block
 block discarded – undo
148 148
 	 *
149 149
 	 * @return void
150 150
 	 */
151
-	public function sync( $response ) {
152
-		if ( ! $response->is_success() ) {
151
+	public function sync($response) {
152
+		if ( ! $response->is_success()) {
153 153
 			return;
154 154
 		}
155
-		$data = json_decode( $response->get_body(), true );
156
-		if ( ! is_array( $data ) || ! array_key_exists( 'networkDatasetId', $data ) ) {
155
+		$data = json_decode($response->get_body(), true);
156
+		if ( ! is_array($data) || ! array_key_exists('networkDatasetId', $data)) {
157 157
 			return;
158 158
 		}
159
-		$this->set_network_dataset_ids( $data['networkDatasetId'] );
159
+		$this->set_network_dataset_ids($data['networkDatasetId']);
160 160
 	}
161 161
 
162 162
 	/**
@@ -177,7 +177,7 @@  discard block
 block discarded – undo
177 177
 	 */
178 178
 	public static function get_instance() {
179 179
 
180
-		if ( ! isset( self::$instance ) ) {
180
+		if ( ! isset(self::$instance)) {
181 181
 			self::$instance = new self();
182 182
 		}
183 183
 
@@ -195,11 +195,11 @@  discard block
 block discarded – undo
195 195
 	 * @return mixed The configuration value or the default value if not found.
196 196
 	 * @since 3.6.0
197 197
 	 */
198
-	private function get( $option, $key, $default = '' ) {
198
+	private function get($option, $key, $default = '') {
199 199
 
200
-		$options = get_option( $option, array() );
200
+		$options = get_option($option, array());
201 201
 
202
-		return isset( $options[ $key ] ) ? $options[ $key ] : $default;
202
+		return isset($options[$key]) ? $options[$key] : $default;
203 203
 	}
204 204
 
205 205
 	/**
@@ -211,12 +211,12 @@  discard block
 block discarded – undo
211 211
 	 *
212 212
 	 * @since 3.9.0
213 213
 	 */
214
-	private function set( $option, $key, $value ) {
214
+	private function set($option, $key, $value) {
215 215
 
216
-		$values         = get_option( $option );
217
-		$values         = isset( $values ) ? $values : array();
218
-		$values[ $key ] = $value;
219
-		update_option( $option, $values );
216
+		$values         = get_option($option);
217
+		$values         = isset($values) ? $values : array();
218
+		$values[$key] = $value;
219
+		update_option($option, $values);
220 220
 
221 221
 	}
222 222
 
@@ -228,7 +228,7 @@  discard block
 block discarded – undo
228 228
 	 */
229 229
 	public function get_entity_base_path() {
230 230
 
231
-		return $this->get( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity' );
231
+		return $this->get('wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity');
232 232
 	}
233 233
 
234 234
 	/**
@@ -238,9 +238,9 @@  discard block
 block discarded – undo
238 238
 	 *
239 239
 	 * @since 3.9.0
240 240
 	 */
241
-	public function set_entity_base_path( $value ) {
241
+	public function set_entity_base_path($value) {
242 242
 
243
-		$this->set( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value );
243
+		$this->set('wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value);
244 244
 
245 245
 	}
246 246
 
@@ -252,7 +252,7 @@  discard block
 block discarded – undo
252 252
 	 */
253 253
 	public function is_skip_wizard() {
254 254
 
255
-		return $this->get( 'wl_general_settings', self::SKIP_WIZARD, false );
255
+		return $this->get('wl_general_settings', self::SKIP_WIZARD, false);
256 256
 	}
257 257
 
258 258
 	/**
@@ -262,9 +262,9 @@  discard block
 block discarded – undo
262 262
 	 *
263 263
 	 * @since 3.9.0
264 264
 	 */
265
-	public function set_skip_wizard( $value ) {
265
+	public function set_skip_wizard($value) {
266 266
 
267
-		$this->set( 'wl_general_settings', self::SKIP_WIZARD, true === $value );
267
+		$this->set('wl_general_settings', self::SKIP_WIZARD, true === $value);
268 268
 
269 269
 	}
270 270
 
@@ -276,7 +276,7 @@  discard block
 block discarded – undo
276 276
 	 */
277 277
 	public function get_key() {
278 278
 
279
-		return $this->get( 'wl_general_settings', self::KEY, '' );
279
+		return $this->get('wl_general_settings', self::KEY, '');
280 280
 	}
281 281
 
282 282
 	/**
@@ -286,9 +286,9 @@  discard block
 block discarded – undo
286 286
 	 *
287 287
 	 * @since 3.9.0
288 288
 	 */
289
-	public function set_key( $value ) {
289
+	public function set_key($value) {
290 290
 
291
-		$this->set( 'wl_general_settings', self::KEY, $value );
291
+		$this->set('wl_general_settings', self::KEY, $value);
292 292
 	}
293 293
 
294 294
 	/**
@@ -302,11 +302,11 @@  discard block
 block discarded – undo
302 302
 	public function get_language_code() {
303 303
 
304 304
 		$language = get_locale();
305
-		if ( ! $language ) {
305
+		if ( ! $language) {
306 306
 			return 'en';
307 307
 		}
308 308
 
309
-		return substr( $language, 0, 2 );
309
+		return substr($language, 0, 2);
310 310
 	}
311 311
 
312 312
 	/**
@@ -321,9 +321,9 @@  discard block
 block discarded – undo
321 321
 	 *
322 322
 	 * @since 3.9.0
323 323
 	 */
324
-	public function set_language_code( $value ) {
324
+	public function set_language_code($value) {
325 325
 
326
-		$this->set( 'wl_general_settings', self::LANGUAGE, $value );
326
+		$this->set('wl_general_settings', self::LANGUAGE, $value);
327 327
 
328 328
 	}
329 329
 
@@ -334,9 +334,9 @@  discard block
 block discarded – undo
334 334
 	 *
335 335
 	 * @since 3.19.0
336 336
 	 */
337
-	public function set_diagnostic_preferences( $value ) {
337
+	public function set_diagnostic_preferences($value) {
338 338
 
339
-		$this->set( 'wl_general_settings', self::SEND_DIAGNOSTIC, $value );
339
+		$this->set('wl_general_settings', self::SEND_DIAGNOSTIC, $value);
340 340
 
341 341
 	}
342 342
 
@@ -347,7 +347,7 @@  discard block
 block discarded – undo
347 347
 	 */
348 348
 	public function get_diagnostic_preferences() {
349 349
 
350
-		return $this->get( 'wl_general_settings', self::SEND_DIAGNOSTIC, 'no' );
350
+		return $this->get('wl_general_settings', self::SEND_DIAGNOSTIC, 'no');
351 351
 	}
352 352
 
353 353
 	/**
@@ -358,7 +358,7 @@  discard block
 block discarded – undo
358 358
 	 */
359 359
 	public function get_country_code() {
360 360
 
361
-		return $this->get( 'wl_general_settings', self::COUNTRY_CODE, 'us' );
361
+		return $this->get('wl_general_settings', self::COUNTRY_CODE, 'us');
362 362
 	}
363 363
 
364 364
 	/**
@@ -368,9 +368,9 @@  discard block
 block discarded – undo
368 368
 	 *
369 369
 	 * @since 3.18.0
370 370
 	 */
371
-	public function set_country_code( $value ) {
371
+	public function set_country_code($value) {
372 372
 
373
-		$this->set( 'wl_general_settings', self::COUNTRY_CODE, $value );
373
+		$this->set('wl_general_settings', self::COUNTRY_CODE, $value);
374 374
 
375 375
 	}
376 376
 
@@ -385,7 +385,7 @@  discard block
 block discarded – undo
385 385
 	 */
386 386
 	public function get_publisher_id() {
387 387
 
388
-		return $this->get( 'wl_general_settings', self::PUBLISHER_ID, null );
388
+		return $this->get('wl_general_settings', self::PUBLISHER_ID, null);
389 389
 	}
390 390
 
391 391
 	/**
@@ -395,9 +395,9 @@  discard block
 block discarded – undo
395 395
 	 *
396 396
 	 * @since 3.9.0
397 397
 	 */
398
-	public function set_publisher_id( $value ) {
398
+	public function set_publisher_id($value) {
399 399
 
400
-		$this->set( 'wl_general_settings', self::PUBLISHER_ID, $value );
400
+		$this->set('wl_general_settings', self::PUBLISHER_ID, $value);
401 401
 
402 402
 	}
403 403
 
@@ -410,8 +410,8 @@  discard block
 block discarded – undo
410 410
 	 */
411 411
 	public function get_dataset_uri() {
412 412
 
413
-		if ( apply_filters( 'wl_feature__enable__dataset', true ) ) {
414
-			return $this->get( 'wl_advanced_settings', self::DATASET_URI, null );
413
+		if (apply_filters('wl_feature__enable__dataset', true)) {
414
+			return $this->get('wl_advanced_settings', self::DATASET_URI, null);
415 415
 		} else {
416 416
 			return null;
417 417
 		}
@@ -424,9 +424,9 @@  discard block
 block discarded – undo
424 424
 	 *
425 425
 	 * @since 3.10.0
426 426
 	 */
427
-	public function set_dataset_uri( $value ) {
427
+	public function set_dataset_uri($value) {
428 428
 
429
-		$this->set( 'wl_advanced_settings', self::DATASET_URI, $value );
429
+		$this->set('wl_advanced_settings', self::DATASET_URI, $value);
430 430
 	}
431 431
 
432 432
 	/**
@@ -437,7 +437,7 @@  discard block
 block discarded – undo
437 437
 	 */
438 438
 	public function get_package_type() {
439 439
 
440
-		return $this->get( 'wl_advanced_settings', self::PACKAGE_TYPE, null );
440
+		return $this->get('wl_advanced_settings', self::PACKAGE_TYPE, null);
441 441
 	}
442 442
 
443 443
 	/**
@@ -447,8 +447,8 @@  discard block
 block discarded – undo
447 447
 	 *
448 448
 	 * @since 3.20.0
449 449
 	 */
450
-	public function set_package_type( $value ) {
451
-		$this->set( 'wl_advanced_settings', self::PACKAGE_TYPE, $value );
450
+	public function set_package_type($value) {
451
+		$this->set('wl_advanced_settings', self::PACKAGE_TYPE, $value);
452 452
 	}
453 453
 
454 454
 	/**
@@ -463,11 +463,11 @@  discard block
 block discarded – undo
463 463
 	 * @param array $old_value The old settings.
464 464
 	 * @param array $new_value The new settings.
465 465
 	 */
466
-	public function update_key( $old_value, $new_value ) {
466
+	public function update_key($old_value, $new_value) {
467 467
 
468 468
 		// Check the old key value and the new one. We're going to ask for the dataset URI only if the key has changed.
469 469
 		// $old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
470
-		$new_key = isset( $new_value['key'] ) ? $new_value['key'] : '';
470
+		$new_key = isset($new_value['key']) ? $new_value['key'] : '';
471 471
 
472 472
 		// If the key hasn't changed, don't do anything.
473 473
 		// WARN The 'update_option' hook is fired only if the new and old value are not equal.
@@ -476,14 +476,14 @@  discard block
 block discarded – undo
476 476
 		// }
477 477
 
478 478
 		// If the key is empty, empty the dataset URI.
479
-		if ( '' === $new_key ) {
480
-			$this->set_dataset_uri( '' );
479
+		if ('' === $new_key) {
480
+			$this->set_dataset_uri('');
481 481
 		}
482 482
 
483 483
 		// make the request to the remote server.
484
-		$this->get_remote_dataset_uri( $new_key );
484
+		$this->get_remote_dataset_uri($new_key);
485 485
 
486
-		do_action( 'wl_key_updated' );
486
+		do_action('wl_key_updated');
487 487
 
488 488
 	}
489 489
 
@@ -499,15 +499,15 @@  discard block
 block discarded – undo
499 499
 	 *
500 500
 	 * @since 3.17.0 send the site URL and get the dataset URI.
501 501
 	 */
502
-	public function get_remote_dataset_uri( $key ) {
502
+	public function get_remote_dataset_uri($key) {
503 503
 
504
-		$this->log->trace( 'Getting the remote dataset URI and package type...' );
504
+		$this->log->trace('Getting the remote dataset URI and package type...');
505 505
 
506
-		if ( empty( $key ) ) {
507
-			$this->log->warn( 'Key set to empty value.' );
506
+		if (empty($key)) {
507
+			$this->log->warn('Key set to empty value.');
508 508
 
509
-			$this->set_dataset_uri( '' );
510
-			$this->set_package_type( null );
509
+			$this->set_dataset_uri('');
510
+			$this->set_package_type(null);
511 511
 
512 512
 			return;
513 513
 		}
@@ -521,15 +521,15 @@  discard block
 block discarded – undo
521 521
 		 *
522 522
 		 * @since 3.20.0
523 523
 		 */
524
-		$home_url = get_option( 'home' );
525
-		$site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
524
+		$home_url = get_option('home');
525
+		$site_url = apply_filters('wl_production_site_url', untrailingslashit($home_url));
526 526
 
527 527
 		// Build the URL.
528 528
 		$url = '/accounts'
529
-			   . '?key=' . rawurlencode( $key )
530
-			   . '&url=' . rawurlencode( $site_url )
531
-			   . '&country=' . $this->get_country_code()
532
-			   . '&language=' . $this->get_language_code();
529
+			   . '?key='.rawurlencode($key)
530
+			   . '&url='.rawurlencode($site_url)
531
+			   . '&country='.$this->get_country_code()
532
+			   . '&language='.$this->get_language_code();
533 533
 
534 534
 		$api_service = Default_Api_Service::get_instance();
535 535
 		/**
@@ -539,32 +539,32 @@  discard block
 block discarded – undo
539 539
 		$headers  = array(
540 540
 			'Authorization' => "Key $key",
541 541
 		);
542
-		$response = $api_service->request( 'PUT', $url, $headers )->get_response();
542
+		$response = $api_service->request('PUT', $url, $headers)->get_response();
543 543
 
544 544
 		// The response is an error.
545
-		if ( is_wp_error( $response ) ) {
546
-			$this->log->error( 'An error occurred setting the dataset URI: ' . $response->get_error_message() );
545
+		if (is_wp_error($response)) {
546
+			$this->log->error('An error occurred setting the dataset URI: '.$response->get_error_message());
547 547
 
548
-			$this->set_dataset_uri( '' );
549
-			$this->set_package_type( null );
548
+			$this->set_dataset_uri('');
549
+			$this->set_package_type(null);
550 550
 
551 551
 			return;
552 552
 		}
553 553
 
554 554
 		// The response is not OK.
555
-		if ( ! is_array( $response ) || 200 !== (int) $response['response']['code'] ) {
555
+		if ( ! is_array($response) || 200 !== (int) $response['response']['code']) {
556 556
 			$base_url = $api_service->get_base_url();
557 557
 
558
-			if ( ! is_array( $response ) ) {
558
+			if ( ! is_array($response)) {
559 559
 				// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
560
-				$this->log->error( "Unexpected response when opening URL $base_url$url: " . var_export( $response, true ) );
560
+				$this->log->error("Unexpected response when opening URL $base_url$url: ".var_export($response, true));
561 561
 			} else {
562 562
 				// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
563
-				$this->log->error( "Unexpected status code when opening URL $base_url$url: " . $response['response']['code'] . "\n" . var_export( $response, true ) );
563
+				$this->log->error("Unexpected status code when opening URL $base_url$url: ".$response['response']['code']."\n".var_export($response, true));
564 564
 			}
565 565
 
566
-			$this->set_dataset_uri( '' );
567
-			$this->set_package_type( null );
566
+			$this->set_dataset_uri('');
567
+			$this->set_package_type(null);
568 568
 
569 569
 			return;
570 570
 		}
@@ -574,20 +574,20 @@  discard block
 block discarded – undo
574 574
 		 *
575 575
 		 * @since 3.20.0
576 576
 		 */
577
-		$json = json_decode( $response['body'] );
577
+		$json = json_decode($response['body']);
578 578
 		/**
579 579
 		 * @since 3.27.7
580 580
 		 * Remove the trailing slash returned from the new platform api.
581 581
 		 */
582 582
 		// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
583
-		$dataset_uri = untrailingslashit( $json->datasetURI );
583
+		$dataset_uri = untrailingslashit($json->datasetURI);
584 584
 		// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
585
-		$package_type = isset( $json->packageType ) ? $json->packageType : null;
585
+		$package_type = isset($json->packageType) ? $json->packageType : null;
586 586
 
587
-		$this->log->info( "Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]..." );
587
+		$this->log->info("Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]...");
588 588
 
589
-		$this->set_dataset_uri( $dataset_uri );
590
-		$this->set_package_type( $package_type );
589
+		$this->set_dataset_uri($dataset_uri);
590
+		$this->set_package_type($package_type);
591 591
 	}
592 592
 
593 593
 	/**
@@ -604,20 +604,20 @@  discard block
 block discarded – undo
604 604
 	 * @return mixed The same value in the $value parameter
605 605
 	 * @since 3.12.0
606 606
 	 */
607
-	public function maybe_update_dataset_uri( $value, $old_value ) {
607
+	public function maybe_update_dataset_uri($value, $old_value) {
608 608
 
609 609
 		// Check the old key value and the new one. Here we're only handling the
610 610
 		// case where the key hasn't changed and the dataset URI isn't set. The
611 611
 		// other case, i.e. a new key is inserted, is handled at `update_key`.
612
-		$old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
613
-		$new_key = isset( $value['key'] ) ? $value['key'] : '';
612
+		$old_key = isset($old_value['key']) ? $old_value['key'] : '';
613
+		$new_key = isset($value['key']) ? $value['key'] : '';
614 614
 
615 615
 		$dataset_uri = $this->get_dataset_uri();
616 616
 
617
-		if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) {
617
+		if ( ! empty($new_key) && $new_key === $old_key && empty($dataset_uri)) {
618 618
 
619 619
 			// make the request to the remote server to try to get the dataset uri.
620
-			$this->get_remote_dataset_uri( $new_key );
620
+			$this->get_remote_dataset_uri($new_key);
621 621
 		}
622 622
 
623 623
 		return $value;
@@ -631,9 +631,9 @@  discard block
 block discarded – undo
631 631
 	 * @return string The API URI.
632 632
 	 * @since 3.11.0
633 633
 	 */
634
-	public function get_accounts_by_key_dataset_uri( $key ) {
634
+	public function get_accounts_by_key_dataset_uri($key) {
635 635
 
636
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri";
636
+		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE."accounts/key=$key/dataset_uri";
637 637
 	}
638 638
 
639 639
 	/**
@@ -644,7 +644,7 @@  discard block
 block discarded – undo
644 644
 	 */
645 645
 	public function get_accounts() {
646 646
 
647
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts';
647
+		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'accounts';
648 648
 	}
649 649
 
650 650
 	/**
@@ -655,7 +655,7 @@  discard block
 block discarded – undo
655 655
 	 */
656 656
 	public function is_link_by_default() {
657 657
 
658
-		return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' );
658
+		return 'yes' === $this->get('wl_general_settings', self::LINK_BY_DEFAULT, 'yes');
659 659
 	}
660 660
 
661 661
 	/**
@@ -665,9 +665,9 @@  discard block
 block discarded – undo
665 665
 	 *
666 666
 	 * @since 3.13.0
667 667
 	 */
668
-	public function set_link_by_default( $value ) {
668
+	public function set_link_by_default($value) {
669 669
 
670
-		$this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' );
670
+		$this->set('wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no');
671 671
 	}
672 672
 
673 673
 	/**
@@ -677,7 +677,7 @@  discard block
 block discarded – undo
677 677
 	 * @since 3.21.0
678 678
 	 */
679 679
 	public function is_analytics_enable() {
680
-		return 'yes' === $this->get( 'wl_analytics_settings', self::ANALYTICS_ENABLE, 'no' );
680
+		return 'yes' === $this->get('wl_analytics_settings', self::ANALYTICS_ENABLE, 'no');
681 681
 	}
682 682
 
683 683
 	/**
@@ -687,9 +687,9 @@  discard block
 block discarded – undo
687 687
 	 *
688 688
 	 * @since 3.21.0
689 689
 	 */
690
-	public function set_is_analytics_enable( $value ) {
690
+	public function set_is_analytics_enable($value) {
691 691
 
692
-		$this->set( 'wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no' );
692
+		$this->set('wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no');
693 693
 	}
694 694
 
695 695
 	/**
@@ -699,7 +699,7 @@  discard block
 block discarded – undo
699 699
 	 * @since 3.21.0
700 700
 	 */
701 701
 	public function get_analytics_entity_uri_dimension() {
702
-		return (int) $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1 );
702
+		return (int) $this->get('wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1);
703 703
 	}
704 704
 
705 705
 	/**
@@ -709,7 +709,7 @@  discard block
 block discarded – undo
709 709
 	 * @since 3.21.0
710 710
 	 */
711 711
 	public function get_analytics_entity_type_dimension() {
712
-		return $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2 );
712
+		return $this->get('wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2);
713 713
 	}
714 714
 
715 715
 	/**
@@ -720,7 +720,7 @@  discard block
 block discarded – undo
720 720
 	 */
721 721
 	public function get_autocomplete_url() {
722 722
 
723
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete';
723
+		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'autocomplete';
724 724
 
725 725
 	}
726 726
 
@@ -732,7 +732,7 @@  discard block
 block discarded – undo
732 732
 	 */
733 733
 	public function get_deactivation_feedback_url() {
734 734
 
735
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'feedbacks';
735
+		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'feedbacks';
736 736
 
737 737
 	}
738 738
 
@@ -748,11 +748,11 @@  discard block
 block discarded – undo
748 748
 	}
749 749
 
750 750
 	public function get_network_dataset_ids() {
751
-		return $this->get( 'wl_advanced_settings', self::NETWORK_DATASET_IDS, array() );
751
+		return $this->get('wl_advanced_settings', self::NETWORK_DATASET_IDS, array());
752 752
 	}
753 753
 
754
-	public function set_network_dataset_ids( $network_dataset_ids ) {
755
-		$this->set( 'wl_advanced_settings', self::NETWORK_DATASET_IDS, $network_dataset_ids );
754
+	public function set_network_dataset_ids($network_dataset_ids) {
755
+		$this->set('wl_advanced_settings', self::NETWORK_DATASET_IDS, $network_dataset_ids);
756 756
 	}
757 757
 
758 758
 }
Please login to merge, or discard this patch.
Indentation   +728 added lines, -728 removed lines patch added patch discarded remove patch
@@ -13,7 +13,7 @@  discard block
 block discarded – undo
13 13
 use Wordlift\Api\Default_Api_Service;
14 14
 
15 15
 if ( ! defined( 'ABSPATH' ) ) {
16
-	exit;
16
+    exit;
17 17
 }
18 18
 
19 19
 /**
@@ -23,736 +23,736 @@  discard block
 block discarded – undo
23 23
  */
24 24
 class Wordlift_Configuration_Service {
25 25
 
26
-	/**
27
-	 * The entity base path option name.
28
-	 *
29
-	 * @since 3.6.0
30
-	 */
31
-	const ENTITY_BASE_PATH_KEY = 'wl_entity_base_path';
32
-
33
-	/**
34
-	 * The skip wizard (admin installation wizard) option name.
35
-	 *
36
-	 * @since 3.9.0
37
-	 */
38
-	const SKIP_WIZARD = 'wl_skip_wizard';
39
-
40
-	/**
41
-	 * WordLift's key option name.
42
-	 *
43
-	 * @since 3.9.0
44
-	 */
45
-	const KEY = 'key';
46
-
47
-	/**
48
-	 * WordLift's configured language option name.
49
-	 *
50
-	 * @since 3.9.0
51
-	 */
52
-	const LANGUAGE = 'site_language';
53
-
54
-	/**
55
-	 * WordLift's configured country code.
56
-	 *
57
-	 * @since 3.18.0
58
-	 */
59
-	const COUNTRY_CODE = 'country_code';
60
-
61
-	/**
62
-	 * The publisher entity post ID option name.
63
-	 *
64
-	 * @since 3.9.0
65
-	 */
66
-	const PUBLISHER_ID = 'publisher_id';
67
-
68
-	/**
69
-	 * The dataset URI option name
70
-	 *
71
-	 * @since 3.10.0
72
-	 */
73
-	const DATASET_URI = 'redlink_dataset_uri';
74
-
75
-	/**
76
-	 * The link by default option name.
77
-	 *
78
-	 * @since 3.11.0
79
-	 */
80
-	const LINK_BY_DEFAULT = 'link_by_default';
81
-
82
-	/**
83
-	 * The analytics enable option.
84
-	 *
85
-	 * @since 3.21.0
86
-	 */
87
-	const ANALYTICS_ENABLE = 'analytics_enable';
88
-
89
-	/**
90
-	 * The analytics entity uri dimension option.
91
-	 *
92
-	 * @since 3.21.0
93
-	 */
94
-	const ANALYTICS_ENTITY_URI_DIMENSION = 'analytics_entity_uri_dimension';
95
-
96
-	/**
97
-	 * The analytics entity type dimension option.
98
-	 *
99
-	 * @since 3.21.0
100
-	 */
101
-	const ANALYTICS_ENTITY_TYPE_DIMENSION = 'analytics_entity_type_dimension';
102
-
103
-	/**
104
-	 * The user preferences about sharing data option.
105
-	 *
106
-	 * @since 3.19.0
107
-	 */
108
-	const SEND_DIAGNOSTIC = 'send_diagnostic';
109
-
110
-	/**
111
-	 * The package type configuration key.
112
-	 *
113
-	 * @since 3.20.0
114
-	 */
115
-	const PACKAGE_TYPE = 'package_type';
116
-	/**
117
-	 * The dataset ids connected to the current key
118
-	 *
119
-	 * @since 3.38.5
120
-	 */
121
-	const NETWORK_DATASET_IDS = 'network_dataset_ids';
122
-
123
-	/**
124
-	 * The {@link Wordlift_Log_Service} instance.
125
-	 *
126
-	 * @since 3.16.0
127
-	 *
128
-	 * @var \Wordlift_Log_Service $log The {@link Wordlift_Log_Service} instance.
129
-	 */
130
-	private $log;
131
-
132
-	/**
133
-	 * Create a Wordlift_Configuration_Service's instance.
134
-	 *
135
-	 * @since 3.6.0
136
-	 */
137
-	protected function __construct() {
138
-
139
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
140
-
141
-		// Sync some configuration properties when key is validated.
142
-		add_action( 'wl_key_validation_response', array( $this, 'sync' ) );
143
-
144
-	}
145
-
146
-	/**
147
-	 * @param $response \Wordlift\Api\Response
148
-	 *
149
-	 * @return void
150
-	 */
151
-	public function sync( $response ) {
152
-		if ( ! $response->is_success() ) {
153
-			return;
154
-		}
155
-		$data = json_decode( $response->get_body(), true );
156
-		if ( ! is_array( $data ) || ! array_key_exists( 'networkDatasetId', $data ) ) {
157
-			return;
158
-		}
159
-		$this->set_network_dataset_ids( $data['networkDatasetId'] );
160
-	}
161
-
162
-	/**
163
-	 * The Wordlift_Configuration_Service's singleton instance.
164
-	 *
165
-	 * @since  3.6.0
166
-	 *
167
-	 * @access private
168
-	 * @var \Wordlift_Configuration_Service $instance Wordlift_Configuration_Service's singleton instance.
169
-	 */
170
-	private static $instance = null;
171
-
172
-	/**
173
-	 * Get the singleton instance.
174
-	 *
175
-	 * @return \Wordlift_Configuration_Service
176
-	 * @since 3.6.0
177
-	 */
178
-	public static function get_instance() {
179
-
180
-		if ( ! isset( self::$instance ) ) {
181
-			self::$instance = new self();
182
-		}
183
-
184
-		return self::$instance;
185
-	}
186
-
187
-	/**
188
-	 * Get a configuration given the option name and a key. The option value is
189
-	 * expected to be an array.
190
-	 *
191
-	 * @param string $option The option name.
192
-	 * @param string $key A key in the option value array.
193
-	 * @param string $default The default value in case the key is not found (by default an empty string).
194
-	 *
195
-	 * @return mixed The configuration value or the default value if not found.
196
-	 * @since 3.6.0
197
-	 */
198
-	private function get( $option, $key, $default = '' ) {
199
-
200
-		$options = get_option( $option, array() );
201
-
202
-		return isset( $options[ $key ] ) ? $options[ $key ] : $default;
203
-	}
204
-
205
-	/**
206
-	 * Set a configuration parameter.
207
-	 *
208
-	 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
209
-	 * @param string $key The value key.
210
-	 * @param mixed  $value The value.
211
-	 *
212
-	 * @since 3.9.0
213
-	 */
214
-	private function set( $option, $key, $value ) {
215
-
216
-		$values         = get_option( $option );
217
-		$values         = isset( $values ) ? $values : array();
218
-		$values[ $key ] = $value;
219
-		update_option( $option, $values );
220
-
221
-	}
222
-
223
-	/**
224
-	 * Get the entity base path, by default 'entity'.
225
-	 *
226
-	 * @return string The entity base path.
227
-	 * @since 3.6.0
228
-	 */
229
-	public function get_entity_base_path() {
230
-
231
-		return $this->get( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity' );
232
-	}
233
-
234
-	/**
235
-	 * Get the entity base path.
236
-	 *
237
-	 * @param string $value The entity base path.
238
-	 *
239
-	 * @since 3.9.0
240
-	 */
241
-	public function set_entity_base_path( $value ) {
242
-
243
-		$this->set( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value );
244
-
245
-	}
246
-
247
-	/**
248
-	 * Whether the installation skip wizard should be skipped.
249
-	 *
250
-	 * @return bool True if it should be skipped otherwise false.
251
-	 * @since 3.9.0
252
-	 */
253
-	public function is_skip_wizard() {
254
-
255
-		return $this->get( 'wl_general_settings', self::SKIP_WIZARD, false );
256
-	}
257
-
258
-	/**
259
-	 * Set the skip wizard parameter.
260
-	 *
261
-	 * @param bool $value True to skip the wizard. We expect a boolean value.
262
-	 *
263
-	 * @since 3.9.0
264
-	 */
265
-	public function set_skip_wizard( $value ) {
266
-
267
-		$this->set( 'wl_general_settings', self::SKIP_WIZARD, true === $value );
268
-
269
-	}
270
-
271
-	/**
272
-	 * Get WordLift's key.
273
-	 *
274
-	 * @return string WordLift's key or an empty string if not set.
275
-	 * @since 3.9.0
276
-	 */
277
-	public function get_key() {
278
-
279
-		return $this->get( 'wl_general_settings', self::KEY, '' );
280
-	}
281
-
282
-	/**
283
-	 * Set WordLift's key.
284
-	 *
285
-	 * @param string $value WordLift's key.
286
-	 *
287
-	 * @since 3.9.0
288
-	 */
289
-	public function set_key( $value ) {
290
-
291
-		$this->set( 'wl_general_settings', self::KEY, $value );
292
-	}
293
-
294
-	/**
295
-	 * Get WordLift's configured language, by default 'en'.
296
-	 *
297
-	 * Note that WordLift's language is used when writing strings to the Linked Data dataset, not for the analysis.
298
-	 *
299
-	 * @return string WordLift's configured language code ('en' by default).
300
-	 * @since 3.9.0
301
-	 */
302
-	public function get_language_code() {
303
-
304
-		$language = get_locale();
305
-		if ( ! $language ) {
306
-			return 'en';
307
-		}
308
-
309
-		return substr( $language, 0, 2 );
310
-	}
311
-
312
-	/**
313
-	 * @param string $value WordLift's language code.
314
-	 *
315
-	 * @see https://github.com/insideout10/wordlift-plugin/issues/1466
316
-	 *
317
-	 * Set WordLift's language code, used when storing strings to the Linked Data dataset.
318
-	 *
319
-	 * @deprecated As of 3.32.7 this below method has no effect on setting the language, we use the
320
-	 * language code form WordPress directly.
321
-	 *
322
-	 * @since 3.9.0
323
-	 */
324
-	public function set_language_code( $value ) {
325
-
326
-		$this->set( 'wl_general_settings', self::LANGUAGE, $value );
327
-
328
-	}
329
-
330
-	/**
331
-	 * Set the user preferences about sharing diagnostic with us.
332
-	 *
333
-	 * @param string $value The user preferences(yes/no).
334
-	 *
335
-	 * @since 3.19.0
336
-	 */
337
-	public function set_diagnostic_preferences( $value ) {
338
-
339
-		$this->set( 'wl_general_settings', self::SEND_DIAGNOSTIC, $value );
340
-
341
-	}
342
-
343
-	/**
344
-	 * Get the user preferences about sharing diagnostic.
345
-	 *
346
-	 * @since 3.19.0
347
-	 */
348
-	public function get_diagnostic_preferences() {
349
-
350
-		return $this->get( 'wl_general_settings', self::SEND_DIAGNOSTIC, 'no' );
351
-	}
352
-
353
-	/**
354
-	 * Get WordLift's configured country code, by default 'us'.
355
-	 *
356
-	 * @return string WordLift's configured country code ('us' by default).
357
-	 * @since 3.18.0
358
-	 */
359
-	public function get_country_code() {
360
-
361
-		return $this->get( 'wl_general_settings', self::COUNTRY_CODE, 'us' );
362
-	}
363
-
364
-	/**
365
-	 * Set WordLift's country code.
366
-	 *
367
-	 * @param string $value WordLift's country code.
368
-	 *
369
-	 * @since 3.18.0
370
-	 */
371
-	public function set_country_code( $value ) {
372
-
373
-		$this->set( 'wl_general_settings', self::COUNTRY_CODE, $value );
374
-
375
-	}
376
-
377
-	/**
378
-	 * Get the publisher entity post id.
379
-	 *
380
-	 * The publisher entity post id points to an entity post which contains the data for the publisher used in schema.org
381
-	 * Article markup.
382
-	 *
383
-	 * @return int|NULL The publisher entity post id or NULL if not set.
384
-	 * @since 3.9.0
385
-	 */
386
-	public function get_publisher_id() {
387
-
388
-		return $this->get( 'wl_general_settings', self::PUBLISHER_ID, null );
389
-	}
390
-
391
-	/**
392
-	 * Set the publisher entity post id.
393
-	 *
394
-	 * @param int $value The publisher entity post id.
395
-	 *
396
-	 * @since 3.9.0
397
-	 */
398
-	public function set_publisher_id( $value ) {
399
-
400
-		$this->set( 'wl_general_settings', self::PUBLISHER_ID, $value );
401
-
402
-	}
403
-
404
-	/**
405
-	 * Get the dataset URI.
406
-	 *
407
-	 * @return string The dataset URI or an empty string if not set.
408
-	 * @since 3.10.0
409
-	 * @since 3.27.7 Always return null if `wl_features__enable__dataset` is disabled.
410
-	 */
411
-	public function get_dataset_uri() {
412
-
413
-		if ( apply_filters( 'wl_feature__enable__dataset', true ) ) {
414
-			return $this->get( 'wl_advanced_settings', self::DATASET_URI, null );
415
-		} else {
416
-			return null;
417
-		}
418
-	}
419
-
420
-	/**
421
-	 * Set the dataset URI.
422
-	 *
423
-	 * @param string $value The dataset URI.
424
-	 *
425
-	 * @since 3.10.0
426
-	 */
427
-	public function set_dataset_uri( $value ) {
428
-
429
-		$this->set( 'wl_advanced_settings', self::DATASET_URI, $value );
430
-	}
431
-
432
-	/**
433
-	 * Get the package type.
434
-	 *
435
-	 * @return string The package type or an empty string if not set.
436
-	 * @since 3.20.0
437
-	 */
438
-	public function get_package_type() {
439
-
440
-		return $this->get( 'wl_advanced_settings', self::PACKAGE_TYPE, null );
441
-	}
442
-
443
-	/**
444
-	 * Set the package type.
445
-	 *
446
-	 * @param string $value The package type.
447
-	 *
448
-	 * @since 3.20.0
449
-	 */
450
-	public function set_package_type( $value ) {
451
-		$this->set( 'wl_advanced_settings', self::PACKAGE_TYPE, $value );
452
-	}
453
-
454
-	/**
455
-	 * Intercept the change of the WordLift key in order to set the dataset URI.
456
-	 *
457
-	 * @since 3.20.0 as of #761, we save settings every time a key is set, not only when the key changes, so to
458
-	 *               store the configuration parameters such as country or language.
459
-	 * @since 3.11.0
460
-	 *
461
-	 * @see https://github.com/insideout10/wordlift-plugin/issues/761
462
-	 *
463
-	 * @param array $old_value The old settings.
464
-	 * @param array $new_value The new settings.
465
-	 */
466
-	public function update_key( $old_value, $new_value ) {
467
-
468
-		// Check the old key value and the new one. We're going to ask for the dataset URI only if the key has changed.
469
-		// $old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
470
-		$new_key = isset( $new_value['key'] ) ? $new_value['key'] : '';
471
-
472
-		// If the key hasn't changed, don't do anything.
473
-		// WARN The 'update_option' hook is fired only if the new and old value are not equal.
474
-		// if ( $old_key === $new_key ) {
475
-		// return;
476
-		// }
477
-
478
-		// If the key is empty, empty the dataset URI.
479
-		if ( '' === $new_key ) {
480
-			$this->set_dataset_uri( '' );
481
-		}
482
-
483
-		// make the request to the remote server.
484
-		$this->get_remote_dataset_uri( $new_key );
485
-
486
-		do_action( 'wl_key_updated' );
487
-
488
-	}
489
-
490
-	/**
491
-	 * Handle retrieving the dataset uri from the remote server.
492
-	 *
493
-	 * If a valid dataset uri is returned it is stored in the appropriate option,
494
-	 * otherwise the option is set to empty string.
495
-	 *
496
-	 * @param string $key The key to be used.
497
-	 *
498
-	 * @since 3.12.0
499
-	 *
500
-	 * @since 3.17.0 send the site URL and get the dataset URI.
501
-	 */
502
-	public function get_remote_dataset_uri( $key ) {
503
-
504
-		$this->log->trace( 'Getting the remote dataset URI and package type...' );
505
-
506
-		if ( empty( $key ) ) {
507
-			$this->log->warn( 'Key set to empty value.' );
508
-
509
-			$this->set_dataset_uri( '' );
510
-			$this->set_package_type( null );
511
-
512
-			return;
513
-		}
514
-
515
-		/**
516
-		 * Allow 3rd parties to change the site_url.
517
-		 *
518
-		 * @param string $site_url The site url.
519
-		 *
520
-		 * @see https://github.com/insideout10/wordlift-plugin/issues/850
521
-		 *
522
-		 * @since 3.20.0
523
-		 */
524
-		$home_url = get_option( 'home' );
525
-		$site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
526
-
527
-		// Build the URL.
528
-		$url = '/accounts'
529
-			   . '?key=' . rawurlencode( $key )
530
-			   . '&url=' . rawurlencode( $site_url )
531
-			   . '&country=' . $this->get_country_code()
532
-			   . '&language=' . $this->get_language_code();
533
-
534
-		$api_service = Default_Api_Service::get_instance();
535
-		/**
536
-		 * @since 3.27.7.1
537
-		 * The Key should be passed to headers, otherwise api would return null.
538
-		 */
539
-		$headers  = array(
540
-			'Authorization' => "Key $key",
541
-		);
542
-		$response = $api_service->request( 'PUT', $url, $headers )->get_response();
543
-
544
-		// The response is an error.
545
-		if ( is_wp_error( $response ) ) {
546
-			$this->log->error( 'An error occurred setting the dataset URI: ' . $response->get_error_message() );
547
-
548
-			$this->set_dataset_uri( '' );
549
-			$this->set_package_type( null );
550
-
551
-			return;
552
-		}
553
-
554
-		// The response is not OK.
555
-		if ( ! is_array( $response ) || 200 !== (int) $response['response']['code'] ) {
556
-			$base_url = $api_service->get_base_url();
557
-
558
-			if ( ! is_array( $response ) ) {
559
-				// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
560
-				$this->log->error( "Unexpected response when opening URL $base_url$url: " . var_export( $response, true ) );
561
-			} else {
562
-				// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
563
-				$this->log->error( "Unexpected status code when opening URL $base_url$url: " . $response['response']['code'] . "\n" . var_export( $response, true ) );
564
-			}
565
-
566
-			$this->set_dataset_uri( '' );
567
-			$this->set_package_type( null );
568
-
569
-			return;
570
-		}
571
-
572
-		/*
26
+    /**
27
+     * The entity base path option name.
28
+     *
29
+     * @since 3.6.0
30
+     */
31
+    const ENTITY_BASE_PATH_KEY = 'wl_entity_base_path';
32
+
33
+    /**
34
+     * The skip wizard (admin installation wizard) option name.
35
+     *
36
+     * @since 3.9.0
37
+     */
38
+    const SKIP_WIZARD = 'wl_skip_wizard';
39
+
40
+    /**
41
+     * WordLift's key option name.
42
+     *
43
+     * @since 3.9.0
44
+     */
45
+    const KEY = 'key';
46
+
47
+    /**
48
+     * WordLift's configured language option name.
49
+     *
50
+     * @since 3.9.0
51
+     */
52
+    const LANGUAGE = 'site_language';
53
+
54
+    /**
55
+     * WordLift's configured country code.
56
+     *
57
+     * @since 3.18.0
58
+     */
59
+    const COUNTRY_CODE = 'country_code';
60
+
61
+    /**
62
+     * The publisher entity post ID option name.
63
+     *
64
+     * @since 3.9.0
65
+     */
66
+    const PUBLISHER_ID = 'publisher_id';
67
+
68
+    /**
69
+     * The dataset URI option name
70
+     *
71
+     * @since 3.10.0
72
+     */
73
+    const DATASET_URI = 'redlink_dataset_uri';
74
+
75
+    /**
76
+     * The link by default option name.
77
+     *
78
+     * @since 3.11.0
79
+     */
80
+    const LINK_BY_DEFAULT = 'link_by_default';
81
+
82
+    /**
83
+     * The analytics enable option.
84
+     *
85
+     * @since 3.21.0
86
+     */
87
+    const ANALYTICS_ENABLE = 'analytics_enable';
88
+
89
+    /**
90
+     * The analytics entity uri dimension option.
91
+     *
92
+     * @since 3.21.0
93
+     */
94
+    const ANALYTICS_ENTITY_URI_DIMENSION = 'analytics_entity_uri_dimension';
95
+
96
+    /**
97
+     * The analytics entity type dimension option.
98
+     *
99
+     * @since 3.21.0
100
+     */
101
+    const ANALYTICS_ENTITY_TYPE_DIMENSION = 'analytics_entity_type_dimension';
102
+
103
+    /**
104
+     * The user preferences about sharing data option.
105
+     *
106
+     * @since 3.19.0
107
+     */
108
+    const SEND_DIAGNOSTIC = 'send_diagnostic';
109
+
110
+    /**
111
+     * The package type configuration key.
112
+     *
113
+     * @since 3.20.0
114
+     */
115
+    const PACKAGE_TYPE = 'package_type';
116
+    /**
117
+     * The dataset ids connected to the current key
118
+     *
119
+     * @since 3.38.5
120
+     */
121
+    const NETWORK_DATASET_IDS = 'network_dataset_ids';
122
+
123
+    /**
124
+     * The {@link Wordlift_Log_Service} instance.
125
+     *
126
+     * @since 3.16.0
127
+     *
128
+     * @var \Wordlift_Log_Service $log The {@link Wordlift_Log_Service} instance.
129
+     */
130
+    private $log;
131
+
132
+    /**
133
+     * Create a Wordlift_Configuration_Service's instance.
134
+     *
135
+     * @since 3.6.0
136
+     */
137
+    protected function __construct() {
138
+
139
+        $this->log = Wordlift_Log_Service::get_logger( get_class() );
140
+
141
+        // Sync some configuration properties when key is validated.
142
+        add_action( 'wl_key_validation_response', array( $this, 'sync' ) );
143
+
144
+    }
145
+
146
+    /**
147
+     * @param $response \Wordlift\Api\Response
148
+     *
149
+     * @return void
150
+     */
151
+    public function sync( $response ) {
152
+        if ( ! $response->is_success() ) {
153
+            return;
154
+        }
155
+        $data = json_decode( $response->get_body(), true );
156
+        if ( ! is_array( $data ) || ! array_key_exists( 'networkDatasetId', $data ) ) {
157
+            return;
158
+        }
159
+        $this->set_network_dataset_ids( $data['networkDatasetId'] );
160
+    }
161
+
162
+    /**
163
+     * The Wordlift_Configuration_Service's singleton instance.
164
+     *
165
+     * @since  3.6.0
166
+     *
167
+     * @access private
168
+     * @var \Wordlift_Configuration_Service $instance Wordlift_Configuration_Service's singleton instance.
169
+     */
170
+    private static $instance = null;
171
+
172
+    /**
173
+     * Get the singleton instance.
174
+     *
175
+     * @return \Wordlift_Configuration_Service
176
+     * @since 3.6.0
177
+     */
178
+    public static function get_instance() {
179
+
180
+        if ( ! isset( self::$instance ) ) {
181
+            self::$instance = new self();
182
+        }
183
+
184
+        return self::$instance;
185
+    }
186
+
187
+    /**
188
+     * Get a configuration given the option name and a key. The option value is
189
+     * expected to be an array.
190
+     *
191
+     * @param string $option The option name.
192
+     * @param string $key A key in the option value array.
193
+     * @param string $default The default value in case the key is not found (by default an empty string).
194
+     *
195
+     * @return mixed The configuration value or the default value if not found.
196
+     * @since 3.6.0
197
+     */
198
+    private function get( $option, $key, $default = '' ) {
199
+
200
+        $options = get_option( $option, array() );
201
+
202
+        return isset( $options[ $key ] ) ? $options[ $key ] : $default;
203
+    }
204
+
205
+    /**
206
+     * Set a configuration parameter.
207
+     *
208
+     * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
209
+     * @param string $key The value key.
210
+     * @param mixed  $value The value.
211
+     *
212
+     * @since 3.9.0
213
+     */
214
+    private function set( $option, $key, $value ) {
215
+
216
+        $values         = get_option( $option );
217
+        $values         = isset( $values ) ? $values : array();
218
+        $values[ $key ] = $value;
219
+        update_option( $option, $values );
220
+
221
+    }
222
+
223
+    /**
224
+     * Get the entity base path, by default 'entity'.
225
+     *
226
+     * @return string The entity base path.
227
+     * @since 3.6.0
228
+     */
229
+    public function get_entity_base_path() {
230
+
231
+        return $this->get( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity' );
232
+    }
233
+
234
+    /**
235
+     * Get the entity base path.
236
+     *
237
+     * @param string $value The entity base path.
238
+     *
239
+     * @since 3.9.0
240
+     */
241
+    public function set_entity_base_path( $value ) {
242
+
243
+        $this->set( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value );
244
+
245
+    }
246
+
247
+    /**
248
+     * Whether the installation skip wizard should be skipped.
249
+     *
250
+     * @return bool True if it should be skipped otherwise false.
251
+     * @since 3.9.0
252
+     */
253
+    public function is_skip_wizard() {
254
+
255
+        return $this->get( 'wl_general_settings', self::SKIP_WIZARD, false );
256
+    }
257
+
258
+    /**
259
+     * Set the skip wizard parameter.
260
+     *
261
+     * @param bool $value True to skip the wizard. We expect a boolean value.
262
+     *
263
+     * @since 3.9.0
264
+     */
265
+    public function set_skip_wizard( $value ) {
266
+
267
+        $this->set( 'wl_general_settings', self::SKIP_WIZARD, true === $value );
268
+
269
+    }
270
+
271
+    /**
272
+     * Get WordLift's key.
273
+     *
274
+     * @return string WordLift's key or an empty string if not set.
275
+     * @since 3.9.0
276
+     */
277
+    public function get_key() {
278
+
279
+        return $this->get( 'wl_general_settings', self::KEY, '' );
280
+    }
281
+
282
+    /**
283
+     * Set WordLift's key.
284
+     *
285
+     * @param string $value WordLift's key.
286
+     *
287
+     * @since 3.9.0
288
+     */
289
+    public function set_key( $value ) {
290
+
291
+        $this->set( 'wl_general_settings', self::KEY, $value );
292
+    }
293
+
294
+    /**
295
+     * Get WordLift's configured language, by default 'en'.
296
+     *
297
+     * Note that WordLift's language is used when writing strings to the Linked Data dataset, not for the analysis.
298
+     *
299
+     * @return string WordLift's configured language code ('en' by default).
300
+     * @since 3.9.0
301
+     */
302
+    public function get_language_code() {
303
+
304
+        $language = get_locale();
305
+        if ( ! $language ) {
306
+            return 'en';
307
+        }
308
+
309
+        return substr( $language, 0, 2 );
310
+    }
311
+
312
+    /**
313
+     * @param string $value WordLift's language code.
314
+     *
315
+     * @see https://github.com/insideout10/wordlift-plugin/issues/1466
316
+     *
317
+     * Set WordLift's language code, used when storing strings to the Linked Data dataset.
318
+     *
319
+     * @deprecated As of 3.32.7 this below method has no effect on setting the language, we use the
320
+     * language code form WordPress directly.
321
+     *
322
+     * @since 3.9.0
323
+     */
324
+    public function set_language_code( $value ) {
325
+
326
+        $this->set( 'wl_general_settings', self::LANGUAGE, $value );
327
+
328
+    }
329
+
330
+    /**
331
+     * Set the user preferences about sharing diagnostic with us.
332
+     *
333
+     * @param string $value The user preferences(yes/no).
334
+     *
335
+     * @since 3.19.0
336
+     */
337
+    public function set_diagnostic_preferences( $value ) {
338
+
339
+        $this->set( 'wl_general_settings', self::SEND_DIAGNOSTIC, $value );
340
+
341
+    }
342
+
343
+    /**
344
+     * Get the user preferences about sharing diagnostic.
345
+     *
346
+     * @since 3.19.0
347
+     */
348
+    public function get_diagnostic_preferences() {
349
+
350
+        return $this->get( 'wl_general_settings', self::SEND_DIAGNOSTIC, 'no' );
351
+    }
352
+
353
+    /**
354
+     * Get WordLift's configured country code, by default 'us'.
355
+     *
356
+     * @return string WordLift's configured country code ('us' by default).
357
+     * @since 3.18.0
358
+     */
359
+    public function get_country_code() {
360
+
361
+        return $this->get( 'wl_general_settings', self::COUNTRY_CODE, 'us' );
362
+    }
363
+
364
+    /**
365
+     * Set WordLift's country code.
366
+     *
367
+     * @param string $value WordLift's country code.
368
+     *
369
+     * @since 3.18.0
370
+     */
371
+    public function set_country_code( $value ) {
372
+
373
+        $this->set( 'wl_general_settings', self::COUNTRY_CODE, $value );
374
+
375
+    }
376
+
377
+    /**
378
+     * Get the publisher entity post id.
379
+     *
380
+     * The publisher entity post id points to an entity post which contains the data for the publisher used in schema.org
381
+     * Article markup.
382
+     *
383
+     * @return int|NULL The publisher entity post id or NULL if not set.
384
+     * @since 3.9.0
385
+     */
386
+    public function get_publisher_id() {
387
+
388
+        return $this->get( 'wl_general_settings', self::PUBLISHER_ID, null );
389
+    }
390
+
391
+    /**
392
+     * Set the publisher entity post id.
393
+     *
394
+     * @param int $value The publisher entity post id.
395
+     *
396
+     * @since 3.9.0
397
+     */
398
+    public function set_publisher_id( $value ) {
399
+
400
+        $this->set( 'wl_general_settings', self::PUBLISHER_ID, $value );
401
+
402
+    }
403
+
404
+    /**
405
+     * Get the dataset URI.
406
+     *
407
+     * @return string The dataset URI or an empty string if not set.
408
+     * @since 3.10.0
409
+     * @since 3.27.7 Always return null if `wl_features__enable__dataset` is disabled.
410
+     */
411
+    public function get_dataset_uri() {
412
+
413
+        if ( apply_filters( 'wl_feature__enable__dataset', true ) ) {
414
+            return $this->get( 'wl_advanced_settings', self::DATASET_URI, null );
415
+        } else {
416
+            return null;
417
+        }
418
+    }
419
+
420
+    /**
421
+     * Set the dataset URI.
422
+     *
423
+     * @param string $value The dataset URI.
424
+     *
425
+     * @since 3.10.0
426
+     */
427
+    public function set_dataset_uri( $value ) {
428
+
429
+        $this->set( 'wl_advanced_settings', self::DATASET_URI, $value );
430
+    }
431
+
432
+    /**
433
+     * Get the package type.
434
+     *
435
+     * @return string The package type or an empty string if not set.
436
+     * @since 3.20.0
437
+     */
438
+    public function get_package_type() {
439
+
440
+        return $this->get( 'wl_advanced_settings', self::PACKAGE_TYPE, null );
441
+    }
442
+
443
+    /**
444
+     * Set the package type.
445
+     *
446
+     * @param string $value The package type.
447
+     *
448
+     * @since 3.20.0
449
+     */
450
+    public function set_package_type( $value ) {
451
+        $this->set( 'wl_advanced_settings', self::PACKAGE_TYPE, $value );
452
+    }
453
+
454
+    /**
455
+     * Intercept the change of the WordLift key in order to set the dataset URI.
456
+     *
457
+     * @since 3.20.0 as of #761, we save settings every time a key is set, not only when the key changes, so to
458
+     *               store the configuration parameters such as country or language.
459
+     * @since 3.11.0
460
+     *
461
+     * @see https://github.com/insideout10/wordlift-plugin/issues/761
462
+     *
463
+     * @param array $old_value The old settings.
464
+     * @param array $new_value The new settings.
465
+     */
466
+    public function update_key( $old_value, $new_value ) {
467
+
468
+        // Check the old key value and the new one. We're going to ask for the dataset URI only if the key has changed.
469
+        // $old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
470
+        $new_key = isset( $new_value['key'] ) ? $new_value['key'] : '';
471
+
472
+        // If the key hasn't changed, don't do anything.
473
+        // WARN The 'update_option' hook is fired only if the new and old value are not equal.
474
+        // if ( $old_key === $new_key ) {
475
+        // return;
476
+        // }
477
+
478
+        // If the key is empty, empty the dataset URI.
479
+        if ( '' === $new_key ) {
480
+            $this->set_dataset_uri( '' );
481
+        }
482
+
483
+        // make the request to the remote server.
484
+        $this->get_remote_dataset_uri( $new_key );
485
+
486
+        do_action( 'wl_key_updated' );
487
+
488
+    }
489
+
490
+    /**
491
+     * Handle retrieving the dataset uri from the remote server.
492
+     *
493
+     * If a valid dataset uri is returned it is stored in the appropriate option,
494
+     * otherwise the option is set to empty string.
495
+     *
496
+     * @param string $key The key to be used.
497
+     *
498
+     * @since 3.12.0
499
+     *
500
+     * @since 3.17.0 send the site URL and get the dataset URI.
501
+     */
502
+    public function get_remote_dataset_uri( $key ) {
503
+
504
+        $this->log->trace( 'Getting the remote dataset URI and package type...' );
505
+
506
+        if ( empty( $key ) ) {
507
+            $this->log->warn( 'Key set to empty value.' );
508
+
509
+            $this->set_dataset_uri( '' );
510
+            $this->set_package_type( null );
511
+
512
+            return;
513
+        }
514
+
515
+        /**
516
+         * Allow 3rd parties to change the site_url.
517
+         *
518
+         * @param string $site_url The site url.
519
+         *
520
+         * @see https://github.com/insideout10/wordlift-plugin/issues/850
521
+         *
522
+         * @since 3.20.0
523
+         */
524
+        $home_url = get_option( 'home' );
525
+        $site_url = apply_filters( 'wl_production_site_url', untrailingslashit( $home_url ) );
526
+
527
+        // Build the URL.
528
+        $url = '/accounts'
529
+                . '?key=' . rawurlencode( $key )
530
+                . '&url=' . rawurlencode( $site_url )
531
+                . '&country=' . $this->get_country_code()
532
+                . '&language=' . $this->get_language_code();
533
+
534
+        $api_service = Default_Api_Service::get_instance();
535
+        /**
536
+         * @since 3.27.7.1
537
+         * The Key should be passed to headers, otherwise api would return null.
538
+         */
539
+        $headers  = array(
540
+            'Authorization' => "Key $key",
541
+        );
542
+        $response = $api_service->request( 'PUT', $url, $headers )->get_response();
543
+
544
+        // The response is an error.
545
+        if ( is_wp_error( $response ) ) {
546
+            $this->log->error( 'An error occurred setting the dataset URI: ' . $response->get_error_message() );
547
+
548
+            $this->set_dataset_uri( '' );
549
+            $this->set_package_type( null );
550
+
551
+            return;
552
+        }
553
+
554
+        // The response is not OK.
555
+        if ( ! is_array( $response ) || 200 !== (int) $response['response']['code'] ) {
556
+            $base_url = $api_service->get_base_url();
557
+
558
+            if ( ! is_array( $response ) ) {
559
+                // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
560
+                $this->log->error( "Unexpected response when opening URL $base_url$url: " . var_export( $response, true ) );
561
+            } else {
562
+                // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
563
+                $this->log->error( "Unexpected status code when opening URL $base_url$url: " . $response['response']['code'] . "\n" . var_export( $response, true ) );
564
+            }
565
+
566
+            $this->set_dataset_uri( '' );
567
+            $this->set_package_type( null );
568
+
569
+            return;
570
+        }
571
+
572
+        /*
573 573
 		 * We also store the package type.
574 574
 		 *
575 575
 		 * @since 3.20.0
576 576
 		 */
577
-		$json = json_decode( $response['body'] );
578
-		/**
579
-		 * @since 3.27.7
580
-		 * Remove the trailing slash returned from the new platform api.
581
-		 */
582
-		// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
583
-		$dataset_uri = untrailingslashit( $json->datasetURI );
584
-		// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
585
-		$package_type = isset( $json->packageType ) ? $json->packageType : null;
586
-
587
-		$this->log->info( "Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]..." );
588
-
589
-		$this->set_dataset_uri( $dataset_uri );
590
-		$this->set_package_type( $package_type );
591
-	}
592
-
593
-	/**
594
-	 * Handle the edge case where a user submits the same key again
595
-	 * when he does not have the dataset uri to regain it.
596
-	 *
597
-	 * This can not be handled in the normal option update hook because
598
-	 * it is not being triggered when the save value equals to the one already
599
-	 * in the DB.
600
-	 *
601
-	 * @param mixed $value The new, unserialized option value.
602
-	 * @param mixed $old_value The old option value.
603
-	 *
604
-	 * @return mixed The same value in the $value parameter
605
-	 * @since 3.12.0
606
-	 */
607
-	public function maybe_update_dataset_uri( $value, $old_value ) {
608
-
609
-		// Check the old key value and the new one. Here we're only handling the
610
-		// case where the key hasn't changed and the dataset URI isn't set. The
611
-		// other case, i.e. a new key is inserted, is handled at `update_key`.
612
-		$old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
613
-		$new_key = isset( $value['key'] ) ? $value['key'] : '';
614
-
615
-		$dataset_uri = $this->get_dataset_uri();
616
-
617
-		if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) {
618
-
619
-			// make the request to the remote server to try to get the dataset uri.
620
-			$this->get_remote_dataset_uri( $new_key );
621
-		}
622
-
623
-		return $value;
624
-	}
625
-
626
-	/**
627
-	 * Get the API URI to retrieve the dataset URI using the WordLift Key.
628
-	 *
629
-	 * @param string $key The WordLift key to use.
630
-	 *
631
-	 * @return string The API URI.
632
-	 * @since 3.11.0
633
-	 */
634
-	public function get_accounts_by_key_dataset_uri( $key ) {
635
-
636
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri";
637
-	}
638
-
639
-	/**
640
-	 * Get the `accounts` end point.
641
-	 *
642
-	 * @return string The `accounts` end point.
643
-	 * @since 3.16.0
644
-	 */
645
-	public function get_accounts() {
646
-
647
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts';
648
-	}
649
-
650
-	/**
651
-	 * Get the `link by default` option.
652
-	 *
653
-	 * @return bool True if entities must be linked by default otherwise false.
654
-	 * @since 3.13.0
655
-	 */
656
-	public function is_link_by_default() {
657
-
658
-		return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' );
659
-	}
660
-
661
-	/**
662
-	 * Set the `link by default` option.
663
-	 *
664
-	 * @param bool $value True to enabling linking by default, otherwise false.
665
-	 *
666
-	 * @since 3.13.0
667
-	 */
668
-	public function set_link_by_default( $value ) {
669
-
670
-		$this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' );
671
-	}
672
-
673
-	/**
674
-	 * Get the 'analytics-enable' option.
675
-	 *
676
-	 * @return string 'no' or 'yes' representing bool.
677
-	 * @since 3.21.0
678
-	 */
679
-	public function is_analytics_enable() {
680
-		return 'yes' === $this->get( 'wl_analytics_settings', self::ANALYTICS_ENABLE, 'no' );
681
-	}
682
-
683
-	/**
684
-	 * Set the `analytics-enable` option.
685
-	 *
686
-	 * @param bool $value True to enabling analytics, otherwise false.
687
-	 *
688
-	 * @since 3.21.0
689
-	 */
690
-	public function set_is_analytics_enable( $value ) {
691
-
692
-		$this->set( 'wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no' );
693
-	}
694
-
695
-	/**
696
-	 * Get the 'analytics-entity-uri-dimention' option.
697
-	 *
698
-	 * @return int
699
-	 * @since 3.21.0
700
-	 */
701
-	public function get_analytics_entity_uri_dimension() {
702
-		return (int) $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1 );
703
-	}
704
-
705
-	/**
706
-	 * Get the 'analytics-entity-type-dimension' option.
707
-	 *
708
-	 * @return int
709
-	 * @since 3.21.0
710
-	 */
711
-	public function get_analytics_entity_type_dimension() {
712
-		return $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2 );
713
-	}
714
-
715
-	/**
716
-	 * Get the URL to perform autocomplete request.
717
-	 *
718
-	 * @return string The URL to call to perform the autocomplete request.
719
-	 * @since 3.15.0
720
-	 */
721
-	public function get_autocomplete_url() {
722
-
723
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete';
724
-
725
-	}
726
-
727
-	/**
728
-	 * Get the URL to perform feedback deactivation request.
729
-	 *
730
-	 * @return string The URL to call to perform the feedback deactivation request.
731
-	 * @since 3.19.0
732
-	 */
733
-	public function get_deactivation_feedback_url() {
734
-
735
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'feedbacks';
736
-
737
-	}
738
-
739
-	/**
740
-	 * Get the base API URL.
741
-	 *
742
-	 * @return string The base API URL.
743
-	 * @since 3.20.0
744
-	 */
745
-	public function get_api_url() {
746
-
747
-		return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE;
748
-	}
749
-
750
-	public function get_network_dataset_ids() {
751
-		return $this->get( 'wl_advanced_settings', self::NETWORK_DATASET_IDS, array() );
752
-	}
753
-
754
-	public function set_network_dataset_ids( $network_dataset_ids ) {
755
-		$this->set( 'wl_advanced_settings', self::NETWORK_DATASET_IDS, $network_dataset_ids );
756
-	}
577
+        $json = json_decode( $response['body'] );
578
+        /**
579
+         * @since 3.27.7
580
+         * Remove the trailing slash returned from the new platform api.
581
+         */
582
+        // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
583
+        $dataset_uri = untrailingslashit( $json->datasetURI );
584
+        // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
585
+        $package_type = isset( $json->packageType ) ? $json->packageType : null;
586
+
587
+        $this->log->info( "Updating [ dataset uri :: $dataset_uri ][ package type :: $package_type ]..." );
588
+
589
+        $this->set_dataset_uri( $dataset_uri );
590
+        $this->set_package_type( $package_type );
591
+    }
592
+
593
+    /**
594
+     * Handle the edge case where a user submits the same key again
595
+     * when he does not have the dataset uri to regain it.
596
+     *
597
+     * This can not be handled in the normal option update hook because
598
+     * it is not being triggered when the save value equals to the one already
599
+     * in the DB.
600
+     *
601
+     * @param mixed $value The new, unserialized option value.
602
+     * @param mixed $old_value The old option value.
603
+     *
604
+     * @return mixed The same value in the $value parameter
605
+     * @since 3.12.0
606
+     */
607
+    public function maybe_update_dataset_uri( $value, $old_value ) {
608
+
609
+        // Check the old key value and the new one. Here we're only handling the
610
+        // case where the key hasn't changed and the dataset URI isn't set. The
611
+        // other case, i.e. a new key is inserted, is handled at `update_key`.
612
+        $old_key = isset( $old_value['key'] ) ? $old_value['key'] : '';
613
+        $new_key = isset( $value['key'] ) ? $value['key'] : '';
614
+
615
+        $dataset_uri = $this->get_dataset_uri();
616
+
617
+        if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) {
618
+
619
+            // make the request to the remote server to try to get the dataset uri.
620
+            $this->get_remote_dataset_uri( $new_key );
621
+        }
622
+
623
+        return $value;
624
+    }
625
+
626
+    /**
627
+     * Get the API URI to retrieve the dataset URI using the WordLift Key.
628
+     *
629
+     * @param string $key The WordLift key to use.
630
+     *
631
+     * @return string The API URI.
632
+     * @since 3.11.0
633
+     */
634
+    public function get_accounts_by_key_dataset_uri( $key ) {
635
+
636
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri";
637
+    }
638
+
639
+    /**
640
+     * Get the `accounts` end point.
641
+     *
642
+     * @return string The `accounts` end point.
643
+     * @since 3.16.0
644
+     */
645
+    public function get_accounts() {
646
+
647
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts';
648
+    }
649
+
650
+    /**
651
+     * Get the `link by default` option.
652
+     *
653
+     * @return bool True if entities must be linked by default otherwise false.
654
+     * @since 3.13.0
655
+     */
656
+    public function is_link_by_default() {
657
+
658
+        return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' );
659
+    }
660
+
661
+    /**
662
+     * Set the `link by default` option.
663
+     *
664
+     * @param bool $value True to enabling linking by default, otherwise false.
665
+     *
666
+     * @since 3.13.0
667
+     */
668
+    public function set_link_by_default( $value ) {
669
+
670
+        $this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' );
671
+    }
672
+
673
+    /**
674
+     * Get the 'analytics-enable' option.
675
+     *
676
+     * @return string 'no' or 'yes' representing bool.
677
+     * @since 3.21.0
678
+     */
679
+    public function is_analytics_enable() {
680
+        return 'yes' === $this->get( 'wl_analytics_settings', self::ANALYTICS_ENABLE, 'no' );
681
+    }
682
+
683
+    /**
684
+     * Set the `analytics-enable` option.
685
+     *
686
+     * @param bool $value True to enabling analytics, otherwise false.
687
+     *
688
+     * @since 3.21.0
689
+     */
690
+    public function set_is_analytics_enable( $value ) {
691
+
692
+        $this->set( 'wl_general_settings', self::ANALYTICS_ENABLE, true === $value ? 'yes' : 'no' );
693
+    }
694
+
695
+    /**
696
+     * Get the 'analytics-entity-uri-dimention' option.
697
+     *
698
+     * @return int
699
+     * @since 3.21.0
700
+     */
701
+    public function get_analytics_entity_uri_dimension() {
702
+        return (int) $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_URI_DIMENSION, 1 );
703
+    }
704
+
705
+    /**
706
+     * Get the 'analytics-entity-type-dimension' option.
707
+     *
708
+     * @return int
709
+     * @since 3.21.0
710
+     */
711
+    public function get_analytics_entity_type_dimension() {
712
+        return $this->get( 'wl_analytics_settings', self::ANALYTICS_ENTITY_TYPE_DIMENSION, 2 );
713
+    }
714
+
715
+    /**
716
+     * Get the URL to perform autocomplete request.
717
+     *
718
+     * @return string The URL to call to perform the autocomplete request.
719
+     * @since 3.15.0
720
+     */
721
+    public function get_autocomplete_url() {
722
+
723
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete';
724
+
725
+    }
726
+
727
+    /**
728
+     * Get the URL to perform feedback deactivation request.
729
+     *
730
+     * @return string The URL to call to perform the feedback deactivation request.
731
+     * @since 3.19.0
732
+     */
733
+    public function get_deactivation_feedback_url() {
734
+
735
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'feedbacks';
736
+
737
+    }
738
+
739
+    /**
740
+     * Get the base API URL.
741
+     *
742
+     * @return string The base API URL.
743
+     * @since 3.20.0
744
+     */
745
+    public function get_api_url() {
746
+
747
+        return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE;
748
+    }
749
+
750
+    public function get_network_dataset_ids() {
751
+        return $this->get( 'wl_advanced_settings', self::NETWORK_DATASET_IDS, array() );
752
+    }
753
+
754
+    public function set_network_dataset_ids( $network_dataset_ids ) {
755
+        $this->set( 'wl_advanced_settings', self::NETWORK_DATASET_IDS, $network_dataset_ids );
756
+    }
757 757
 
758 758
 }
Please login to merge, or discard this patch.
src/wordlift/vocabulary/class-analysis-service.php 2 patches
Indentation   +171 added lines, -171 removed lines patch added patch discarded remove patch
@@ -12,177 +12,177 @@
 block discarded – undo
12 12
  */
13 13
 class Analysis_Service {
14 14
 
15
-	/**
16
-	 * @var Default_Api_Service
17
-	 */
18
-	private $api_service;
19
-	/**
20
-	 * @var Cache
21
-	 */
22
-	private $cache_service;
23
-	/**
24
-	 * @var \Wordlift_Log_Service
25
-	 */
26
-	private $log;
27
-
28
-	/**
29
-	 * Tag_Rest_Endpoint constructor.
30
-	 *
31
-	 * @param Default_Api_Service $api_service
32
-	 * @param Cache               $cache_service
33
-	 */
34
-	public function __construct( $api_service, $cache_service ) {
35
-
36
-		$this->api_service = $api_service;
37
-
38
-		$this->cache_service = $cache_service;
39
-
40
-		$this->log = \Wordlift_Log_Service::get_logger( get_class() );
41
-
42
-	}
43
-
44
-	/**
45
-	 * Check if entities are in cache, if not return the results from
46
-	 * cache service.
47
-	 *
48
-	 * @param $tag \WP_Term
49
-	 */
50
-	public function get_entities( $tag ) {
51
-
52
-		$cache_key    = $tag->term_id;
53
-		$cache_result = $this->cache_service->get( $cache_key );
54
-		if ( false !== $cache_result ) {
55
-			return $cache_result;
56
-		}
57
-
58
-		// send the request.
59
-		$tag_name = $tag->name;
60
-
61
-		$entities = $this->get_entities_by_search_query( $tag_name );
62
-
63
-		if ( ! $entities ) {
64
-			return false;
65
-		}
66
-
67
-		$this->cache_service->put( $cache_key, $entities );
68
-
69
-		return $entities;
70
-
71
-	}
72
-
73
-	/**
74
-	 * @param $entity_url string
75
-	 * Formats the entity url from https://foo.com/some/path to
76
-	 * https/foo.com/some/path
77
-	 *
78
-	 * @return bool|string
79
-	 */
80
-	public static function format_entity_url( $entity_url ) {
81
-		$result = wp_parse_url( $entity_url );
82
-		if ( ! $result ) {
83
-			return false;
84
-		}
85
-		if ( ! array_key_exists( 'scheme', $result )
86
-			 || ! array_key_exists( 'host', $result )
87
-			 || ! array_key_exists( 'path', $result ) ) {
88
-			return false;
89
-		}
90
-
91
-		return $result['scheme'] . '/' . $result['host'] . $result['path'];
92
-	}
93
-
94
-	private function get_meta( $entity_url ) {
95
-
96
-		$cache_results = $this->cache_service->get( $entity_url );
97
-
98
-		if ( false !== $cache_results ) {
99
-			return $cache_results;
100
-		}
101
-
102
-		$formatted_url = self::format_entity_url( $entity_url );
103
-
104
-		if ( ! $formatted_url ) {
105
-			return array();
106
-		}
107
-
108
-		$meta_url = 'https://api.wordlift.io/id/' . $formatted_url;
109
-
110
-		$response = wp_remote_get( $meta_url );
111
-
112
-		if ( is_wp_error( $response )
113
-			 || ! isset( $response['response']['code'] )
114
-			 || 2 !== (int) $response['response']['code'] / 100 ) {
115
-			return false;
116
-		}
117
-
118
-		if ( ! is_wp_error( $response ) ) {
119
-			$meta = json_decode( wp_remote_retrieve_body( $response ), true );
120
-			$this->cache_service->put( $entity_url, $meta );
121
-
122
-			return $meta;
123
-		}
124
-
125
-		return array();
126
-
127
-	}
128
-
129
-	private function get_meta_for_entities( $entities ) {
130
-
131
-		$filtered_entities = array();
132
-		foreach ( $entities as $entity ) {
133
-			$entity['meta'] = array();
134
-			$meta           = $this->get_meta( $entity['entityId'] );
135
-			if ( $meta ) {
136
-				$meta                = Default_Entity_List::compact_jsonld( $meta );
137
-				$entity['meta']      = $meta;
138
-				$filtered_entities[] = $entity;
139
-			}
140
-		}
141
-
142
-		return $filtered_entities;
143
-
144
-	}
145
-
146
-	/**
147
-	 * @param $tag_name
148
-	 *
149
-	 * @return array
150
-	 */
151
-	public function get_entities_by_search_query( $tag_name ) {
152
-		$response = $this->api_service->request(
153
-			'POST',
154
-			'/analysis/single',
155
-			array( 'Content-Type' => 'application/json' ),
156
-			wp_json_encode(
157
-				array(
158
-					'content'         => $tag_name,
159
-					'contentType'     => 'text/plain',
160
-					'version'         => '1.0.0',
161
-					'contentLanguage' => 'en',
162
-					'scope'           => $this->get_scope(),
163
-				)
164
-			)
165
-		);
166
-
167
-		if ( ! $response->is_success() ) {
168
-			return false;
169
-		}
170
-
171
-		$response = json_decode( $response->get_body(), true );
172
-
173
-		if ( ! array_key_exists( 'entities', $response ) ) {
174
-			return false;
175
-		}
176
-
177
-		$entities = $this->get_meta_for_entities( $response['entities'] );
178
-
179
-		return $entities;
180
-	}
181
-
182
-	public function get_scope() {
183
-		$service = \Wordlift_Configuration_Service::get_instance();
15
+    /**
16
+     * @var Default_Api_Service
17
+     */
18
+    private $api_service;
19
+    /**
20
+     * @var Cache
21
+     */
22
+    private $cache_service;
23
+    /**
24
+     * @var \Wordlift_Log_Service
25
+     */
26
+    private $log;
27
+
28
+    /**
29
+     * Tag_Rest_Endpoint constructor.
30
+     *
31
+     * @param Default_Api_Service $api_service
32
+     * @param Cache               $cache_service
33
+     */
34
+    public function __construct( $api_service, $cache_service ) {
35
+
36
+        $this->api_service = $api_service;
37
+
38
+        $this->cache_service = $cache_service;
39
+
40
+        $this->log = \Wordlift_Log_Service::get_logger( get_class() );
41
+
42
+    }
43
+
44
+    /**
45
+     * Check if entities are in cache, if not return the results from
46
+     * cache service.
47
+     *
48
+     * @param $tag \WP_Term
49
+     */
50
+    public function get_entities( $tag ) {
51
+
52
+        $cache_key    = $tag->term_id;
53
+        $cache_result = $this->cache_service->get( $cache_key );
54
+        if ( false !== $cache_result ) {
55
+            return $cache_result;
56
+        }
57
+
58
+        // send the request.
59
+        $tag_name = $tag->name;
60
+
61
+        $entities = $this->get_entities_by_search_query( $tag_name );
62
+
63
+        if ( ! $entities ) {
64
+            return false;
65
+        }
66
+
67
+        $this->cache_service->put( $cache_key, $entities );
68
+
69
+        return $entities;
70
+
71
+    }
72
+
73
+    /**
74
+     * @param $entity_url string
75
+     * Formats the entity url from https://foo.com/some/path to
76
+     * https/foo.com/some/path
77
+     *
78
+     * @return bool|string
79
+     */
80
+    public static function format_entity_url( $entity_url ) {
81
+        $result = wp_parse_url( $entity_url );
82
+        if ( ! $result ) {
83
+            return false;
84
+        }
85
+        if ( ! array_key_exists( 'scheme', $result )
86
+             || ! array_key_exists( 'host', $result )
87
+             || ! array_key_exists( 'path', $result ) ) {
88
+            return false;
89
+        }
90
+
91
+        return $result['scheme'] . '/' . $result['host'] . $result['path'];
92
+    }
93
+
94
+    private function get_meta( $entity_url ) {
95
+
96
+        $cache_results = $this->cache_service->get( $entity_url );
97
+
98
+        if ( false !== $cache_results ) {
99
+            return $cache_results;
100
+        }
101
+
102
+        $formatted_url = self::format_entity_url( $entity_url );
103
+
104
+        if ( ! $formatted_url ) {
105
+            return array();
106
+        }
107
+
108
+        $meta_url = 'https://api.wordlift.io/id/' . $formatted_url;
109
+
110
+        $response = wp_remote_get( $meta_url );
111
+
112
+        if ( is_wp_error( $response )
113
+             || ! isset( $response['response']['code'] )
114
+             || 2 !== (int) $response['response']['code'] / 100 ) {
115
+            return false;
116
+        }
117
+
118
+        if ( ! is_wp_error( $response ) ) {
119
+            $meta = json_decode( wp_remote_retrieve_body( $response ), true );
120
+            $this->cache_service->put( $entity_url, $meta );
121
+
122
+            return $meta;
123
+        }
124
+
125
+        return array();
126
+
127
+    }
128
+
129
+    private function get_meta_for_entities( $entities ) {
130
+
131
+        $filtered_entities = array();
132
+        foreach ( $entities as $entity ) {
133
+            $entity['meta'] = array();
134
+            $meta           = $this->get_meta( $entity['entityId'] );
135
+            if ( $meta ) {
136
+                $meta                = Default_Entity_List::compact_jsonld( $meta );
137
+                $entity['meta']      = $meta;
138
+                $filtered_entities[] = $entity;
139
+            }
140
+        }
141
+
142
+        return $filtered_entities;
143
+
144
+    }
145
+
146
+    /**
147
+     * @param $tag_name
148
+     *
149
+     * @return array
150
+     */
151
+    public function get_entities_by_search_query( $tag_name ) {
152
+        $response = $this->api_service->request(
153
+            'POST',
154
+            '/analysis/single',
155
+            array( 'Content-Type' => 'application/json' ),
156
+            wp_json_encode(
157
+                array(
158
+                    'content'         => $tag_name,
159
+                    'contentType'     => 'text/plain',
160
+                    'version'         => '1.0.0',
161
+                    'contentLanguage' => 'en',
162
+                    'scope'           => $this->get_scope(),
163
+                )
164
+            )
165
+        );
166
+
167
+        if ( ! $response->is_success() ) {
168
+            return false;
169
+        }
170
+
171
+        $response = json_decode( $response->get_body(), true );
172
+
173
+        if ( ! array_key_exists( 'entities', $response ) ) {
174
+            return false;
175
+        }
176
+
177
+        $entities = $this->get_meta_for_entities( $response['entities'] );
178
+
179
+        return $entities;
180
+    }
181
+
182
+    public function get_scope() {
183
+        $service = \Wordlift_Configuration_Service::get_instance();
184 184
 
185
-		return count( $service->get_network_dataset_ids() ) > 0 ? 'network-only' : 'cloud';
186
-	}
185
+        return count( $service->get_network_dataset_ids() ) > 0 ? 'network-only' : 'cloud';
186
+    }
187 187
 
188 188
 }
Please login to merge, or discard this patch.
Spacing   +40 added lines, -40 removed lines patch added patch discarded remove patch
@@ -31,13 +31,13 @@  discard block
 block discarded – undo
31 31
 	 * @param Default_Api_Service $api_service
32 32
 	 * @param Cache               $cache_service
33 33
 	 */
34
-	public function __construct( $api_service, $cache_service ) {
34
+	public function __construct($api_service, $cache_service) {
35 35
 
36 36
 		$this->api_service = $api_service;
37 37
 
38 38
 		$this->cache_service = $cache_service;
39 39
 
40
-		$this->log = \Wordlift_Log_Service::get_logger( get_class() );
40
+		$this->log = \Wordlift_Log_Service::get_logger(get_class());
41 41
 
42 42
 	}
43 43
 
@@ -47,24 +47,24 @@  discard block
 block discarded – undo
47 47
 	 *
48 48
 	 * @param $tag \WP_Term
49 49
 	 */
50
-	public function get_entities( $tag ) {
50
+	public function get_entities($tag) {
51 51
 
52 52
 		$cache_key    = $tag->term_id;
53
-		$cache_result = $this->cache_service->get( $cache_key );
54
-		if ( false !== $cache_result ) {
53
+		$cache_result = $this->cache_service->get($cache_key);
54
+		if (false !== $cache_result) {
55 55
 			return $cache_result;
56 56
 		}
57 57
 
58 58
 		// send the request.
59 59
 		$tag_name = $tag->name;
60 60
 
61
-		$entities = $this->get_entities_by_search_query( $tag_name );
61
+		$entities = $this->get_entities_by_search_query($tag_name);
62 62
 
63
-		if ( ! $entities ) {
63
+		if ( ! $entities) {
64 64
 			return false;
65 65
 		}
66 66
 
67
-		$this->cache_service->put( $cache_key, $entities );
67
+		$this->cache_service->put($cache_key, $entities);
68 68
 
69 69
 		return $entities;
70 70
 
@@ -77,47 +77,47 @@  discard block
 block discarded – undo
77 77
 	 *
78 78
 	 * @return bool|string
79 79
 	 */
80
-	public static function format_entity_url( $entity_url ) {
81
-		$result = wp_parse_url( $entity_url );
82
-		if ( ! $result ) {
80
+	public static function format_entity_url($entity_url) {
81
+		$result = wp_parse_url($entity_url);
82
+		if ( ! $result) {
83 83
 			return false;
84 84
 		}
85
-		if ( ! array_key_exists( 'scheme', $result )
86
-			 || ! array_key_exists( 'host', $result )
87
-			 || ! array_key_exists( 'path', $result ) ) {
85
+		if ( ! array_key_exists('scheme', $result)
86
+			 || ! array_key_exists('host', $result)
87
+			 || ! array_key_exists('path', $result)) {
88 88
 			return false;
89 89
 		}
90 90
 
91
-		return $result['scheme'] . '/' . $result['host'] . $result['path'];
91
+		return $result['scheme'].'/'.$result['host'].$result['path'];
92 92
 	}
93 93
 
94
-	private function get_meta( $entity_url ) {
94
+	private function get_meta($entity_url) {
95 95
 
96
-		$cache_results = $this->cache_service->get( $entity_url );
96
+		$cache_results = $this->cache_service->get($entity_url);
97 97
 
98
-		if ( false !== $cache_results ) {
98
+		if (false !== $cache_results) {
99 99
 			return $cache_results;
100 100
 		}
101 101
 
102
-		$formatted_url = self::format_entity_url( $entity_url );
102
+		$formatted_url = self::format_entity_url($entity_url);
103 103
 
104
-		if ( ! $formatted_url ) {
104
+		if ( ! $formatted_url) {
105 105
 			return array();
106 106
 		}
107 107
 
108
-		$meta_url = 'https://api.wordlift.io/id/' . $formatted_url;
108
+		$meta_url = 'https://api.wordlift.io/id/'.$formatted_url;
109 109
 
110
-		$response = wp_remote_get( $meta_url );
110
+		$response = wp_remote_get($meta_url);
111 111
 
112
-		if ( is_wp_error( $response )
113
-			 || ! isset( $response['response']['code'] )
114
-			 || 2 !== (int) $response['response']['code'] / 100 ) {
112
+		if (is_wp_error($response)
113
+			 || ! isset($response['response']['code'])
114
+			 || 2 !== (int) $response['response']['code'] / 100) {
115 115
 			return false;
116 116
 		}
117 117
 
118
-		if ( ! is_wp_error( $response ) ) {
119
-			$meta = json_decode( wp_remote_retrieve_body( $response ), true );
120
-			$this->cache_service->put( $entity_url, $meta );
118
+		if ( ! is_wp_error($response)) {
119
+			$meta = json_decode(wp_remote_retrieve_body($response), true);
120
+			$this->cache_service->put($entity_url, $meta);
121 121
 
122 122
 			return $meta;
123 123
 		}
@@ -126,14 +126,14 @@  discard block
 block discarded – undo
126 126
 
127 127
 	}
128 128
 
129
-	private function get_meta_for_entities( $entities ) {
129
+	private function get_meta_for_entities($entities) {
130 130
 
131 131
 		$filtered_entities = array();
132
-		foreach ( $entities as $entity ) {
132
+		foreach ($entities as $entity) {
133 133
 			$entity['meta'] = array();
134
-			$meta           = $this->get_meta( $entity['entityId'] );
135
-			if ( $meta ) {
136
-				$meta                = Default_Entity_List::compact_jsonld( $meta );
134
+			$meta           = $this->get_meta($entity['entityId']);
135
+			if ($meta) {
136
+				$meta                = Default_Entity_List::compact_jsonld($meta);
137 137
 				$entity['meta']      = $meta;
138 138
 				$filtered_entities[] = $entity;
139 139
 			}
@@ -148,11 +148,11 @@  discard block
 block discarded – undo
148 148
 	 *
149 149
 	 * @return array
150 150
 	 */
151
-	public function get_entities_by_search_query( $tag_name ) {
151
+	public function get_entities_by_search_query($tag_name) {
152 152
 		$response = $this->api_service->request(
153 153
 			'POST',
154 154
 			'/analysis/single',
155
-			array( 'Content-Type' => 'application/json' ),
155
+			array('Content-Type' => 'application/json'),
156 156
 			wp_json_encode(
157 157
 				array(
158 158
 					'content'         => $tag_name,
@@ -164,17 +164,17 @@  discard block
 block discarded – undo
164 164
 			)
165 165
 		);
166 166
 
167
-		if ( ! $response->is_success() ) {
167
+		if ( ! $response->is_success()) {
168 168
 			return false;
169 169
 		}
170 170
 
171
-		$response = json_decode( $response->get_body(), true );
171
+		$response = json_decode($response->get_body(), true);
172 172
 
173
-		if ( ! array_key_exists( 'entities', $response ) ) {
173
+		if ( ! array_key_exists('entities', $response)) {
174 174
 			return false;
175 175
 		}
176 176
 
177
-		$entities = $this->get_meta_for_entities( $response['entities'] );
177
+		$entities = $this->get_meta_for_entities($response['entities']);
178 178
 
179 179
 		return $entities;
180 180
 	}
@@ -182,7 +182,7 @@  discard block
 block discarded – undo
182 182
 	public function get_scope() {
183 183
 		$service = \Wordlift_Configuration_Service::get_instance();
184 184
 
185
-		return count( $service->get_network_dataset_ids() ) > 0 ? 'network-only' : 'cloud';
185
+		return count($service->get_network_dataset_ids()) > 0 ? 'network-only' : 'cloud';
186 186
 	}
187 187
 
188 188
 }
Please login to merge, or discard this patch.