@@ -12,336 +12,336 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | final class Mappings_DBO { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * The {@link wpdb} instance. |
|
| 17 | - * |
|
| 18 | - * @since 3.25.0 |
|
| 19 | - * @access private |
|
| 20 | - * @var \wpdb $wpdb The {@link wpdb} instance. |
|
| 21 | - */ |
|
| 22 | - private $wpdb = null; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Construct DBO object. |
|
| 26 | - */ |
|
| 27 | - public function __construct() { |
|
| 28 | - global $wpdb; |
|
| 29 | - $this->wpdb = $wpdb; |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Returns an array of mappings. |
|
| 34 | - * |
|
| 35 | - * Each mapping contains one or more rule groups which in turn contain one or more rules. |
|
| 36 | - * |
|
| 37 | - * @return array An array of mappings. |
|
| 38 | - */ |
|
| 39 | - public function get_mappings() { |
|
| 40 | - $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 41 | - |
|
| 42 | - return $this->wpdb->get_results( |
|
| 43 | - "SELECT * FROM $mapping_table_name", |
|
| 44 | - ARRAY_A |
|
| 45 | - ); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Returns an array of active mappings. |
|
| 50 | - * |
|
| 51 | - * Each mapping contains one or more rule groups which in turn contain one or more rules. |
|
| 52 | - * |
|
| 53 | - * @return array An array of mappings. |
|
| 54 | - */ |
|
| 55 | - public function get_active_mappings() { |
|
| 56 | - $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 57 | - |
|
| 58 | - return $this->wpdb->get_results( |
|
| 59 | - "SELECT * FROM $mapping_table_name WHERE mapping_status = 'active'", |
|
| 60 | - ARRAY_A |
|
| 61 | - ); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Returns a list of property rows |
|
| 66 | - * |
|
| 67 | - * @param int $mapping_id Primary key of mapping table. |
|
| 68 | - * |
|
| 69 | - * @return array List of property items belong to $mapping_id. |
|
| 70 | - */ |
|
| 71 | - public function get_properties( $mapping_id ) { |
|
| 72 | - $property_table_name = $this->wpdb->prefix . WL_PROPERTY_TABLE_NAME; |
|
| 73 | - $property_rows = $this->wpdb->get_results( |
|
| 74 | - $this->wpdb->prepare( "SELECT * FROM $property_table_name WHERE mapping_id=%d", $mapping_id ), |
|
| 75 | - ARRAY_A |
|
| 76 | - ); |
|
| 77 | - |
|
| 78 | - return $property_rows; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Check if the row exists in the table |
|
| 83 | - * |
|
| 84 | - * @param string $table_name The table name you want to query, completely escaped value. |
|
| 85 | - * @param string $primary_key_name The primary key you want to query, should be escaped before passing. |
|
| 86 | - * @param int $primary_key_value The primary key value, no need to escape. |
|
| 87 | - * |
|
| 88 | - * @return bool Returns true if the row exists, false if it does not |
|
| 89 | - */ |
|
| 90 | - private function check_if_row_exists( $table_name, $primary_key_name, $primary_key_value ) { |
|
| 91 | - $primary_key_value = (int) $primary_key_value; |
|
| 92 | - $count = (int) $this->wpdb->get_var( |
|
| 93 | - $this->wpdb->prepare( |
|
| 94 | - "SELECT COUNT($primary_key_name) from $table_name where $primary_key_name = %d", |
|
| 95 | - $primary_key_value |
|
| 96 | - ) |
|
| 97 | - ); |
|
| 98 | - |
|
| 99 | - return $count > 0; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Insert new mapping item with title |
|
| 104 | - * |
|
| 105 | - * @param string $title Title of the mapping item. |
|
| 106 | - * |
|
| 107 | - * @return int Id of the inserted mapping item. |
|
| 108 | - */ |
|
| 109 | - public function insert_mapping_item( $title ) { |
|
| 110 | - $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 111 | - $this->wpdb->insert( |
|
| 112 | - $mapping_table_name, |
|
| 113 | - array( 'mapping_title' => $title ) |
|
| 114 | - ); |
|
| 115 | - |
|
| 116 | - return $this->wpdb->insert_id; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Update mapping item with new title |
|
| 121 | - * |
|
| 122 | - * @param array $mapping_data Array of the mapping data. |
|
| 123 | - * |
|
| 124 | - * @return int Id of the inserted mapping item |
|
| 125 | - */ |
|
| 126 | - public function insert_or_update_mapping_item( $mapping_data ) { |
|
| 127 | - $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 128 | - $mapping_id = array_key_exists( 'mapping_id', $mapping_data ) ? (int) $mapping_data['mapping_id'] : null; |
|
| 129 | - if ( $this->check_if_row_exists( $mapping_table_name, 'mapping_id', $mapping_data['mapping_id'] ) ) { |
|
| 130 | - $this->wpdb->update( |
|
| 131 | - $mapping_table_name, |
|
| 132 | - $mapping_data, |
|
| 133 | - array( 'mapping_id' => $mapping_id ) |
|
| 134 | - ); |
|
| 135 | - } else { |
|
| 136 | - $this->wpdb->insert( |
|
| 137 | - $mapping_table_name, |
|
| 138 | - $mapping_data |
|
| 139 | - ); |
|
| 140 | - $mapping_id = (int) $this->wpdb->insert_id; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - return $mapping_id; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Updates rule item. |
|
| 148 | - * |
|
| 149 | - * @param array $rule_item_data The rule_item_data, should contain rule_id. |
|
| 150 | - * |
|
| 151 | - * @return int $rule_id The inserted rule id. |
|
| 152 | - */ |
|
| 153 | - public function insert_or_update_rule_item( $rule_item_data ) { |
|
| 154 | - $rule_table_name = $this->wpdb->prefix . WL_RULE_TABLE_NAME; |
|
| 155 | - $rule_id = array_key_exists( 'rule_id', $rule_item_data ) ? $rule_item_data['rule_id'] : null; |
|
| 156 | - if ( $this->check_if_row_exists( $rule_table_name, 'rule_id', $rule_id ) ) { |
|
| 157 | - $this->wpdb->update( $rule_table_name, $rule_item_data, array( 'rule_id' => $rule_id ) ); |
|
| 158 | - } else { |
|
| 159 | - $this->wpdb->insert( $rule_table_name, $rule_item_data ); |
|
| 160 | - $rule_id = $this->wpdb->insert_id; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - return $rule_id; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * If a rule group exists doesn't do anything, but if rule group |
|
| 168 | - * didn't exist then it inserts a new entry. |
|
| 169 | - * |
|
| 170 | - * @param int $mapping_id Primary key for mapping table. |
|
| 171 | - * |
|
| 172 | - * @return int The inserted rule group id. |
|
| 173 | - */ |
|
| 174 | - public function insert_rule_group( $mapping_id ) { |
|
| 175 | - $rule_group_table_name = $this->wpdb->prefix . WL_RULE_GROUP_TABLE_NAME; |
|
| 176 | - $this->wpdb->insert( |
|
| 177 | - $rule_group_table_name, |
|
| 178 | - array( |
|
| 179 | - 'mapping_id' => $mapping_id, |
|
| 180 | - ) |
|
| 181 | - ); |
|
| 182 | - |
|
| 183 | - return $this->wpdb->insert_id; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - /** |
|
| 187 | - * Deletes a rule item by rule_id from rule and rule group table. |
|
| 188 | - * |
|
| 189 | - * @param int $rule_id Primary key for rule table. |
|
| 190 | - * |
|
| 191 | - * @return void |
|
| 192 | - */ |
|
| 193 | - public function delete_rule_item( $rule_id ) { |
|
| 194 | - $rule_table_name = $this->wpdb->prefix . WL_RULE_TABLE_NAME; |
|
| 195 | - $this->wpdb->delete( $rule_table_name, array( 'rule_id' => $rule_id ) ); |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * Deletes a rule group item by rule_group_id from rule group table. |
|
| 200 | - * |
|
| 201 | - * @param int $rule_group_id Primary key for rule table. |
|
| 202 | - * |
|
| 203 | - * @return void |
|
| 204 | - */ |
|
| 205 | - public function delete_rule_group_item( $rule_group_id ) { |
|
| 206 | - $rule_group_table_name = $this->wpdb->prefix . WL_RULE_GROUP_TABLE_NAME; |
|
| 207 | - $this->wpdb->delete( $rule_group_table_name, array( 'rule_group_id' => $rule_group_id ) ); |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * Deletes a mapping item by mapping_id |
|
| 212 | - * |
|
| 213 | - * @param int $mapping_id Primary key for mapping table. |
|
| 214 | - * |
|
| 215 | - * @return void |
|
| 216 | - */ |
|
| 217 | - public function delete_mapping_item( $mapping_id ) { |
|
| 218 | - $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 219 | - $this->wpdb->delete( $mapping_table_name, array( 'mapping_id' => $mapping_id ) ); |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * Gets a list of rule group items. |
|
| 224 | - * |
|
| 225 | - * @param int $mapping_id Primary key for mapping table. |
|
| 226 | - * |
|
| 227 | - * @return array Get list of rule group items. |
|
| 228 | - */ |
|
| 229 | - public function get_rule_group_list( $mapping_id ) { |
|
| 230 | - $rule_group_table_name = $this->wpdb->prefix . WL_RULE_GROUP_TABLE_NAME; |
|
| 231 | - |
|
| 232 | - return $this->wpdb->get_results( |
|
| 233 | - $this->wpdb->prepare( |
|
| 234 | - "SELECT rule_group_id FROM $rule_group_table_name WHERE mapping_id=%d", |
|
| 235 | - $mapping_id |
|
| 236 | - ), |
|
| 237 | - ARRAY_A |
|
| 238 | - ); |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * Gets a list of rule group items. |
|
| 243 | - * |
|
| 244 | - * @param int $mapping_id Primary key for mapping table. |
|
| 245 | - * |
|
| 246 | - * @return array Get list of rule group items. |
|
| 247 | - */ |
|
| 248 | - public function get_rule_groups_by_mapping( $mapping_id ) { |
|
| 249 | - |
|
| 250 | - $rule_group_table_name = $this->wpdb->prefix . WL_RULE_GROUP_TABLE_NAME; |
|
| 251 | - $rule_group_rows = $this->wpdb->get_results( |
|
| 252 | - $this->wpdb->prepare( |
|
| 253 | - "SELECT rule_group_id FROM $rule_group_table_name WHERE mapping_id=%d", |
|
| 254 | - $mapping_id |
|
| 255 | - ), |
|
| 256 | - ARRAY_A |
|
| 257 | - ); |
|
| 258 | - |
|
| 259 | - // List of all rule group items. |
|
| 260 | - $rule_groups = array(); |
|
| 261 | - foreach ( $rule_group_rows as $rule_group_row ) { |
|
| 262 | - $rule_groups[] = array( |
|
| 263 | - 'rule_group_id' => $rule_group_row['rule_group_id'], |
|
| 264 | - 'rules' => $this->get_rules_by_rule_group( $rule_group_row['rule_group_id'] ), |
|
| 265 | - ); |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - return $rule_groups; |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - /** |
|
| 272 | - * Gets a list of rule items belong to rule_group_id. |
|
| 273 | - * |
|
| 274 | - * @param int $rule_group_id Indicates which group the item belongs to. |
|
| 275 | - * |
|
| 276 | - * @return array Get list of rule items. |
|
| 277 | - */ |
|
| 278 | - public function get_rules_by_rule_group( $rule_group_id ) { |
|
| 279 | - $rule_table_name = $this->wpdb->prefix . WL_RULE_TABLE_NAME; |
|
| 280 | - |
|
| 281 | - return $this->wpdb->get_results( |
|
| 282 | - $this->wpdb->prepare( |
|
| 283 | - "SELECT * FROM $rule_table_name WHERE rule_group_id=%d", |
|
| 284 | - $rule_group_id |
|
| 285 | - ), |
|
| 286 | - ARRAY_A |
|
| 287 | - ); |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * Insert/Update property item. |
|
| 292 | - * |
|
| 293 | - * @param array $property_data Property row from table/ui. |
|
| 294 | - * |
|
| 295 | - * @return int Inserted Property Id. |
|
| 296 | - */ |
|
| 297 | - public function insert_or_update_property( $property_data ) { |
|
| 298 | - $property_table_name = $this->wpdb->prefix . WL_PROPERTY_TABLE_NAME; |
|
| 299 | - $property_id = array_key_exists( 'property_id', $property_data ) ? $property_data['property_id'] : null; |
|
| 300 | - |
|
| 301 | - if ( $this->check_if_row_exists( $property_table_name, 'property_id', $property_id ) ) { |
|
| 302 | - $this->wpdb->update( |
|
| 303 | - $property_table_name, |
|
| 304 | - $property_data, |
|
| 305 | - array( 'property_id' => $property_id ) |
|
| 306 | - ); |
|
| 307 | - } else { |
|
| 308 | - $this->wpdb->insert( $property_table_name, $property_data ); |
|
| 309 | - $property_id = $this->wpdb->insert_id; |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - return $property_id; |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - /** |
|
| 316 | - * Gets a single mapping item row. |
|
| 317 | - * |
|
| 318 | - * @param int $mapping_id Primary key of mapping table. |
|
| 319 | - * |
|
| 320 | - * @return array Returns single mapping table row.. |
|
| 321 | - */ |
|
| 322 | - public function get_mapping_item_data( $mapping_id ) { |
|
| 323 | - $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 324 | - |
|
| 325 | - return $this->wpdb->get_row( |
|
| 326 | - $this->wpdb->prepare( |
|
| 327 | - "SELECT * FROM $mapping_table_name WHERE mapping_id=%d", |
|
| 328 | - $mapping_id |
|
| 329 | - ), |
|
| 330 | - ARRAY_A |
|
| 331 | - ); |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - /** |
|
| 335 | - * Delete property item. |
|
| 336 | - * |
|
| 337 | - * @param int $property_id Primary key for property table. |
|
| 338 | - * |
|
| 339 | - * @return int|false The number of rows updated, or false on error. |
|
| 340 | - */ |
|
| 341 | - public function delete_property( $property_id ) { |
|
| 342 | - $property_table_name = $this->wpdb->prefix . WL_PROPERTY_TABLE_NAME; |
|
| 343 | - |
|
| 344 | - return $this->wpdb->delete( $property_table_name, array( 'property_id' => $property_id ) ); |
|
| 345 | - } |
|
| 15 | + /** |
|
| 16 | + * The {@link wpdb} instance. |
|
| 17 | + * |
|
| 18 | + * @since 3.25.0 |
|
| 19 | + * @access private |
|
| 20 | + * @var \wpdb $wpdb The {@link wpdb} instance. |
|
| 21 | + */ |
|
| 22 | + private $wpdb = null; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Construct DBO object. |
|
| 26 | + */ |
|
| 27 | + public function __construct() { |
|
| 28 | + global $wpdb; |
|
| 29 | + $this->wpdb = $wpdb; |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Returns an array of mappings. |
|
| 34 | + * |
|
| 35 | + * Each mapping contains one or more rule groups which in turn contain one or more rules. |
|
| 36 | + * |
|
| 37 | + * @return array An array of mappings. |
|
| 38 | + */ |
|
| 39 | + public function get_mappings() { |
|
| 40 | + $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 41 | + |
|
| 42 | + return $this->wpdb->get_results( |
|
| 43 | + "SELECT * FROM $mapping_table_name", |
|
| 44 | + ARRAY_A |
|
| 45 | + ); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Returns an array of active mappings. |
|
| 50 | + * |
|
| 51 | + * Each mapping contains one or more rule groups which in turn contain one or more rules. |
|
| 52 | + * |
|
| 53 | + * @return array An array of mappings. |
|
| 54 | + */ |
|
| 55 | + public function get_active_mappings() { |
|
| 56 | + $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 57 | + |
|
| 58 | + return $this->wpdb->get_results( |
|
| 59 | + "SELECT * FROM $mapping_table_name WHERE mapping_status = 'active'", |
|
| 60 | + ARRAY_A |
|
| 61 | + ); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Returns a list of property rows |
|
| 66 | + * |
|
| 67 | + * @param int $mapping_id Primary key of mapping table. |
|
| 68 | + * |
|
| 69 | + * @return array List of property items belong to $mapping_id. |
|
| 70 | + */ |
|
| 71 | + public function get_properties( $mapping_id ) { |
|
| 72 | + $property_table_name = $this->wpdb->prefix . WL_PROPERTY_TABLE_NAME; |
|
| 73 | + $property_rows = $this->wpdb->get_results( |
|
| 74 | + $this->wpdb->prepare( "SELECT * FROM $property_table_name WHERE mapping_id=%d", $mapping_id ), |
|
| 75 | + ARRAY_A |
|
| 76 | + ); |
|
| 77 | + |
|
| 78 | + return $property_rows; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Check if the row exists in the table |
|
| 83 | + * |
|
| 84 | + * @param string $table_name The table name you want to query, completely escaped value. |
|
| 85 | + * @param string $primary_key_name The primary key you want to query, should be escaped before passing. |
|
| 86 | + * @param int $primary_key_value The primary key value, no need to escape. |
|
| 87 | + * |
|
| 88 | + * @return bool Returns true if the row exists, false if it does not |
|
| 89 | + */ |
|
| 90 | + private function check_if_row_exists( $table_name, $primary_key_name, $primary_key_value ) { |
|
| 91 | + $primary_key_value = (int) $primary_key_value; |
|
| 92 | + $count = (int) $this->wpdb->get_var( |
|
| 93 | + $this->wpdb->prepare( |
|
| 94 | + "SELECT COUNT($primary_key_name) from $table_name where $primary_key_name = %d", |
|
| 95 | + $primary_key_value |
|
| 96 | + ) |
|
| 97 | + ); |
|
| 98 | + |
|
| 99 | + return $count > 0; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Insert new mapping item with title |
|
| 104 | + * |
|
| 105 | + * @param string $title Title of the mapping item. |
|
| 106 | + * |
|
| 107 | + * @return int Id of the inserted mapping item. |
|
| 108 | + */ |
|
| 109 | + public function insert_mapping_item( $title ) { |
|
| 110 | + $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 111 | + $this->wpdb->insert( |
|
| 112 | + $mapping_table_name, |
|
| 113 | + array( 'mapping_title' => $title ) |
|
| 114 | + ); |
|
| 115 | + |
|
| 116 | + return $this->wpdb->insert_id; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Update mapping item with new title |
|
| 121 | + * |
|
| 122 | + * @param array $mapping_data Array of the mapping data. |
|
| 123 | + * |
|
| 124 | + * @return int Id of the inserted mapping item |
|
| 125 | + */ |
|
| 126 | + public function insert_or_update_mapping_item( $mapping_data ) { |
|
| 127 | + $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 128 | + $mapping_id = array_key_exists( 'mapping_id', $mapping_data ) ? (int) $mapping_data['mapping_id'] : null; |
|
| 129 | + if ( $this->check_if_row_exists( $mapping_table_name, 'mapping_id', $mapping_data['mapping_id'] ) ) { |
|
| 130 | + $this->wpdb->update( |
|
| 131 | + $mapping_table_name, |
|
| 132 | + $mapping_data, |
|
| 133 | + array( 'mapping_id' => $mapping_id ) |
|
| 134 | + ); |
|
| 135 | + } else { |
|
| 136 | + $this->wpdb->insert( |
|
| 137 | + $mapping_table_name, |
|
| 138 | + $mapping_data |
|
| 139 | + ); |
|
| 140 | + $mapping_id = (int) $this->wpdb->insert_id; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + return $mapping_id; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Updates rule item. |
|
| 148 | + * |
|
| 149 | + * @param array $rule_item_data The rule_item_data, should contain rule_id. |
|
| 150 | + * |
|
| 151 | + * @return int $rule_id The inserted rule id. |
|
| 152 | + */ |
|
| 153 | + public function insert_or_update_rule_item( $rule_item_data ) { |
|
| 154 | + $rule_table_name = $this->wpdb->prefix . WL_RULE_TABLE_NAME; |
|
| 155 | + $rule_id = array_key_exists( 'rule_id', $rule_item_data ) ? $rule_item_data['rule_id'] : null; |
|
| 156 | + if ( $this->check_if_row_exists( $rule_table_name, 'rule_id', $rule_id ) ) { |
|
| 157 | + $this->wpdb->update( $rule_table_name, $rule_item_data, array( 'rule_id' => $rule_id ) ); |
|
| 158 | + } else { |
|
| 159 | + $this->wpdb->insert( $rule_table_name, $rule_item_data ); |
|
| 160 | + $rule_id = $this->wpdb->insert_id; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + return $rule_id; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * If a rule group exists doesn't do anything, but if rule group |
|
| 168 | + * didn't exist then it inserts a new entry. |
|
| 169 | + * |
|
| 170 | + * @param int $mapping_id Primary key for mapping table. |
|
| 171 | + * |
|
| 172 | + * @return int The inserted rule group id. |
|
| 173 | + */ |
|
| 174 | + public function insert_rule_group( $mapping_id ) { |
|
| 175 | + $rule_group_table_name = $this->wpdb->prefix . WL_RULE_GROUP_TABLE_NAME; |
|
| 176 | + $this->wpdb->insert( |
|
| 177 | + $rule_group_table_name, |
|
| 178 | + array( |
|
| 179 | + 'mapping_id' => $mapping_id, |
|
| 180 | + ) |
|
| 181 | + ); |
|
| 182 | + |
|
| 183 | + return $this->wpdb->insert_id; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + /** |
|
| 187 | + * Deletes a rule item by rule_id from rule and rule group table. |
|
| 188 | + * |
|
| 189 | + * @param int $rule_id Primary key for rule table. |
|
| 190 | + * |
|
| 191 | + * @return void |
|
| 192 | + */ |
|
| 193 | + public function delete_rule_item( $rule_id ) { |
|
| 194 | + $rule_table_name = $this->wpdb->prefix . WL_RULE_TABLE_NAME; |
|
| 195 | + $this->wpdb->delete( $rule_table_name, array( 'rule_id' => $rule_id ) ); |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * Deletes a rule group item by rule_group_id from rule group table. |
|
| 200 | + * |
|
| 201 | + * @param int $rule_group_id Primary key for rule table. |
|
| 202 | + * |
|
| 203 | + * @return void |
|
| 204 | + */ |
|
| 205 | + public function delete_rule_group_item( $rule_group_id ) { |
|
| 206 | + $rule_group_table_name = $this->wpdb->prefix . WL_RULE_GROUP_TABLE_NAME; |
|
| 207 | + $this->wpdb->delete( $rule_group_table_name, array( 'rule_group_id' => $rule_group_id ) ); |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * Deletes a mapping item by mapping_id |
|
| 212 | + * |
|
| 213 | + * @param int $mapping_id Primary key for mapping table. |
|
| 214 | + * |
|
| 215 | + * @return void |
|
| 216 | + */ |
|
| 217 | + public function delete_mapping_item( $mapping_id ) { |
|
| 218 | + $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 219 | + $this->wpdb->delete( $mapping_table_name, array( 'mapping_id' => $mapping_id ) ); |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * Gets a list of rule group items. |
|
| 224 | + * |
|
| 225 | + * @param int $mapping_id Primary key for mapping table. |
|
| 226 | + * |
|
| 227 | + * @return array Get list of rule group items. |
|
| 228 | + */ |
|
| 229 | + public function get_rule_group_list( $mapping_id ) { |
|
| 230 | + $rule_group_table_name = $this->wpdb->prefix . WL_RULE_GROUP_TABLE_NAME; |
|
| 231 | + |
|
| 232 | + return $this->wpdb->get_results( |
|
| 233 | + $this->wpdb->prepare( |
|
| 234 | + "SELECT rule_group_id FROM $rule_group_table_name WHERE mapping_id=%d", |
|
| 235 | + $mapping_id |
|
| 236 | + ), |
|
| 237 | + ARRAY_A |
|
| 238 | + ); |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * Gets a list of rule group items. |
|
| 243 | + * |
|
| 244 | + * @param int $mapping_id Primary key for mapping table. |
|
| 245 | + * |
|
| 246 | + * @return array Get list of rule group items. |
|
| 247 | + */ |
|
| 248 | + public function get_rule_groups_by_mapping( $mapping_id ) { |
|
| 249 | + |
|
| 250 | + $rule_group_table_name = $this->wpdb->prefix . WL_RULE_GROUP_TABLE_NAME; |
|
| 251 | + $rule_group_rows = $this->wpdb->get_results( |
|
| 252 | + $this->wpdb->prepare( |
|
| 253 | + "SELECT rule_group_id FROM $rule_group_table_name WHERE mapping_id=%d", |
|
| 254 | + $mapping_id |
|
| 255 | + ), |
|
| 256 | + ARRAY_A |
|
| 257 | + ); |
|
| 258 | + |
|
| 259 | + // List of all rule group items. |
|
| 260 | + $rule_groups = array(); |
|
| 261 | + foreach ( $rule_group_rows as $rule_group_row ) { |
|
| 262 | + $rule_groups[] = array( |
|
| 263 | + 'rule_group_id' => $rule_group_row['rule_group_id'], |
|
| 264 | + 'rules' => $this->get_rules_by_rule_group( $rule_group_row['rule_group_id'] ), |
|
| 265 | + ); |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + return $rule_groups; |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + /** |
|
| 272 | + * Gets a list of rule items belong to rule_group_id. |
|
| 273 | + * |
|
| 274 | + * @param int $rule_group_id Indicates which group the item belongs to. |
|
| 275 | + * |
|
| 276 | + * @return array Get list of rule items. |
|
| 277 | + */ |
|
| 278 | + public function get_rules_by_rule_group( $rule_group_id ) { |
|
| 279 | + $rule_table_name = $this->wpdb->prefix . WL_RULE_TABLE_NAME; |
|
| 280 | + |
|
| 281 | + return $this->wpdb->get_results( |
|
| 282 | + $this->wpdb->prepare( |
|
| 283 | + "SELECT * FROM $rule_table_name WHERE rule_group_id=%d", |
|
| 284 | + $rule_group_id |
|
| 285 | + ), |
|
| 286 | + ARRAY_A |
|
| 287 | + ); |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * Insert/Update property item. |
|
| 292 | + * |
|
| 293 | + * @param array $property_data Property row from table/ui. |
|
| 294 | + * |
|
| 295 | + * @return int Inserted Property Id. |
|
| 296 | + */ |
|
| 297 | + public function insert_or_update_property( $property_data ) { |
|
| 298 | + $property_table_name = $this->wpdb->prefix . WL_PROPERTY_TABLE_NAME; |
|
| 299 | + $property_id = array_key_exists( 'property_id', $property_data ) ? $property_data['property_id'] : null; |
|
| 300 | + |
|
| 301 | + if ( $this->check_if_row_exists( $property_table_name, 'property_id', $property_id ) ) { |
|
| 302 | + $this->wpdb->update( |
|
| 303 | + $property_table_name, |
|
| 304 | + $property_data, |
|
| 305 | + array( 'property_id' => $property_id ) |
|
| 306 | + ); |
|
| 307 | + } else { |
|
| 308 | + $this->wpdb->insert( $property_table_name, $property_data ); |
|
| 309 | + $property_id = $this->wpdb->insert_id; |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + return $property_id; |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + /** |
|
| 316 | + * Gets a single mapping item row. |
|
| 317 | + * |
|
| 318 | + * @param int $mapping_id Primary key of mapping table. |
|
| 319 | + * |
|
| 320 | + * @return array Returns single mapping table row.. |
|
| 321 | + */ |
|
| 322 | + public function get_mapping_item_data( $mapping_id ) { |
|
| 323 | + $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 324 | + |
|
| 325 | + return $this->wpdb->get_row( |
|
| 326 | + $this->wpdb->prepare( |
|
| 327 | + "SELECT * FROM $mapping_table_name WHERE mapping_id=%d", |
|
| 328 | + $mapping_id |
|
| 329 | + ), |
|
| 330 | + ARRAY_A |
|
| 331 | + ); |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + /** |
|
| 335 | + * Delete property item. |
|
| 336 | + * |
|
| 337 | + * @param int $property_id Primary key for property table. |
|
| 338 | + * |
|
| 339 | + * @return int|false The number of rows updated, or false on error. |
|
| 340 | + */ |
|
| 341 | + public function delete_property( $property_id ) { |
|
| 342 | + $property_table_name = $this->wpdb->prefix . WL_PROPERTY_TABLE_NAME; |
|
| 343 | + |
|
| 344 | + return $this->wpdb->delete( $property_table_name, array( 'property_id' => $property_id ) ); |
|
| 345 | + } |
|
| 346 | 346 | |
| 347 | 347 | } |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | * @return array An array of mappings. |
| 38 | 38 | */ |
| 39 | 39 | public function get_mappings() { |
| 40 | - $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 40 | + $mapping_table_name = $this->wpdb->prefix.WL_MAPPING_TABLE_NAME; |
|
| 41 | 41 | |
| 42 | 42 | return $this->wpdb->get_results( |
| 43 | 43 | "SELECT * FROM $mapping_table_name", |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | * @return array An array of mappings. |
| 54 | 54 | */ |
| 55 | 55 | public function get_active_mappings() { |
| 56 | - $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 56 | + $mapping_table_name = $this->wpdb->prefix.WL_MAPPING_TABLE_NAME; |
|
| 57 | 57 | |
| 58 | 58 | return $this->wpdb->get_results( |
| 59 | 59 | "SELECT * FROM $mapping_table_name WHERE mapping_status = 'active'", |
@@ -68,10 +68,10 @@ discard block |
||
| 68 | 68 | * |
| 69 | 69 | * @return array List of property items belong to $mapping_id. |
| 70 | 70 | */ |
| 71 | - public function get_properties( $mapping_id ) { |
|
| 72 | - $property_table_name = $this->wpdb->prefix . WL_PROPERTY_TABLE_NAME; |
|
| 71 | + public function get_properties($mapping_id) { |
|
| 72 | + $property_table_name = $this->wpdb->prefix.WL_PROPERTY_TABLE_NAME; |
|
| 73 | 73 | $property_rows = $this->wpdb->get_results( |
| 74 | - $this->wpdb->prepare( "SELECT * FROM $property_table_name WHERE mapping_id=%d", $mapping_id ), |
|
| 74 | + $this->wpdb->prepare("SELECT * FROM $property_table_name WHERE mapping_id=%d", $mapping_id), |
|
| 75 | 75 | ARRAY_A |
| 76 | 76 | ); |
| 77 | 77 | |
@@ -87,7 +87,7 @@ discard block |
||
| 87 | 87 | * |
| 88 | 88 | * @return bool Returns true if the row exists, false if it does not |
| 89 | 89 | */ |
| 90 | - private function check_if_row_exists( $table_name, $primary_key_name, $primary_key_value ) { |
|
| 90 | + private function check_if_row_exists($table_name, $primary_key_name, $primary_key_value) { |
|
| 91 | 91 | $primary_key_value = (int) $primary_key_value; |
| 92 | 92 | $count = (int) $this->wpdb->get_var( |
| 93 | 93 | $this->wpdb->prepare( |
@@ -106,11 +106,11 @@ discard block |
||
| 106 | 106 | * |
| 107 | 107 | * @return int Id of the inserted mapping item. |
| 108 | 108 | */ |
| 109 | - public function insert_mapping_item( $title ) { |
|
| 110 | - $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 109 | + public function insert_mapping_item($title) { |
|
| 110 | + $mapping_table_name = $this->wpdb->prefix.WL_MAPPING_TABLE_NAME; |
|
| 111 | 111 | $this->wpdb->insert( |
| 112 | 112 | $mapping_table_name, |
| 113 | - array( 'mapping_title' => $title ) |
|
| 113 | + array('mapping_title' => $title) |
|
| 114 | 114 | ); |
| 115 | 115 | |
| 116 | 116 | return $this->wpdb->insert_id; |
@@ -123,14 +123,14 @@ discard block |
||
| 123 | 123 | * |
| 124 | 124 | * @return int Id of the inserted mapping item |
| 125 | 125 | */ |
| 126 | - public function insert_or_update_mapping_item( $mapping_data ) { |
|
| 127 | - $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 128 | - $mapping_id = array_key_exists( 'mapping_id', $mapping_data ) ? (int) $mapping_data['mapping_id'] : null; |
|
| 129 | - if ( $this->check_if_row_exists( $mapping_table_name, 'mapping_id', $mapping_data['mapping_id'] ) ) { |
|
| 126 | + public function insert_or_update_mapping_item($mapping_data) { |
|
| 127 | + $mapping_table_name = $this->wpdb->prefix.WL_MAPPING_TABLE_NAME; |
|
| 128 | + $mapping_id = array_key_exists('mapping_id', $mapping_data) ? (int) $mapping_data['mapping_id'] : null; |
|
| 129 | + if ($this->check_if_row_exists($mapping_table_name, 'mapping_id', $mapping_data['mapping_id'])) { |
|
| 130 | 130 | $this->wpdb->update( |
| 131 | 131 | $mapping_table_name, |
| 132 | 132 | $mapping_data, |
| 133 | - array( 'mapping_id' => $mapping_id ) |
|
| 133 | + array('mapping_id' => $mapping_id) |
|
| 134 | 134 | ); |
| 135 | 135 | } else { |
| 136 | 136 | $this->wpdb->insert( |
@@ -150,13 +150,13 @@ discard block |
||
| 150 | 150 | * |
| 151 | 151 | * @return int $rule_id The inserted rule id. |
| 152 | 152 | */ |
| 153 | - public function insert_or_update_rule_item( $rule_item_data ) { |
|
| 154 | - $rule_table_name = $this->wpdb->prefix . WL_RULE_TABLE_NAME; |
|
| 155 | - $rule_id = array_key_exists( 'rule_id', $rule_item_data ) ? $rule_item_data['rule_id'] : null; |
|
| 156 | - if ( $this->check_if_row_exists( $rule_table_name, 'rule_id', $rule_id ) ) { |
|
| 157 | - $this->wpdb->update( $rule_table_name, $rule_item_data, array( 'rule_id' => $rule_id ) ); |
|
| 153 | + public function insert_or_update_rule_item($rule_item_data) { |
|
| 154 | + $rule_table_name = $this->wpdb->prefix.WL_RULE_TABLE_NAME; |
|
| 155 | + $rule_id = array_key_exists('rule_id', $rule_item_data) ? $rule_item_data['rule_id'] : null; |
|
| 156 | + if ($this->check_if_row_exists($rule_table_name, 'rule_id', $rule_id)) { |
|
| 157 | + $this->wpdb->update($rule_table_name, $rule_item_data, array('rule_id' => $rule_id)); |
|
| 158 | 158 | } else { |
| 159 | - $this->wpdb->insert( $rule_table_name, $rule_item_data ); |
|
| 159 | + $this->wpdb->insert($rule_table_name, $rule_item_data); |
|
| 160 | 160 | $rule_id = $this->wpdb->insert_id; |
| 161 | 161 | } |
| 162 | 162 | |
@@ -171,8 +171,8 @@ discard block |
||
| 171 | 171 | * |
| 172 | 172 | * @return int The inserted rule group id. |
| 173 | 173 | */ |
| 174 | - public function insert_rule_group( $mapping_id ) { |
|
| 175 | - $rule_group_table_name = $this->wpdb->prefix . WL_RULE_GROUP_TABLE_NAME; |
|
| 174 | + public function insert_rule_group($mapping_id) { |
|
| 175 | + $rule_group_table_name = $this->wpdb->prefix.WL_RULE_GROUP_TABLE_NAME; |
|
| 176 | 176 | $this->wpdb->insert( |
| 177 | 177 | $rule_group_table_name, |
| 178 | 178 | array( |
@@ -190,9 +190,9 @@ discard block |
||
| 190 | 190 | * |
| 191 | 191 | * @return void |
| 192 | 192 | */ |
| 193 | - public function delete_rule_item( $rule_id ) { |
|
| 194 | - $rule_table_name = $this->wpdb->prefix . WL_RULE_TABLE_NAME; |
|
| 195 | - $this->wpdb->delete( $rule_table_name, array( 'rule_id' => $rule_id ) ); |
|
| 193 | + public function delete_rule_item($rule_id) { |
|
| 194 | + $rule_table_name = $this->wpdb->prefix.WL_RULE_TABLE_NAME; |
|
| 195 | + $this->wpdb->delete($rule_table_name, array('rule_id' => $rule_id)); |
|
| 196 | 196 | } |
| 197 | 197 | |
| 198 | 198 | /** |
@@ -202,9 +202,9 @@ discard block |
||
| 202 | 202 | * |
| 203 | 203 | * @return void |
| 204 | 204 | */ |
| 205 | - public function delete_rule_group_item( $rule_group_id ) { |
|
| 206 | - $rule_group_table_name = $this->wpdb->prefix . WL_RULE_GROUP_TABLE_NAME; |
|
| 207 | - $this->wpdb->delete( $rule_group_table_name, array( 'rule_group_id' => $rule_group_id ) ); |
|
| 205 | + public function delete_rule_group_item($rule_group_id) { |
|
| 206 | + $rule_group_table_name = $this->wpdb->prefix.WL_RULE_GROUP_TABLE_NAME; |
|
| 207 | + $this->wpdb->delete($rule_group_table_name, array('rule_group_id' => $rule_group_id)); |
|
| 208 | 208 | } |
| 209 | 209 | |
| 210 | 210 | /** |
@@ -214,9 +214,9 @@ discard block |
||
| 214 | 214 | * |
| 215 | 215 | * @return void |
| 216 | 216 | */ |
| 217 | - public function delete_mapping_item( $mapping_id ) { |
|
| 218 | - $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 219 | - $this->wpdb->delete( $mapping_table_name, array( 'mapping_id' => $mapping_id ) ); |
|
| 217 | + public function delete_mapping_item($mapping_id) { |
|
| 218 | + $mapping_table_name = $this->wpdb->prefix.WL_MAPPING_TABLE_NAME; |
|
| 219 | + $this->wpdb->delete($mapping_table_name, array('mapping_id' => $mapping_id)); |
|
| 220 | 220 | } |
| 221 | 221 | |
| 222 | 222 | /** |
@@ -226,8 +226,8 @@ discard block |
||
| 226 | 226 | * |
| 227 | 227 | * @return array Get list of rule group items. |
| 228 | 228 | */ |
| 229 | - public function get_rule_group_list( $mapping_id ) { |
|
| 230 | - $rule_group_table_name = $this->wpdb->prefix . WL_RULE_GROUP_TABLE_NAME; |
|
| 229 | + public function get_rule_group_list($mapping_id) { |
|
| 230 | + $rule_group_table_name = $this->wpdb->prefix.WL_RULE_GROUP_TABLE_NAME; |
|
| 231 | 231 | |
| 232 | 232 | return $this->wpdb->get_results( |
| 233 | 233 | $this->wpdb->prepare( |
@@ -245,9 +245,9 @@ discard block |
||
| 245 | 245 | * |
| 246 | 246 | * @return array Get list of rule group items. |
| 247 | 247 | */ |
| 248 | - public function get_rule_groups_by_mapping( $mapping_id ) { |
|
| 248 | + public function get_rule_groups_by_mapping($mapping_id) { |
|
| 249 | 249 | |
| 250 | - $rule_group_table_name = $this->wpdb->prefix . WL_RULE_GROUP_TABLE_NAME; |
|
| 250 | + $rule_group_table_name = $this->wpdb->prefix.WL_RULE_GROUP_TABLE_NAME; |
|
| 251 | 251 | $rule_group_rows = $this->wpdb->get_results( |
| 252 | 252 | $this->wpdb->prepare( |
| 253 | 253 | "SELECT rule_group_id FROM $rule_group_table_name WHERE mapping_id=%d", |
@@ -258,10 +258,10 @@ discard block |
||
| 258 | 258 | |
| 259 | 259 | // List of all rule group items. |
| 260 | 260 | $rule_groups = array(); |
| 261 | - foreach ( $rule_group_rows as $rule_group_row ) { |
|
| 261 | + foreach ($rule_group_rows as $rule_group_row) { |
|
| 262 | 262 | $rule_groups[] = array( |
| 263 | 263 | 'rule_group_id' => $rule_group_row['rule_group_id'], |
| 264 | - 'rules' => $this->get_rules_by_rule_group( $rule_group_row['rule_group_id'] ), |
|
| 264 | + 'rules' => $this->get_rules_by_rule_group($rule_group_row['rule_group_id']), |
|
| 265 | 265 | ); |
| 266 | 266 | } |
| 267 | 267 | |
@@ -275,8 +275,8 @@ discard block |
||
| 275 | 275 | * |
| 276 | 276 | * @return array Get list of rule items. |
| 277 | 277 | */ |
| 278 | - public function get_rules_by_rule_group( $rule_group_id ) { |
|
| 279 | - $rule_table_name = $this->wpdb->prefix . WL_RULE_TABLE_NAME; |
|
| 278 | + public function get_rules_by_rule_group($rule_group_id) { |
|
| 279 | + $rule_table_name = $this->wpdb->prefix.WL_RULE_TABLE_NAME; |
|
| 280 | 280 | |
| 281 | 281 | return $this->wpdb->get_results( |
| 282 | 282 | $this->wpdb->prepare( |
@@ -294,18 +294,18 @@ discard block |
||
| 294 | 294 | * |
| 295 | 295 | * @return int Inserted Property Id. |
| 296 | 296 | */ |
| 297 | - public function insert_or_update_property( $property_data ) { |
|
| 298 | - $property_table_name = $this->wpdb->prefix . WL_PROPERTY_TABLE_NAME; |
|
| 299 | - $property_id = array_key_exists( 'property_id', $property_data ) ? $property_data['property_id'] : null; |
|
| 297 | + public function insert_or_update_property($property_data) { |
|
| 298 | + $property_table_name = $this->wpdb->prefix.WL_PROPERTY_TABLE_NAME; |
|
| 299 | + $property_id = array_key_exists('property_id', $property_data) ? $property_data['property_id'] : null; |
|
| 300 | 300 | |
| 301 | - if ( $this->check_if_row_exists( $property_table_name, 'property_id', $property_id ) ) { |
|
| 301 | + if ($this->check_if_row_exists($property_table_name, 'property_id', $property_id)) { |
|
| 302 | 302 | $this->wpdb->update( |
| 303 | 303 | $property_table_name, |
| 304 | 304 | $property_data, |
| 305 | - array( 'property_id' => $property_id ) |
|
| 305 | + array('property_id' => $property_id) |
|
| 306 | 306 | ); |
| 307 | 307 | } else { |
| 308 | - $this->wpdb->insert( $property_table_name, $property_data ); |
|
| 308 | + $this->wpdb->insert($property_table_name, $property_data); |
|
| 309 | 309 | $property_id = $this->wpdb->insert_id; |
| 310 | 310 | } |
| 311 | 311 | |
@@ -319,8 +319,8 @@ discard block |
||
| 319 | 319 | * |
| 320 | 320 | * @return array Returns single mapping table row.. |
| 321 | 321 | */ |
| 322 | - public function get_mapping_item_data( $mapping_id ) { |
|
| 323 | - $mapping_table_name = $this->wpdb->prefix . WL_MAPPING_TABLE_NAME; |
|
| 322 | + public function get_mapping_item_data($mapping_id) { |
|
| 323 | + $mapping_table_name = $this->wpdb->prefix.WL_MAPPING_TABLE_NAME; |
|
| 324 | 324 | |
| 325 | 325 | return $this->wpdb->get_row( |
| 326 | 326 | $this->wpdb->prepare( |
@@ -338,10 +338,10 @@ discard block |
||
| 338 | 338 | * |
| 339 | 339 | * @return int|false The number of rows updated, or false on error. |
| 340 | 340 | */ |
| 341 | - public function delete_property( $property_id ) { |
|
| 342 | - $property_table_name = $this->wpdb->prefix . WL_PROPERTY_TABLE_NAME; |
|
| 341 | + public function delete_property($property_id) { |
|
| 342 | + $property_table_name = $this->wpdb->prefix.WL_PROPERTY_TABLE_NAME; |
|
| 343 | 343 | |
| 344 | - return $this->wpdb->delete( $property_table_name, array( 'property_id' => $property_id ) ); |
|
| 344 | + return $this->wpdb->delete($property_table_name, array('property_id' => $property_id)); |
|
| 345 | 345 | } |
| 346 | 346 | |
| 347 | 347 | } |
@@ -15,32 +15,32 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | interface Mappings_Transform_Function { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Returns unique name of the transform function. |
|
| 20 | - * |
|
| 21 | - * @return string $name Unique name of the transform function, it should not be repeated |
|
| 22 | - * for any other transform function. |
|
| 23 | - */ |
|
| 24 | - public function get_name(); |
|
| 18 | + /** |
|
| 19 | + * Returns unique name of the transform function. |
|
| 20 | + * |
|
| 21 | + * @return string $name Unique name of the transform function, it should not be repeated |
|
| 22 | + * for any other transform function. |
|
| 23 | + */ |
|
| 24 | + public function get_name(); |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Returns label of the transform function. |
|
| 28 | - * |
|
| 29 | - * @return string $label Label of the transform function to be used in UI, need not |
|
| 30 | - * be unique. |
|
| 31 | - */ |
|
| 32 | - public function get_label(); |
|
| 26 | + /** |
|
| 27 | + * Returns label of the transform function. |
|
| 28 | + * |
|
| 29 | + * @return string $label Label of the transform function to be used in UI, need not |
|
| 30 | + * be unique. |
|
| 31 | + */ |
|
| 32 | + public function get_label(); |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Transform data and map to the desired keys. |
|
| 36 | - * |
|
| 37 | - * @param array|string $data An Associative Array containing raw data or string. |
|
| 38 | - * @param array $jsonld The JSON-LD structure. |
|
| 39 | - * @param int[] $references An array of post IDs referenced by the JSON-LD structure. |
|
| 40 | - * @param int $post_id The post ID. |
|
| 41 | - * |
|
| 42 | - * @return array|string Return Mapped data. |
|
| 43 | - */ |
|
| 44 | - public function transform_data( $data, $jsonld, &$references, $post_id ); |
|
| 34 | + /** |
|
| 35 | + * Transform data and map to the desired keys. |
|
| 36 | + * |
|
| 37 | + * @param array|string $data An Associative Array containing raw data or string. |
|
| 38 | + * @param array $jsonld The JSON-LD structure. |
|
| 39 | + * @param int[] $references An array of post IDs referenced by the JSON-LD structure. |
|
| 40 | + * @param int $post_id The post ID. |
|
| 41 | + * |
|
| 42 | + * @return array|string Return Mapped data. |
|
| 43 | + */ |
|
| 44 | + public function transform_data( $data, $jsonld, &$references, $post_id ); |
|
| 45 | 45 | |
| 46 | 46 | } |
@@ -41,6 +41,6 @@ |
||
| 41 | 41 | * |
| 42 | 42 | * @return array|string Return Mapped data. |
| 43 | 43 | */ |
| 44 | - public function transform_data( $data, $jsonld, &$references, $post_id ); |
|
| 44 | + public function transform_data($data, $jsonld, &$references, $post_id); |
|
| 45 | 45 | |
| 46 | 46 | } |
@@ -23,68 +23,68 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | class Admin_Mappings_Page extends Wordlift_Admin_Page { |
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Provides script and js global values used by react component. |
|
| 28 | - */ |
|
| 29 | - public static function provide_ui_dependencies() { |
|
| 30 | - // Create ui settings array to be used by js client. |
|
| 31 | - $mapping_settings = array(); |
|
| 32 | - $mapping_settings['rest_url'] = get_rest_url( |
|
| 33 | - null, |
|
| 34 | - WL_REST_ROUTE_DEFAULT_NAMESPACE . Mappings_REST_Controller::MAPPINGS_NAMESPACE |
|
| 35 | - ); |
|
| 36 | - $mapping_settings['wl_mapping_nonce'] = wp_create_nonce( 'wp_rest' ); |
|
| 37 | - $mapping_settings['wl_edit_mapping_nonce'] = wp_create_nonce( 'wl-edit-mapping-nonce' ); |
|
| 38 | - wp_localize_script( 'wl-mappings-admin', 'wlMappingsConfig', $mapping_settings ); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * {@inheritdoc} |
|
| 43 | - */ |
|
| 44 | - public function get_page_title() { |
|
| 45 | - |
|
| 46 | - return __( 'Mappings', 'wordlift' ); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * {@inheritdoc} |
|
| 51 | - */ |
|
| 52 | - public function get_menu_title() { |
|
| 53 | - |
|
| 54 | - return __( 'Mappings', 'wordlift' ); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * {@inheritdoc} |
|
| 59 | - */ |
|
| 60 | - public function get_menu_slug() { |
|
| 61 | - |
|
| 62 | - return 'wl_mappings_admin'; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * {@inheritdoc} |
|
| 67 | - */ |
|
| 68 | - public function get_partial_name() { |
|
| 69 | - |
|
| 70 | - return 'wordlift-admin-mappings-admin.php'; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - public function enqueue_scripts() { |
|
| 74 | - |
|
| 75 | - Scripts_Helper::enqueue_based_on_wordpress_version( |
|
| 76 | - 'wl-mappings-admin', |
|
| 77 | - plugin_dir_url( dirname( dirname( dirname( __FILE__ ) ) ) ) . 'js/dist/mappings', |
|
| 78 | - array( 'react', 'react-dom', 'wp-polyfill', ), |
|
| 79 | - true |
|
| 80 | - ); |
|
| 81 | - |
|
| 82 | - wp_enqueue_style( |
|
| 83 | - 'wl-mappings-admin', |
|
| 84 | - plugin_dir_url( dirname( dirname( dirname( __FILE__ ) ) ) ) . 'js/dist/mappings.css', |
|
| 85 | - Wordlift::get_instance()->get_version() |
|
| 86 | - ); |
|
| 87 | - Admin_Mappings_Page::provide_ui_dependencies(); |
|
| 88 | - } |
|
| 26 | + /** |
|
| 27 | + * Provides script and js global values used by react component. |
|
| 28 | + */ |
|
| 29 | + public static function provide_ui_dependencies() { |
|
| 30 | + // Create ui settings array to be used by js client. |
|
| 31 | + $mapping_settings = array(); |
|
| 32 | + $mapping_settings['rest_url'] = get_rest_url( |
|
| 33 | + null, |
|
| 34 | + WL_REST_ROUTE_DEFAULT_NAMESPACE . Mappings_REST_Controller::MAPPINGS_NAMESPACE |
|
| 35 | + ); |
|
| 36 | + $mapping_settings['wl_mapping_nonce'] = wp_create_nonce( 'wp_rest' ); |
|
| 37 | + $mapping_settings['wl_edit_mapping_nonce'] = wp_create_nonce( 'wl-edit-mapping-nonce' ); |
|
| 38 | + wp_localize_script( 'wl-mappings-admin', 'wlMappingsConfig', $mapping_settings ); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * {@inheritdoc} |
|
| 43 | + */ |
|
| 44 | + public function get_page_title() { |
|
| 45 | + |
|
| 46 | + return __( 'Mappings', 'wordlift' ); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * {@inheritdoc} |
|
| 51 | + */ |
|
| 52 | + public function get_menu_title() { |
|
| 53 | + |
|
| 54 | + return __( 'Mappings', 'wordlift' ); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * {@inheritdoc} |
|
| 59 | + */ |
|
| 60 | + public function get_menu_slug() { |
|
| 61 | + |
|
| 62 | + return 'wl_mappings_admin'; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * {@inheritdoc} |
|
| 67 | + */ |
|
| 68 | + public function get_partial_name() { |
|
| 69 | + |
|
| 70 | + return 'wordlift-admin-mappings-admin.php'; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + public function enqueue_scripts() { |
|
| 74 | + |
|
| 75 | + Scripts_Helper::enqueue_based_on_wordpress_version( |
|
| 76 | + 'wl-mappings-admin', |
|
| 77 | + plugin_dir_url( dirname( dirname( dirname( __FILE__ ) ) ) ) . 'js/dist/mappings', |
|
| 78 | + array( 'react', 'react-dom', 'wp-polyfill', ), |
|
| 79 | + true |
|
| 80 | + ); |
|
| 81 | + |
|
| 82 | + wp_enqueue_style( |
|
| 83 | + 'wl-mappings-admin', |
|
| 84 | + plugin_dir_url( dirname( dirname( dirname( __FILE__ ) ) ) ) . 'js/dist/mappings.css', |
|
| 85 | + Wordlift::get_instance()->get_version() |
|
| 86 | + ); |
|
| 87 | + Admin_Mappings_Page::provide_ui_dependencies(); |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | 90 | } |
@@ -31,11 +31,11 @@ discard block |
||
| 31 | 31 | $mapping_settings = array(); |
| 32 | 32 | $mapping_settings['rest_url'] = get_rest_url( |
| 33 | 33 | null, |
| 34 | - WL_REST_ROUTE_DEFAULT_NAMESPACE . Mappings_REST_Controller::MAPPINGS_NAMESPACE |
|
| 34 | + WL_REST_ROUTE_DEFAULT_NAMESPACE.Mappings_REST_Controller::MAPPINGS_NAMESPACE |
|
| 35 | 35 | ); |
| 36 | - $mapping_settings['wl_mapping_nonce'] = wp_create_nonce( 'wp_rest' ); |
|
| 37 | - $mapping_settings['wl_edit_mapping_nonce'] = wp_create_nonce( 'wl-edit-mapping-nonce' ); |
|
| 38 | - wp_localize_script( 'wl-mappings-admin', 'wlMappingsConfig', $mapping_settings ); |
|
| 36 | + $mapping_settings['wl_mapping_nonce'] = wp_create_nonce('wp_rest'); |
|
| 37 | + $mapping_settings['wl_edit_mapping_nonce'] = wp_create_nonce('wl-edit-mapping-nonce'); |
|
| 38 | + wp_localize_script('wl-mappings-admin', 'wlMappingsConfig', $mapping_settings); |
|
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | /** |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | */ |
| 44 | 44 | public function get_page_title() { |
| 45 | 45 | |
| 46 | - return __( 'Mappings', 'wordlift' ); |
|
| 46 | + return __('Mappings', 'wordlift'); |
|
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | /** |
@@ -51,7 +51,7 @@ discard block |
||
| 51 | 51 | */ |
| 52 | 52 | public function get_menu_title() { |
| 53 | 53 | |
| 54 | - return __( 'Mappings', 'wordlift' ); |
|
| 54 | + return __('Mappings', 'wordlift'); |
|
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | /** |
@@ -74,14 +74,14 @@ discard block |
||
| 74 | 74 | |
| 75 | 75 | Scripts_Helper::enqueue_based_on_wordpress_version( |
| 76 | 76 | 'wl-mappings-admin', |
| 77 | - plugin_dir_url( dirname( dirname( dirname( __FILE__ ) ) ) ) . 'js/dist/mappings', |
|
| 78 | - array( 'react', 'react-dom', 'wp-polyfill', ), |
|
| 77 | + plugin_dir_url(dirname(dirname(dirname(__FILE__)))).'js/dist/mappings', |
|
| 78 | + array('react', 'react-dom', 'wp-polyfill',), |
|
| 79 | 79 | true |
| 80 | 80 | ); |
| 81 | 81 | |
| 82 | 82 | wp_enqueue_style( |
| 83 | 83 | 'wl-mappings-admin', |
| 84 | - plugin_dir_url( dirname( dirname( dirname( __FILE__ ) ) ) ) . 'js/dist/mappings.css', |
|
| 84 | + plugin_dir_url(dirname(dirname(dirname(__FILE__)))).'js/dist/mappings.css', |
|
| 85 | 85 | Wordlift::get_instance()->get_version() |
| 86 | 86 | ); |
| 87 | 87 | Admin_Mappings_Page::provide_ui_dependencies(); |
@@ -18,43 +18,43 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Rule_Validators_Registry { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * An array of {@link Rule_Validator}s. |
|
| 23 | - * |
|
| 24 | - * @var array An array of {@link Rule_Validator}s. |
|
| 25 | - */ |
|
| 26 | - private $rule_validators; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Rule_Validators_Registry constructor. |
|
| 30 | - * |
|
| 31 | - * @param Rule_Validator $default The default rule validator. |
|
| 32 | - * |
|
| 33 | - * @throws Exception throws an exception if an invalid validator has been provided. |
|
| 34 | - */ |
|
| 35 | - public function __construct( $default ) { |
|
| 36 | - |
|
| 37 | - // Check that a valid validator has been provided. |
|
| 38 | - if ( ! ( $default instanceof Rule_Validator ) ) { |
|
| 39 | - throw new Exception( "An invalid Rule_Validator was provided as default validator." ); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - // Allow 3rd parties to register other validators. |
|
| 43 | - $this->rule_validators = apply_filters( 'wl_mappings_rule_validators', array( '__default__' => $default, ) ); |
|
| 44 | - |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Get a rule validator by its key. |
|
| 49 | - * |
|
| 50 | - * @param string $key A key uniquely identifying a validator. |
|
| 51 | - * |
|
| 52 | - * @return Rule_Validator A {@link Rule_Validator} instance or the default one when not found. |
|
| 53 | - */ |
|
| 54 | - public function get_rule_validator( $key ) { |
|
| 55 | - |
|
| 56 | - return isset( $this->rule_validators[ $key ] ) |
|
| 57 | - ? $this->rule_validators[ $key ] : $this->rule_validators['__default__']; |
|
| 58 | - } |
|
| 21 | + /** |
|
| 22 | + * An array of {@link Rule_Validator}s. |
|
| 23 | + * |
|
| 24 | + * @var array An array of {@link Rule_Validator}s. |
|
| 25 | + */ |
|
| 26 | + private $rule_validators; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Rule_Validators_Registry constructor. |
|
| 30 | + * |
|
| 31 | + * @param Rule_Validator $default The default rule validator. |
|
| 32 | + * |
|
| 33 | + * @throws Exception throws an exception if an invalid validator has been provided. |
|
| 34 | + */ |
|
| 35 | + public function __construct( $default ) { |
|
| 36 | + |
|
| 37 | + // Check that a valid validator has been provided. |
|
| 38 | + if ( ! ( $default instanceof Rule_Validator ) ) { |
|
| 39 | + throw new Exception( "An invalid Rule_Validator was provided as default validator." ); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + // Allow 3rd parties to register other validators. |
|
| 43 | + $this->rule_validators = apply_filters( 'wl_mappings_rule_validators', array( '__default__' => $default, ) ); |
|
| 44 | + |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Get a rule validator by its key. |
|
| 49 | + * |
|
| 50 | + * @param string $key A key uniquely identifying a validator. |
|
| 51 | + * |
|
| 52 | + * @return Rule_Validator A {@link Rule_Validator} instance or the default one when not found. |
|
| 53 | + */ |
|
| 54 | + public function get_rule_validator( $key ) { |
|
| 55 | + |
|
| 56 | + return isset( $this->rule_validators[ $key ] ) |
|
| 57 | + ? $this->rule_validators[ $key ] : $this->rule_validators['__default__']; |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | 60 | } |
@@ -32,15 +32,15 @@ discard block |
||
| 32 | 32 | * |
| 33 | 33 | * @throws Exception throws an exception if an invalid validator has been provided. |
| 34 | 34 | */ |
| 35 | - public function __construct( $default ) { |
|
| 35 | + public function __construct($default) { |
|
| 36 | 36 | |
| 37 | 37 | // Check that a valid validator has been provided. |
| 38 | - if ( ! ( $default instanceof Rule_Validator ) ) { |
|
| 39 | - throw new Exception( "An invalid Rule_Validator was provided as default validator." ); |
|
| 38 | + if ( ! ($default instanceof Rule_Validator)) { |
|
| 39 | + throw new Exception("An invalid Rule_Validator was provided as default validator."); |
|
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | // Allow 3rd parties to register other validators. |
| 43 | - $this->rule_validators = apply_filters( 'wl_mappings_rule_validators', array( '__default__' => $default, ) ); |
|
| 43 | + $this->rule_validators = apply_filters('wl_mappings_rule_validators', array('__default__' => $default,)); |
|
| 44 | 44 | |
| 45 | 45 | } |
| 46 | 46 | |
@@ -51,10 +51,10 @@ discard block |
||
| 51 | 51 | * |
| 52 | 52 | * @return Rule_Validator A {@link Rule_Validator} instance or the default one when not found. |
| 53 | 53 | */ |
| 54 | - public function get_rule_validator( $key ) { |
|
| 54 | + public function get_rule_validator($key) { |
|
| 55 | 55 | |
| 56 | - return isset( $this->rule_validators[ $key ] ) |
|
| 57 | - ? $this->rule_validators[ $key ] : $this->rule_validators['__default__']; |
|
| 56 | + return isset($this->rule_validators[$key]) |
|
| 57 | + ? $this->rule_validators[$key] : $this->rule_validators['__default__']; |
|
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | } |
@@ -12,109 +12,109 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class Mappings_Transform_Functions_Registry { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Holds an array of transformation functions, all the transformation |
|
| 17 | - * functions are instance of { @link \Mappings_Transform_Function} Interface |
|
| 18 | - * |
|
| 19 | - * @since 3.25.0 |
|
| 20 | - * @access private |
|
| 21 | - * @var Mappings_Validator $validator The {@link Mappings_Validator} instance to test. |
|
| 22 | - */ |
|
| 23 | - private $transform_function_array = array(); |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * Construct a list of transform function array. |
|
| 27 | - */ |
|
| 28 | - public function __construct() { |
|
| 29 | - |
|
| 30 | - $this->transform_function_array = apply_filters( 'wl_mappings_transformation_functions', array() ); |
|
| 31 | - |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Return options required for ui |
|
| 36 | - * |
|
| 37 | - * @return array An Array of transform function options. |
|
| 38 | - */ |
|
| 39 | - public function get_options() { |
|
| 40 | - $this->sync_transformation_functions_from_external_plugins(); |
|
| 41 | - $options = array(); |
|
| 42 | - foreach ( $this->transform_function_array as $transform_function ) { |
|
| 43 | - array_push( |
|
| 44 | - $options, |
|
| 45 | - array( |
|
| 46 | - 'label' => $transform_function->get_label(), |
|
| 47 | - 'value' => $transform_function->get_name(), |
|
| 48 | - ) |
|
| 49 | - ); |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - return $options; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Return instance of the transform function. |
|
| 57 | - * |
|
| 58 | - * @param string $transform_function_name The name of the transform function which needs to applied. |
|
| 59 | - * |
|
| 60 | - * @return Mappings_Transform_Function|null An Instance of transform function from any one of |
|
| 61 | - * the classes extending this interface, if nothing matches null is returned. |
|
| 62 | - */ |
|
| 63 | - public function get_transform_function( $transform_function_name ) { |
|
| 64 | - $this->sync_transformation_functions_from_external_plugins(); |
|
| 65 | - foreach ( $this->transform_function_array as $transform_function_instance ) { |
|
| 66 | - if ( $transform_function_instance->get_name() === $transform_function_name ) { |
|
| 67 | - return $transform_function_instance; |
|
| 68 | - } |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - // Returns null if the transform function doesn't match. |
|
| 72 | - return null; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Sync the transformation function from external plugins to registry. |
|
| 77 | - * @return void |
|
| 78 | - */ |
|
| 79 | - private function sync_transformation_functions_from_external_plugins() { |
|
| 80 | - $this->transform_function_array = apply_filters( |
|
| 81 | - 'wl_mappings_transformation_functions', |
|
| 82 | - $this->transform_function_array |
|
| 83 | - ); |
|
| 84 | - $this->remove_duplicate_transformation_functions_after_sync(); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * @return int|void Returns the number of transformation functions present in registry. |
|
| 89 | - */ |
|
| 90 | - public function get_transform_function_count() { |
|
| 91 | - return count( $this->transform_function_array ); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Check if any duplicate transformation functions are present in registry, run this |
|
| 96 | - * function after syncing with the registry. |
|
| 97 | - */ |
|
| 98 | - private function remove_duplicate_transformation_functions_after_sync() { |
|
| 99 | - /** |
|
| 100 | - * This check is done to avoid transformation function conflicts if they share a same name |
|
| 101 | - * For example if plugin A and plugin B registers transformation function with same name C, |
|
| 102 | - * we remove the duplicated transformation function |
|
| 103 | - */ |
|
| 104 | - $transformation_function_names = array(); |
|
| 105 | - $transformation_functions = array(); |
|
| 106 | - foreach ( $this->transform_function_array as $transformation_function ) { |
|
| 107 | - if ( ! in_array( $transformation_function->get_name(), $transformation_function_names ) ) { |
|
| 108 | - array_push( $transformation_function_names, $transformation_function->get_name() ); |
|
| 109 | - array_push( $transformation_functions, $transformation_function ); |
|
| 110 | - } |
|
| 111 | - } |
|
| 112 | - $this->transform_function_array = $transformation_functions; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - public function get_transforms() { |
|
| 116 | - |
|
| 117 | - return $this->transform_function_array; |
|
| 118 | - } |
|
| 15 | + /** |
|
| 16 | + * Holds an array of transformation functions, all the transformation |
|
| 17 | + * functions are instance of { @link \Mappings_Transform_Function} Interface |
|
| 18 | + * |
|
| 19 | + * @since 3.25.0 |
|
| 20 | + * @access private |
|
| 21 | + * @var Mappings_Validator $validator The {@link Mappings_Validator} instance to test. |
|
| 22 | + */ |
|
| 23 | + private $transform_function_array = array(); |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * Construct a list of transform function array. |
|
| 27 | + */ |
|
| 28 | + public function __construct() { |
|
| 29 | + |
|
| 30 | + $this->transform_function_array = apply_filters( 'wl_mappings_transformation_functions', array() ); |
|
| 31 | + |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Return options required for ui |
|
| 36 | + * |
|
| 37 | + * @return array An Array of transform function options. |
|
| 38 | + */ |
|
| 39 | + public function get_options() { |
|
| 40 | + $this->sync_transformation_functions_from_external_plugins(); |
|
| 41 | + $options = array(); |
|
| 42 | + foreach ( $this->transform_function_array as $transform_function ) { |
|
| 43 | + array_push( |
|
| 44 | + $options, |
|
| 45 | + array( |
|
| 46 | + 'label' => $transform_function->get_label(), |
|
| 47 | + 'value' => $transform_function->get_name(), |
|
| 48 | + ) |
|
| 49 | + ); |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + return $options; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Return instance of the transform function. |
|
| 57 | + * |
|
| 58 | + * @param string $transform_function_name The name of the transform function which needs to applied. |
|
| 59 | + * |
|
| 60 | + * @return Mappings_Transform_Function|null An Instance of transform function from any one of |
|
| 61 | + * the classes extending this interface, if nothing matches null is returned. |
|
| 62 | + */ |
|
| 63 | + public function get_transform_function( $transform_function_name ) { |
|
| 64 | + $this->sync_transformation_functions_from_external_plugins(); |
|
| 65 | + foreach ( $this->transform_function_array as $transform_function_instance ) { |
|
| 66 | + if ( $transform_function_instance->get_name() === $transform_function_name ) { |
|
| 67 | + return $transform_function_instance; |
|
| 68 | + } |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + // Returns null if the transform function doesn't match. |
|
| 72 | + return null; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Sync the transformation function from external plugins to registry. |
|
| 77 | + * @return void |
|
| 78 | + */ |
|
| 79 | + private function sync_transformation_functions_from_external_plugins() { |
|
| 80 | + $this->transform_function_array = apply_filters( |
|
| 81 | + 'wl_mappings_transformation_functions', |
|
| 82 | + $this->transform_function_array |
|
| 83 | + ); |
|
| 84 | + $this->remove_duplicate_transformation_functions_after_sync(); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * @return int|void Returns the number of transformation functions present in registry. |
|
| 89 | + */ |
|
| 90 | + public function get_transform_function_count() { |
|
| 91 | + return count( $this->transform_function_array ); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Check if any duplicate transformation functions are present in registry, run this |
|
| 96 | + * function after syncing with the registry. |
|
| 97 | + */ |
|
| 98 | + private function remove_duplicate_transformation_functions_after_sync() { |
|
| 99 | + /** |
|
| 100 | + * This check is done to avoid transformation function conflicts if they share a same name |
|
| 101 | + * For example if plugin A and plugin B registers transformation function with same name C, |
|
| 102 | + * we remove the duplicated transformation function |
|
| 103 | + */ |
|
| 104 | + $transformation_function_names = array(); |
|
| 105 | + $transformation_functions = array(); |
|
| 106 | + foreach ( $this->transform_function_array as $transformation_function ) { |
|
| 107 | + if ( ! in_array( $transformation_function->get_name(), $transformation_function_names ) ) { |
|
| 108 | + array_push( $transformation_function_names, $transformation_function->get_name() ); |
|
| 109 | + array_push( $transformation_functions, $transformation_function ); |
|
| 110 | + } |
|
| 111 | + } |
|
| 112 | + $this->transform_function_array = $transformation_functions; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + public function get_transforms() { |
|
| 116 | + |
|
| 117 | + return $this->transform_function_array; |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | 120 | } |
@@ -27,7 +27,7 @@ discard block |
||
| 27 | 27 | */ |
| 28 | 28 | public function __construct() { |
| 29 | 29 | |
| 30 | - $this->transform_function_array = apply_filters( 'wl_mappings_transformation_functions', array() ); |
|
| 30 | + $this->transform_function_array = apply_filters('wl_mappings_transformation_functions', array()); |
|
| 31 | 31 | |
| 32 | 32 | } |
| 33 | 33 | |
@@ -39,7 +39,7 @@ discard block |
||
| 39 | 39 | public function get_options() { |
| 40 | 40 | $this->sync_transformation_functions_from_external_plugins(); |
| 41 | 41 | $options = array(); |
| 42 | - foreach ( $this->transform_function_array as $transform_function ) { |
|
| 42 | + foreach ($this->transform_function_array as $transform_function) { |
|
| 43 | 43 | array_push( |
| 44 | 44 | $options, |
| 45 | 45 | array( |
@@ -60,10 +60,10 @@ discard block |
||
| 60 | 60 | * @return Mappings_Transform_Function|null An Instance of transform function from any one of |
| 61 | 61 | * the classes extending this interface, if nothing matches null is returned. |
| 62 | 62 | */ |
| 63 | - public function get_transform_function( $transform_function_name ) { |
|
| 63 | + public function get_transform_function($transform_function_name) { |
|
| 64 | 64 | $this->sync_transformation_functions_from_external_plugins(); |
| 65 | - foreach ( $this->transform_function_array as $transform_function_instance ) { |
|
| 66 | - if ( $transform_function_instance->get_name() === $transform_function_name ) { |
|
| 65 | + foreach ($this->transform_function_array as $transform_function_instance) { |
|
| 66 | + if ($transform_function_instance->get_name() === $transform_function_name) { |
|
| 67 | 67 | return $transform_function_instance; |
| 68 | 68 | } |
| 69 | 69 | } |
@@ -88,7 +88,7 @@ discard block |
||
| 88 | 88 | * @return int|void Returns the number of transformation functions present in registry. |
| 89 | 89 | */ |
| 90 | 90 | public function get_transform_function_count() { |
| 91 | - return count( $this->transform_function_array ); |
|
| 91 | + return count($this->transform_function_array); |
|
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 94 | /** |
@@ -103,10 +103,10 @@ discard block |
||
| 103 | 103 | */ |
| 104 | 104 | $transformation_function_names = array(); |
| 105 | 105 | $transformation_functions = array(); |
| 106 | - foreach ( $this->transform_function_array as $transformation_function ) { |
|
| 107 | - if ( ! in_array( $transformation_function->get_name(), $transformation_function_names ) ) { |
|
| 108 | - array_push( $transformation_function_names, $transformation_function->get_name() ); |
|
| 109 | - array_push( $transformation_functions, $transformation_function ); |
|
| 106 | + foreach ($this->transform_function_array as $transformation_function) { |
|
| 107 | + if ( ! in_array($transformation_function->get_name(), $transformation_function_names)) { |
|
| 108 | + array_push($transformation_function_names, $transformation_function->get_name()); |
|
| 109 | + array_push($transformation_functions, $transformation_function); |
|
| 110 | 110 | } |
| 111 | 111 | } |
| 112 | 112 | $this->transform_function_array = $transformation_functions; |
@@ -19,71 +19,71 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class Url_To_Entity_Transform_Function implements Mappings_Transform_Function { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 24 | - * |
|
| 25 | - * @var Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 26 | - */ |
|
| 27 | - private $entity_uri_service; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Url_To_Entity_Transform_Function constructor. |
|
| 31 | - * |
|
| 32 | - * @param Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 33 | - */ |
|
| 34 | - public function __construct( $entity_uri_service ) { |
|
| 35 | - |
|
| 36 | - $this->entity_uri_service = $entity_uri_service; |
|
| 37 | - |
|
| 38 | - add_filter( 'wl_mappings_transformation_functions', array( $this, 'wl_mappings_transformation_functions' ) ); |
|
| 39 | - |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Hook to add ourselves to the list of available transform functions. |
|
| 44 | - * |
|
| 45 | - * @param Mappings_Transform_Function[] $value An array of {@link Mappings_Transform_Function}s. |
|
| 46 | - * |
|
| 47 | - * @return Mappings_Transform_Function[] An updated array with ourselves too. |
|
| 48 | - */ |
|
| 49 | - public function wl_mappings_transformation_functions( $value ) { |
|
| 50 | - |
|
| 51 | - $value[] = $this; |
|
| 52 | - |
|
| 53 | - return $value; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * @inheritDoc |
|
| 58 | - */ |
|
| 59 | - public function get_name() { |
|
| 60 | - |
|
| 61 | - return 'url_to_entity'; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * @inheritDoc |
|
| 66 | - */ |
|
| 67 | - public function get_label() { |
|
| 68 | - |
|
| 69 | - return __( 'URL to Entity', 'wordlift' ); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * @inheritDoc |
|
| 74 | - */ |
|
| 75 | - public function transform_data( $data, $jsonld, &$references, $post_id ) { |
|
| 76 | - |
|
| 77 | - // Get the entity by URI. |
|
| 78 | - $post = $this->entity_uri_service->get_entity( $data ); |
|
| 79 | - |
|
| 80 | - // If found, add the reference. |
|
| 81 | - if ( is_a( $post, 'WP_Post' ) ) { |
|
| 82 | - // Add the entity among the references using the post ID. |
|
| 83 | - $references[] = $post->ID; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - return array( "@id" => $data, ); |
|
| 87 | - } |
|
| 22 | + /** |
|
| 23 | + * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 24 | + * |
|
| 25 | + * @var Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 26 | + */ |
|
| 27 | + private $entity_uri_service; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Url_To_Entity_Transform_Function constructor. |
|
| 31 | + * |
|
| 32 | + * @param Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 33 | + */ |
|
| 34 | + public function __construct( $entity_uri_service ) { |
|
| 35 | + |
|
| 36 | + $this->entity_uri_service = $entity_uri_service; |
|
| 37 | + |
|
| 38 | + add_filter( 'wl_mappings_transformation_functions', array( $this, 'wl_mappings_transformation_functions' ) ); |
|
| 39 | + |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Hook to add ourselves to the list of available transform functions. |
|
| 44 | + * |
|
| 45 | + * @param Mappings_Transform_Function[] $value An array of {@link Mappings_Transform_Function}s. |
|
| 46 | + * |
|
| 47 | + * @return Mappings_Transform_Function[] An updated array with ourselves too. |
|
| 48 | + */ |
|
| 49 | + public function wl_mappings_transformation_functions( $value ) { |
|
| 50 | + |
|
| 51 | + $value[] = $this; |
|
| 52 | + |
|
| 53 | + return $value; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * @inheritDoc |
|
| 58 | + */ |
|
| 59 | + public function get_name() { |
|
| 60 | + |
|
| 61 | + return 'url_to_entity'; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * @inheritDoc |
|
| 66 | + */ |
|
| 67 | + public function get_label() { |
|
| 68 | + |
|
| 69 | + return __( 'URL to Entity', 'wordlift' ); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * @inheritDoc |
|
| 74 | + */ |
|
| 75 | + public function transform_data( $data, $jsonld, &$references, $post_id ) { |
|
| 76 | + |
|
| 77 | + // Get the entity by URI. |
|
| 78 | + $post = $this->entity_uri_service->get_entity( $data ); |
|
| 79 | + |
|
| 80 | + // If found, add the reference. |
|
| 81 | + if ( is_a( $post, 'WP_Post' ) ) { |
|
| 82 | + // Add the entity among the references using the post ID. |
|
| 83 | + $references[] = $post->ID; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + return array( "@id" => $data, ); |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | 89 | } |
@@ -31,11 +31,11 @@ discard block |
||
| 31 | 31 | * |
| 32 | 32 | * @param Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
| 33 | 33 | */ |
| 34 | - public function __construct( $entity_uri_service ) { |
|
| 34 | + public function __construct($entity_uri_service) { |
|
| 35 | 35 | |
| 36 | 36 | $this->entity_uri_service = $entity_uri_service; |
| 37 | 37 | |
| 38 | - add_filter( 'wl_mappings_transformation_functions', array( $this, 'wl_mappings_transformation_functions' ) ); |
|
| 38 | + add_filter('wl_mappings_transformation_functions', array($this, 'wl_mappings_transformation_functions')); |
|
| 39 | 39 | |
| 40 | 40 | } |
| 41 | 41 | |
@@ -46,7 +46,7 @@ discard block |
||
| 46 | 46 | * |
| 47 | 47 | * @return Mappings_Transform_Function[] An updated array with ourselves too. |
| 48 | 48 | */ |
| 49 | - public function wl_mappings_transformation_functions( $value ) { |
|
| 49 | + public function wl_mappings_transformation_functions($value) { |
|
| 50 | 50 | |
| 51 | 51 | $value[] = $this; |
| 52 | 52 | |
@@ -66,24 +66,24 @@ discard block |
||
| 66 | 66 | */ |
| 67 | 67 | public function get_label() { |
| 68 | 68 | |
| 69 | - return __( 'URL to Entity', 'wordlift' ); |
|
| 69 | + return __('URL to Entity', 'wordlift'); |
|
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | /** |
| 73 | 73 | * @inheritDoc |
| 74 | 74 | */ |
| 75 | - public function transform_data( $data, $jsonld, &$references, $post_id ) { |
|
| 75 | + public function transform_data($data, $jsonld, &$references, $post_id) { |
|
| 76 | 76 | |
| 77 | 77 | // Get the entity by URI. |
| 78 | - $post = $this->entity_uri_service->get_entity( $data ); |
|
| 78 | + $post = $this->entity_uri_service->get_entity($data); |
|
| 79 | 79 | |
| 80 | 80 | // If found, add the reference. |
| 81 | - if ( is_a( $post, 'WP_Post' ) ) { |
|
| 81 | + if (is_a($post, 'WP_Post')) { |
|
| 82 | 82 | // Add the entity among the references using the post ID. |
| 83 | 83 | $references[] = $post->ID; |
| 84 | 84 | } |
| 85 | 85 | |
| 86 | - return array( "@id" => $data, ); |
|
| 86 | + return array("@id" => $data,); |
|
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | } |
@@ -19,57 +19,57 @@ |
||
| 19 | 19 | class Taxonomy_To_Terms_Transform_Function implements Mappings_Transform_Function { |
| 20 | 20 | |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Taxonomy_To_Terms_Transform_Function constructor. |
|
| 24 | - */ |
|
| 25 | - public function __construct() { |
|
| 22 | + /** |
|
| 23 | + * Taxonomy_To_Terms_Transform_Function constructor. |
|
| 24 | + */ |
|
| 25 | + public function __construct() { |
|
| 26 | 26 | |
| 27 | - add_filter( 'wl_mappings_transformation_functions', array( $this, 'wl_mappings_transformation_functions' ) ); |
|
| 27 | + add_filter( 'wl_mappings_transformation_functions', array( $this, 'wl_mappings_transformation_functions' ) ); |
|
| 28 | 28 | |
| 29 | - } |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Hook to add ourselves to the list of available transform functions. |
|
| 33 | - * |
|
| 34 | - * @param Mappings_Transform_Function[] $value An array of {@link Mappings_Transform_Function}s. |
|
| 35 | - * |
|
| 36 | - * @return Mappings_Transform_Function[] An updated array with ourselves too. |
|
| 37 | - */ |
|
| 38 | - public function wl_mappings_transformation_functions( $value ) { |
|
| 31 | + /** |
|
| 32 | + * Hook to add ourselves to the list of available transform functions. |
|
| 33 | + * |
|
| 34 | + * @param Mappings_Transform_Function[] $value An array of {@link Mappings_Transform_Function}s. |
|
| 35 | + * |
|
| 36 | + * @return Mappings_Transform_Function[] An updated array with ourselves too. |
|
| 37 | + */ |
|
| 38 | + public function wl_mappings_transformation_functions( $value ) { |
|
| 39 | 39 | |
| 40 | - $value[] = $this; |
|
| 40 | + $value[] = $this; |
|
| 41 | 41 | |
| 42 | - return $value; |
|
| 43 | - } |
|
| 42 | + return $value; |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * @inheritDoc |
|
| 47 | - */ |
|
| 48 | - public function get_name() { |
|
| 45 | + /** |
|
| 46 | + * @inheritDoc |
|
| 47 | + */ |
|
| 48 | + public function get_name() { |
|
| 49 | 49 | |
| 50 | - return 'taxonomy_to_terms'; |
|
| 51 | - } |
|
| 50 | + return 'taxonomy_to_terms'; |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * @inheritDoc |
|
| 55 | - */ |
|
| 56 | - public function get_label() { |
|
| 53 | + /** |
|
| 54 | + * @inheritDoc |
|
| 55 | + */ |
|
| 56 | + public function get_label() { |
|
| 57 | 57 | |
| 58 | - return __( 'Taxonomy to Terms', 'wordlift' ); |
|
| 59 | - } |
|
| 58 | + return __( 'Taxonomy to Terms', 'wordlift' ); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * @inheritDoc |
|
| 63 | - */ |
|
| 64 | - public function transform_data( $data, $jsonld, &$references, $post_id ) { |
|
| 61 | + /** |
|
| 62 | + * @inheritDoc |
|
| 63 | + */ |
|
| 64 | + public function transform_data( $data, $jsonld, &$references, $post_id ) { |
|
| 65 | 65 | |
| 66 | - $terms = wp_get_object_terms( $post_id, $data, array( 'fields' => 'names', ) ); |
|
| 66 | + $terms = wp_get_object_terms( $post_id, $data, array( 'fields' => 'names', ) ); |
|
| 67 | 67 | |
| 68 | - if ( ! is_array( $terms ) || empty( $terms ) ) { |
|
| 69 | - return null; |
|
| 70 | - } |
|
| 68 | + if ( ! is_array( $terms ) || empty( $terms ) ) { |
|
| 69 | + return null; |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - return $terms; |
|
| 73 | - } |
|
| 72 | + return $terms; |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | 75 | } |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | */ |
| 25 | 25 | public function __construct() { |
| 26 | 26 | |
| 27 | - add_filter( 'wl_mappings_transformation_functions', array( $this, 'wl_mappings_transformation_functions' ) ); |
|
| 27 | + add_filter('wl_mappings_transformation_functions', array($this, 'wl_mappings_transformation_functions')); |
|
| 28 | 28 | |
| 29 | 29 | } |
| 30 | 30 | |
@@ -35,7 +35,7 @@ discard block |
||
| 35 | 35 | * |
| 36 | 36 | * @return Mappings_Transform_Function[] An updated array with ourselves too. |
| 37 | 37 | */ |
| 38 | - public function wl_mappings_transformation_functions( $value ) { |
|
| 38 | + public function wl_mappings_transformation_functions($value) { |
|
| 39 | 39 | |
| 40 | 40 | $value[] = $this; |
| 41 | 41 | |
@@ -55,17 +55,17 @@ discard block |
||
| 55 | 55 | */ |
| 56 | 56 | public function get_label() { |
| 57 | 57 | |
| 58 | - return __( 'Taxonomy to Terms', 'wordlift' ); |
|
| 58 | + return __('Taxonomy to Terms', 'wordlift'); |
|
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | /** |
| 62 | 62 | * @inheritDoc |
| 63 | 63 | */ |
| 64 | - public function transform_data( $data, $jsonld, &$references, $post_id ) { |
|
| 64 | + public function transform_data($data, $jsonld, &$references, $post_id) { |
|
| 65 | 65 | |
| 66 | - $terms = wp_get_object_terms( $post_id, $data, array( 'fields' => 'names', ) ); |
|
| 66 | + $terms = wp_get_object_terms($post_id, $data, array('fields' => 'names',)); |
|
| 67 | 67 | |
| 68 | - if ( ! is_array( $terms ) || empty( $terms ) ) { |
|
| 68 | + if ( ! is_array($terms) || empty($terms)) { |
|
| 69 | 69 | return null; |
| 70 | 70 | } |
| 71 | 71 | |
@@ -19,70 +19,70 @@ |
||
| 19 | 19 | |
| 20 | 20 | class Scripts_Helper { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * This function loads the javascript file according to the WordPress version. |
|
| 24 | - * |
|
| 25 | - * For WordPress < 5.0 it'll load the javascript file using the `.full` suffix i.e. the file that embeds all the |
|
| 26 | - * dependencies. |
|
| 27 | - * |
|
| 28 | - * For WordPress >= 5.0 it'll load the stripped down js. |
|
| 29 | - * |
|
| 30 | - * @param string $handle The handle name. |
|
| 31 | - * @param string $script_name The full script URL without the `.js` extension. |
|
| 32 | - * @param array $dependencies An array of dependencies to be added only in WordPress > 5.0. |
|
| 33 | - */ |
|
| 34 | - public static function enqueue_based_on_wordpress_version( $handle, $script_name, $dependencies, $in_footer = false ) { |
|
| 35 | - global $wp_version; |
|
| 22 | + /** |
|
| 23 | + * This function loads the javascript file according to the WordPress version. |
|
| 24 | + * |
|
| 25 | + * For WordPress < 5.0 it'll load the javascript file using the `.full` suffix i.e. the file that embeds all the |
|
| 26 | + * dependencies. |
|
| 27 | + * |
|
| 28 | + * For WordPress >= 5.0 it'll load the stripped down js. |
|
| 29 | + * |
|
| 30 | + * @param string $handle The handle name. |
|
| 31 | + * @param string $script_name The full script URL without the `.js` extension. |
|
| 32 | + * @param array $dependencies An array of dependencies to be added only in WordPress > 5.0. |
|
| 33 | + */ |
|
| 34 | + public static function enqueue_based_on_wordpress_version( $handle, $script_name, $dependencies, $in_footer = false ) { |
|
| 35 | + global $wp_version; |
|
| 36 | 36 | |
| 37 | - if ( version_compare( $wp_version, '5.0', '<' ) ) { |
|
| 38 | - $actual_script_name = "$script_name.full.js"; |
|
| 39 | - $actual_dependencies = array(); |
|
| 40 | - } else { |
|
| 41 | - $actual_script_name = "$script_name.js"; |
|
| 42 | - $actual_dependencies = $dependencies; |
|
| 43 | - } |
|
| 37 | + if ( version_compare( $wp_version, '5.0', '<' ) ) { |
|
| 38 | + $actual_script_name = "$script_name.full.js"; |
|
| 39 | + $actual_dependencies = array(); |
|
| 40 | + } else { |
|
| 41 | + $actual_script_name = "$script_name.js"; |
|
| 42 | + $actual_dependencies = $dependencies; |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - $wordlift = \Wordlift::get_instance(); |
|
| 46 | - wp_enqueue_script( $handle, $actual_script_name, $actual_dependencies, $wordlift->get_version(), $in_footer ); |
|
| 45 | + $wordlift = \Wordlift::get_instance(); |
|
| 46 | + wp_enqueue_script( $handle, $actual_script_name, $actual_dependencies, $wordlift->get_version(), $in_footer ); |
|
| 47 | 47 | |
| 48 | - } |
|
| 49 | - /** |
|
| 50 | - * This function registers the javascript file according to the WordPress version. |
|
| 51 | - * |
|
| 52 | - * For WordPress < 5.0 it'll register the javascript file using the `.full` suffix i.e. the file that embeds all the |
|
| 53 | - * dependencies. |
|
| 54 | - * |
|
| 55 | - * For WordPress >= 5.0 it'll register the stripped down js. |
|
| 56 | - * |
|
| 57 | - * @param string $handle The handle name. |
|
| 58 | - * @param string $script_name The full script URL without the `.js` extension. |
|
| 59 | - * @param array $dependencies An array of dependencies to be added only in WordPress > 5.0. |
|
| 60 | - */ |
|
| 61 | - public static function register_based_on_wordpress_version( |
|
| 62 | - $handle, |
|
| 63 | - $script_name, |
|
| 64 | - $dependencies, |
|
| 65 | - $action = 'wp_enqueue_scripts', |
|
| 66 | - $in_footer = false |
|
| 67 | - ) { |
|
| 68 | - global $wp_version; |
|
| 48 | + } |
|
| 49 | + /** |
|
| 50 | + * This function registers the javascript file according to the WordPress version. |
|
| 51 | + * |
|
| 52 | + * For WordPress < 5.0 it'll register the javascript file using the `.full` suffix i.e. the file that embeds all the |
|
| 53 | + * dependencies. |
|
| 54 | + * |
|
| 55 | + * For WordPress >= 5.0 it'll register the stripped down js. |
|
| 56 | + * |
|
| 57 | + * @param string $handle The handle name. |
|
| 58 | + * @param string $script_name The full script URL without the `.js` extension. |
|
| 59 | + * @param array $dependencies An array of dependencies to be added only in WordPress > 5.0. |
|
| 60 | + */ |
|
| 61 | + public static function register_based_on_wordpress_version( |
|
| 62 | + $handle, |
|
| 63 | + $script_name, |
|
| 64 | + $dependencies, |
|
| 65 | + $action = 'wp_enqueue_scripts', |
|
| 66 | + $in_footer = false |
|
| 67 | + ) { |
|
| 68 | + global $wp_version; |
|
| 69 | 69 | |
| 70 | - if ( version_compare( $wp_version, '5.0', '<' ) ) { |
|
| 71 | - $actual_script_name = "$script_name.full.js"; |
|
| 72 | - $actual_dependencies = array(); |
|
| 73 | - } else { |
|
| 74 | - $actual_script_name = "$script_name.js"; |
|
| 75 | - $actual_dependencies = $dependencies; |
|
| 76 | - } |
|
| 70 | + if ( version_compare( $wp_version, '5.0', '<' ) ) { |
|
| 71 | + $actual_script_name = "$script_name.full.js"; |
|
| 72 | + $actual_dependencies = array(); |
|
| 73 | + } else { |
|
| 74 | + $actual_script_name = "$script_name.js"; |
|
| 75 | + $actual_dependencies = $dependencies; |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - $wordlift = \Wordlift::get_instance(); |
|
| 79 | - add_action( |
|
| 80 | - $action, |
|
| 81 | - function () use ( $handle, $actual_script_name, $actual_dependencies, $wordlift, $in_footer ) { |
|
| 82 | - wp_register_script( $handle, $actual_script_name, $actual_dependencies, $wordlift->get_version(), $in_footer ); |
|
| 83 | - } |
|
| 84 | - ); |
|
| 78 | + $wordlift = \Wordlift::get_instance(); |
|
| 79 | + add_action( |
|
| 80 | + $action, |
|
| 81 | + function () use ( $handle, $actual_script_name, $actual_dependencies, $wordlift, $in_footer ) { |
|
| 82 | + wp_register_script( $handle, $actual_script_name, $actual_dependencies, $wordlift->get_version(), $in_footer ); |
|
| 83 | + } |
|
| 84 | + ); |
|
| 85 | 85 | |
| 86 | - } |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | 88 | } |
| 89 | 89 | \ No newline at end of file |
@@ -31,10 +31,10 @@ discard block |
||
| 31 | 31 | * @param string $script_name The full script URL without the `.js` extension. |
| 32 | 32 | * @param array $dependencies An array of dependencies to be added only in WordPress > 5.0. |
| 33 | 33 | */ |
| 34 | - public static function enqueue_based_on_wordpress_version( $handle, $script_name, $dependencies, $in_footer = false ) { |
|
| 34 | + public static function enqueue_based_on_wordpress_version($handle, $script_name, $dependencies, $in_footer = false) { |
|
| 35 | 35 | global $wp_version; |
| 36 | 36 | |
| 37 | - if ( version_compare( $wp_version, '5.0', '<' ) ) { |
|
| 37 | + if (version_compare($wp_version, '5.0', '<')) { |
|
| 38 | 38 | $actual_script_name = "$script_name.full.js"; |
| 39 | 39 | $actual_dependencies = array(); |
| 40 | 40 | } else { |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | $wordlift = \Wordlift::get_instance(); |
| 46 | - wp_enqueue_script( $handle, $actual_script_name, $actual_dependencies, $wordlift->get_version(), $in_footer ); |
|
| 46 | + wp_enqueue_script($handle, $actual_script_name, $actual_dependencies, $wordlift->get_version(), $in_footer); |
|
| 47 | 47 | |
| 48 | 48 | } |
| 49 | 49 | /** |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | ) { |
| 68 | 68 | global $wp_version; |
| 69 | 69 | |
| 70 | - if ( version_compare( $wp_version, '5.0', '<' ) ) { |
|
| 70 | + if (version_compare($wp_version, '5.0', '<')) { |
|
| 71 | 71 | $actual_script_name = "$script_name.full.js"; |
| 72 | 72 | $actual_dependencies = array(); |
| 73 | 73 | } else { |
@@ -78,8 +78,8 @@ discard block |
||
| 78 | 78 | $wordlift = \Wordlift::get_instance(); |
| 79 | 79 | add_action( |
| 80 | 80 | $action, |
| 81 | - function () use ( $handle, $actual_script_name, $actual_dependencies, $wordlift, $in_footer ) { |
|
| 82 | - wp_register_script( $handle, $actual_script_name, $actual_dependencies, $wordlift->get_version(), $in_footer ); |
|
| 81 | + function() use ($handle, $actual_script_name, $actual_dependencies, $wordlift, $in_footer) { |
|
| 82 | + wp_register_script($handle, $actual_script_name, $actual_dependencies, $wordlift->get_version(), $in_footer); |
|
| 83 | 83 | } |
| 84 | 84 | ); |
| 85 | 85 | |
@@ -17,33 +17,33 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class Templates_Ajax_Endpoint { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Templates_Ajax_Endpoint constructor. |
|
| 22 | - * |
|
| 23 | - * Hook to `wl_templates` Ajax action. |
|
| 24 | - */ |
|
| 25 | - public function __construct() { |
|
| 20 | + /** |
|
| 21 | + * Templates_Ajax_Endpoint constructor. |
|
| 22 | + * |
|
| 23 | + * Hook to `wl_templates` Ajax action. |
|
| 24 | + */ |
|
| 25 | + public function __construct() { |
|
| 26 | 26 | |
| 27 | - add_action( 'wp_ajax_wl_templates', array( $this, 'template' ) ); |
|
| 27 | + add_action( 'wp_ajax_wl_templates', array( $this, 'template' ) ); |
|
| 28 | 28 | |
| 29 | - } |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Display the requested template. The template is searched in wp-content/wordlift/templates/wordlift-widget-be/ |
|
| 33 | - * |
|
| 34 | - * Non alphanumeric names (including `-`) are considered invalid. |
|
| 35 | - */ |
|
| 36 | - public function template() { |
|
| 31 | + /** |
|
| 32 | + * Display the requested template. The template is searched in wp-content/wordlift/templates/wordlift-widget-be/ |
|
| 33 | + * |
|
| 34 | + * Non alphanumeric names (including `-`) are considered invalid. |
|
| 35 | + */ |
|
| 36 | + public function template() { |
|
| 37 | 37 | |
| 38 | - $name = filter_input( INPUT_GET, 'name' ); |
|
| 38 | + $name = filter_input( INPUT_GET, 'name' ); |
|
| 39 | 39 | |
| 40 | - if ( 1 !== preg_match( '|^[a-z0-9\-]+$|', $name ) ) { |
|
| 41 | - return wp_send_json_error( 'Invalid name.' ); |
|
| 42 | - } |
|
| 40 | + if ( 1 !== preg_match( '|^[a-z0-9\-]+$|', $name ) ) { |
|
| 41 | + return wp_send_json_error( 'Invalid name.' ); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - require( dirname( dirname( dirname( __FILE__ ) ) ) . "/templates/wordlift-widget-be/$name.html" ); |
|
| 44 | + require( dirname( dirname( dirname( __FILE__ ) ) ) . "/templates/wordlift-widget-be/$name.html" ); |
|
| 45 | 45 | |
| 46 | - die(); |
|
| 47 | - } |
|
| 46 | + die(); |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | 49 | } |
| 50 | 50 | \ No newline at end of file |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | */ |
| 25 | 25 | public function __construct() { |
| 26 | 26 | |
| 27 | - add_action( 'wp_ajax_wl_templates', array( $this, 'template' ) ); |
|
| 27 | + add_action('wp_ajax_wl_templates', array($this, 'template')); |
|
| 28 | 28 | |
| 29 | 29 | } |
| 30 | 30 | |
@@ -35,13 +35,13 @@ discard block |
||
| 35 | 35 | */ |
| 36 | 36 | public function template() { |
| 37 | 37 | |
| 38 | - $name = filter_input( INPUT_GET, 'name' ); |
|
| 38 | + $name = filter_input(INPUT_GET, 'name'); |
|
| 39 | 39 | |
| 40 | - if ( 1 !== preg_match( '|^[a-z0-9\-]+$|', $name ) ) { |
|
| 41 | - return wp_send_json_error( 'Invalid name.' ); |
|
| 40 | + if (1 !== preg_match('|^[a-z0-9\-]+$|', $name)) { |
|
| 41 | + return wp_send_json_error('Invalid name.'); |
|
| 42 | 42 | } |
| 43 | 43 | |
| 44 | - require( dirname( dirname( dirname( __FILE__ ) ) ) . "/templates/wordlift-widget-be/$name.html" ); |
|
| 44 | + require(dirname(dirname(dirname(__FILE__)))."/templates/wordlift-widget-be/$name.html"); |
|
| 45 | 45 | |
| 46 | 46 | die(); |
| 47 | 47 | } |