@@ -17,58 +17,58 @@ |
||
17 | 17 | */ |
18 | 18 | class Taxonomy_Term_Rule_Validator implements Rule_Validator { |
19 | 19 | |
20 | - const TAXONOMY = 'taxonomy'; |
|
20 | + const TAXONOMY = 'taxonomy'; |
|
21 | 21 | |
22 | - public function __construct() { |
|
23 | - add_filter( 'wl_mappings_rule_validators', array( $this, 'wl_mappings_rule_validators' ) ); |
|
24 | - } |
|
22 | + public function __construct() { |
|
23 | + add_filter( 'wl_mappings_rule_validators', array( $this, 'wl_mappings_rule_validators' ) ); |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * Hook to `wl_mappings_rule_validators` to register ourselves. |
|
28 | - * |
|
29 | - * @param array $value An array with validators. |
|
30 | - * |
|
31 | - * @return array An array with validators plus ours. |
|
32 | - */ |
|
33 | - public function wl_mappings_rule_validators( $value ) { |
|
34 | - $value[ self::TAXONOMY ] = $this; |
|
26 | + /** |
|
27 | + * Hook to `wl_mappings_rule_validators` to register ourselves. |
|
28 | + * |
|
29 | + * @param array $value An array with validators. |
|
30 | + * |
|
31 | + * @return array An array with validators plus ours. |
|
32 | + */ |
|
33 | + public function wl_mappings_rule_validators( $value ) { |
|
34 | + $value[ self::TAXONOMY ] = $this; |
|
35 | 35 | |
36 | - return $value; |
|
37 | - } |
|
36 | + return $value; |
|
37 | + } |
|
38 | 38 | |
39 | - public function get_label() { |
|
40 | - return __( 'TaxonomyTerm', 'wordlift' ); |
|
41 | - } |
|
39 | + public function get_label() { |
|
40 | + return __( 'TaxonomyTerm', 'wordlift' ); |
|
41 | + } |
|
42 | 42 | |
43 | - public function is_valid( $identifier, $operator, $operand_1, $taxonomy, $type ) { |
|
44 | - if ( Jsonld_Converter::TERM !== $type ) { |
|
45 | - return false; |
|
46 | - } |
|
47 | - $current_term = get_term( $identifier ); |
|
48 | - // If it is not a term page, then return false for two operators. |
|
49 | - if ( ! $current_term instanceof \WP_Term ) { |
|
50 | - return false; |
|
51 | - } |
|
52 | - $terms = get_terms( $taxonomy, array( 'get' => 'all' ) ); |
|
53 | - $terms = array_map( |
|
54 | - function ( $term ) { |
|
55 | - /** |
|
56 | - * @var $term \WP_Term |
|
57 | - */ |
|
58 | - return $term->term_id; |
|
59 | - }, |
|
60 | - $terms |
|
61 | - ); |
|
62 | - if ( Rule_Validator::IS_EQUAL_TO === $operator ) { |
|
63 | - // if we dont have term id, then skip the flow. |
|
64 | - // If we are in term page, then we need to check if the current |
|
65 | - // term belongs to the taxonomy |
|
66 | - return in_array( $current_term->term_id, $terms, true ); |
|
67 | - } |
|
43 | + public function is_valid( $identifier, $operator, $operand_1, $taxonomy, $type ) { |
|
44 | + if ( Jsonld_Converter::TERM !== $type ) { |
|
45 | + return false; |
|
46 | + } |
|
47 | + $current_term = get_term( $identifier ); |
|
48 | + // If it is not a term page, then return false for two operators. |
|
49 | + if ( ! $current_term instanceof \WP_Term ) { |
|
50 | + return false; |
|
51 | + } |
|
52 | + $terms = get_terms( $taxonomy, array( 'get' => 'all' ) ); |
|
53 | + $terms = array_map( |
|
54 | + function ( $term ) { |
|
55 | + /** |
|
56 | + * @var $term \WP_Term |
|
57 | + */ |
|
58 | + return $term->term_id; |
|
59 | + }, |
|
60 | + $terms |
|
61 | + ); |
|
62 | + if ( Rule_Validator::IS_EQUAL_TO === $operator ) { |
|
63 | + // if we dont have term id, then skip the flow. |
|
64 | + // If we are in term page, then we need to check if the current |
|
65 | + // term belongs to the taxonomy |
|
66 | + return in_array( $current_term->term_id, $terms, true ); |
|
67 | + } |
|
68 | 68 | |
69 | - if ( Rule_Validator::IS_NOT_EQUAL_TO === $operator ) { |
|
70 | - return ! in_array( $current_term->term_id, $terms, true ); |
|
71 | - } |
|
69 | + if ( Rule_Validator::IS_NOT_EQUAL_TO === $operator ) { |
|
70 | + return ! in_array( $current_term->term_id, $terms, true ); |
|
71 | + } |
|
72 | 72 | |
73 | - } |
|
73 | + } |
|
74 | 74 | } |
@@ -20,7 +20,7 @@ discard block |
||
20 | 20 | const TAXONOMY = 'taxonomy'; |
21 | 21 | |
22 | 22 | public function __construct() { |
23 | - add_filter( 'wl_mappings_rule_validators', array( $this, 'wl_mappings_rule_validators' ) ); |
|
23 | + add_filter('wl_mappings_rule_validators', array($this, 'wl_mappings_rule_validators')); |
|
24 | 24 | } |
25 | 25 | |
26 | 26 | /** |
@@ -30,28 +30,28 @@ discard block |
||
30 | 30 | * |
31 | 31 | * @return array An array with validators plus ours. |
32 | 32 | */ |
33 | - public function wl_mappings_rule_validators( $value ) { |
|
34 | - $value[ self::TAXONOMY ] = $this; |
|
33 | + public function wl_mappings_rule_validators($value) { |
|
34 | + $value[self::TAXONOMY] = $this; |
|
35 | 35 | |
36 | 36 | return $value; |
37 | 37 | } |
38 | 38 | |
39 | 39 | public function get_label() { |
40 | - return __( 'TaxonomyTerm', 'wordlift' ); |
|
40 | + return __('TaxonomyTerm', 'wordlift'); |
|
41 | 41 | } |
42 | 42 | |
43 | - public function is_valid( $identifier, $operator, $operand_1, $taxonomy, $type ) { |
|
44 | - if ( Jsonld_Converter::TERM !== $type ) { |
|
43 | + public function is_valid($identifier, $operator, $operand_1, $taxonomy, $type) { |
|
44 | + if (Jsonld_Converter::TERM !== $type) { |
|
45 | 45 | return false; |
46 | 46 | } |
47 | - $current_term = get_term( $identifier ); |
|
47 | + $current_term = get_term($identifier); |
|
48 | 48 | // If it is not a term page, then return false for two operators. |
49 | - if ( ! $current_term instanceof \WP_Term ) { |
|
49 | + if ( ! $current_term instanceof \WP_Term) { |
|
50 | 50 | return false; |
51 | 51 | } |
52 | - $terms = get_terms( $taxonomy, array( 'get' => 'all' ) ); |
|
52 | + $terms = get_terms($taxonomy, array('get' => 'all')); |
|
53 | 53 | $terms = array_map( |
54 | - function ( $term ) { |
|
54 | + function($term) { |
|
55 | 55 | /** |
56 | 56 | * @var $term \WP_Term |
57 | 57 | */ |
@@ -59,15 +59,15 @@ discard block |
||
59 | 59 | }, |
60 | 60 | $terms |
61 | 61 | ); |
62 | - if ( Rule_Validator::IS_EQUAL_TO === $operator ) { |
|
62 | + if (Rule_Validator::IS_EQUAL_TO === $operator) { |
|
63 | 63 | // if we dont have term id, then skip the flow. |
64 | 64 | // If we are in term page, then we need to check if the current |
65 | 65 | // term belongs to the taxonomy |
66 | - return in_array( $current_term->term_id, $terms, true ); |
|
66 | + return in_array($current_term->term_id, $terms, true); |
|
67 | 67 | } |
68 | 68 | |
69 | - if ( Rule_Validator::IS_NOT_EQUAL_TO === $operator ) { |
|
70 | - return ! in_array( $current_term->term_id, $terms, true ); |
|
69 | + if (Rule_Validator::IS_NOT_EQUAL_TO === $operator) { |
|
70 | + return ! in_array($current_term->term_id, $terms, true); |
|
71 | 71 | } |
72 | 72 | |
73 | 73 | } |
@@ -16,29 +16,29 @@ |
||
16 | 16 | */ |
17 | 17 | class Taxonomy_Rule_Validator implements Rule_Validator { |
18 | 18 | |
19 | - /** |
|
20 | - * {@inheritdoc} |
|
21 | - */ |
|
22 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
23 | - public function is_valid( $identifier, $operator, $operand_1, $operand_2, $type ) { |
|
24 | - |
|
25 | - $taxonomy = $operand_1; |
|
26 | - $term_slug = $operand_2; |
|
27 | - |
|
28 | - $is_object_in_term = is_object_in_term( $identifier, $taxonomy, $term_slug ); |
|
29 | - |
|
30 | - if ( is_wp_error( $is_object_in_term ) ) { |
|
31 | - return false; |
|
32 | - } |
|
33 | - |
|
34 | - return ( $is_object_in_term && self::IS_EQUAL_TO === $operator ) |
|
35 | - || ( ! $is_object_in_term && self::IS_NOT_EQUAL_TO === $operator ); |
|
36 | - } |
|
37 | - |
|
38 | - /** |
|
39 | - * {@inheritdoc} |
|
40 | - */ |
|
41 | - public function get_label() { |
|
42 | - return __( 'Taxonomy', 'wordlift' ); |
|
43 | - } |
|
19 | + /** |
|
20 | + * {@inheritdoc} |
|
21 | + */ |
|
22 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
23 | + public function is_valid( $identifier, $operator, $operand_1, $operand_2, $type ) { |
|
24 | + |
|
25 | + $taxonomy = $operand_1; |
|
26 | + $term_slug = $operand_2; |
|
27 | + |
|
28 | + $is_object_in_term = is_object_in_term( $identifier, $taxonomy, $term_slug ); |
|
29 | + |
|
30 | + if ( is_wp_error( $is_object_in_term ) ) { |
|
31 | + return false; |
|
32 | + } |
|
33 | + |
|
34 | + return ( $is_object_in_term && self::IS_EQUAL_TO === $operator ) |
|
35 | + || ( ! $is_object_in_term && self::IS_NOT_EQUAL_TO === $operator ); |
|
36 | + } |
|
37 | + |
|
38 | + /** |
|
39 | + * {@inheritdoc} |
|
40 | + */ |
|
41 | + public function get_label() { |
|
42 | + return __( 'Taxonomy', 'wordlift' ); |
|
43 | + } |
|
44 | 44 | } |
@@ -20,25 +20,25 @@ |
||
20 | 20 | * {@inheritdoc} |
21 | 21 | */ |
22 | 22 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
23 | - public function is_valid( $identifier, $operator, $operand_1, $operand_2, $type ) { |
|
23 | + public function is_valid($identifier, $operator, $operand_1, $operand_2, $type) { |
|
24 | 24 | |
25 | 25 | $taxonomy = $operand_1; |
26 | 26 | $term_slug = $operand_2; |
27 | 27 | |
28 | - $is_object_in_term = is_object_in_term( $identifier, $taxonomy, $term_slug ); |
|
28 | + $is_object_in_term = is_object_in_term($identifier, $taxonomy, $term_slug); |
|
29 | 29 | |
30 | - if ( is_wp_error( $is_object_in_term ) ) { |
|
30 | + if (is_wp_error($is_object_in_term)) { |
|
31 | 31 | return false; |
32 | 32 | } |
33 | 33 | |
34 | - return ( $is_object_in_term && self::IS_EQUAL_TO === $operator ) |
|
35 | - || ( ! $is_object_in_term && self::IS_NOT_EQUAL_TO === $operator ); |
|
34 | + return ($is_object_in_term && self::IS_EQUAL_TO === $operator) |
|
35 | + || ( ! $is_object_in_term && self::IS_NOT_EQUAL_TO === $operator); |
|
36 | 36 | } |
37 | 37 | |
38 | 38 | /** |
39 | 39 | * {@inheritdoc} |
40 | 40 | */ |
41 | 41 | public function get_label() { |
42 | - return __( 'Taxonomy', 'wordlift' ); |
|
42 | + return __('Taxonomy', 'wordlift'); |
|
43 | 43 | } |
44 | 44 | } |
@@ -10,23 +10,23 @@ |
||
10 | 10 | */ |
11 | 11 | class Acf_Group_Formatter { |
12 | 12 | |
13 | - public function __construct() { |
|
14 | - // Hook in to format value filter and apply format value. |
|
15 | - add_filter( 'wl_mapping_acf_format_value', array( $this, 'format_group_value' ), 10, 2 ); |
|
16 | - } |
|
13 | + public function __construct() { |
|
14 | + // Hook in to format value filter and apply format value. |
|
15 | + add_filter( 'wl_mapping_acf_format_value', array( $this, 'format_group_value' ), 10, 2 ); |
|
16 | + } |
|
17 | 17 | |
18 | - public function format_group_value( $field_data, $field_type ) { |
|
19 | - if ( 'group' !== $field_type ) { |
|
20 | - // Return early if the field type is not group |
|
21 | - return $field_data; |
|
22 | - } |
|
23 | - // we need to check if atleast one key is present. |
|
24 | - $filtered_group_data = array_filter( $field_data, 'strlen' ); |
|
25 | - if ( ! $filtered_group_data ) { |
|
26 | - return false; |
|
27 | - } |
|
18 | + public function format_group_value( $field_data, $field_type ) { |
|
19 | + if ( 'group' !== $field_type ) { |
|
20 | + // Return early if the field type is not group |
|
21 | + return $field_data; |
|
22 | + } |
|
23 | + // we need to check if atleast one key is present. |
|
24 | + $filtered_group_data = array_filter( $field_data, 'strlen' ); |
|
25 | + if ( ! $filtered_group_data ) { |
|
26 | + return false; |
|
27 | + } |
|
28 | 28 | |
29 | - return $filtered_group_data; |
|
29 | + return $filtered_group_data; |
|
30 | 30 | |
31 | - } |
|
31 | + } |
|
32 | 32 | } |
@@ -12,17 +12,17 @@ |
||
12 | 12 | |
13 | 13 | public function __construct() { |
14 | 14 | // Hook in to format value filter and apply format value. |
15 | - add_filter( 'wl_mapping_acf_format_value', array( $this, 'format_group_value' ), 10, 2 ); |
|
15 | + add_filter('wl_mapping_acf_format_value', array($this, 'format_group_value'), 10, 2); |
|
16 | 16 | } |
17 | 17 | |
18 | - public function format_group_value( $field_data, $field_type ) { |
|
19 | - if ( 'group' !== $field_type ) { |
|
18 | + public function format_group_value($field_data, $field_type) { |
|
19 | + if ('group' !== $field_type) { |
|
20 | 20 | // Return early if the field type is not group |
21 | 21 | return $field_data; |
22 | 22 | } |
23 | 23 | // we need to check if atleast one key is present. |
24 | - $filtered_group_data = array_filter( $field_data, 'strlen' ); |
|
25 | - if ( ! $filtered_group_data ) { |
|
24 | + $filtered_group_data = array_filter($field_data, 'strlen'); |
|
25 | + if ( ! $filtered_group_data) { |
|
26 | 26 | return false; |
27 | 27 | } |
28 | 28 |
@@ -12,110 +12,110 @@ |
||
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 | - * |
|
78 | - * @return void |
|
79 | - */ |
|
80 | - private function sync_transformation_functions_from_external_plugins() { |
|
81 | - $this->transform_function_array = apply_filters( |
|
82 | - 'wl_mappings_transformation_functions', |
|
83 | - $this->transform_function_array |
|
84 | - ); |
|
85 | - $this->remove_duplicate_transformation_functions_after_sync(); |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * @return int|void Returns the number of transformation functions present in registry. |
|
90 | - */ |
|
91 | - public function get_transform_function_count() { |
|
92 | - return count( $this->transform_function_array ); |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * Check if any duplicate transformation functions are present in registry, run this |
|
97 | - * function after syncing with the registry. |
|
98 | - */ |
|
99 | - private function remove_duplicate_transformation_functions_after_sync() { |
|
100 | - /** |
|
101 | - * This check is done to avoid transformation function conflicts if they share a same name |
|
102 | - * For example if plugin A and plugin B registers transformation function with same name C, |
|
103 | - * we remove the duplicated transformation function |
|
104 | - */ |
|
105 | - $transformation_function_names = array(); |
|
106 | - $transformation_functions = array(); |
|
107 | - foreach ( $this->transform_function_array as $transformation_function ) { |
|
108 | - if ( ! in_array( $transformation_function->get_name(), $transformation_function_names, true ) ) { |
|
109 | - array_push( $transformation_function_names, $transformation_function->get_name() ); |
|
110 | - array_push( $transformation_functions, $transformation_function ); |
|
111 | - } |
|
112 | - } |
|
113 | - $this->transform_function_array = $transformation_functions; |
|
114 | - } |
|
115 | - |
|
116 | - public function get_transforms() { |
|
117 | - |
|
118 | - return $this->transform_function_array; |
|
119 | - } |
|
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 | + * |
|
78 | + * @return void |
|
79 | + */ |
|
80 | + private function sync_transformation_functions_from_external_plugins() { |
|
81 | + $this->transform_function_array = apply_filters( |
|
82 | + 'wl_mappings_transformation_functions', |
|
83 | + $this->transform_function_array |
|
84 | + ); |
|
85 | + $this->remove_duplicate_transformation_functions_after_sync(); |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * @return int|void Returns the number of transformation functions present in registry. |
|
90 | + */ |
|
91 | + public function get_transform_function_count() { |
|
92 | + return count( $this->transform_function_array ); |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * Check if any duplicate transformation functions are present in registry, run this |
|
97 | + * function after syncing with the registry. |
|
98 | + */ |
|
99 | + private function remove_duplicate_transformation_functions_after_sync() { |
|
100 | + /** |
|
101 | + * This check is done to avoid transformation function conflicts if they share a same name |
|
102 | + * For example if plugin A and plugin B registers transformation function with same name C, |
|
103 | + * we remove the duplicated transformation function |
|
104 | + */ |
|
105 | + $transformation_function_names = array(); |
|
106 | + $transformation_functions = array(); |
|
107 | + foreach ( $this->transform_function_array as $transformation_function ) { |
|
108 | + if ( ! in_array( $transformation_function->get_name(), $transformation_function_names, true ) ) { |
|
109 | + array_push( $transformation_function_names, $transformation_function->get_name() ); |
|
110 | + array_push( $transformation_functions, $transformation_function ); |
|
111 | + } |
|
112 | + } |
|
113 | + $this->transform_function_array = $transformation_functions; |
|
114 | + } |
|
115 | + |
|
116 | + public function get_transforms() { |
|
117 | + |
|
118 | + return $this->transform_function_array; |
|
119 | + } |
|
120 | 120 | |
121 | 121 | } |
@@ -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 | } |
@@ -89,7 +89,7 @@ discard block |
||
89 | 89 | * @return int|void Returns the number of transformation functions present in registry. |
90 | 90 | */ |
91 | 91 | public function get_transform_function_count() { |
92 | - return count( $this->transform_function_array ); |
|
92 | + return count($this->transform_function_array); |
|
93 | 93 | } |
94 | 94 | |
95 | 95 | /** |
@@ -104,10 +104,10 @@ discard block |
||
104 | 104 | */ |
105 | 105 | $transformation_function_names = array(); |
106 | 106 | $transformation_functions = array(); |
107 | - foreach ( $this->transform_function_array as $transformation_function ) { |
|
108 | - if ( ! in_array( $transformation_function->get_name(), $transformation_function_names, true ) ) { |
|
109 | - array_push( $transformation_function_names, $transformation_function->get_name() ); |
|
110 | - array_push( $transformation_functions, $transformation_function ); |
|
107 | + foreach ($this->transform_function_array as $transformation_function) { |
|
108 | + if ( ! in_array($transformation_function->get_name(), $transformation_function_names, true)) { |
|
109 | + array_push($transformation_function_names, $transformation_function->get_name()); |
|
110 | + array_push($transformation_functions, $transformation_function); |
|
111 | 111 | } |
112 | 112 | } |
113 | 113 | $this->transform_function_array = $transformation_functions; |
@@ -13,540 +13,540 @@ |
||
13 | 13 | * @subpackage Wordlift/includes/sync-mappings |
14 | 14 | */ |
15 | 15 | class Mappings_REST_Controller { |
16 | - // Namespace for CRUD mappings. |
|
17 | - const MAPPINGS_NAMESPACE = '/mappings'; |
|
18 | - |
|
19 | - /** |
|
20 | - * Registers route on rest api initialisation. |
|
21 | - */ |
|
22 | - public static function register_routes() { |
|
23 | - |
|
24 | - add_action( 'rest_api_init', 'Wordlift\Mappings\Mappings_REST_Controller::register_route_callback' ); |
|
25 | - |
|
26 | - } |
|
27 | - |
|
28 | - /** |
|
29 | - * Get a single mapping item by its mapping_id |
|
30 | - * |
|
31 | - * @param WP_REST_Request $request {@link WP_REST_Request instance}. |
|
32 | - * |
|
33 | - * @return array |
|
34 | - */ |
|
35 | - public static function get_mapping_item( $request ) { |
|
36 | - $dbo = new Mappings_DBO(); |
|
37 | - $mapping_id = $request['id']; |
|
38 | - $mapping_id_data = array(); |
|
39 | - $rule_groups = $dbo->get_rule_groups_by_mapping( $mapping_id ); |
|
40 | - $properties = $dbo->get_properties( $mapping_id ); |
|
41 | - $mapping_row = $dbo->get_mapping_item_data( $mapping_id ); |
|
42 | - |
|
43 | - $mapping_id_data['mapping_id'] = $mapping_id; |
|
44 | - $mapping_id_data['property_list'] = $properties; |
|
45 | - $mapping_id_data['rule_group_list'] = $rule_groups; |
|
46 | - $mapping_id_data['mapping_title'] = $mapping_row['mapping_title']; |
|
47 | - |
|
48 | - return $mapping_id_data; |
|
49 | - } |
|
50 | - |
|
51 | - /** |
|
52 | - * Register route call back function, called when rest api gets initialised |
|
53 | - * |
|
54 | - * @return void |
|
55 | - */ |
|
56 | - public static function register_route_callback() { |
|
57 | - register_rest_route( |
|
58 | - WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
59 | - '/mappings', |
|
60 | - array( |
|
61 | - 'methods' => WP_REST_Server::CREATABLE, |
|
62 | - 'callback' => 'Wordlift\Mappings\Mappings_REST_Controller::insert_or_update_mapping_item', |
|
63 | - 'permission_callback' => function () { |
|
64 | - return current_user_can( 'manage_options' ); |
|
65 | - }, |
|
66 | - ) |
|
67 | - ); |
|
68 | - // Get list of mapping items. |
|
69 | - register_rest_route( |
|
70 | - WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
71 | - '/mappings', |
|
72 | - array( |
|
73 | - 'methods' => WP_REST_Server::READABLE, |
|
74 | - 'callback' => 'Wordlift\Mappings\Mappings_REST_Controller::list_mapping_items', |
|
75 | - 'permission_callback' => function () { |
|
76 | - return current_user_can( 'manage_options' ); |
|
77 | - }, |
|
78 | - ) |
|
79 | - ); |
|
80 | - |
|
81 | - // Delete mapping items by id. |
|
82 | - register_rest_route( |
|
83 | - WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
84 | - 'mappings', |
|
85 | - array( |
|
86 | - 'methods' => WP_REST_Server::DELETABLE, |
|
87 | - 'callback' => 'Wordlift\Mappings\Mappings_REST_Controller::delete_mapping_items', |
|
88 | - 'permission_callback' => function () { |
|
89 | - return current_user_can( 'manage_options' ); |
|
90 | - }, |
|
91 | - ) |
|
92 | - ); |
|
93 | - |
|
94 | - // Get single mapping item route. |
|
95 | - register_rest_route( |
|
96 | - WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
97 | - 'mappings/(?P<id>\d+)', |
|
98 | - array( |
|
99 | - 'methods' => WP_REST_Server::READABLE, |
|
100 | - 'callback' => 'Wordlift\Mappings\Mappings_REST_Controller::get_mapping_item', |
|
101 | - 'permission_callback' => function () { |
|
102 | - return current_user_can( 'manage_options' ); |
|
103 | - }, |
|
104 | - ) |
|
105 | - ); |
|
106 | - |
|
107 | - // Update mapping items. |
|
108 | - register_rest_route( |
|
109 | - WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
110 | - 'mappings', |
|
111 | - array( |
|
112 | - 'methods' => WP_REST_Server::EDITABLE, |
|
113 | - 'callback' => 'Wordlift\Mappings\Mappings_REST_Controller::update_mapping_items', |
|
114 | - 'permission_callback' => function () { |
|
115 | - return current_user_can( 'manage_options' ); |
|
116 | - }, |
|
117 | - ) |
|
118 | - ); |
|
119 | - |
|
120 | - // Clone mapping items. |
|
121 | - register_rest_route( |
|
122 | - WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
123 | - 'mappings/clone', |
|
124 | - array( |
|
125 | - 'methods' => WP_REST_Server::CREATABLE, |
|
126 | - 'callback' => 'Wordlift\Mappings\Mappings_REST_Controller::clone_mapping_items', |
|
127 | - 'permission_callback' => function () { |
|
128 | - return current_user_can( 'manage_options' ); |
|
129 | - }, |
|
130 | - ) |
|
131 | - ); |
|
132 | - |
|
133 | - // Register rest endpoint to get the terms. |
|
134 | - register_rest_route( |
|
135 | - WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
136 | - 'mappings/get_terms', |
|
137 | - array( |
|
138 | - 'methods' => WP_REST_Server::CREATABLE, |
|
139 | - 'callback' => 'Wordlift\Mappings\Mappings_REST_Controller::get_terms_for_the_posted_taxonomy', |
|
140 | - 'permission_callback' => function () { |
|
141 | - return current_user_can( 'manage_options' ); |
|
142 | - }, |
|
143 | - ) |
|
144 | - ); |
|
145 | - |
|
146 | - // Register rest endpoint to get the terms. |
|
147 | - register_rest_route( |
|
148 | - WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
149 | - 'mappings/get_taxonomy_terms', |
|
150 | - array( |
|
151 | - 'methods' => WP_REST_Server::CREATABLE, |
|
152 | - 'callback' => 'Wordlift\Mappings\Mappings_REST_Controller::get_taxonomy_terms_for_the_posted_taxonomy', |
|
153 | - 'permission_callback' => function () { |
|
154 | - return current_user_can( 'manage_options' ); |
|
155 | - }, |
|
156 | - ) |
|
157 | - ); |
|
158 | - } |
|
159 | - |
|
160 | - /** |
|
161 | - * Get the taxonomy & terms |
|
162 | - * |
|
163 | - * @param WP_REST_Request $request {@link WP_REST_Request instance}. |
|
164 | - * |
|
165 | - * @return array The array of the taxonomies & terms. |
|
166 | - */ |
|
167 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
168 | - public static function get_taxonomy_terms_for_the_posted_taxonomy( $request ) { |
|
169 | - $taxonomy_terms = array(); |
|
170 | - $post_taxonomies = get_taxonomies( array(), 'objects' ); |
|
171 | - |
|
172 | - foreach ( $post_taxonomies as $post_taxonomy ) { |
|
173 | - $taxonomy_config = array( |
|
174 | - 'taxonomy' => $post_taxonomy->name, |
|
175 | - 'hide_empty' => false, |
|
176 | - ); |
|
177 | - |
|
178 | - $total_terms = wp_count_terms( $taxonomy_config ); |
|
179 | - |
|
180 | - $post_taxonomy_terms = get_terms( $taxonomy_config ); |
|
181 | - |
|
182 | - if ( $total_terms ) { |
|
183 | - $group_taxonomy = array( |
|
184 | - 'parentValue' => 'post_taxonomy', |
|
185 | - 'group_name' => $post_taxonomy->label, |
|
186 | - 'group_options' => array(), |
|
187 | - ); |
|
188 | - |
|
189 | - foreach ( $post_taxonomy_terms as $post_taxonomy_term ) { |
|
190 | - array_push( |
|
191 | - $group_taxonomy['group_options'], |
|
192 | - array( |
|
193 | - 'label' => ' - ' . $post_taxonomy_term->name, |
|
194 | - 'value' => $post_taxonomy_term->slug, |
|
195 | - 'taxonomy' => 'post_taxonomy', |
|
196 | - ) |
|
197 | - ); |
|
198 | - |
|
199 | - $post_term_children = get_term_children( $post_taxonomy_term->term_id, $post_taxonomy->name ); |
|
200 | - |
|
201 | - foreach ( $post_term_children as $post_term_child ) { |
|
202 | - $child_term = get_term_by( 'id', $post_term_child, $post_taxonomy->name ); |
|
203 | - |
|
204 | - array_push( |
|
205 | - $group_taxonomy['group_options'], |
|
206 | - array( |
|
207 | - 'label' => ' -- ' . $child_term->name, |
|
208 | - 'value' => $child_term->slug, |
|
209 | - 'taxonomy' => 'post_taxonomy', |
|
210 | - ) |
|
211 | - ); |
|
212 | - } |
|
213 | - } |
|
214 | - array_push( $taxonomy_terms, $group_taxonomy ); |
|
215 | - } |
|
216 | - } |
|
217 | - |
|
218 | - return $taxonomy_terms; |
|
219 | - } |
|
220 | - |
|
221 | - /** |
|
222 | - * Get the terms for the posted taxonomy name. |
|
223 | - * |
|
224 | - * @param WP_REST_Request $request {@link WP_REST_Request instance}. |
|
225 | - * |
|
226 | - * @return array The array of the terms for the taxonomy. |
|
227 | - */ |
|
228 | - public static function get_terms_for_the_posted_taxonomy( $request ) { |
|
229 | - $post_data = $request->get_params(); |
|
230 | - if ( ! array_key_exists( 'taxonomy', $post_data ) ) { |
|
231 | - return array( |
|
232 | - 'status' => 'failure', |
|
233 | - 'message' => __( 'Request not valid, must post a taxonomy to get terms', 'wordlift' ), |
|
234 | - ); |
|
235 | - } else { |
|
236 | - $taxonomy = $post_data['taxonomy']; |
|
237 | - $terms = get_terms( $taxonomy, array( 'hide_empty' => false ) ); |
|
238 | - if ( is_wp_error( $terms ) ) { |
|
239 | - // Return error response, if the taxonomy is not valid. |
|
240 | - return array( |
|
241 | - 'status' => 'failure', |
|
242 | - 'message' => __( 'Request not valid, must post a valid taxonomy', 'wordlift' ), |
|
243 | - ); |
|
244 | - } |
|
245 | - |
|
246 | - return $terms; |
|
247 | - } |
|
248 | - } |
|
249 | - |
|
250 | - /** |
|
251 | - * Clone posted mapping items. |
|
252 | - * |
|
253 | - * @param WP_REST_Request $request {@link WP_REST_Request instance}. |
|
254 | - * |
|
255 | - * @return array |
|
256 | - */ |
|
257 | - public static function clone_mapping_items( $request ) { |
|
258 | - $dbo = new Mappings_DBO(); |
|
259 | - $post_data = (array) $request->get_params(); |
|
260 | - $mapping_items = (array) $post_data['mapping_items']; |
|
261 | - foreach ( $mapping_items as $mapping_item ) { |
|
262 | - $mapping_id = (int) $mapping_item['mapping_id']; |
|
263 | - // Clone the current mapping item. |
|
264 | - $cloned_mapping_id = $dbo->insert_mapping_item( $mapping_item['mapping_title'] ); |
|
265 | - // Clone all the rule groups. |
|
266 | - $rule_groups_to_be_cloned = $dbo->get_rule_groups_by_mapping( $mapping_id ); |
|
267 | - // Clone all the properties. |
|
268 | - $properties_to_be_cloned = $dbo->get_properties( $mapping_id ); |
|
269 | - foreach ( $properties_to_be_cloned as $property ) { |
|
270 | - // Assign a new mapping id. |
|
271 | - $property['mapping_id'] = $cloned_mapping_id; |
|
272 | - // Removing this property id, since a new id needed to be created for |
|
273 | - // new property. |
|
274 | - unset( $property['property_id'] ); |
|
275 | - $dbo->insert_or_update_property( $property ); |
|
276 | - } |
|
277 | - // Loop through the rule groups and insert them in table with the mapping id. |
|
278 | - foreach ( $rule_groups_to_be_cloned as $rule_group ) { |
|
279 | - $cloned_rule_group_id = $dbo->insert_rule_group( $cloned_mapping_id ); |
|
280 | - $original_rules = (array) $rule_group['rules']; |
|
281 | - // Now we need to insert these rules for the cloned rule group id. |
|
282 | - foreach ( $original_rules as $clone_rule ) { |
|
283 | - // We should replace only rule group id in the cloned rules. |
|
284 | - $clone_rule['rule_group_id'] = (int) $cloned_rule_group_id; |
|
285 | - unset( $clone_rule['rule_id'] ); |
|
286 | - $dbo->insert_or_update_rule_item( $clone_rule ); |
|
287 | - } |
|
288 | - } |
|
289 | - } |
|
290 | - |
|
291 | - return array( |
|
292 | - 'status' => 'success', |
|
293 | - 'message' => __( 'Successfully cloned mapping items', 'wordlift' ), |
|
294 | - ); |
|
295 | - } |
|
296 | - |
|
297 | - /** |
|
298 | - * Update posted mapping items. |
|
299 | - * |
|
300 | - * @param WP_REST_Request $request {@link WP_REST_Request instance}. |
|
301 | - * |
|
302 | - * @return array |
|
303 | - */ |
|
304 | - public static function update_mapping_items( $request ) { |
|
305 | - $dbo = new Mappings_DBO(); |
|
306 | - $post_data = $request->get_params(); |
|
307 | - if ( array_key_exists( 'mapping_items', $post_data ) ) { |
|
308 | - $mapping_items = (array) $post_data['mapping_items']; |
|
309 | - foreach ( $mapping_items as $mapping_item ) { |
|
310 | - $dbo->insert_or_update_mapping_item( $mapping_item ); |
|
311 | - } |
|
312 | - |
|
313 | - return array( |
|
314 | - 'status' => 'success', |
|
315 | - 'message' => __( 'Mapping items successfully updated', 'wordlift' ), |
|
316 | - ); |
|
317 | - } else { |
|
318 | - return array( |
|
319 | - 'status' => 'failure', |
|
320 | - 'message' => __( 'Unable to update mapping item', 'wordlift' ), |
|
321 | - ); |
|
322 | - } |
|
323 | - } |
|
324 | - |
|
325 | - /** |
|
326 | - * Delete mapping items by mapping id |
|
327 | - * |
|
328 | - * @param WP_REST_Request $request {@link WP_REST_Request instance}. |
|
329 | - * |
|
330 | - * @return array |
|
331 | - */ |
|
332 | - public static function delete_mapping_items( $request ) { |
|
333 | - $dbo = new Mappings_DBO(); |
|
334 | - $post_data = $request->get_params(); |
|
335 | - if ( array_key_exists( 'mapping_items', $post_data ) ) { |
|
336 | - $mapping_items = (array) $post_data['mapping_items']; |
|
337 | - foreach ( $mapping_items as $mapping_item ) { |
|
338 | - $dbo->delete_mapping_item( $mapping_item['mapping_id'] ); |
|
339 | - } |
|
340 | - |
|
341 | - return array( |
|
342 | - 'status' => 'success', |
|
343 | - 'message' => __( 'successfully deleted mapping items', 'wordlift' ), |
|
344 | - ); |
|
345 | - } else { |
|
346 | - return array( |
|
347 | - 'status' => 'failure', |
|
348 | - 'message' => __( 'Unable to delete mapping items', 'wordlift' ), |
|
349 | - ); |
|
350 | - } |
|
351 | - } |
|
352 | - |
|
353 | - /** |
|
354 | - * Get all mapping items |
|
355 | - * |
|
356 | - * @param WP_REST_Request $request {@link WP_REST_Request instance}. |
|
357 | - * |
|
358 | - * @return array |
|
359 | - */ |
|
360 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
361 | - public static function list_mapping_items( $request ) { |
|
362 | - $dbo = new Mappings_DBO(); |
|
363 | - |
|
364 | - return $dbo->get_mappings(); |
|
365 | - } |
|
366 | - |
|
367 | - /** |
|
368 | - * Returns a array of rule ids for the rule group id |
|
369 | - * |
|
370 | - * @param Object $dbo Instance of {@link Mappings_DBO } class. |
|
371 | - * @param int $rule_group_id Primary key of rule group table. |
|
372 | - * |
|
373 | - * @return array A list of rule ids. |
|
374 | - */ |
|
375 | - private static function get_rule_ids( $dbo, $rule_group_id ) { |
|
376 | - $rule_rows_in_db = $dbo->get_rules_by_rule_group( $rule_group_id ); |
|
377 | - $rule_ids = array(); |
|
378 | - foreach ( $rule_rows_in_db as $rule_row ) { |
|
379 | - array_push( $rule_ids, (int) $rule_row['rule_id'] ); |
|
380 | - } |
|
381 | - |
|
382 | - return $rule_ids; |
|
383 | - } |
|
384 | - |
|
385 | - /** |
|
386 | - * Insert or update mapping item depends on data |
|
387 | - * |
|
388 | - * @param Object $dbo Instance of {@link Mappings_DBO } class. |
|
389 | - * @param int $rule_group_id Refers to a rule group which this rule belongs to. |
|
390 | - * @param array $rule_list Array of rule items. |
|
391 | - * |
|
392 | - * @return void |
|
393 | - */ |
|
394 | - private static function save_rules( $dbo, $rule_group_id, $rule_list ) { |
|
395 | - $rule_ids = self::get_rule_ids( $dbo, $rule_group_id ); |
|
396 | - foreach ( $rule_list as $rule ) { |
|
397 | - // Some rules may not have rule group id, because they are inserted |
|
398 | - // in ui, so lets add them any way. |
|
399 | - $rule['rule_group_id'] = $rule_group_id; |
|
400 | - $dbo->insert_or_update_rule_item( $rule ); |
|
401 | - if ( array_key_exists( 'rule_id', $rule ) ) { |
|
402 | - $index_to_be_removed = array_search( |
|
403 | - (int) $rule['rule_id'], |
|
404 | - $rule_ids, |
|
405 | - true |
|
406 | - ); |
|
407 | - if ( false !== $index_to_be_removed ) { |
|
408 | - unset( $rule_ids[ $index_to_be_removed ] ); |
|
409 | - } |
|
410 | - } |
|
411 | - } |
|
412 | - foreach ( $rule_ids as $rule_id ) { |
|
413 | - // Delete all the rule ids which are not posted. |
|
414 | - $dbo->delete_rule_item( $rule_id ); |
|
415 | - } |
|
416 | - } |
|
417 | - |
|
418 | - /** |
|
419 | - * Insert or update rule group list based on data |
|
420 | - * |
|
421 | - * @param Object $dbo Instance of {@link Mappings_DBO } class. |
|
422 | - * @param int $mapping_id Primary key of mapping table. |
|
423 | - * @param array $property_list { Array of property items }. |
|
424 | - * |
|
425 | - * @return void |
|
426 | - */ |
|
427 | - private static function save_property_list( $dbo, $mapping_id, $property_list ) { |
|
428 | - $properties_needed_to_be_deleted = $dbo->get_properties( $mapping_id ); |
|
429 | - $property_ids = array(); |
|
430 | - foreach ( $properties_needed_to_be_deleted as $property ) { |
|
431 | - array_push( $property_ids, (int) $property['property_id'] ); |
|
432 | - } |
|
433 | - foreach ( $property_list as $property ) { |
|
434 | - if ( array_key_exists( 'property_id', $property ) ) { |
|
435 | - // Remove the id from the list of property ids needed to be deleted |
|
436 | - // because it is posted. |
|
437 | - $index_to_be_removed = array_search( |
|
438 | - (int) $property['property_id'], |
|
439 | - $property_ids, |
|
440 | - true |
|
441 | - ); |
|
442 | - if ( false !== $index_to_be_removed ) { |
|
443 | - unset( $property_ids[ $index_to_be_removed ] ); |
|
444 | - } |
|
445 | - } |
|
446 | - // Add mapping id to property data. |
|
447 | - $property['mapping_id'] = $mapping_id; |
|
448 | - $dbo->insert_or_update_property( $property ); |
|
449 | - |
|
450 | - } |
|
451 | - // At the end remove all the property ids which are not posted. |
|
452 | - foreach ( $property_ids as $property_id ) { |
|
453 | - $dbo->delete_property( $property_id ); |
|
454 | - } |
|
455 | - |
|
456 | - } |
|
457 | - |
|
458 | - /** |
|
459 | - * Returns a array of rule group ids for the mapping id |
|
460 | - * |
|
461 | - * @param Object $dbo Instance of {@link Mappings_DBO } class. |
|
462 | - * @param int $mapping_id Primary key of mapping table. |
|
463 | - * |
|
464 | - * @return array $rule_group_ids A list of rule group ids. |
|
465 | - */ |
|
466 | - private static function get_rule_group_ids( $dbo, $mapping_id ) { |
|
467 | - $rule_group_rows = $dbo->get_rule_group_list( $mapping_id ); |
|
468 | - $rule_group_ids = array(); |
|
469 | - foreach ( $rule_group_rows as $rule_group_row ) { |
|
470 | - array_push( $rule_group_ids, (int) $rule_group_row['rule_group_id'] ); |
|
471 | - } |
|
472 | - |
|
473 | - return $rule_group_ids; |
|
474 | - } |
|
475 | - |
|
476 | - /** |
|
477 | - * Insert or update rule group list |
|
478 | - * |
|
479 | - * @param Object $dbo Instance of {@link Mappings_DBO } class. |
|
480 | - * @param int $mapping_id Primary key of mapping table. |
|
481 | - * @param array $rule_group_list { Array of rule group items }. |
|
482 | - * |
|
483 | - * @return void |
|
484 | - */ |
|
485 | - private static function save_rule_group_list( $dbo, $mapping_id, $rule_group_list ) { |
|
486 | - // The rule groups not posted should be deleted. |
|
487 | - $rule_group_ids = self::get_rule_group_ids( $dbo, $mapping_id ); |
|
488 | - // Loop through rule group list and save the rule group. |
|
489 | - foreach ( $rule_group_list as $rule_group ) { |
|
490 | - if ( array_key_exists( 'rule_group_id', $rule_group ) ) { |
|
491 | - $rule_group_id = $rule_group['rule_group_id']; |
|
492 | - } else { |
|
493 | - // New rule group, should create new rule group id. |
|
494 | - $rule_group_id = $dbo->insert_rule_group( $mapping_id ); |
|
495 | - } |
|
496 | - $index_to_be_removed = array_search( |
|
497 | - (int) $rule_group_id, |
|
498 | - $rule_group_ids, |
|
499 | - true |
|
500 | - ); |
|
501 | - if ( false !== $index_to_be_removed ) { |
|
502 | - unset( $rule_group_ids[ $index_to_be_removed ] ); |
|
503 | - } |
|
504 | - self::save_rules( $dbo, $rule_group_id, $rule_group['rules'] ); |
|
505 | - } |
|
506 | - |
|
507 | - // Remove all the rule groups which are not posted. |
|
508 | - foreach ( $rule_group_ids as $rule_group_id ) { |
|
509 | - $dbo->delete_rule_group_item( $rule_group_id ); |
|
510 | - } |
|
511 | - } |
|
512 | - |
|
513 | - /** |
|
514 | - * Insert or update mapping item depends on data |
|
515 | - * |
|
516 | - * @param WP_REST_Request $request {@link WP_REST_Request instance}. |
|
517 | - * |
|
518 | - * @return array |
|
519 | - */ |
|
520 | - public static function insert_or_update_mapping_item( $request ) { |
|
521 | - $post_data = $request->get_params() === null ? array() : $request->get_params(); |
|
522 | - $dbo = new Mappings_DBO(); |
|
523 | - // check if valid object is posted. |
|
524 | - if ( array_key_exists( 'mapping_title', $post_data ) && |
|
525 | - array_key_exists( 'rule_group_list', $post_data ) && |
|
526 | - array_key_exists( 'property_list', $post_data ) ) { |
|
527 | - // Do validation, remove all incomplete data. |
|
528 | - $mapping_item = array(); |
|
529 | - if ( array_key_exists( 'mapping_id', $post_data ) ) { |
|
530 | - $mapping_item['mapping_id'] = $post_data['mapping_id']; |
|
531 | - } |
|
532 | - $mapping_item['mapping_title'] = $post_data['mapping_title']; |
|
533 | - // lets save the mapping item. |
|
534 | - $mapping_id = $dbo->insert_or_update_mapping_item( $mapping_item ); |
|
535 | - self::save_rule_group_list( $dbo, $mapping_id, $post_data['rule_group_list'] ); |
|
536 | - self::save_property_list( $dbo, $mapping_id, $post_data['property_list'] ); |
|
537 | - |
|
538 | - return array( |
|
539 | - 'status' => 'success', |
|
540 | - 'message' => __( 'Successfully saved mapping item', 'wordlift' ), |
|
541 | - 'mapping_id' => (int) $mapping_id, |
|
542 | - ); |
|
543 | - } else { |
|
544 | - return array( |
|
545 | - 'status' => 'error', |
|
546 | - 'message' => __( 'Unable to save mapping item', 'wordlift' ), |
|
547 | - ); |
|
548 | - } |
|
549 | - } |
|
16 | + // Namespace for CRUD mappings. |
|
17 | + const MAPPINGS_NAMESPACE = '/mappings'; |
|
18 | + |
|
19 | + /** |
|
20 | + * Registers route on rest api initialisation. |
|
21 | + */ |
|
22 | + public static function register_routes() { |
|
23 | + |
|
24 | + add_action( 'rest_api_init', 'Wordlift\Mappings\Mappings_REST_Controller::register_route_callback' ); |
|
25 | + |
|
26 | + } |
|
27 | + |
|
28 | + /** |
|
29 | + * Get a single mapping item by its mapping_id |
|
30 | + * |
|
31 | + * @param WP_REST_Request $request {@link WP_REST_Request instance}. |
|
32 | + * |
|
33 | + * @return array |
|
34 | + */ |
|
35 | + public static function get_mapping_item( $request ) { |
|
36 | + $dbo = new Mappings_DBO(); |
|
37 | + $mapping_id = $request['id']; |
|
38 | + $mapping_id_data = array(); |
|
39 | + $rule_groups = $dbo->get_rule_groups_by_mapping( $mapping_id ); |
|
40 | + $properties = $dbo->get_properties( $mapping_id ); |
|
41 | + $mapping_row = $dbo->get_mapping_item_data( $mapping_id ); |
|
42 | + |
|
43 | + $mapping_id_data['mapping_id'] = $mapping_id; |
|
44 | + $mapping_id_data['property_list'] = $properties; |
|
45 | + $mapping_id_data['rule_group_list'] = $rule_groups; |
|
46 | + $mapping_id_data['mapping_title'] = $mapping_row['mapping_title']; |
|
47 | + |
|
48 | + return $mapping_id_data; |
|
49 | + } |
|
50 | + |
|
51 | + /** |
|
52 | + * Register route call back function, called when rest api gets initialised |
|
53 | + * |
|
54 | + * @return void |
|
55 | + */ |
|
56 | + public static function register_route_callback() { |
|
57 | + register_rest_route( |
|
58 | + WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
59 | + '/mappings', |
|
60 | + array( |
|
61 | + 'methods' => WP_REST_Server::CREATABLE, |
|
62 | + 'callback' => 'Wordlift\Mappings\Mappings_REST_Controller::insert_or_update_mapping_item', |
|
63 | + 'permission_callback' => function () { |
|
64 | + return current_user_can( 'manage_options' ); |
|
65 | + }, |
|
66 | + ) |
|
67 | + ); |
|
68 | + // Get list of mapping items. |
|
69 | + register_rest_route( |
|
70 | + WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
71 | + '/mappings', |
|
72 | + array( |
|
73 | + 'methods' => WP_REST_Server::READABLE, |
|
74 | + 'callback' => 'Wordlift\Mappings\Mappings_REST_Controller::list_mapping_items', |
|
75 | + 'permission_callback' => function () { |
|
76 | + return current_user_can( 'manage_options' ); |
|
77 | + }, |
|
78 | + ) |
|
79 | + ); |
|
80 | + |
|
81 | + // Delete mapping items by id. |
|
82 | + register_rest_route( |
|
83 | + WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
84 | + 'mappings', |
|
85 | + array( |
|
86 | + 'methods' => WP_REST_Server::DELETABLE, |
|
87 | + 'callback' => 'Wordlift\Mappings\Mappings_REST_Controller::delete_mapping_items', |
|
88 | + 'permission_callback' => function () { |
|
89 | + return current_user_can( 'manage_options' ); |
|
90 | + }, |
|
91 | + ) |
|
92 | + ); |
|
93 | + |
|
94 | + // Get single mapping item route. |
|
95 | + register_rest_route( |
|
96 | + WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
97 | + 'mappings/(?P<id>\d+)', |
|
98 | + array( |
|
99 | + 'methods' => WP_REST_Server::READABLE, |
|
100 | + 'callback' => 'Wordlift\Mappings\Mappings_REST_Controller::get_mapping_item', |
|
101 | + 'permission_callback' => function () { |
|
102 | + return current_user_can( 'manage_options' ); |
|
103 | + }, |
|
104 | + ) |
|
105 | + ); |
|
106 | + |
|
107 | + // Update mapping items. |
|
108 | + register_rest_route( |
|
109 | + WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
110 | + 'mappings', |
|
111 | + array( |
|
112 | + 'methods' => WP_REST_Server::EDITABLE, |
|
113 | + 'callback' => 'Wordlift\Mappings\Mappings_REST_Controller::update_mapping_items', |
|
114 | + 'permission_callback' => function () { |
|
115 | + return current_user_can( 'manage_options' ); |
|
116 | + }, |
|
117 | + ) |
|
118 | + ); |
|
119 | + |
|
120 | + // Clone mapping items. |
|
121 | + register_rest_route( |
|
122 | + WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
123 | + 'mappings/clone', |
|
124 | + array( |
|
125 | + 'methods' => WP_REST_Server::CREATABLE, |
|
126 | + 'callback' => 'Wordlift\Mappings\Mappings_REST_Controller::clone_mapping_items', |
|
127 | + 'permission_callback' => function () { |
|
128 | + return current_user_can( 'manage_options' ); |
|
129 | + }, |
|
130 | + ) |
|
131 | + ); |
|
132 | + |
|
133 | + // Register rest endpoint to get the terms. |
|
134 | + register_rest_route( |
|
135 | + WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
136 | + 'mappings/get_terms', |
|
137 | + array( |
|
138 | + 'methods' => WP_REST_Server::CREATABLE, |
|
139 | + 'callback' => 'Wordlift\Mappings\Mappings_REST_Controller::get_terms_for_the_posted_taxonomy', |
|
140 | + 'permission_callback' => function () { |
|
141 | + return current_user_can( 'manage_options' ); |
|
142 | + }, |
|
143 | + ) |
|
144 | + ); |
|
145 | + |
|
146 | + // Register rest endpoint to get the terms. |
|
147 | + register_rest_route( |
|
148 | + WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
149 | + 'mappings/get_taxonomy_terms', |
|
150 | + array( |
|
151 | + 'methods' => WP_REST_Server::CREATABLE, |
|
152 | + 'callback' => 'Wordlift\Mappings\Mappings_REST_Controller::get_taxonomy_terms_for_the_posted_taxonomy', |
|
153 | + 'permission_callback' => function () { |
|
154 | + return current_user_can( 'manage_options' ); |
|
155 | + }, |
|
156 | + ) |
|
157 | + ); |
|
158 | + } |
|
159 | + |
|
160 | + /** |
|
161 | + * Get the taxonomy & terms |
|
162 | + * |
|
163 | + * @param WP_REST_Request $request {@link WP_REST_Request instance}. |
|
164 | + * |
|
165 | + * @return array The array of the taxonomies & terms. |
|
166 | + */ |
|
167 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
168 | + public static function get_taxonomy_terms_for_the_posted_taxonomy( $request ) { |
|
169 | + $taxonomy_terms = array(); |
|
170 | + $post_taxonomies = get_taxonomies( array(), 'objects' ); |
|
171 | + |
|
172 | + foreach ( $post_taxonomies as $post_taxonomy ) { |
|
173 | + $taxonomy_config = array( |
|
174 | + 'taxonomy' => $post_taxonomy->name, |
|
175 | + 'hide_empty' => false, |
|
176 | + ); |
|
177 | + |
|
178 | + $total_terms = wp_count_terms( $taxonomy_config ); |
|
179 | + |
|
180 | + $post_taxonomy_terms = get_terms( $taxonomy_config ); |
|
181 | + |
|
182 | + if ( $total_terms ) { |
|
183 | + $group_taxonomy = array( |
|
184 | + 'parentValue' => 'post_taxonomy', |
|
185 | + 'group_name' => $post_taxonomy->label, |
|
186 | + 'group_options' => array(), |
|
187 | + ); |
|
188 | + |
|
189 | + foreach ( $post_taxonomy_terms as $post_taxonomy_term ) { |
|
190 | + array_push( |
|
191 | + $group_taxonomy['group_options'], |
|
192 | + array( |
|
193 | + 'label' => ' - ' . $post_taxonomy_term->name, |
|
194 | + 'value' => $post_taxonomy_term->slug, |
|
195 | + 'taxonomy' => 'post_taxonomy', |
|
196 | + ) |
|
197 | + ); |
|
198 | + |
|
199 | + $post_term_children = get_term_children( $post_taxonomy_term->term_id, $post_taxonomy->name ); |
|
200 | + |
|
201 | + foreach ( $post_term_children as $post_term_child ) { |
|
202 | + $child_term = get_term_by( 'id', $post_term_child, $post_taxonomy->name ); |
|
203 | + |
|
204 | + array_push( |
|
205 | + $group_taxonomy['group_options'], |
|
206 | + array( |
|
207 | + 'label' => ' -- ' . $child_term->name, |
|
208 | + 'value' => $child_term->slug, |
|
209 | + 'taxonomy' => 'post_taxonomy', |
|
210 | + ) |
|
211 | + ); |
|
212 | + } |
|
213 | + } |
|
214 | + array_push( $taxonomy_terms, $group_taxonomy ); |
|
215 | + } |
|
216 | + } |
|
217 | + |
|
218 | + return $taxonomy_terms; |
|
219 | + } |
|
220 | + |
|
221 | + /** |
|
222 | + * Get the terms for the posted taxonomy name. |
|
223 | + * |
|
224 | + * @param WP_REST_Request $request {@link WP_REST_Request instance}. |
|
225 | + * |
|
226 | + * @return array The array of the terms for the taxonomy. |
|
227 | + */ |
|
228 | + public static function get_terms_for_the_posted_taxonomy( $request ) { |
|
229 | + $post_data = $request->get_params(); |
|
230 | + if ( ! array_key_exists( 'taxonomy', $post_data ) ) { |
|
231 | + return array( |
|
232 | + 'status' => 'failure', |
|
233 | + 'message' => __( 'Request not valid, must post a taxonomy to get terms', 'wordlift' ), |
|
234 | + ); |
|
235 | + } else { |
|
236 | + $taxonomy = $post_data['taxonomy']; |
|
237 | + $terms = get_terms( $taxonomy, array( 'hide_empty' => false ) ); |
|
238 | + if ( is_wp_error( $terms ) ) { |
|
239 | + // Return error response, if the taxonomy is not valid. |
|
240 | + return array( |
|
241 | + 'status' => 'failure', |
|
242 | + 'message' => __( 'Request not valid, must post a valid taxonomy', 'wordlift' ), |
|
243 | + ); |
|
244 | + } |
|
245 | + |
|
246 | + return $terms; |
|
247 | + } |
|
248 | + } |
|
249 | + |
|
250 | + /** |
|
251 | + * Clone posted mapping items. |
|
252 | + * |
|
253 | + * @param WP_REST_Request $request {@link WP_REST_Request instance}. |
|
254 | + * |
|
255 | + * @return array |
|
256 | + */ |
|
257 | + public static function clone_mapping_items( $request ) { |
|
258 | + $dbo = new Mappings_DBO(); |
|
259 | + $post_data = (array) $request->get_params(); |
|
260 | + $mapping_items = (array) $post_data['mapping_items']; |
|
261 | + foreach ( $mapping_items as $mapping_item ) { |
|
262 | + $mapping_id = (int) $mapping_item['mapping_id']; |
|
263 | + // Clone the current mapping item. |
|
264 | + $cloned_mapping_id = $dbo->insert_mapping_item( $mapping_item['mapping_title'] ); |
|
265 | + // Clone all the rule groups. |
|
266 | + $rule_groups_to_be_cloned = $dbo->get_rule_groups_by_mapping( $mapping_id ); |
|
267 | + // Clone all the properties. |
|
268 | + $properties_to_be_cloned = $dbo->get_properties( $mapping_id ); |
|
269 | + foreach ( $properties_to_be_cloned as $property ) { |
|
270 | + // Assign a new mapping id. |
|
271 | + $property['mapping_id'] = $cloned_mapping_id; |
|
272 | + // Removing this property id, since a new id needed to be created for |
|
273 | + // new property. |
|
274 | + unset( $property['property_id'] ); |
|
275 | + $dbo->insert_or_update_property( $property ); |
|
276 | + } |
|
277 | + // Loop through the rule groups and insert them in table with the mapping id. |
|
278 | + foreach ( $rule_groups_to_be_cloned as $rule_group ) { |
|
279 | + $cloned_rule_group_id = $dbo->insert_rule_group( $cloned_mapping_id ); |
|
280 | + $original_rules = (array) $rule_group['rules']; |
|
281 | + // Now we need to insert these rules for the cloned rule group id. |
|
282 | + foreach ( $original_rules as $clone_rule ) { |
|
283 | + // We should replace only rule group id in the cloned rules. |
|
284 | + $clone_rule['rule_group_id'] = (int) $cloned_rule_group_id; |
|
285 | + unset( $clone_rule['rule_id'] ); |
|
286 | + $dbo->insert_or_update_rule_item( $clone_rule ); |
|
287 | + } |
|
288 | + } |
|
289 | + } |
|
290 | + |
|
291 | + return array( |
|
292 | + 'status' => 'success', |
|
293 | + 'message' => __( 'Successfully cloned mapping items', 'wordlift' ), |
|
294 | + ); |
|
295 | + } |
|
296 | + |
|
297 | + /** |
|
298 | + * Update posted mapping items. |
|
299 | + * |
|
300 | + * @param WP_REST_Request $request {@link WP_REST_Request instance}. |
|
301 | + * |
|
302 | + * @return array |
|
303 | + */ |
|
304 | + public static function update_mapping_items( $request ) { |
|
305 | + $dbo = new Mappings_DBO(); |
|
306 | + $post_data = $request->get_params(); |
|
307 | + if ( array_key_exists( 'mapping_items', $post_data ) ) { |
|
308 | + $mapping_items = (array) $post_data['mapping_items']; |
|
309 | + foreach ( $mapping_items as $mapping_item ) { |
|
310 | + $dbo->insert_or_update_mapping_item( $mapping_item ); |
|
311 | + } |
|
312 | + |
|
313 | + return array( |
|
314 | + 'status' => 'success', |
|
315 | + 'message' => __( 'Mapping items successfully updated', 'wordlift' ), |
|
316 | + ); |
|
317 | + } else { |
|
318 | + return array( |
|
319 | + 'status' => 'failure', |
|
320 | + 'message' => __( 'Unable to update mapping item', 'wordlift' ), |
|
321 | + ); |
|
322 | + } |
|
323 | + } |
|
324 | + |
|
325 | + /** |
|
326 | + * Delete mapping items by mapping id |
|
327 | + * |
|
328 | + * @param WP_REST_Request $request {@link WP_REST_Request instance}. |
|
329 | + * |
|
330 | + * @return array |
|
331 | + */ |
|
332 | + public static function delete_mapping_items( $request ) { |
|
333 | + $dbo = new Mappings_DBO(); |
|
334 | + $post_data = $request->get_params(); |
|
335 | + if ( array_key_exists( 'mapping_items', $post_data ) ) { |
|
336 | + $mapping_items = (array) $post_data['mapping_items']; |
|
337 | + foreach ( $mapping_items as $mapping_item ) { |
|
338 | + $dbo->delete_mapping_item( $mapping_item['mapping_id'] ); |
|
339 | + } |
|
340 | + |
|
341 | + return array( |
|
342 | + 'status' => 'success', |
|
343 | + 'message' => __( 'successfully deleted mapping items', 'wordlift' ), |
|
344 | + ); |
|
345 | + } else { |
|
346 | + return array( |
|
347 | + 'status' => 'failure', |
|
348 | + 'message' => __( 'Unable to delete mapping items', 'wordlift' ), |
|
349 | + ); |
|
350 | + } |
|
351 | + } |
|
352 | + |
|
353 | + /** |
|
354 | + * Get all mapping items |
|
355 | + * |
|
356 | + * @param WP_REST_Request $request {@link WP_REST_Request instance}. |
|
357 | + * |
|
358 | + * @return array |
|
359 | + */ |
|
360 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
361 | + public static function list_mapping_items( $request ) { |
|
362 | + $dbo = new Mappings_DBO(); |
|
363 | + |
|
364 | + return $dbo->get_mappings(); |
|
365 | + } |
|
366 | + |
|
367 | + /** |
|
368 | + * Returns a array of rule ids for the rule group id |
|
369 | + * |
|
370 | + * @param Object $dbo Instance of {@link Mappings_DBO } class. |
|
371 | + * @param int $rule_group_id Primary key of rule group table. |
|
372 | + * |
|
373 | + * @return array A list of rule ids. |
|
374 | + */ |
|
375 | + private static function get_rule_ids( $dbo, $rule_group_id ) { |
|
376 | + $rule_rows_in_db = $dbo->get_rules_by_rule_group( $rule_group_id ); |
|
377 | + $rule_ids = array(); |
|
378 | + foreach ( $rule_rows_in_db as $rule_row ) { |
|
379 | + array_push( $rule_ids, (int) $rule_row['rule_id'] ); |
|
380 | + } |
|
381 | + |
|
382 | + return $rule_ids; |
|
383 | + } |
|
384 | + |
|
385 | + /** |
|
386 | + * Insert or update mapping item depends on data |
|
387 | + * |
|
388 | + * @param Object $dbo Instance of {@link Mappings_DBO } class. |
|
389 | + * @param int $rule_group_id Refers to a rule group which this rule belongs to. |
|
390 | + * @param array $rule_list Array of rule items. |
|
391 | + * |
|
392 | + * @return void |
|
393 | + */ |
|
394 | + private static function save_rules( $dbo, $rule_group_id, $rule_list ) { |
|
395 | + $rule_ids = self::get_rule_ids( $dbo, $rule_group_id ); |
|
396 | + foreach ( $rule_list as $rule ) { |
|
397 | + // Some rules may not have rule group id, because they are inserted |
|
398 | + // in ui, so lets add them any way. |
|
399 | + $rule['rule_group_id'] = $rule_group_id; |
|
400 | + $dbo->insert_or_update_rule_item( $rule ); |
|
401 | + if ( array_key_exists( 'rule_id', $rule ) ) { |
|
402 | + $index_to_be_removed = array_search( |
|
403 | + (int) $rule['rule_id'], |
|
404 | + $rule_ids, |
|
405 | + true |
|
406 | + ); |
|
407 | + if ( false !== $index_to_be_removed ) { |
|
408 | + unset( $rule_ids[ $index_to_be_removed ] ); |
|
409 | + } |
|
410 | + } |
|
411 | + } |
|
412 | + foreach ( $rule_ids as $rule_id ) { |
|
413 | + // Delete all the rule ids which are not posted. |
|
414 | + $dbo->delete_rule_item( $rule_id ); |
|
415 | + } |
|
416 | + } |
|
417 | + |
|
418 | + /** |
|
419 | + * Insert or update rule group list based on data |
|
420 | + * |
|
421 | + * @param Object $dbo Instance of {@link Mappings_DBO } class. |
|
422 | + * @param int $mapping_id Primary key of mapping table. |
|
423 | + * @param array $property_list { Array of property items }. |
|
424 | + * |
|
425 | + * @return void |
|
426 | + */ |
|
427 | + private static function save_property_list( $dbo, $mapping_id, $property_list ) { |
|
428 | + $properties_needed_to_be_deleted = $dbo->get_properties( $mapping_id ); |
|
429 | + $property_ids = array(); |
|
430 | + foreach ( $properties_needed_to_be_deleted as $property ) { |
|
431 | + array_push( $property_ids, (int) $property['property_id'] ); |
|
432 | + } |
|
433 | + foreach ( $property_list as $property ) { |
|
434 | + if ( array_key_exists( 'property_id', $property ) ) { |
|
435 | + // Remove the id from the list of property ids needed to be deleted |
|
436 | + // because it is posted. |
|
437 | + $index_to_be_removed = array_search( |
|
438 | + (int) $property['property_id'], |
|
439 | + $property_ids, |
|
440 | + true |
|
441 | + ); |
|
442 | + if ( false !== $index_to_be_removed ) { |
|
443 | + unset( $property_ids[ $index_to_be_removed ] ); |
|
444 | + } |
|
445 | + } |
|
446 | + // Add mapping id to property data. |
|
447 | + $property['mapping_id'] = $mapping_id; |
|
448 | + $dbo->insert_or_update_property( $property ); |
|
449 | + |
|
450 | + } |
|
451 | + // At the end remove all the property ids which are not posted. |
|
452 | + foreach ( $property_ids as $property_id ) { |
|
453 | + $dbo->delete_property( $property_id ); |
|
454 | + } |
|
455 | + |
|
456 | + } |
|
457 | + |
|
458 | + /** |
|
459 | + * Returns a array of rule group ids for the mapping id |
|
460 | + * |
|
461 | + * @param Object $dbo Instance of {@link Mappings_DBO } class. |
|
462 | + * @param int $mapping_id Primary key of mapping table. |
|
463 | + * |
|
464 | + * @return array $rule_group_ids A list of rule group ids. |
|
465 | + */ |
|
466 | + private static function get_rule_group_ids( $dbo, $mapping_id ) { |
|
467 | + $rule_group_rows = $dbo->get_rule_group_list( $mapping_id ); |
|
468 | + $rule_group_ids = array(); |
|
469 | + foreach ( $rule_group_rows as $rule_group_row ) { |
|
470 | + array_push( $rule_group_ids, (int) $rule_group_row['rule_group_id'] ); |
|
471 | + } |
|
472 | + |
|
473 | + return $rule_group_ids; |
|
474 | + } |
|
475 | + |
|
476 | + /** |
|
477 | + * Insert or update rule group list |
|
478 | + * |
|
479 | + * @param Object $dbo Instance of {@link Mappings_DBO } class. |
|
480 | + * @param int $mapping_id Primary key of mapping table. |
|
481 | + * @param array $rule_group_list { Array of rule group items }. |
|
482 | + * |
|
483 | + * @return void |
|
484 | + */ |
|
485 | + private static function save_rule_group_list( $dbo, $mapping_id, $rule_group_list ) { |
|
486 | + // The rule groups not posted should be deleted. |
|
487 | + $rule_group_ids = self::get_rule_group_ids( $dbo, $mapping_id ); |
|
488 | + // Loop through rule group list and save the rule group. |
|
489 | + foreach ( $rule_group_list as $rule_group ) { |
|
490 | + if ( array_key_exists( 'rule_group_id', $rule_group ) ) { |
|
491 | + $rule_group_id = $rule_group['rule_group_id']; |
|
492 | + } else { |
|
493 | + // New rule group, should create new rule group id. |
|
494 | + $rule_group_id = $dbo->insert_rule_group( $mapping_id ); |
|
495 | + } |
|
496 | + $index_to_be_removed = array_search( |
|
497 | + (int) $rule_group_id, |
|
498 | + $rule_group_ids, |
|
499 | + true |
|
500 | + ); |
|
501 | + if ( false !== $index_to_be_removed ) { |
|
502 | + unset( $rule_group_ids[ $index_to_be_removed ] ); |
|
503 | + } |
|
504 | + self::save_rules( $dbo, $rule_group_id, $rule_group['rules'] ); |
|
505 | + } |
|
506 | + |
|
507 | + // Remove all the rule groups which are not posted. |
|
508 | + foreach ( $rule_group_ids as $rule_group_id ) { |
|
509 | + $dbo->delete_rule_group_item( $rule_group_id ); |
|
510 | + } |
|
511 | + } |
|
512 | + |
|
513 | + /** |
|
514 | + * Insert or update mapping item depends on data |
|
515 | + * |
|
516 | + * @param WP_REST_Request $request {@link WP_REST_Request instance}. |
|
517 | + * |
|
518 | + * @return array |
|
519 | + */ |
|
520 | + public static function insert_or_update_mapping_item( $request ) { |
|
521 | + $post_data = $request->get_params() === null ? array() : $request->get_params(); |
|
522 | + $dbo = new Mappings_DBO(); |
|
523 | + // check if valid object is posted. |
|
524 | + if ( array_key_exists( 'mapping_title', $post_data ) && |
|
525 | + array_key_exists( 'rule_group_list', $post_data ) && |
|
526 | + array_key_exists( 'property_list', $post_data ) ) { |
|
527 | + // Do validation, remove all incomplete data. |
|
528 | + $mapping_item = array(); |
|
529 | + if ( array_key_exists( 'mapping_id', $post_data ) ) { |
|
530 | + $mapping_item['mapping_id'] = $post_data['mapping_id']; |
|
531 | + } |
|
532 | + $mapping_item['mapping_title'] = $post_data['mapping_title']; |
|
533 | + // lets save the mapping item. |
|
534 | + $mapping_id = $dbo->insert_or_update_mapping_item( $mapping_item ); |
|
535 | + self::save_rule_group_list( $dbo, $mapping_id, $post_data['rule_group_list'] ); |
|
536 | + self::save_property_list( $dbo, $mapping_id, $post_data['property_list'] ); |
|
537 | + |
|
538 | + return array( |
|
539 | + 'status' => 'success', |
|
540 | + 'message' => __( 'Successfully saved mapping item', 'wordlift' ), |
|
541 | + 'mapping_id' => (int) $mapping_id, |
|
542 | + ); |
|
543 | + } else { |
|
544 | + return array( |
|
545 | + 'status' => 'error', |
|
546 | + 'message' => __( 'Unable to save mapping item', 'wordlift' ), |
|
547 | + ); |
|
548 | + } |
|
549 | + } |
|
550 | 550 | } |
551 | 551 | |
552 | 552 | Mappings_REST_Controller::register_routes(); |
@@ -21,7 +21,7 @@ discard block |
||
21 | 21 | */ |
22 | 22 | public static function register_routes() { |
23 | 23 | |
24 | - add_action( 'rest_api_init', 'Wordlift\Mappings\Mappings_REST_Controller::register_route_callback' ); |
|
24 | + add_action('rest_api_init', 'Wordlift\Mappings\Mappings_REST_Controller::register_route_callback'); |
|
25 | 25 | |
26 | 26 | } |
27 | 27 | |
@@ -32,13 +32,13 @@ discard block |
||
32 | 32 | * |
33 | 33 | * @return array |
34 | 34 | */ |
35 | - public static function get_mapping_item( $request ) { |
|
35 | + public static function get_mapping_item($request) { |
|
36 | 36 | $dbo = new Mappings_DBO(); |
37 | 37 | $mapping_id = $request['id']; |
38 | 38 | $mapping_id_data = array(); |
39 | - $rule_groups = $dbo->get_rule_groups_by_mapping( $mapping_id ); |
|
40 | - $properties = $dbo->get_properties( $mapping_id ); |
|
41 | - $mapping_row = $dbo->get_mapping_item_data( $mapping_id ); |
|
39 | + $rule_groups = $dbo->get_rule_groups_by_mapping($mapping_id); |
|
40 | + $properties = $dbo->get_properties($mapping_id); |
|
41 | + $mapping_row = $dbo->get_mapping_item_data($mapping_id); |
|
42 | 42 | |
43 | 43 | $mapping_id_data['mapping_id'] = $mapping_id; |
44 | 44 | $mapping_id_data['property_list'] = $properties; |
@@ -60,8 +60,8 @@ discard block |
||
60 | 60 | array( |
61 | 61 | 'methods' => WP_REST_Server::CREATABLE, |
62 | 62 | 'callback' => 'Wordlift\Mappings\Mappings_REST_Controller::insert_or_update_mapping_item', |
63 | - 'permission_callback' => function () { |
|
64 | - return current_user_can( 'manage_options' ); |
|
63 | + 'permission_callback' => function() { |
|
64 | + return current_user_can('manage_options'); |
|
65 | 65 | }, |
66 | 66 | ) |
67 | 67 | ); |
@@ -72,8 +72,8 @@ discard block |
||
72 | 72 | array( |
73 | 73 | 'methods' => WP_REST_Server::READABLE, |
74 | 74 | 'callback' => 'Wordlift\Mappings\Mappings_REST_Controller::list_mapping_items', |
75 | - 'permission_callback' => function () { |
|
76 | - return current_user_can( 'manage_options' ); |
|
75 | + 'permission_callback' => function() { |
|
76 | + return current_user_can('manage_options'); |
|
77 | 77 | }, |
78 | 78 | ) |
79 | 79 | ); |
@@ -85,8 +85,8 @@ discard block |
||
85 | 85 | array( |
86 | 86 | 'methods' => WP_REST_Server::DELETABLE, |
87 | 87 | 'callback' => 'Wordlift\Mappings\Mappings_REST_Controller::delete_mapping_items', |
88 | - 'permission_callback' => function () { |
|
89 | - return current_user_can( 'manage_options' ); |
|
88 | + 'permission_callback' => function() { |
|
89 | + return current_user_can('manage_options'); |
|
90 | 90 | }, |
91 | 91 | ) |
92 | 92 | ); |
@@ -98,8 +98,8 @@ discard block |
||
98 | 98 | array( |
99 | 99 | 'methods' => WP_REST_Server::READABLE, |
100 | 100 | 'callback' => 'Wordlift\Mappings\Mappings_REST_Controller::get_mapping_item', |
101 | - 'permission_callback' => function () { |
|
102 | - return current_user_can( 'manage_options' ); |
|
101 | + 'permission_callback' => function() { |
|
102 | + return current_user_can('manage_options'); |
|
103 | 103 | }, |
104 | 104 | ) |
105 | 105 | ); |
@@ -111,8 +111,8 @@ discard block |
||
111 | 111 | array( |
112 | 112 | 'methods' => WP_REST_Server::EDITABLE, |
113 | 113 | 'callback' => 'Wordlift\Mappings\Mappings_REST_Controller::update_mapping_items', |
114 | - 'permission_callback' => function () { |
|
115 | - return current_user_can( 'manage_options' ); |
|
114 | + 'permission_callback' => function() { |
|
115 | + return current_user_can('manage_options'); |
|
116 | 116 | }, |
117 | 117 | ) |
118 | 118 | ); |
@@ -124,8 +124,8 @@ discard block |
||
124 | 124 | array( |
125 | 125 | 'methods' => WP_REST_Server::CREATABLE, |
126 | 126 | 'callback' => 'Wordlift\Mappings\Mappings_REST_Controller::clone_mapping_items', |
127 | - 'permission_callback' => function () { |
|
128 | - return current_user_can( 'manage_options' ); |
|
127 | + 'permission_callback' => function() { |
|
128 | + return current_user_can('manage_options'); |
|
129 | 129 | }, |
130 | 130 | ) |
131 | 131 | ); |
@@ -137,8 +137,8 @@ discard block |
||
137 | 137 | array( |
138 | 138 | 'methods' => WP_REST_Server::CREATABLE, |
139 | 139 | 'callback' => 'Wordlift\Mappings\Mappings_REST_Controller::get_terms_for_the_posted_taxonomy', |
140 | - 'permission_callback' => function () { |
|
141 | - return current_user_can( 'manage_options' ); |
|
140 | + 'permission_callback' => function() { |
|
141 | + return current_user_can('manage_options'); |
|
142 | 142 | }, |
143 | 143 | ) |
144 | 144 | ); |
@@ -150,8 +150,8 @@ discard block |
||
150 | 150 | array( |
151 | 151 | 'methods' => WP_REST_Server::CREATABLE, |
152 | 152 | 'callback' => 'Wordlift\Mappings\Mappings_REST_Controller::get_taxonomy_terms_for_the_posted_taxonomy', |
153 | - 'permission_callback' => function () { |
|
154 | - return current_user_can( 'manage_options' ); |
|
153 | + 'permission_callback' => function() { |
|
154 | + return current_user_can('manage_options'); |
|
155 | 155 | }, |
156 | 156 | ) |
157 | 157 | ); |
@@ -165,53 +165,53 @@ discard block |
||
165 | 165 | * @return array The array of the taxonomies & terms. |
166 | 166 | */ |
167 | 167 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
168 | - public static function get_taxonomy_terms_for_the_posted_taxonomy( $request ) { |
|
168 | + public static function get_taxonomy_terms_for_the_posted_taxonomy($request) { |
|
169 | 169 | $taxonomy_terms = array(); |
170 | - $post_taxonomies = get_taxonomies( array(), 'objects' ); |
|
170 | + $post_taxonomies = get_taxonomies(array(), 'objects'); |
|
171 | 171 | |
172 | - foreach ( $post_taxonomies as $post_taxonomy ) { |
|
172 | + foreach ($post_taxonomies as $post_taxonomy) { |
|
173 | 173 | $taxonomy_config = array( |
174 | 174 | 'taxonomy' => $post_taxonomy->name, |
175 | 175 | 'hide_empty' => false, |
176 | 176 | ); |
177 | 177 | |
178 | - $total_terms = wp_count_terms( $taxonomy_config ); |
|
178 | + $total_terms = wp_count_terms($taxonomy_config); |
|
179 | 179 | |
180 | - $post_taxonomy_terms = get_terms( $taxonomy_config ); |
|
180 | + $post_taxonomy_terms = get_terms($taxonomy_config); |
|
181 | 181 | |
182 | - if ( $total_terms ) { |
|
182 | + if ($total_terms) { |
|
183 | 183 | $group_taxonomy = array( |
184 | 184 | 'parentValue' => 'post_taxonomy', |
185 | 185 | 'group_name' => $post_taxonomy->label, |
186 | 186 | 'group_options' => array(), |
187 | 187 | ); |
188 | 188 | |
189 | - foreach ( $post_taxonomy_terms as $post_taxonomy_term ) { |
|
189 | + foreach ($post_taxonomy_terms as $post_taxonomy_term) { |
|
190 | 190 | array_push( |
191 | 191 | $group_taxonomy['group_options'], |
192 | 192 | array( |
193 | - 'label' => ' - ' . $post_taxonomy_term->name, |
|
193 | + 'label' => ' - '.$post_taxonomy_term->name, |
|
194 | 194 | 'value' => $post_taxonomy_term->slug, |
195 | 195 | 'taxonomy' => 'post_taxonomy', |
196 | 196 | ) |
197 | 197 | ); |
198 | 198 | |
199 | - $post_term_children = get_term_children( $post_taxonomy_term->term_id, $post_taxonomy->name ); |
|
199 | + $post_term_children = get_term_children($post_taxonomy_term->term_id, $post_taxonomy->name); |
|
200 | 200 | |
201 | - foreach ( $post_term_children as $post_term_child ) { |
|
202 | - $child_term = get_term_by( 'id', $post_term_child, $post_taxonomy->name ); |
|
201 | + foreach ($post_term_children as $post_term_child) { |
|
202 | + $child_term = get_term_by('id', $post_term_child, $post_taxonomy->name); |
|
203 | 203 | |
204 | 204 | array_push( |
205 | 205 | $group_taxonomy['group_options'], |
206 | 206 | array( |
207 | - 'label' => ' -- ' . $child_term->name, |
|
207 | + 'label' => ' -- '.$child_term->name, |
|
208 | 208 | 'value' => $child_term->slug, |
209 | 209 | 'taxonomy' => 'post_taxonomy', |
210 | 210 | ) |
211 | 211 | ); |
212 | 212 | } |
213 | 213 | } |
214 | - array_push( $taxonomy_terms, $group_taxonomy ); |
|
214 | + array_push($taxonomy_terms, $group_taxonomy); |
|
215 | 215 | } |
216 | 216 | } |
217 | 217 | |
@@ -225,21 +225,21 @@ discard block |
||
225 | 225 | * |
226 | 226 | * @return array The array of the terms for the taxonomy. |
227 | 227 | */ |
228 | - public static function get_terms_for_the_posted_taxonomy( $request ) { |
|
228 | + public static function get_terms_for_the_posted_taxonomy($request) { |
|
229 | 229 | $post_data = $request->get_params(); |
230 | - if ( ! array_key_exists( 'taxonomy', $post_data ) ) { |
|
230 | + if ( ! array_key_exists('taxonomy', $post_data)) { |
|
231 | 231 | return array( |
232 | 232 | 'status' => 'failure', |
233 | - 'message' => __( 'Request not valid, must post a taxonomy to get terms', 'wordlift' ), |
|
233 | + 'message' => __('Request not valid, must post a taxonomy to get terms', 'wordlift'), |
|
234 | 234 | ); |
235 | 235 | } else { |
236 | 236 | $taxonomy = $post_data['taxonomy']; |
237 | - $terms = get_terms( $taxonomy, array( 'hide_empty' => false ) ); |
|
238 | - if ( is_wp_error( $terms ) ) { |
|
237 | + $terms = get_terms($taxonomy, array('hide_empty' => false)); |
|
238 | + if (is_wp_error($terms)) { |
|
239 | 239 | // Return error response, if the taxonomy is not valid. |
240 | 240 | return array( |
241 | 241 | 'status' => 'failure', |
242 | - 'message' => __( 'Request not valid, must post a valid taxonomy', 'wordlift' ), |
|
242 | + 'message' => __('Request not valid, must post a valid taxonomy', 'wordlift'), |
|
243 | 243 | ); |
244 | 244 | } |
245 | 245 | |
@@ -254,43 +254,43 @@ discard block |
||
254 | 254 | * |
255 | 255 | * @return array |
256 | 256 | */ |
257 | - public static function clone_mapping_items( $request ) { |
|
257 | + public static function clone_mapping_items($request) { |
|
258 | 258 | $dbo = new Mappings_DBO(); |
259 | 259 | $post_data = (array) $request->get_params(); |
260 | 260 | $mapping_items = (array) $post_data['mapping_items']; |
261 | - foreach ( $mapping_items as $mapping_item ) { |
|
261 | + foreach ($mapping_items as $mapping_item) { |
|
262 | 262 | $mapping_id = (int) $mapping_item['mapping_id']; |
263 | 263 | // Clone the current mapping item. |
264 | - $cloned_mapping_id = $dbo->insert_mapping_item( $mapping_item['mapping_title'] ); |
|
264 | + $cloned_mapping_id = $dbo->insert_mapping_item($mapping_item['mapping_title']); |
|
265 | 265 | // Clone all the rule groups. |
266 | - $rule_groups_to_be_cloned = $dbo->get_rule_groups_by_mapping( $mapping_id ); |
|
266 | + $rule_groups_to_be_cloned = $dbo->get_rule_groups_by_mapping($mapping_id); |
|
267 | 267 | // Clone all the properties. |
268 | - $properties_to_be_cloned = $dbo->get_properties( $mapping_id ); |
|
269 | - foreach ( $properties_to_be_cloned as $property ) { |
|
268 | + $properties_to_be_cloned = $dbo->get_properties($mapping_id); |
|
269 | + foreach ($properties_to_be_cloned as $property) { |
|
270 | 270 | // Assign a new mapping id. |
271 | 271 | $property['mapping_id'] = $cloned_mapping_id; |
272 | 272 | // Removing this property id, since a new id needed to be created for |
273 | 273 | // new property. |
274 | - unset( $property['property_id'] ); |
|
275 | - $dbo->insert_or_update_property( $property ); |
|
274 | + unset($property['property_id']); |
|
275 | + $dbo->insert_or_update_property($property); |
|
276 | 276 | } |
277 | 277 | // Loop through the rule groups and insert them in table with the mapping id. |
278 | - foreach ( $rule_groups_to_be_cloned as $rule_group ) { |
|
279 | - $cloned_rule_group_id = $dbo->insert_rule_group( $cloned_mapping_id ); |
|
278 | + foreach ($rule_groups_to_be_cloned as $rule_group) { |
|
279 | + $cloned_rule_group_id = $dbo->insert_rule_group($cloned_mapping_id); |
|
280 | 280 | $original_rules = (array) $rule_group['rules']; |
281 | 281 | // Now we need to insert these rules for the cloned rule group id. |
282 | - foreach ( $original_rules as $clone_rule ) { |
|
282 | + foreach ($original_rules as $clone_rule) { |
|
283 | 283 | // We should replace only rule group id in the cloned rules. |
284 | 284 | $clone_rule['rule_group_id'] = (int) $cloned_rule_group_id; |
285 | - unset( $clone_rule['rule_id'] ); |
|
286 | - $dbo->insert_or_update_rule_item( $clone_rule ); |
|
285 | + unset($clone_rule['rule_id']); |
|
286 | + $dbo->insert_or_update_rule_item($clone_rule); |
|
287 | 287 | } |
288 | 288 | } |
289 | 289 | } |
290 | 290 | |
291 | 291 | return array( |
292 | 292 | 'status' => 'success', |
293 | - 'message' => __( 'Successfully cloned mapping items', 'wordlift' ), |
|
293 | + 'message' => __('Successfully cloned mapping items', 'wordlift'), |
|
294 | 294 | ); |
295 | 295 | } |
296 | 296 | |
@@ -301,23 +301,23 @@ discard block |
||
301 | 301 | * |
302 | 302 | * @return array |
303 | 303 | */ |
304 | - public static function update_mapping_items( $request ) { |
|
304 | + public static function update_mapping_items($request) { |
|
305 | 305 | $dbo = new Mappings_DBO(); |
306 | 306 | $post_data = $request->get_params(); |
307 | - if ( array_key_exists( 'mapping_items', $post_data ) ) { |
|
307 | + if (array_key_exists('mapping_items', $post_data)) { |
|
308 | 308 | $mapping_items = (array) $post_data['mapping_items']; |
309 | - foreach ( $mapping_items as $mapping_item ) { |
|
310 | - $dbo->insert_or_update_mapping_item( $mapping_item ); |
|
309 | + foreach ($mapping_items as $mapping_item) { |
|
310 | + $dbo->insert_or_update_mapping_item($mapping_item); |
|
311 | 311 | } |
312 | 312 | |
313 | 313 | return array( |
314 | 314 | 'status' => 'success', |
315 | - 'message' => __( 'Mapping items successfully updated', 'wordlift' ), |
|
315 | + 'message' => __('Mapping items successfully updated', 'wordlift'), |
|
316 | 316 | ); |
317 | 317 | } else { |
318 | 318 | return array( |
319 | 319 | 'status' => 'failure', |
320 | - 'message' => __( 'Unable to update mapping item', 'wordlift' ), |
|
320 | + 'message' => __('Unable to update mapping item', 'wordlift'), |
|
321 | 321 | ); |
322 | 322 | } |
323 | 323 | } |
@@ -329,23 +329,23 @@ discard block |
||
329 | 329 | * |
330 | 330 | * @return array |
331 | 331 | */ |
332 | - public static function delete_mapping_items( $request ) { |
|
332 | + public static function delete_mapping_items($request) { |
|
333 | 333 | $dbo = new Mappings_DBO(); |
334 | 334 | $post_data = $request->get_params(); |
335 | - if ( array_key_exists( 'mapping_items', $post_data ) ) { |
|
335 | + if (array_key_exists('mapping_items', $post_data)) { |
|
336 | 336 | $mapping_items = (array) $post_data['mapping_items']; |
337 | - foreach ( $mapping_items as $mapping_item ) { |
|
338 | - $dbo->delete_mapping_item( $mapping_item['mapping_id'] ); |
|
337 | + foreach ($mapping_items as $mapping_item) { |
|
338 | + $dbo->delete_mapping_item($mapping_item['mapping_id']); |
|
339 | 339 | } |
340 | 340 | |
341 | 341 | return array( |
342 | 342 | 'status' => 'success', |
343 | - 'message' => __( 'successfully deleted mapping items', 'wordlift' ), |
|
343 | + 'message' => __('successfully deleted mapping items', 'wordlift'), |
|
344 | 344 | ); |
345 | 345 | } else { |
346 | 346 | return array( |
347 | 347 | 'status' => 'failure', |
348 | - 'message' => __( 'Unable to delete mapping items', 'wordlift' ), |
|
348 | + 'message' => __('Unable to delete mapping items', 'wordlift'), |
|
349 | 349 | ); |
350 | 350 | } |
351 | 351 | } |
@@ -358,7 +358,7 @@ discard block |
||
358 | 358 | * @return array |
359 | 359 | */ |
360 | 360 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
361 | - public static function list_mapping_items( $request ) { |
|
361 | + public static function list_mapping_items($request) { |
|
362 | 362 | $dbo = new Mappings_DBO(); |
363 | 363 | |
364 | 364 | return $dbo->get_mappings(); |
@@ -372,11 +372,11 @@ discard block |
||
372 | 372 | * |
373 | 373 | * @return array A list of rule ids. |
374 | 374 | */ |
375 | - private static function get_rule_ids( $dbo, $rule_group_id ) { |
|
376 | - $rule_rows_in_db = $dbo->get_rules_by_rule_group( $rule_group_id ); |
|
375 | + private static function get_rule_ids($dbo, $rule_group_id) { |
|
376 | + $rule_rows_in_db = $dbo->get_rules_by_rule_group($rule_group_id); |
|
377 | 377 | $rule_ids = array(); |
378 | - foreach ( $rule_rows_in_db as $rule_row ) { |
|
379 | - array_push( $rule_ids, (int) $rule_row['rule_id'] ); |
|
378 | + foreach ($rule_rows_in_db as $rule_row) { |
|
379 | + array_push($rule_ids, (int) $rule_row['rule_id']); |
|
380 | 380 | } |
381 | 381 | |
382 | 382 | return $rule_ids; |
@@ -391,27 +391,27 @@ discard block |
||
391 | 391 | * |
392 | 392 | * @return void |
393 | 393 | */ |
394 | - private static function save_rules( $dbo, $rule_group_id, $rule_list ) { |
|
395 | - $rule_ids = self::get_rule_ids( $dbo, $rule_group_id ); |
|
396 | - foreach ( $rule_list as $rule ) { |
|
394 | + private static function save_rules($dbo, $rule_group_id, $rule_list) { |
|
395 | + $rule_ids = self::get_rule_ids($dbo, $rule_group_id); |
|
396 | + foreach ($rule_list as $rule) { |
|
397 | 397 | // Some rules may not have rule group id, because they are inserted |
398 | 398 | // in ui, so lets add them any way. |
399 | 399 | $rule['rule_group_id'] = $rule_group_id; |
400 | - $dbo->insert_or_update_rule_item( $rule ); |
|
401 | - if ( array_key_exists( 'rule_id', $rule ) ) { |
|
400 | + $dbo->insert_or_update_rule_item($rule); |
|
401 | + if (array_key_exists('rule_id', $rule)) { |
|
402 | 402 | $index_to_be_removed = array_search( |
403 | 403 | (int) $rule['rule_id'], |
404 | 404 | $rule_ids, |
405 | 405 | true |
406 | 406 | ); |
407 | - if ( false !== $index_to_be_removed ) { |
|
408 | - unset( $rule_ids[ $index_to_be_removed ] ); |
|
407 | + if (false !== $index_to_be_removed) { |
|
408 | + unset($rule_ids[$index_to_be_removed]); |
|
409 | 409 | } |
410 | 410 | } |
411 | 411 | } |
412 | - foreach ( $rule_ids as $rule_id ) { |
|
412 | + foreach ($rule_ids as $rule_id) { |
|
413 | 413 | // Delete all the rule ids which are not posted. |
414 | - $dbo->delete_rule_item( $rule_id ); |
|
414 | + $dbo->delete_rule_item($rule_id); |
|
415 | 415 | } |
416 | 416 | } |
417 | 417 | |
@@ -424,14 +424,14 @@ discard block |
||
424 | 424 | * |
425 | 425 | * @return void |
426 | 426 | */ |
427 | - private static function save_property_list( $dbo, $mapping_id, $property_list ) { |
|
428 | - $properties_needed_to_be_deleted = $dbo->get_properties( $mapping_id ); |
|
427 | + private static function save_property_list($dbo, $mapping_id, $property_list) { |
|
428 | + $properties_needed_to_be_deleted = $dbo->get_properties($mapping_id); |
|
429 | 429 | $property_ids = array(); |
430 | - foreach ( $properties_needed_to_be_deleted as $property ) { |
|
431 | - array_push( $property_ids, (int) $property['property_id'] ); |
|
430 | + foreach ($properties_needed_to_be_deleted as $property) { |
|
431 | + array_push($property_ids, (int) $property['property_id']); |
|
432 | 432 | } |
433 | - foreach ( $property_list as $property ) { |
|
434 | - if ( array_key_exists( 'property_id', $property ) ) { |
|
433 | + foreach ($property_list as $property) { |
|
434 | + if (array_key_exists('property_id', $property)) { |
|
435 | 435 | // Remove the id from the list of property ids needed to be deleted |
436 | 436 | // because it is posted. |
437 | 437 | $index_to_be_removed = array_search( |
@@ -439,18 +439,18 @@ discard block |
||
439 | 439 | $property_ids, |
440 | 440 | true |
441 | 441 | ); |
442 | - if ( false !== $index_to_be_removed ) { |
|
443 | - unset( $property_ids[ $index_to_be_removed ] ); |
|
442 | + if (false !== $index_to_be_removed) { |
|
443 | + unset($property_ids[$index_to_be_removed]); |
|
444 | 444 | } |
445 | 445 | } |
446 | 446 | // Add mapping id to property data. |
447 | 447 | $property['mapping_id'] = $mapping_id; |
448 | - $dbo->insert_or_update_property( $property ); |
|
448 | + $dbo->insert_or_update_property($property); |
|
449 | 449 | |
450 | 450 | } |
451 | 451 | // At the end remove all the property ids which are not posted. |
452 | - foreach ( $property_ids as $property_id ) { |
|
453 | - $dbo->delete_property( $property_id ); |
|
452 | + foreach ($property_ids as $property_id) { |
|
453 | + $dbo->delete_property($property_id); |
|
454 | 454 | } |
455 | 455 | |
456 | 456 | } |
@@ -463,11 +463,11 @@ discard block |
||
463 | 463 | * |
464 | 464 | * @return array $rule_group_ids A list of rule group ids. |
465 | 465 | */ |
466 | - private static function get_rule_group_ids( $dbo, $mapping_id ) { |
|
467 | - $rule_group_rows = $dbo->get_rule_group_list( $mapping_id ); |
|
466 | + private static function get_rule_group_ids($dbo, $mapping_id) { |
|
467 | + $rule_group_rows = $dbo->get_rule_group_list($mapping_id); |
|
468 | 468 | $rule_group_ids = array(); |
469 | - foreach ( $rule_group_rows as $rule_group_row ) { |
|
470 | - array_push( $rule_group_ids, (int) $rule_group_row['rule_group_id'] ); |
|
469 | + foreach ($rule_group_rows as $rule_group_row) { |
|
470 | + array_push($rule_group_ids, (int) $rule_group_row['rule_group_id']); |
|
471 | 471 | } |
472 | 472 | |
473 | 473 | return $rule_group_ids; |
@@ -482,31 +482,31 @@ discard block |
||
482 | 482 | * |
483 | 483 | * @return void |
484 | 484 | */ |
485 | - private static function save_rule_group_list( $dbo, $mapping_id, $rule_group_list ) { |
|
485 | + private static function save_rule_group_list($dbo, $mapping_id, $rule_group_list) { |
|
486 | 486 | // The rule groups not posted should be deleted. |
487 | - $rule_group_ids = self::get_rule_group_ids( $dbo, $mapping_id ); |
|
487 | + $rule_group_ids = self::get_rule_group_ids($dbo, $mapping_id); |
|
488 | 488 | // Loop through rule group list and save the rule group. |
489 | - foreach ( $rule_group_list as $rule_group ) { |
|
490 | - if ( array_key_exists( 'rule_group_id', $rule_group ) ) { |
|
489 | + foreach ($rule_group_list as $rule_group) { |
|
490 | + if (array_key_exists('rule_group_id', $rule_group)) { |
|
491 | 491 | $rule_group_id = $rule_group['rule_group_id']; |
492 | 492 | } else { |
493 | 493 | // New rule group, should create new rule group id. |
494 | - $rule_group_id = $dbo->insert_rule_group( $mapping_id ); |
|
494 | + $rule_group_id = $dbo->insert_rule_group($mapping_id); |
|
495 | 495 | } |
496 | 496 | $index_to_be_removed = array_search( |
497 | 497 | (int) $rule_group_id, |
498 | 498 | $rule_group_ids, |
499 | 499 | true |
500 | 500 | ); |
501 | - if ( false !== $index_to_be_removed ) { |
|
502 | - unset( $rule_group_ids[ $index_to_be_removed ] ); |
|
501 | + if (false !== $index_to_be_removed) { |
|
502 | + unset($rule_group_ids[$index_to_be_removed]); |
|
503 | 503 | } |
504 | - self::save_rules( $dbo, $rule_group_id, $rule_group['rules'] ); |
|
504 | + self::save_rules($dbo, $rule_group_id, $rule_group['rules']); |
|
505 | 505 | } |
506 | 506 | |
507 | 507 | // Remove all the rule groups which are not posted. |
508 | - foreach ( $rule_group_ids as $rule_group_id ) { |
|
509 | - $dbo->delete_rule_group_item( $rule_group_id ); |
|
508 | + foreach ($rule_group_ids as $rule_group_id) { |
|
509 | + $dbo->delete_rule_group_item($rule_group_id); |
|
510 | 510 | } |
511 | 511 | } |
512 | 512 | |
@@ -517,33 +517,33 @@ discard block |
||
517 | 517 | * |
518 | 518 | * @return array |
519 | 519 | */ |
520 | - public static function insert_or_update_mapping_item( $request ) { |
|
520 | + public static function insert_or_update_mapping_item($request) { |
|
521 | 521 | $post_data = $request->get_params() === null ? array() : $request->get_params(); |
522 | 522 | $dbo = new Mappings_DBO(); |
523 | 523 | // check if valid object is posted. |
524 | - if ( array_key_exists( 'mapping_title', $post_data ) && |
|
525 | - array_key_exists( 'rule_group_list', $post_data ) && |
|
526 | - array_key_exists( 'property_list', $post_data ) ) { |
|
524 | + if (array_key_exists('mapping_title', $post_data) && |
|
525 | + array_key_exists('rule_group_list', $post_data) && |
|
526 | + array_key_exists('property_list', $post_data)) { |
|
527 | 527 | // Do validation, remove all incomplete data. |
528 | 528 | $mapping_item = array(); |
529 | - if ( array_key_exists( 'mapping_id', $post_data ) ) { |
|
529 | + if (array_key_exists('mapping_id', $post_data)) { |
|
530 | 530 | $mapping_item['mapping_id'] = $post_data['mapping_id']; |
531 | 531 | } |
532 | 532 | $mapping_item['mapping_title'] = $post_data['mapping_title']; |
533 | 533 | // lets save the mapping item. |
534 | - $mapping_id = $dbo->insert_or_update_mapping_item( $mapping_item ); |
|
535 | - self::save_rule_group_list( $dbo, $mapping_id, $post_data['rule_group_list'] ); |
|
536 | - self::save_property_list( $dbo, $mapping_id, $post_data['property_list'] ); |
|
534 | + $mapping_id = $dbo->insert_or_update_mapping_item($mapping_item); |
|
535 | + self::save_rule_group_list($dbo, $mapping_id, $post_data['rule_group_list']); |
|
536 | + self::save_property_list($dbo, $mapping_id, $post_data['property_list']); |
|
537 | 537 | |
538 | 538 | return array( |
539 | 539 | 'status' => 'success', |
540 | - 'message' => __( 'Successfully saved mapping item', 'wordlift' ), |
|
540 | + 'message' => __('Successfully saved mapping item', 'wordlift'), |
|
541 | 541 | 'mapping_id' => (int) $mapping_id, |
542 | 542 | ); |
543 | 543 | } else { |
544 | 544 | return array( |
545 | 545 | 'status' => 'error', |
546 | - 'message' => __( 'Unable to save mapping item', 'wordlift' ), |
|
546 | + 'message' => __('Unable to save mapping item', 'wordlift'), |
|
547 | 547 | ); |
548 | 548 | } |
549 | 549 | } |
@@ -16,144 +16,144 @@ |
||
16 | 16 | use Wordlift\Mappings\Validators\Rule_Groups_Validator; |
17 | 17 | |
18 | 18 | final class Mappings_Validator { |
19 | - const TRASH_CATEGORY = 'trash'; |
|
20 | - const ACTIVE_CATEGORY = 'active'; |
|
21 | - |
|
22 | - /** |
|
23 | - * The {@link Mappings_DBO} instance to test. |
|
24 | - * |
|
25 | - * @since 3.25.0 |
|
26 | - * @access private |
|
27 | - * @var Mappings_DBO $dbo The {@link Mappings_DBO} instance to test. |
|
28 | - */ |
|
29 | - private $dbo; |
|
30 | - |
|
31 | - /** |
|
32 | - * @var Rule_Groups_Validator |
|
33 | - */ |
|
34 | - private $rule_groups_validator; |
|
35 | - |
|
36 | - /** |
|
37 | - * Constructor for Wordlift_Mapping_Validator. |
|
38 | - * |
|
39 | - * @param Mappings_DBO $dbo The {@link Mappings_DBO} instance. |
|
40 | - * @param Rule_Groups_Validator $rule_groups_validator |
|
41 | - */ |
|
42 | - public function __construct( $dbo, $rule_groups_validator ) { |
|
43 | - |
|
44 | - $this->dbo = $dbo; |
|
45 | - $this->rule_groups_validator = $rule_groups_validator; |
|
46 | - |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * This method is used to filter properties based on presence |
|
51 | - * of certain key values. |
|
52 | - * |
|
53 | - * @param $items array Array of properties. |
|
54 | - * |
|
55 | - * @return array |
|
56 | - */ |
|
57 | - private static function filter_properties_for_required_keys( $items ) { |
|
58 | - return array_filter( |
|
59 | - $items, |
|
60 | - function ( $item ) { |
|
61 | - /** |
|
62 | - * Since the properties might also be passed |
|
63 | - * by external plugins, we might need to check if |
|
64 | - * they have correct data format. |
|
65 | - */ |
|
66 | - if ( ! array_key_exists( 'property_name', $item ) || |
|
67 | - ! array_key_exists( 'field_type', $item ) || |
|
68 | - ! array_key_exists( 'field_name', $item ) || |
|
69 | - ! array_key_exists( 'transform_function', $item ) |
|
70 | - ) { |
|
71 | - // If these keys doesnt exist, then dont process. |
|
72 | - return false; |
|
73 | - } else { |
|
74 | - // If the keys exist, then filter it. |
|
75 | - return true; |
|
76 | - } |
|
77 | - } |
|
78 | - ); |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Validates two values based on the passed logic |
|
83 | - * a single rule passes the user defined logic. |
|
84 | - * |
|
85 | - * @param string $key The key which every object has mapped to our value. |
|
86 | - * @param array $items The array of items. |
|
87 | - * @param string $status The value which the items should have. |
|
88 | - * |
|
89 | - * @return array |
|
90 | - */ |
|
91 | - private static function get_property_item_by_status( $key, $items, $status ) { |
|
92 | - return array_filter( |
|
93 | - $items, |
|
94 | - function ( $item ) use ( $key, $status ) { |
|
95 | - return $item[ $key ] === (string) $status; |
|
96 | - } |
|
97 | - ); |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Validates a post id with the list of active mapping items and check if |
|
102 | - * a mapping can be applied. |
|
103 | - * |
|
104 | - * @param int $identifier The post id or term id based on type. |
|
105 | - * |
|
106 | - * @param string $type Post or term. |
|
107 | - * |
|
108 | - * @return array |
|
109 | - */ |
|
110 | - public function validate( $identifier, $type ) { |
|
111 | - // Reset the valid property items before making the validation. |
|
112 | - $properties = array(); |
|
113 | - |
|
114 | - // Filter registered properties |
|
115 | - $filter_registered_properties = array(); |
|
116 | - |
|
117 | - // Get active mappings. |
|
118 | - $mappings = $this->dbo->get_active_mappings(); |
|
119 | - /** |
|
120 | - * Apply this filter to get mappings from external plugins. |
|
121 | - * |
|
122 | - * @param $mappings array Array of mappings from database. |
|
123 | - * @param $identifier int The post id or term id based on type. |
|
124 | - */ |
|
125 | - $mappings = apply_filters( 'wl_mappings_post', $mappings, $identifier ); |
|
126 | - |
|
127 | - // Get all active rule groups for the mapping items. |
|
128 | - foreach ( $mappings as $mapping ) { |
|
129 | - if ( array_key_exists( 'mapping_id', $mapping ) ) { |
|
130 | - $rule_groups = $this->dbo->get_rule_groups_by_mapping( (int) $mapping['mapping_id'] ); |
|
131 | - $should_apply_mapping = $this->rule_groups_validator->is_valid( $identifier, $rule_groups, $type ); |
|
132 | - if ( $should_apply_mapping ) { |
|
133 | - $mapping_item_properties = $this->dbo->get_properties( $mapping['mapping_id'] ); |
|
134 | - $properties = array_merge( $properties, $mapping_item_properties ); |
|
135 | - } |
|
136 | - } else { |
|
137 | - /** |
|
138 | - * This is a programmatically defined mapping, |
|
139 | - * so we will have the rule groups and the properties in the array keys |
|
140 | - */ |
|
141 | - if ( array_key_exists( 'properties', $mapping ) && |
|
142 | - is_array( $mapping['properties'] ) ) { |
|
143 | - $filter_registered_properties = array_merge( $filter_registered_properties, $mapping['properties'] ); |
|
144 | - } |
|
145 | - } |
|
146 | - } |
|
147 | - // Filter all registered properties based on required key values. |
|
148 | - $filter_registered_properties = self::filter_properties_for_required_keys( $filter_registered_properties ); |
|
149 | - $active_properties = self::get_property_item_by_status( |
|
150 | - 'property_status', |
|
151 | - $properties, |
|
152 | - self::ACTIVE_CATEGORY |
|
153 | - ); |
|
154 | - |
|
155 | - // Merge ui defined properties with filter registered properties. |
|
156 | - return array_merge( $active_properties, $filter_registered_properties ); |
|
157 | - } |
|
19 | + const TRASH_CATEGORY = 'trash'; |
|
20 | + const ACTIVE_CATEGORY = 'active'; |
|
21 | + |
|
22 | + /** |
|
23 | + * The {@link Mappings_DBO} instance to test. |
|
24 | + * |
|
25 | + * @since 3.25.0 |
|
26 | + * @access private |
|
27 | + * @var Mappings_DBO $dbo The {@link Mappings_DBO} instance to test. |
|
28 | + */ |
|
29 | + private $dbo; |
|
30 | + |
|
31 | + /** |
|
32 | + * @var Rule_Groups_Validator |
|
33 | + */ |
|
34 | + private $rule_groups_validator; |
|
35 | + |
|
36 | + /** |
|
37 | + * Constructor for Wordlift_Mapping_Validator. |
|
38 | + * |
|
39 | + * @param Mappings_DBO $dbo The {@link Mappings_DBO} instance. |
|
40 | + * @param Rule_Groups_Validator $rule_groups_validator |
|
41 | + */ |
|
42 | + public function __construct( $dbo, $rule_groups_validator ) { |
|
43 | + |
|
44 | + $this->dbo = $dbo; |
|
45 | + $this->rule_groups_validator = $rule_groups_validator; |
|
46 | + |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * This method is used to filter properties based on presence |
|
51 | + * of certain key values. |
|
52 | + * |
|
53 | + * @param $items array Array of properties. |
|
54 | + * |
|
55 | + * @return array |
|
56 | + */ |
|
57 | + private static function filter_properties_for_required_keys( $items ) { |
|
58 | + return array_filter( |
|
59 | + $items, |
|
60 | + function ( $item ) { |
|
61 | + /** |
|
62 | + * Since the properties might also be passed |
|
63 | + * by external plugins, we might need to check if |
|
64 | + * they have correct data format. |
|
65 | + */ |
|
66 | + if ( ! array_key_exists( 'property_name', $item ) || |
|
67 | + ! array_key_exists( 'field_type', $item ) || |
|
68 | + ! array_key_exists( 'field_name', $item ) || |
|
69 | + ! array_key_exists( 'transform_function', $item ) |
|
70 | + ) { |
|
71 | + // If these keys doesnt exist, then dont process. |
|
72 | + return false; |
|
73 | + } else { |
|
74 | + // If the keys exist, then filter it. |
|
75 | + return true; |
|
76 | + } |
|
77 | + } |
|
78 | + ); |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Validates two values based on the passed logic |
|
83 | + * a single rule passes the user defined logic. |
|
84 | + * |
|
85 | + * @param string $key The key which every object has mapped to our value. |
|
86 | + * @param array $items The array of items. |
|
87 | + * @param string $status The value which the items should have. |
|
88 | + * |
|
89 | + * @return array |
|
90 | + */ |
|
91 | + private static function get_property_item_by_status( $key, $items, $status ) { |
|
92 | + return array_filter( |
|
93 | + $items, |
|
94 | + function ( $item ) use ( $key, $status ) { |
|
95 | + return $item[ $key ] === (string) $status; |
|
96 | + } |
|
97 | + ); |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Validates a post id with the list of active mapping items and check if |
|
102 | + * a mapping can be applied. |
|
103 | + * |
|
104 | + * @param int $identifier The post id or term id based on type. |
|
105 | + * |
|
106 | + * @param string $type Post or term. |
|
107 | + * |
|
108 | + * @return array |
|
109 | + */ |
|
110 | + public function validate( $identifier, $type ) { |
|
111 | + // Reset the valid property items before making the validation. |
|
112 | + $properties = array(); |
|
113 | + |
|
114 | + // Filter registered properties |
|
115 | + $filter_registered_properties = array(); |
|
116 | + |
|
117 | + // Get active mappings. |
|
118 | + $mappings = $this->dbo->get_active_mappings(); |
|
119 | + /** |
|
120 | + * Apply this filter to get mappings from external plugins. |
|
121 | + * |
|
122 | + * @param $mappings array Array of mappings from database. |
|
123 | + * @param $identifier int The post id or term id based on type. |
|
124 | + */ |
|
125 | + $mappings = apply_filters( 'wl_mappings_post', $mappings, $identifier ); |
|
126 | + |
|
127 | + // Get all active rule groups for the mapping items. |
|
128 | + foreach ( $mappings as $mapping ) { |
|
129 | + if ( array_key_exists( 'mapping_id', $mapping ) ) { |
|
130 | + $rule_groups = $this->dbo->get_rule_groups_by_mapping( (int) $mapping['mapping_id'] ); |
|
131 | + $should_apply_mapping = $this->rule_groups_validator->is_valid( $identifier, $rule_groups, $type ); |
|
132 | + if ( $should_apply_mapping ) { |
|
133 | + $mapping_item_properties = $this->dbo->get_properties( $mapping['mapping_id'] ); |
|
134 | + $properties = array_merge( $properties, $mapping_item_properties ); |
|
135 | + } |
|
136 | + } else { |
|
137 | + /** |
|
138 | + * This is a programmatically defined mapping, |
|
139 | + * so we will have the rule groups and the properties in the array keys |
|
140 | + */ |
|
141 | + if ( array_key_exists( 'properties', $mapping ) && |
|
142 | + is_array( $mapping['properties'] ) ) { |
|
143 | + $filter_registered_properties = array_merge( $filter_registered_properties, $mapping['properties'] ); |
|
144 | + } |
|
145 | + } |
|
146 | + } |
|
147 | + // Filter all registered properties based on required key values. |
|
148 | + $filter_registered_properties = self::filter_properties_for_required_keys( $filter_registered_properties ); |
|
149 | + $active_properties = self::get_property_item_by_status( |
|
150 | + 'property_status', |
|
151 | + $properties, |
|
152 | + self::ACTIVE_CATEGORY |
|
153 | + ); |
|
154 | + |
|
155 | + // Merge ui defined properties with filter registered properties. |
|
156 | + return array_merge( $active_properties, $filter_registered_properties ); |
|
157 | + } |
|
158 | 158 | |
159 | 159 | } |
@@ -39,7 +39,7 @@ discard block |
||
39 | 39 | * @param Mappings_DBO $dbo The {@link Mappings_DBO} instance. |
40 | 40 | * @param Rule_Groups_Validator $rule_groups_validator |
41 | 41 | */ |
42 | - public function __construct( $dbo, $rule_groups_validator ) { |
|
42 | + public function __construct($dbo, $rule_groups_validator) { |
|
43 | 43 | |
44 | 44 | $this->dbo = $dbo; |
45 | 45 | $this->rule_groups_validator = $rule_groups_validator; |
@@ -54,19 +54,19 @@ discard block |
||
54 | 54 | * |
55 | 55 | * @return array |
56 | 56 | */ |
57 | - private static function filter_properties_for_required_keys( $items ) { |
|
57 | + private static function filter_properties_for_required_keys($items) { |
|
58 | 58 | return array_filter( |
59 | 59 | $items, |
60 | - function ( $item ) { |
|
60 | + function($item) { |
|
61 | 61 | /** |
62 | 62 | * Since the properties might also be passed |
63 | 63 | * by external plugins, we might need to check if |
64 | 64 | * they have correct data format. |
65 | 65 | */ |
66 | - if ( ! array_key_exists( 'property_name', $item ) || |
|
67 | - ! array_key_exists( 'field_type', $item ) || |
|
68 | - ! array_key_exists( 'field_name', $item ) || |
|
69 | - ! array_key_exists( 'transform_function', $item ) |
|
66 | + if ( ! array_key_exists('property_name', $item) || |
|
67 | + ! array_key_exists('field_type', $item) || |
|
68 | + ! array_key_exists('field_name', $item) || |
|
69 | + ! array_key_exists('transform_function', $item) |
|
70 | 70 | ) { |
71 | 71 | // If these keys doesnt exist, then dont process. |
72 | 72 | return false; |
@@ -88,11 +88,11 @@ discard block |
||
88 | 88 | * |
89 | 89 | * @return array |
90 | 90 | */ |
91 | - private static function get_property_item_by_status( $key, $items, $status ) { |
|
91 | + private static function get_property_item_by_status($key, $items, $status) { |
|
92 | 92 | return array_filter( |
93 | 93 | $items, |
94 | - function ( $item ) use ( $key, $status ) { |
|
95 | - return $item[ $key ] === (string) $status; |
|
94 | + function($item) use ($key, $status) { |
|
95 | + return $item[$key] === (string) $status; |
|
96 | 96 | } |
97 | 97 | ); |
98 | 98 | } |
@@ -107,7 +107,7 @@ discard block |
||
107 | 107 | * |
108 | 108 | * @return array |
109 | 109 | */ |
110 | - public function validate( $identifier, $type ) { |
|
110 | + public function validate($identifier, $type) { |
|
111 | 111 | // Reset the valid property items before making the validation. |
112 | 112 | $properties = array(); |
113 | 113 | |
@@ -122,30 +122,30 @@ discard block |
||
122 | 122 | * @param $mappings array Array of mappings from database. |
123 | 123 | * @param $identifier int The post id or term id based on type. |
124 | 124 | */ |
125 | - $mappings = apply_filters( 'wl_mappings_post', $mappings, $identifier ); |
|
125 | + $mappings = apply_filters('wl_mappings_post', $mappings, $identifier); |
|
126 | 126 | |
127 | 127 | // Get all active rule groups for the mapping items. |
128 | - foreach ( $mappings as $mapping ) { |
|
129 | - if ( array_key_exists( 'mapping_id', $mapping ) ) { |
|
130 | - $rule_groups = $this->dbo->get_rule_groups_by_mapping( (int) $mapping['mapping_id'] ); |
|
131 | - $should_apply_mapping = $this->rule_groups_validator->is_valid( $identifier, $rule_groups, $type ); |
|
132 | - if ( $should_apply_mapping ) { |
|
133 | - $mapping_item_properties = $this->dbo->get_properties( $mapping['mapping_id'] ); |
|
134 | - $properties = array_merge( $properties, $mapping_item_properties ); |
|
128 | + foreach ($mappings as $mapping) { |
|
129 | + if (array_key_exists('mapping_id', $mapping)) { |
|
130 | + $rule_groups = $this->dbo->get_rule_groups_by_mapping((int) $mapping['mapping_id']); |
|
131 | + $should_apply_mapping = $this->rule_groups_validator->is_valid($identifier, $rule_groups, $type); |
|
132 | + if ($should_apply_mapping) { |
|
133 | + $mapping_item_properties = $this->dbo->get_properties($mapping['mapping_id']); |
|
134 | + $properties = array_merge($properties, $mapping_item_properties); |
|
135 | 135 | } |
136 | 136 | } else { |
137 | 137 | /** |
138 | 138 | * This is a programmatically defined mapping, |
139 | 139 | * so we will have the rule groups and the properties in the array keys |
140 | 140 | */ |
141 | - if ( array_key_exists( 'properties', $mapping ) && |
|
142 | - is_array( $mapping['properties'] ) ) { |
|
143 | - $filter_registered_properties = array_merge( $filter_registered_properties, $mapping['properties'] ); |
|
141 | + if (array_key_exists('properties', $mapping) && |
|
142 | + is_array($mapping['properties'])) { |
|
143 | + $filter_registered_properties = array_merge($filter_registered_properties, $mapping['properties']); |
|
144 | 144 | } |
145 | 145 | } |
146 | 146 | } |
147 | 147 | // Filter all registered properties based on required key values. |
148 | - $filter_registered_properties = self::filter_properties_for_required_keys( $filter_registered_properties ); |
|
148 | + $filter_registered_properties = self::filter_properties_for_required_keys($filter_registered_properties); |
|
149 | 149 | $active_properties = self::get_property_item_by_status( |
150 | 150 | 'property_status', |
151 | 151 | $properties, |
@@ -153,7 +153,7 @@ discard block |
||
153 | 153 | ); |
154 | 154 | |
155 | 155 | // Merge ui defined properties with filter registered properties. |
156 | - return array_merge( $active_properties, $filter_registered_properties ); |
|
156 | + return array_merge($active_properties, $filter_registered_properties); |
|
157 | 157 | } |
158 | 158 | |
159 | 159 | } |
@@ -15,64 +15,64 @@ |
||
15 | 15 | */ |
16 | 16 | class Taxonomy_Option { |
17 | 17 | |
18 | - /** |
|
19 | - * This value is used in rule field one and rule field two |
|
20 | - * options are linked to one by this value |
|
21 | - */ |
|
22 | - const PARENT_VALUE = 'taxonomy'; |
|
18 | + /** |
|
19 | + * This value is used in rule field one and rule field two |
|
20 | + * options are linked to one by this value |
|
21 | + */ |
|
22 | + const PARENT_VALUE = 'taxonomy'; |
|
23 | 23 | |
24 | - public function add_taxonomy_option() { |
|
25 | - $this->add_rule_field_one_options(); |
|
26 | - $this->add_rule_field_two_options(); |
|
27 | - } |
|
24 | + public function add_taxonomy_option() { |
|
25 | + $this->add_rule_field_one_options(); |
|
26 | + $this->add_rule_field_two_options(); |
|
27 | + } |
|
28 | 28 | |
29 | - private function add_rule_field_one_options() { |
|
29 | + private function add_rule_field_one_options() { |
|
30 | 30 | |
31 | - add_filter( |
|
32 | - 'wl_mappings_rule_field_one_options', |
|
33 | - function ( $rule_field_one_options ) { |
|
31 | + add_filter( |
|
32 | + 'wl_mappings_rule_field_one_options', |
|
33 | + function ( $rule_field_one_options ) { |
|
34 | 34 | |
35 | - $rule_field_one_options[] = array( |
|
36 | - 'label' => __( 'Taxonomy', 'wordlift' ), |
|
37 | - 'value' => Taxonomy_Option::PARENT_VALUE, |
|
38 | - // Left empty since these values are provided locally, not needed to be fetched from |
|
39 | - // api. |
|
40 | - 'api_source' => '', |
|
41 | - ); |
|
35 | + $rule_field_one_options[] = array( |
|
36 | + 'label' => __( 'Taxonomy', 'wordlift' ), |
|
37 | + 'value' => Taxonomy_Option::PARENT_VALUE, |
|
38 | + // Left empty since these values are provided locally, not needed to be fetched from |
|
39 | + // api. |
|
40 | + 'api_source' => '', |
|
41 | + ); |
|
42 | 42 | |
43 | - return $rule_field_one_options; |
|
43 | + return $rule_field_one_options; |
|
44 | 44 | |
45 | - } |
|
46 | - ); |
|
47 | - } |
|
45 | + } |
|
46 | + ); |
|
47 | + } |
|
48 | 48 | |
49 | - private function add_rule_field_two_options() { |
|
49 | + private function add_rule_field_two_options() { |
|
50 | 50 | |
51 | - add_filter( |
|
52 | - 'wl_mappings_rule_field_two_options', |
|
53 | - function ( $rule_field_two_options ) { |
|
51 | + add_filter( |
|
52 | + 'wl_mappings_rule_field_two_options', |
|
53 | + function ( $rule_field_two_options ) { |
|
54 | 54 | |
55 | - $taxonomies = get_object_taxonomies( 'post', 'objects' ); |
|
55 | + $taxonomies = get_object_taxonomies( 'post', 'objects' ); |
|
56 | 56 | |
57 | - $taxonomy_options = array(); |
|
57 | + $taxonomy_options = array(); |
|
58 | 58 | |
59 | - foreach ( $taxonomies as $item ) { |
|
60 | - /** |
|
61 | - * $item Taxonomy |
|
62 | - */ |
|
63 | - $taxonomy_options[] = array( |
|
64 | - 'label' => $item->label, |
|
65 | - 'value' => $item->name, |
|
66 | - // The value of parent option on rule field one. |
|
67 | - 'parent_value' => Taxonomy_Option::PARENT_VALUE, |
|
68 | - ); |
|
69 | - } |
|
59 | + foreach ( $taxonomies as $item ) { |
|
60 | + /** |
|
61 | + * $item Taxonomy |
|
62 | + */ |
|
63 | + $taxonomy_options[] = array( |
|
64 | + 'label' => $item->label, |
|
65 | + 'value' => $item->name, |
|
66 | + // The value of parent option on rule field one. |
|
67 | + 'parent_value' => Taxonomy_Option::PARENT_VALUE, |
|
68 | + ); |
|
69 | + } |
|
70 | 70 | |
71 | - return array_merge( $rule_field_two_options, $taxonomy_options ); |
|
71 | + return array_merge( $rule_field_two_options, $taxonomy_options ); |
|
72 | 72 | |
73 | - } |
|
74 | - ); |
|
73 | + } |
|
74 | + ); |
|
75 | 75 | |
76 | - } |
|
76 | + } |
|
77 | 77 | |
78 | 78 | } |
@@ -30,10 +30,10 @@ discard block |
||
30 | 30 | |
31 | 31 | add_filter( |
32 | 32 | 'wl_mappings_rule_field_one_options', |
33 | - function ( $rule_field_one_options ) { |
|
33 | + function($rule_field_one_options) { |
|
34 | 34 | |
35 | 35 | $rule_field_one_options[] = array( |
36 | - 'label' => __( 'Taxonomy', 'wordlift' ), |
|
36 | + 'label' => __('Taxonomy', 'wordlift'), |
|
37 | 37 | 'value' => Taxonomy_Option::PARENT_VALUE, |
38 | 38 | // Left empty since these values are provided locally, not needed to be fetched from |
39 | 39 | // api. |
@@ -50,13 +50,13 @@ discard block |
||
50 | 50 | |
51 | 51 | add_filter( |
52 | 52 | 'wl_mappings_rule_field_two_options', |
53 | - function ( $rule_field_two_options ) { |
|
53 | + function($rule_field_two_options) { |
|
54 | 54 | |
55 | - $taxonomies = get_object_taxonomies( 'post', 'objects' ); |
|
55 | + $taxonomies = get_object_taxonomies('post', 'objects'); |
|
56 | 56 | |
57 | 57 | $taxonomy_options = array(); |
58 | 58 | |
59 | - foreach ( $taxonomies as $item ) { |
|
59 | + foreach ($taxonomies as $item) { |
|
60 | 60 | /** |
61 | 61 | * $item Taxonomy |
62 | 62 | */ |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | ); |
69 | 69 | } |
70 | 70 | |
71 | - return array_merge( $rule_field_two_options, $taxonomy_options ); |
|
71 | + return array_merge($rule_field_two_options, $taxonomy_options); |
|
72 | 72 | |
73 | 73 | } |
74 | 74 | ); |
@@ -11,44 +11,44 @@ |
||
11 | 11 | |
12 | 12 | class Data_Source_Factory { |
13 | 13 | |
14 | - private static $instance = null; |
|
15 | - |
|
16 | - private $data_sources = array(); |
|
17 | - |
|
18 | - public function __construct() { |
|
19 | - $this->data_sources = array( |
|
20 | - 'acf' => new Acf_Data_Source(), |
|
21 | - 'meta' => new Meta_Data_Source(), |
|
22 | - ); |
|
23 | - } |
|
24 | - |
|
25 | - /** |
|
26 | - * @param $identifier int post id or term id based on type. |
|
27 | - * @param $property_data array |
|
28 | - * @param $type string post or term. |
|
29 | - * |
|
30 | - * @return mixed |
|
31 | - */ |
|
32 | - public function get_data( $identifier, $property_data, $type ) { |
|
33 | - switch ( $property_data['field_type'] ) { |
|
34 | - case Jsonld_Converter::FIELD_TYPE_ACF: |
|
35 | - return $this->data_sources['acf']->get_data( $identifier, $property_data, $type ); |
|
36 | - case Jsonld_Converter::FIELD_TYPE_CUSTOM_FIELD: |
|
37 | - return $this->data_sources['meta']->get_data( $identifier, $property_data, $type ); |
|
38 | - default: |
|
39 | - return $property_data['field_name']; |
|
40 | - } |
|
41 | - } |
|
42 | - |
|
43 | - /** |
|
44 | - * @return Data_Source_Factory |
|
45 | - */ |
|
46 | - public static function get_instance() { |
|
47 | - if ( null === self::$instance ) { |
|
48 | - self::$instance = new Data_Source_Factory(); |
|
49 | - } |
|
50 | - |
|
51 | - return self::$instance; |
|
52 | - } |
|
14 | + private static $instance = null; |
|
15 | + |
|
16 | + private $data_sources = array(); |
|
17 | + |
|
18 | + public function __construct() { |
|
19 | + $this->data_sources = array( |
|
20 | + 'acf' => new Acf_Data_Source(), |
|
21 | + 'meta' => new Meta_Data_Source(), |
|
22 | + ); |
|
23 | + } |
|
24 | + |
|
25 | + /** |
|
26 | + * @param $identifier int post id or term id based on type. |
|
27 | + * @param $property_data array |
|
28 | + * @param $type string post or term. |
|
29 | + * |
|
30 | + * @return mixed |
|
31 | + */ |
|
32 | + public function get_data( $identifier, $property_data, $type ) { |
|
33 | + switch ( $property_data['field_type'] ) { |
|
34 | + case Jsonld_Converter::FIELD_TYPE_ACF: |
|
35 | + return $this->data_sources['acf']->get_data( $identifier, $property_data, $type ); |
|
36 | + case Jsonld_Converter::FIELD_TYPE_CUSTOM_FIELD: |
|
37 | + return $this->data_sources['meta']->get_data( $identifier, $property_data, $type ); |
|
38 | + default: |
|
39 | + return $property_data['field_name']; |
|
40 | + } |
|
41 | + } |
|
42 | + |
|
43 | + /** |
|
44 | + * @return Data_Source_Factory |
|
45 | + */ |
|
46 | + public static function get_instance() { |
|
47 | + if ( null === self::$instance ) { |
|
48 | + self::$instance = new Data_Source_Factory(); |
|
49 | + } |
|
50 | + |
|
51 | + return self::$instance; |
|
52 | + } |
|
53 | 53 | |
54 | 54 | } |
@@ -29,12 +29,12 @@ discard block |
||
29 | 29 | * |
30 | 30 | * @return mixed |
31 | 31 | */ |
32 | - public function get_data( $identifier, $property_data, $type ) { |
|
33 | - switch ( $property_data['field_type'] ) { |
|
32 | + public function get_data($identifier, $property_data, $type) { |
|
33 | + switch ($property_data['field_type']) { |
|
34 | 34 | case Jsonld_Converter::FIELD_TYPE_ACF: |
35 | - return $this->data_sources['acf']->get_data( $identifier, $property_data, $type ); |
|
35 | + return $this->data_sources['acf']->get_data($identifier, $property_data, $type); |
|
36 | 36 | case Jsonld_Converter::FIELD_TYPE_CUSTOM_FIELD: |
37 | - return $this->data_sources['meta']->get_data( $identifier, $property_data, $type ); |
|
37 | + return $this->data_sources['meta']->get_data($identifier, $property_data, $type); |
|
38 | 38 | default: |
39 | 39 | return $property_data['field_name']; |
40 | 40 | } |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | * @return Data_Source_Factory |
45 | 45 | */ |
46 | 46 | public static function get_instance() { |
47 | - if ( null === self::$instance ) { |
|
47 | + if (null === self::$instance) { |
|
48 | 48 | self::$instance = new Data_Source_Factory(); |
49 | 49 | } |
50 | 50 |
@@ -11,63 +11,63 @@ |
||
11 | 11 | |
12 | 12 | class Acf_Data_Source implements Abstract_Data_Source { |
13 | 13 | |
14 | - public function get_data( $identifier, $property_data, $type ) { |
|
14 | + public function get_data( $identifier, $property_data, $type ) { |
|
15 | 15 | |
16 | - if ( ! function_exists( 'get_field' ) || ! function_exists( 'get_field_object' ) ) { |
|
17 | - return array(); |
|
18 | - } |
|
16 | + if ( ! function_exists( 'get_field' ) || ! function_exists( 'get_field_object' ) ) { |
|
17 | + return array(); |
|
18 | + } |
|
19 | 19 | |
20 | - return $this->get_data_for_acf_field( $property_data['field_name'], $identifier, $type ); |
|
21 | - } |
|
20 | + return $this->get_data_for_acf_field( $property_data['field_name'], $identifier, $type ); |
|
21 | + } |
|
22 | 22 | |
23 | - /** |
|
24 | - * Gets data from acf, format the data if it is a repeater field. |
|
25 | - * |
|
26 | - * @param $field_name string |
|
27 | - * @param $identifier int Identifier ( post id or term id ) |
|
28 | - * |
|
29 | - * @return array|mixed |
|
30 | - */ |
|
31 | - private function get_data_for_acf_field( $field_name, $identifier, $type ) { |
|
32 | - if ( Jsonld_Converter::TERM === $type ) { |
|
33 | - $term = get_term( $identifier ); |
|
34 | - // Data fetching method for term is different. |
|
35 | - $field_data = get_field_object( $field_name, $term ); |
|
36 | - $data = get_field( $field_name, $term ); |
|
37 | - } else { |
|
38 | - $field_data = get_field_object( $field_name, $identifier ); |
|
39 | - $data = get_field( $field_name, $identifier ); |
|
40 | - } |
|
41 | - // only process if it is a repeater field, else return the data. |
|
42 | - if ( is_array( $field_data ) && array_key_exists( 'type', $field_data ) |
|
43 | - && 'repeater' === $field_data['type'] ) { |
|
44 | - /** |
|
45 | - * check if we have only one sub field, currently we only support one subfield, |
|
46 | - * so each repeater item should be checked if there is a single sub field. |
|
47 | - */ |
|
48 | - if ( is_array( $data ) && |
|
49 | - count( $data ) > 0 && |
|
50 | - count( array_keys( $data[0] ) ) === 1 ) { |
|
51 | - $repeater_formatted_data = array(); |
|
52 | - foreach ( $data as $item ) { |
|
53 | - $repeater_formatted_data = array_merge( $repeater_formatted_data, array_values( $item ) ); |
|
54 | - } |
|
55 | - // Remove non unique values. |
|
56 | - $repeater_formatted_data = array_unique( $repeater_formatted_data ); |
|
57 | - // Remove empty values |
|
58 | - $repeater_formatted_data = array_filter( |
|
59 | - $repeater_formatted_data, |
|
60 | - function ( $item ) { |
|
61 | - return is_array( $item ) || strlen( $item ); |
|
62 | - } |
|
63 | - ); |
|
23 | + /** |
|
24 | + * Gets data from acf, format the data if it is a repeater field. |
|
25 | + * |
|
26 | + * @param $field_name string |
|
27 | + * @param $identifier int Identifier ( post id or term id ) |
|
28 | + * |
|
29 | + * @return array|mixed |
|
30 | + */ |
|
31 | + private function get_data_for_acf_field( $field_name, $identifier, $type ) { |
|
32 | + if ( Jsonld_Converter::TERM === $type ) { |
|
33 | + $term = get_term( $identifier ); |
|
34 | + // Data fetching method for term is different. |
|
35 | + $field_data = get_field_object( $field_name, $term ); |
|
36 | + $data = get_field( $field_name, $term ); |
|
37 | + } else { |
|
38 | + $field_data = get_field_object( $field_name, $identifier ); |
|
39 | + $data = get_field( $field_name, $identifier ); |
|
40 | + } |
|
41 | + // only process if it is a repeater field, else return the data. |
|
42 | + if ( is_array( $field_data ) && array_key_exists( 'type', $field_data ) |
|
43 | + && 'repeater' === $field_data['type'] ) { |
|
44 | + /** |
|
45 | + * check if we have only one sub field, currently we only support one subfield, |
|
46 | + * so each repeater item should be checked if there is a single sub field. |
|
47 | + */ |
|
48 | + if ( is_array( $data ) && |
|
49 | + count( $data ) > 0 && |
|
50 | + count( array_keys( $data[0] ) ) === 1 ) { |
|
51 | + $repeater_formatted_data = array(); |
|
52 | + foreach ( $data as $item ) { |
|
53 | + $repeater_formatted_data = array_merge( $repeater_formatted_data, array_values( $item ) ); |
|
54 | + } |
|
55 | + // Remove non unique values. |
|
56 | + $repeater_formatted_data = array_unique( $repeater_formatted_data ); |
|
57 | + // Remove empty values |
|
58 | + $repeater_formatted_data = array_filter( |
|
59 | + $repeater_formatted_data, |
|
60 | + function ( $item ) { |
|
61 | + return is_array( $item ) || strlen( $item ); |
|
62 | + } |
|
63 | + ); |
|
64 | 64 | |
65 | - // re-index all the values. |
|
66 | - return array_values( $repeater_formatted_data ); |
|
67 | - } |
|
68 | - } |
|
65 | + // re-index all the values. |
|
66 | + return array_values( $repeater_formatted_data ); |
|
67 | + } |
|
68 | + } |
|
69 | 69 | |
70 | - // Return normal acf data if it is not a repeater field. |
|
71 | - return $data; |
|
72 | - } |
|
70 | + // Return normal acf data if it is not a repeater field. |
|
71 | + return $data; |
|
72 | + } |
|
73 | 73 | } |
@@ -11,13 +11,13 @@ discard block |
||
11 | 11 | |
12 | 12 | class Acf_Data_Source implements Abstract_Data_Source { |
13 | 13 | |
14 | - public function get_data( $identifier, $property_data, $type ) { |
|
14 | + public function get_data($identifier, $property_data, $type) { |
|
15 | 15 | |
16 | - if ( ! function_exists( 'get_field' ) || ! function_exists( 'get_field_object' ) ) { |
|
16 | + if ( ! function_exists('get_field') || ! function_exists('get_field_object')) { |
|
17 | 17 | return array(); |
18 | 18 | } |
19 | 19 | |
20 | - return $this->get_data_for_acf_field( $property_data['field_name'], $identifier, $type ); |
|
20 | + return $this->get_data_for_acf_field($property_data['field_name'], $identifier, $type); |
|
21 | 21 | } |
22 | 22 | |
23 | 23 | /** |
@@ -28,42 +28,42 @@ discard block |
||
28 | 28 | * |
29 | 29 | * @return array|mixed |
30 | 30 | */ |
31 | - private function get_data_for_acf_field( $field_name, $identifier, $type ) { |
|
32 | - if ( Jsonld_Converter::TERM === $type ) { |
|
33 | - $term = get_term( $identifier ); |
|
31 | + private function get_data_for_acf_field($field_name, $identifier, $type) { |
|
32 | + if (Jsonld_Converter::TERM === $type) { |
|
33 | + $term = get_term($identifier); |
|
34 | 34 | // Data fetching method for term is different. |
35 | - $field_data = get_field_object( $field_name, $term ); |
|
36 | - $data = get_field( $field_name, $term ); |
|
35 | + $field_data = get_field_object($field_name, $term); |
|
36 | + $data = get_field($field_name, $term); |
|
37 | 37 | } else { |
38 | - $field_data = get_field_object( $field_name, $identifier ); |
|
39 | - $data = get_field( $field_name, $identifier ); |
|
38 | + $field_data = get_field_object($field_name, $identifier); |
|
39 | + $data = get_field($field_name, $identifier); |
|
40 | 40 | } |
41 | 41 | // only process if it is a repeater field, else return the data. |
42 | - if ( is_array( $field_data ) && array_key_exists( 'type', $field_data ) |
|
43 | - && 'repeater' === $field_data['type'] ) { |
|
42 | + if (is_array($field_data) && array_key_exists('type', $field_data) |
|
43 | + && 'repeater' === $field_data['type']) { |
|
44 | 44 | /** |
45 | 45 | * check if we have only one sub field, currently we only support one subfield, |
46 | 46 | * so each repeater item should be checked if there is a single sub field. |
47 | 47 | */ |
48 | - if ( is_array( $data ) && |
|
49 | - count( $data ) > 0 && |
|
50 | - count( array_keys( $data[0] ) ) === 1 ) { |
|
48 | + if (is_array($data) && |
|
49 | + count($data) > 0 && |
|
50 | + count(array_keys($data[0])) === 1) { |
|
51 | 51 | $repeater_formatted_data = array(); |
52 | - foreach ( $data as $item ) { |
|
53 | - $repeater_formatted_data = array_merge( $repeater_formatted_data, array_values( $item ) ); |
|
52 | + foreach ($data as $item) { |
|
53 | + $repeater_formatted_data = array_merge($repeater_formatted_data, array_values($item)); |
|
54 | 54 | } |
55 | 55 | // Remove non unique values. |
56 | - $repeater_formatted_data = array_unique( $repeater_formatted_data ); |
|
56 | + $repeater_formatted_data = array_unique($repeater_formatted_data); |
|
57 | 57 | // Remove empty values |
58 | 58 | $repeater_formatted_data = array_filter( |
59 | 59 | $repeater_formatted_data, |
60 | - function ( $item ) { |
|
61 | - return is_array( $item ) || strlen( $item ); |
|
60 | + function($item) { |
|
61 | + return is_array($item) || strlen($item); |
|
62 | 62 | } |
63 | 63 | ); |
64 | 64 | |
65 | 65 | // re-index all the values. |
66 | - return array_values( $repeater_formatted_data ); |
|
66 | + return array_values($repeater_formatted_data); |
|
67 | 67 | } |
68 | 68 | } |
69 | 69 |