@@ -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. |
|
| 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. |
|
| 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. |
|
| 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. |
|
| 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. |
| 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 | } |
@@ -18,237 +18,237 @@ |
||
| 18 | 18 | * @since 3.25.0 |
| 19 | 19 | */ |
| 20 | 20 | class Jsonld_Converter { |
| 21 | - /** |
|
| 22 | - * Enumerations for the field types. |
|
| 23 | - */ |
|
| 24 | - const FIELD_TYPE_TEXT_FIELD = 'text'; |
|
| 25 | - const FIELD_TYPE_CUSTOM_FIELD = 'custom_field'; |
|
| 26 | - const FIELD_TYPE_ACF = 'acf'; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Mappings can be applied to either post |
|
| 30 | - * or term, the below is used to specify whether it is a post or a term. |
|
| 31 | - */ |
|
| 32 | - const POST = 'post'; |
|
| 33 | - const TERM = 'term'; |
|
| 34 | - /** |
|
| 35 | - * The {@link Mappings_Validator} instance to test. |
|
| 36 | - * |
|
| 37 | - * @since 3.25.0 |
|
| 38 | - * @access private |
|
| 39 | - * @var Mappings_Validator $validator The {@link Mappings_Validator} instance. |
|
| 40 | - */ |
|
| 41 | - private $validator; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * The {@link Mappings_Transform_Functions_Registry} instance. |
|
| 45 | - * |
|
| 46 | - * @since 3.25.0 |
|
| 47 | - * @access private |
|
| 48 | - * @var Mappings_Transform_Functions_Registry $transform_functions_registry The {@link Mappings_Transform_Functions_Registry} instance. |
|
| 49 | - */ |
|
| 50 | - private $transform_functions_registry; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Initialize all dependencies required. |
|
| 54 | - * |
|
| 55 | - * @param Mappings_Validator $validator A {@link Mappings_Validator} instance. |
|
| 56 | - * @param Mappings_Transform_Functions_Registry $transform_functions_registry |
|
| 57 | - */ |
|
| 58 | - public function __construct( $validator, $transform_functions_registry ) { |
|
| 59 | - |
|
| 60 | - $this->validator = $validator; |
|
| 61 | - $this->transform_functions_registry = $transform_functions_registry; |
|
| 62 | - |
|
| 63 | - // Hook to refactor the JSON-LD. |
|
| 64 | - add_filter( 'wl_post_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 2 ); |
|
| 65 | - add_filter( 'wl_entity_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 2 ); |
|
| 66 | - |
|
| 67 | - // This is wrong: `wl_term_jsonld_array` will provide a term ID not a post ID (that `wl_post_jsonld_array` expects). |
|
| 68 | - add_filter( 'wl_term_jsonld_array', array( $this, 'wl_term_jsonld_array' ), 11, 2 ); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Hook to `wl_term_jsonld_array`. |
|
| 73 | - * |
|
| 74 | - * Receive the JSON-LD and the references in the array along with the term ID and transform them according to |
|
| 75 | - * the configuration. |
|
| 76 | - * |
|
| 77 | - * @param array $value { |
|
| 78 | - * The array containing the JSON-LD and the references. |
|
| 79 | - * |
|
| 80 | - * @type array $jsonld The JSON-LD array. |
|
| 81 | - * @type int[] $references An array of post ID referenced by the JSON-LD (will be expanded by the converter). |
|
| 82 | - * } |
|
| 83 | - * |
|
| 84 | - * @param int $term_id The Term ID. |
|
| 85 | - * |
|
| 86 | - * @return array An array with the updated JSON-LD and references. |
|
| 87 | - */ |
|
| 88 | - public function wl_term_jsonld_array( $value, $term_id ) { |
|
| 89 | - $jsonld = $value['jsonld']; |
|
| 90 | - $references = $value['references']; |
|
| 91 | - |
|
| 92 | - return array( |
|
| 93 | - 'jsonld' => $this->build_jsonld( $jsonld, $term_id, $references, self::TERM ), |
|
| 94 | - 'references' => $references, |
|
| 95 | - ); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Hook to `wl_post_jsonld_array` and `wl_entity_jsonld_array`. |
|
| 101 | - * |
|
| 102 | - * Receive the JSON-LD and the references in the array along with the post ID and transform them according to |
|
| 103 | - * the configuration. |
|
| 104 | - * |
|
| 105 | - * @param array $value { |
|
| 106 | - * The array containing the JSON-LD and the references. |
|
| 107 | - * |
|
| 108 | - * @type array $jsonld The JSON-LD array. |
|
| 109 | - * @type int[] $references An array of post ID referenced by the JSON-LD (will be expanded by the converter). |
|
| 110 | - * } |
|
| 111 | - * |
|
| 112 | - * @param int $post_id The post ID. |
|
| 113 | - * |
|
| 114 | - * @return array An array with the updated JSON-LD and references. |
|
| 115 | - */ |
|
| 116 | - public function wl_post_jsonld_array( $value, $post_id ) { |
|
| 117 | - |
|
| 118 | - $jsonld = $value['jsonld']; |
|
| 119 | - $references = $value['references']; |
|
| 120 | - |
|
| 121 | - return array( |
|
| 122 | - 'jsonld' => $this->build_jsonld( $jsonld, $post_id, $references, self::POST ), |
|
| 123 | - 'references' => $references, |
|
| 124 | - ); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * Returns JSON-LD data after applying transformation functions. |
|
| 129 | - * |
|
| 130 | - * @param array $jsonld The JSON-LD structure. |
|
| 131 | - * @param int $identifier The {@link WP_Post} id or {@link \WP_Term} id. |
|
| 132 | - * @param array $references An array of post references. |
|
| 133 | - * |
|
| 134 | - * @param string $type Post or term. |
|
| 135 | - * |
|
| 136 | - * @return array the new refactored array structure. |
|
| 137 | - * @since 3.25.0 |
|
| 138 | - */ |
|
| 139 | - private function build_jsonld( $jsonld, $identifier, &$references, $type ) { |
|
| 140 | - |
|
| 141 | - // @@todo I think there's an issue here with the Validator, because you're changing the instance state and the |
|
| 142 | - // instance may be reused afterwards. |
|
| 143 | - |
|
| 144 | - $properties = $this->validator->validate( $identifier, $type ); |
|
| 145 | - $nested_properties = array(); |
|
| 146 | - |
|
| 147 | - foreach ( $properties as $property ) { |
|
| 148 | - // If the property has the character '/' in the property name then it is a nested property. |
|
| 149 | - if ( strpos( $property['property_name'], '/' ) !== false ) { |
|
| 150 | - $nested_properties[] = $property; |
|
| 151 | - continue; |
|
| 152 | - } |
|
| 153 | - $property_transformed_data = $this->get_property_data( $property, $jsonld, $identifier, $references, $type ); |
|
| 154 | - if ( false !== $property_transformed_data ) { |
|
| 155 | - $jsonld[ $property['property_name'] ] = $property_transformed_data; |
|
| 156 | - } |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - $jsonld = $this->process_nested_properties( $nested_properties, $jsonld, $identifier, $references, $type ); |
|
| 160 | - |
|
| 161 | - return $jsonld; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Get the property data by applying the transformation function |
|
| 166 | - * |
|
| 167 | - * @param $property |
|
| 168 | - * @param $jsonld |
|
| 169 | - * @param $post_id |
|
| 170 | - * @param $references |
|
| 171 | - * |
|
| 172 | - * @param $type |
|
| 173 | - * |
|
| 174 | - * @return array|bool|null |
|
| 175 | - */ |
|
| 176 | - public function get_property_data( $property, $jsonld, $post_id, &$references, $type ) { |
|
| 177 | - $transform_instance = $this->transform_functions_registry->get_transform_function( $property['transform_function'] ); |
|
| 178 | - $data = Data_Source_Factory::get_instance()->get_data( $post_id, $property, $type ); |
|
| 179 | - if ( null !== $transform_instance ) { |
|
| 180 | - $transform_data = $transform_instance->transform_data( $data, $jsonld, $references, $post_id ); |
|
| 181 | - if ( null !== $transform_data ) { |
|
| 182 | - return $this->make_single( $transform_data ); |
|
| 183 | - } |
|
| 184 | - } else { |
|
| 185 | - return $this->make_single( $data ); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - return false; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * Process all the nested properties. |
|
| 193 | - * |
|
| 194 | - * @param $nested_properties array |
|
| 195 | - * @param $jsonld array |
|
| 196 | - * |
|
| 197 | - * @param $post_id |
|
| 198 | - * @param $references |
|
| 199 | - * @param string $type Post or term. |
|
| 200 | - * |
|
| 201 | - * @return array |
|
| 202 | - */ |
|
| 203 | - public function process_nested_properties( $nested_properties, $jsonld, $post_id, &$references, $type ) { |
|
| 204 | - foreach ( $nested_properties as $property ) { |
|
| 205 | - $property_data = $this->get_property_data( $property, $jsonld, $post_id, $references, $type ); |
|
| 206 | - if ( false === $property_data ) { |
|
| 207 | - // No need to create nested levels. |
|
| 208 | - continue; |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - $keys = explode( '/', $property['property_name'] ); |
|
| 212 | - // end is the last level of the nested property. |
|
| 213 | - $end = array_pop( $keys ); |
|
| 214 | - $current_property_pointer = &$jsonld; |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * Once we find all the nested levels from the property name |
|
| 218 | - * loop through it and create associative array if the levels |
|
| 219 | - * didnt exist. |
|
| 220 | - */ |
|
| 221 | - while ( count( $keys ) > 0 ) { |
|
| 222 | - $key = array_shift( $keys ); |
|
| 223 | - if ( $key === "" ) { |
|
| 224 | - continue; |
|
| 225 | - } |
|
| 226 | - if ( ! array_key_exists( $key, $current_property_pointer ) ) { |
|
| 227 | - $current_property_pointer[ $key ] = array(); |
|
| 228 | - } |
|
| 229 | - // We are setting the pointer to the current key, so that at the end |
|
| 230 | - // we can add the data at last level. |
|
| 231 | - $current_property_pointer = &$current_property_pointer[ $key ]; |
|
| 232 | - } |
|
| 233 | - $current_property_pointer[ $end ] = $property_data; |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - return $jsonld; |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - private function make_single( $value ) { |
|
| 240 | - |
|
| 241 | - $values = (array) $value; |
|
| 242 | - |
|
| 243 | - if ( empty( $values ) ) { |
|
| 244 | - return false; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - if ( 1 === count( $values ) && 0 === key( $values ) ) { |
|
| 248 | - return current( $values ); |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - return $values; |
|
| 252 | - } |
|
| 21 | + /** |
|
| 22 | + * Enumerations for the field types. |
|
| 23 | + */ |
|
| 24 | + const FIELD_TYPE_TEXT_FIELD = 'text'; |
|
| 25 | + const FIELD_TYPE_CUSTOM_FIELD = 'custom_field'; |
|
| 26 | + const FIELD_TYPE_ACF = 'acf'; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Mappings can be applied to either post |
|
| 30 | + * or term, the below is used to specify whether it is a post or a term. |
|
| 31 | + */ |
|
| 32 | + const POST = 'post'; |
|
| 33 | + const TERM = 'term'; |
|
| 34 | + /** |
|
| 35 | + * The {@link Mappings_Validator} instance to test. |
|
| 36 | + * |
|
| 37 | + * @since 3.25.0 |
|
| 38 | + * @access private |
|
| 39 | + * @var Mappings_Validator $validator The {@link Mappings_Validator} instance. |
|
| 40 | + */ |
|
| 41 | + private $validator; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * The {@link Mappings_Transform_Functions_Registry} instance. |
|
| 45 | + * |
|
| 46 | + * @since 3.25.0 |
|
| 47 | + * @access private |
|
| 48 | + * @var Mappings_Transform_Functions_Registry $transform_functions_registry The {@link Mappings_Transform_Functions_Registry} instance. |
|
| 49 | + */ |
|
| 50 | + private $transform_functions_registry; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Initialize all dependencies required. |
|
| 54 | + * |
|
| 55 | + * @param Mappings_Validator $validator A {@link Mappings_Validator} instance. |
|
| 56 | + * @param Mappings_Transform_Functions_Registry $transform_functions_registry |
|
| 57 | + */ |
|
| 58 | + public function __construct( $validator, $transform_functions_registry ) { |
|
| 59 | + |
|
| 60 | + $this->validator = $validator; |
|
| 61 | + $this->transform_functions_registry = $transform_functions_registry; |
|
| 62 | + |
|
| 63 | + // Hook to refactor the JSON-LD. |
|
| 64 | + add_filter( 'wl_post_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 2 ); |
|
| 65 | + add_filter( 'wl_entity_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 2 ); |
|
| 66 | + |
|
| 67 | + // This is wrong: `wl_term_jsonld_array` will provide a term ID not a post ID (that `wl_post_jsonld_array` expects). |
|
| 68 | + add_filter( 'wl_term_jsonld_array', array( $this, 'wl_term_jsonld_array' ), 11, 2 ); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Hook to `wl_term_jsonld_array`. |
|
| 73 | + * |
|
| 74 | + * Receive the JSON-LD and the references in the array along with the term ID and transform them according to |
|
| 75 | + * the configuration. |
|
| 76 | + * |
|
| 77 | + * @param array $value { |
|
| 78 | + * The array containing the JSON-LD and the references. |
|
| 79 | + * |
|
| 80 | + * @type array $jsonld The JSON-LD array. |
|
| 81 | + * @type int[] $references An array of post ID referenced by the JSON-LD (will be expanded by the converter). |
|
| 82 | + * } |
|
| 83 | + * |
|
| 84 | + * @param int $term_id The Term ID. |
|
| 85 | + * |
|
| 86 | + * @return array An array with the updated JSON-LD and references. |
|
| 87 | + */ |
|
| 88 | + public function wl_term_jsonld_array( $value, $term_id ) { |
|
| 89 | + $jsonld = $value['jsonld']; |
|
| 90 | + $references = $value['references']; |
|
| 91 | + |
|
| 92 | + return array( |
|
| 93 | + 'jsonld' => $this->build_jsonld( $jsonld, $term_id, $references, self::TERM ), |
|
| 94 | + 'references' => $references, |
|
| 95 | + ); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Hook to `wl_post_jsonld_array` and `wl_entity_jsonld_array`. |
|
| 101 | + * |
|
| 102 | + * Receive the JSON-LD and the references in the array along with the post ID and transform them according to |
|
| 103 | + * the configuration. |
|
| 104 | + * |
|
| 105 | + * @param array $value { |
|
| 106 | + * The array containing the JSON-LD and the references. |
|
| 107 | + * |
|
| 108 | + * @type array $jsonld The JSON-LD array. |
|
| 109 | + * @type int[] $references An array of post ID referenced by the JSON-LD (will be expanded by the converter). |
|
| 110 | + * } |
|
| 111 | + * |
|
| 112 | + * @param int $post_id The post ID. |
|
| 113 | + * |
|
| 114 | + * @return array An array with the updated JSON-LD and references. |
|
| 115 | + */ |
|
| 116 | + public function wl_post_jsonld_array( $value, $post_id ) { |
|
| 117 | + |
|
| 118 | + $jsonld = $value['jsonld']; |
|
| 119 | + $references = $value['references']; |
|
| 120 | + |
|
| 121 | + return array( |
|
| 122 | + 'jsonld' => $this->build_jsonld( $jsonld, $post_id, $references, self::POST ), |
|
| 123 | + 'references' => $references, |
|
| 124 | + ); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * Returns JSON-LD data after applying transformation functions. |
|
| 129 | + * |
|
| 130 | + * @param array $jsonld The JSON-LD structure. |
|
| 131 | + * @param int $identifier The {@link WP_Post} id or {@link \WP_Term} id. |
|
| 132 | + * @param array $references An array of post references. |
|
| 133 | + * |
|
| 134 | + * @param string $type Post or term. |
|
| 135 | + * |
|
| 136 | + * @return array the new refactored array structure. |
|
| 137 | + * @since 3.25.0 |
|
| 138 | + */ |
|
| 139 | + private function build_jsonld( $jsonld, $identifier, &$references, $type ) { |
|
| 140 | + |
|
| 141 | + // @@todo I think there's an issue here with the Validator, because you're changing the instance state and the |
|
| 142 | + // instance may be reused afterwards. |
|
| 143 | + |
|
| 144 | + $properties = $this->validator->validate( $identifier, $type ); |
|
| 145 | + $nested_properties = array(); |
|
| 146 | + |
|
| 147 | + foreach ( $properties as $property ) { |
|
| 148 | + // If the property has the character '/' in the property name then it is a nested property. |
|
| 149 | + if ( strpos( $property['property_name'], '/' ) !== false ) { |
|
| 150 | + $nested_properties[] = $property; |
|
| 151 | + continue; |
|
| 152 | + } |
|
| 153 | + $property_transformed_data = $this->get_property_data( $property, $jsonld, $identifier, $references, $type ); |
|
| 154 | + if ( false !== $property_transformed_data ) { |
|
| 155 | + $jsonld[ $property['property_name'] ] = $property_transformed_data; |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + $jsonld = $this->process_nested_properties( $nested_properties, $jsonld, $identifier, $references, $type ); |
|
| 160 | + |
|
| 161 | + return $jsonld; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Get the property data by applying the transformation function |
|
| 166 | + * |
|
| 167 | + * @param $property |
|
| 168 | + * @param $jsonld |
|
| 169 | + * @param $post_id |
|
| 170 | + * @param $references |
|
| 171 | + * |
|
| 172 | + * @param $type |
|
| 173 | + * |
|
| 174 | + * @return array|bool|null |
|
| 175 | + */ |
|
| 176 | + public function get_property_data( $property, $jsonld, $post_id, &$references, $type ) { |
|
| 177 | + $transform_instance = $this->transform_functions_registry->get_transform_function( $property['transform_function'] ); |
|
| 178 | + $data = Data_Source_Factory::get_instance()->get_data( $post_id, $property, $type ); |
|
| 179 | + if ( null !== $transform_instance ) { |
|
| 180 | + $transform_data = $transform_instance->transform_data( $data, $jsonld, $references, $post_id ); |
|
| 181 | + if ( null !== $transform_data ) { |
|
| 182 | + return $this->make_single( $transform_data ); |
|
| 183 | + } |
|
| 184 | + } else { |
|
| 185 | + return $this->make_single( $data ); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + return false; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * Process all the nested properties. |
|
| 193 | + * |
|
| 194 | + * @param $nested_properties array |
|
| 195 | + * @param $jsonld array |
|
| 196 | + * |
|
| 197 | + * @param $post_id |
|
| 198 | + * @param $references |
|
| 199 | + * @param string $type Post or term. |
|
| 200 | + * |
|
| 201 | + * @return array |
|
| 202 | + */ |
|
| 203 | + public function process_nested_properties( $nested_properties, $jsonld, $post_id, &$references, $type ) { |
|
| 204 | + foreach ( $nested_properties as $property ) { |
|
| 205 | + $property_data = $this->get_property_data( $property, $jsonld, $post_id, $references, $type ); |
|
| 206 | + if ( false === $property_data ) { |
|
| 207 | + // No need to create nested levels. |
|
| 208 | + continue; |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + $keys = explode( '/', $property['property_name'] ); |
|
| 212 | + // end is the last level of the nested property. |
|
| 213 | + $end = array_pop( $keys ); |
|
| 214 | + $current_property_pointer = &$jsonld; |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * Once we find all the nested levels from the property name |
|
| 218 | + * loop through it and create associative array if the levels |
|
| 219 | + * didnt exist. |
|
| 220 | + */ |
|
| 221 | + while ( count( $keys ) > 0 ) { |
|
| 222 | + $key = array_shift( $keys ); |
|
| 223 | + if ( $key === "" ) { |
|
| 224 | + continue; |
|
| 225 | + } |
|
| 226 | + if ( ! array_key_exists( $key, $current_property_pointer ) ) { |
|
| 227 | + $current_property_pointer[ $key ] = array(); |
|
| 228 | + } |
|
| 229 | + // We are setting the pointer to the current key, so that at the end |
|
| 230 | + // we can add the data at last level. |
|
| 231 | + $current_property_pointer = &$current_property_pointer[ $key ]; |
|
| 232 | + } |
|
| 233 | + $current_property_pointer[ $end ] = $property_data; |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + return $jsonld; |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + private function make_single( $value ) { |
|
| 240 | + |
|
| 241 | + $values = (array) $value; |
|
| 242 | + |
|
| 243 | + if ( empty( $values ) ) { |
|
| 244 | + return false; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + if ( 1 === count( $values ) && 0 === key( $values ) ) { |
|
| 248 | + return current( $values ); |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + return $values; |
|
| 252 | + } |
|
| 253 | 253 | |
| 254 | 254 | } |
@@ -55,17 +55,17 @@ discard block |
||
| 55 | 55 | * @param Mappings_Validator $validator A {@link Mappings_Validator} instance. |
| 56 | 56 | * @param Mappings_Transform_Functions_Registry $transform_functions_registry |
| 57 | 57 | */ |
| 58 | - public function __construct( $validator, $transform_functions_registry ) { |
|
| 58 | + public function __construct($validator, $transform_functions_registry) { |
|
| 59 | 59 | |
| 60 | 60 | $this->validator = $validator; |
| 61 | 61 | $this->transform_functions_registry = $transform_functions_registry; |
| 62 | 62 | |
| 63 | 63 | // Hook to refactor the JSON-LD. |
| 64 | - add_filter( 'wl_post_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 2 ); |
|
| 65 | - add_filter( 'wl_entity_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 2 ); |
|
| 64 | + add_filter('wl_post_jsonld_array', array($this, 'wl_post_jsonld_array'), 11, 2); |
|
| 65 | + add_filter('wl_entity_jsonld_array', array($this, 'wl_post_jsonld_array'), 11, 2); |
|
| 66 | 66 | |
| 67 | 67 | // This is wrong: `wl_term_jsonld_array` will provide a term ID not a post ID (that `wl_post_jsonld_array` expects). |
| 68 | - add_filter( 'wl_term_jsonld_array', array( $this, 'wl_term_jsonld_array' ), 11, 2 ); |
|
| 68 | + add_filter('wl_term_jsonld_array', array($this, 'wl_term_jsonld_array'), 11, 2); |
|
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | /** |
@@ -85,12 +85,12 @@ discard block |
||
| 85 | 85 | * |
| 86 | 86 | * @return array An array with the updated JSON-LD and references. |
| 87 | 87 | */ |
| 88 | - public function wl_term_jsonld_array( $value, $term_id ) { |
|
| 88 | + public function wl_term_jsonld_array($value, $term_id) { |
|
| 89 | 89 | $jsonld = $value['jsonld']; |
| 90 | 90 | $references = $value['references']; |
| 91 | 91 | |
| 92 | 92 | return array( |
| 93 | - 'jsonld' => $this->build_jsonld( $jsonld, $term_id, $references, self::TERM ), |
|
| 93 | + 'jsonld' => $this->build_jsonld($jsonld, $term_id, $references, self::TERM), |
|
| 94 | 94 | 'references' => $references, |
| 95 | 95 | ); |
| 96 | 96 | } |
@@ -113,13 +113,13 @@ discard block |
||
| 113 | 113 | * |
| 114 | 114 | * @return array An array with the updated JSON-LD and references. |
| 115 | 115 | */ |
| 116 | - public function wl_post_jsonld_array( $value, $post_id ) { |
|
| 116 | + public function wl_post_jsonld_array($value, $post_id) { |
|
| 117 | 117 | |
| 118 | 118 | $jsonld = $value['jsonld']; |
| 119 | 119 | $references = $value['references']; |
| 120 | 120 | |
| 121 | 121 | return array( |
| 122 | - 'jsonld' => $this->build_jsonld( $jsonld, $post_id, $references, self::POST ), |
|
| 122 | + 'jsonld' => $this->build_jsonld($jsonld, $post_id, $references, self::POST), |
|
| 123 | 123 | 'references' => $references, |
| 124 | 124 | ); |
| 125 | 125 | } |
@@ -136,27 +136,27 @@ discard block |
||
| 136 | 136 | * @return array the new refactored array structure. |
| 137 | 137 | * @since 3.25.0 |
| 138 | 138 | */ |
| 139 | - private function build_jsonld( $jsonld, $identifier, &$references, $type ) { |
|
| 139 | + private function build_jsonld($jsonld, $identifier, &$references, $type) { |
|
| 140 | 140 | |
| 141 | 141 | // @@todo I think there's an issue here with the Validator, because you're changing the instance state and the |
| 142 | 142 | // instance may be reused afterwards. |
| 143 | 143 | |
| 144 | - $properties = $this->validator->validate( $identifier, $type ); |
|
| 144 | + $properties = $this->validator->validate($identifier, $type); |
|
| 145 | 145 | $nested_properties = array(); |
| 146 | 146 | |
| 147 | - foreach ( $properties as $property ) { |
|
| 147 | + foreach ($properties as $property) { |
|
| 148 | 148 | // If the property has the character '/' in the property name then it is a nested property. |
| 149 | - if ( strpos( $property['property_name'], '/' ) !== false ) { |
|
| 149 | + if (strpos($property['property_name'], '/') !== false) { |
|
| 150 | 150 | $nested_properties[] = $property; |
| 151 | 151 | continue; |
| 152 | 152 | } |
| 153 | - $property_transformed_data = $this->get_property_data( $property, $jsonld, $identifier, $references, $type ); |
|
| 154 | - if ( false !== $property_transformed_data ) { |
|
| 155 | - $jsonld[ $property['property_name'] ] = $property_transformed_data; |
|
| 153 | + $property_transformed_data = $this->get_property_data($property, $jsonld, $identifier, $references, $type); |
|
| 154 | + if (false !== $property_transformed_data) { |
|
| 155 | + $jsonld[$property['property_name']] = $property_transformed_data; |
|
| 156 | 156 | } |
| 157 | 157 | } |
| 158 | 158 | |
| 159 | - $jsonld = $this->process_nested_properties( $nested_properties, $jsonld, $identifier, $references, $type ); |
|
| 159 | + $jsonld = $this->process_nested_properties($nested_properties, $jsonld, $identifier, $references, $type); |
|
| 160 | 160 | |
| 161 | 161 | return $jsonld; |
| 162 | 162 | } |
@@ -173,16 +173,16 @@ discard block |
||
| 173 | 173 | * |
| 174 | 174 | * @return array|bool|null |
| 175 | 175 | */ |
| 176 | - public function get_property_data( $property, $jsonld, $post_id, &$references, $type ) { |
|
| 177 | - $transform_instance = $this->transform_functions_registry->get_transform_function( $property['transform_function'] ); |
|
| 178 | - $data = Data_Source_Factory::get_instance()->get_data( $post_id, $property, $type ); |
|
| 179 | - if ( null !== $transform_instance ) { |
|
| 180 | - $transform_data = $transform_instance->transform_data( $data, $jsonld, $references, $post_id ); |
|
| 181 | - if ( null !== $transform_data ) { |
|
| 182 | - return $this->make_single( $transform_data ); |
|
| 176 | + public function get_property_data($property, $jsonld, $post_id, &$references, $type) { |
|
| 177 | + $transform_instance = $this->transform_functions_registry->get_transform_function($property['transform_function']); |
|
| 178 | + $data = Data_Source_Factory::get_instance()->get_data($post_id, $property, $type); |
|
| 179 | + if (null !== $transform_instance) { |
|
| 180 | + $transform_data = $transform_instance->transform_data($data, $jsonld, $references, $post_id); |
|
| 181 | + if (null !== $transform_data) { |
|
| 182 | + return $this->make_single($transform_data); |
|
| 183 | 183 | } |
| 184 | 184 | } else { |
| 185 | - return $this->make_single( $data ); |
|
| 185 | + return $this->make_single($data); |
|
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | return false; |
@@ -200,17 +200,17 @@ discard block |
||
| 200 | 200 | * |
| 201 | 201 | * @return array |
| 202 | 202 | */ |
| 203 | - public function process_nested_properties( $nested_properties, $jsonld, $post_id, &$references, $type ) { |
|
| 204 | - foreach ( $nested_properties as $property ) { |
|
| 205 | - $property_data = $this->get_property_data( $property, $jsonld, $post_id, $references, $type ); |
|
| 206 | - if ( false === $property_data ) { |
|
| 203 | + public function process_nested_properties($nested_properties, $jsonld, $post_id, &$references, $type) { |
|
| 204 | + foreach ($nested_properties as $property) { |
|
| 205 | + $property_data = $this->get_property_data($property, $jsonld, $post_id, $references, $type); |
|
| 206 | + if (false === $property_data) { |
|
| 207 | 207 | // No need to create nested levels. |
| 208 | 208 | continue; |
| 209 | 209 | } |
| 210 | 210 | |
| 211 | - $keys = explode( '/', $property['property_name'] ); |
|
| 211 | + $keys = explode('/', $property['property_name']); |
|
| 212 | 212 | // end is the last level of the nested property. |
| 213 | - $end = array_pop( $keys ); |
|
| 213 | + $end = array_pop($keys); |
|
| 214 | 214 | $current_property_pointer = &$jsonld; |
| 215 | 215 | |
| 216 | 216 | /** |
@@ -218,34 +218,34 @@ discard block |
||
| 218 | 218 | * loop through it and create associative array if the levels |
| 219 | 219 | * didnt exist. |
| 220 | 220 | */ |
| 221 | - while ( count( $keys ) > 0 ) { |
|
| 222 | - $key = array_shift( $keys ); |
|
| 223 | - if ( $key === "" ) { |
|
| 221 | + while (count($keys) > 0) { |
|
| 222 | + $key = array_shift($keys); |
|
| 223 | + if ($key === "") { |
|
| 224 | 224 | continue; |
| 225 | 225 | } |
| 226 | - if ( ! array_key_exists( $key, $current_property_pointer ) ) { |
|
| 227 | - $current_property_pointer[ $key ] = array(); |
|
| 226 | + if ( ! array_key_exists($key, $current_property_pointer)) { |
|
| 227 | + $current_property_pointer[$key] = array(); |
|
| 228 | 228 | } |
| 229 | 229 | // We are setting the pointer to the current key, so that at the end |
| 230 | 230 | // we can add the data at last level. |
| 231 | - $current_property_pointer = &$current_property_pointer[ $key ]; |
|
| 231 | + $current_property_pointer = &$current_property_pointer[$key]; |
|
| 232 | 232 | } |
| 233 | - $current_property_pointer[ $end ] = $property_data; |
|
| 233 | + $current_property_pointer[$end] = $property_data; |
|
| 234 | 234 | } |
| 235 | 235 | |
| 236 | 236 | return $jsonld; |
| 237 | 237 | } |
| 238 | 238 | |
| 239 | - private function make_single( $value ) { |
|
| 239 | + private function make_single($value) { |
|
| 240 | 240 | |
| 241 | 241 | $values = (array) $value; |
| 242 | 242 | |
| 243 | - if ( empty( $values ) ) { |
|
| 243 | + if (empty($values)) { |
|
| 244 | 244 | return false; |
| 245 | 245 | } |
| 246 | 246 | |
| 247 | - if ( 1 === count( $values ) && 0 === key( $values ) ) { |
|
| 248 | - return current( $values ); |
|
| 247 | + if (1 === count($values) && 0 === key($values)) { |
|
| 248 | + return current($values); |
|
| 249 | 249 | } |
| 250 | 250 | |
| 251 | 251 | return $values; |
@@ -19,27 +19,27 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | interface Rule_Validator { |
| 21 | 21 | |
| 22 | - const IS_EQUAL_TO = '==='; |
|
| 23 | - const IS_NOT_EQUAL_TO = '!=='; |
|
| 22 | + const IS_EQUAL_TO = '==='; |
|
| 23 | + const IS_NOT_EQUAL_TO = '!=='; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Get the validator label. |
|
| 27 | - * |
|
| 28 | - * @return string The validator label. |
|
| 29 | - */ |
|
| 30 | - public function get_label(); |
|
| 25 | + /** |
|
| 26 | + * Get the validator label. |
|
| 27 | + * |
|
| 28 | + * @return string The validator label. |
|
| 29 | + */ |
|
| 30 | + public function get_label(); |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * Test whether a post passes a validation. |
|
| 34 | - * |
|
| 35 | - * @param int $identifier The post id or term id. |
|
| 36 | - * @param string $operator The operator. |
|
| 37 | - * @param string $operand_1 The first operand. |
|
| 38 | - * @param string $operand_2 The second operand. |
|
| 39 | - * @param string $type The type is either post or term. |
|
| 40 | - * |
|
| 41 | - * @return bool Whether the post passes or not the validation. |
|
| 42 | - */ |
|
| 43 | - public function is_valid( $identifier, $operator, $operand_1, $operand_2, $type ); |
|
| 32 | + /** |
|
| 33 | + * Test whether a post passes a validation. |
|
| 34 | + * |
|
| 35 | + * @param int $identifier The post id or term id. |
|
| 36 | + * @param string $operator The operator. |
|
| 37 | + * @param string $operand_1 The first operand. |
|
| 38 | + * @param string $operand_2 The second operand. |
|
| 39 | + * @param string $type The type is either post or term. |
|
| 40 | + * |
|
| 41 | + * @return bool Whether the post passes or not the validation. |
|
| 42 | + */ |
|
| 43 | + public function is_valid( $identifier, $operator, $operand_1, $operand_2, $type ); |
|
| 44 | 44 | |
| 45 | 45 | } |
@@ -40,6 +40,6 @@ |
||
| 40 | 40 | * |
| 41 | 41 | * @return bool Whether the post passes or not the validation. |
| 42 | 42 | */ |
| 43 | - public function is_valid( $identifier, $operator, $operand_1, $operand_2, $type ); |
|
| 43 | + public function is_valid($identifier, $operator, $operand_1, $operand_2, $type); |
|
| 44 | 44 | |
| 45 | 45 | } |
@@ -19,55 +19,55 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class Rule_Groups_Validator { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * The {@link Rule_Validators_Registry} instance. |
|
| 24 | - * |
|
| 25 | - * @var Rule_Validators_Registry $rule_validators_registry The {@link Rule_Validators_Registry} instance. |
|
| 26 | - */ |
|
| 27 | - private $rule_validators_registry; |
|
| 22 | + /** |
|
| 23 | + * The {@link Rule_Validators_Registry} instance. |
|
| 24 | + * |
|
| 25 | + * @var Rule_Validators_Registry $rule_validators_registry The {@link Rule_Validators_Registry} instance. |
|
| 26 | + */ |
|
| 27 | + private $rule_validators_registry; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Rule_Groups_Validator constructor. |
|
| 31 | - * |
|
| 32 | - * @param Rule_Validators_Registry $rule_validators_registry |
|
| 33 | - */ |
|
| 34 | - public function __construct( $rule_validators_registry ) { |
|
| 29 | + /** |
|
| 30 | + * Rule_Groups_Validator constructor. |
|
| 31 | + * |
|
| 32 | + * @param Rule_Validators_Registry $rule_validators_registry |
|
| 33 | + */ |
|
| 34 | + public function __construct( $rule_validators_registry ) { |
|
| 35 | 35 | |
| 36 | - $this->rule_validators_registry = $rule_validators_registry; |
|
| 36 | + $this->rule_validators_registry = $rule_validators_registry; |
|
| 37 | 37 | |
| 38 | - } |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Check whether the specified post passes at least one group of rules. |
|
| 42 | - * |
|
| 43 | - * @param int $identifier The post id. |
|
| 44 | - * @param array $rule_groups An array of rules' groups. |
|
| 45 | - * @param $type string Post or term |
|
| 46 | - * |
|
| 47 | - * @return bool Whether the post passes at least one rule group. |
|
| 48 | - */ |
|
| 49 | - public function is_valid( $identifier, $rule_groups, $type ) { |
|
| 40 | + /** |
|
| 41 | + * Check whether the specified post passes at least one group of rules. |
|
| 42 | + * |
|
| 43 | + * @param int $identifier The post id. |
|
| 44 | + * @param array $rule_groups An array of rules' groups. |
|
| 45 | + * @param $type string Post or term |
|
| 46 | + * |
|
| 47 | + * @return bool Whether the post passes at least one rule group. |
|
| 48 | + */ |
|
| 49 | + public function is_valid( $identifier, $rule_groups, $type ) { |
|
| 50 | 50 | |
| 51 | - // Validate each group. Return true as soon as one group is validated (all rules). |
|
| 52 | - foreach ( (array) $rule_groups as $rule_group ) { |
|
| 53 | - foreach ( $rule_group['rules'] as $rule ) { |
|
| 54 | - $rule_field_one = $rule['rule_field_one']; |
|
| 55 | - $rule_logic_field = $rule['rule_logic_field']; |
|
| 56 | - $rule_field_two = $rule['rule_field_two']; |
|
| 51 | + // Validate each group. Return true as soon as one group is validated (all rules). |
|
| 52 | + foreach ( (array) $rule_groups as $rule_group ) { |
|
| 53 | + foreach ( $rule_group['rules'] as $rule ) { |
|
| 54 | + $rule_field_one = $rule['rule_field_one']; |
|
| 55 | + $rule_logic_field = $rule['rule_logic_field']; |
|
| 56 | + $rule_field_two = $rule['rule_field_two']; |
|
| 57 | 57 | |
| 58 | - $rule_validator = $this->rule_validators_registry->get_rule_validator( $rule_field_one ); |
|
| 59 | - // Skip to the next Rule Group if a rule isn't valid. |
|
| 60 | - if ( ! $rule_validator->is_valid( $identifier, $rule_logic_field, $rule_field_one, $rule_field_two, $type ) ) { |
|
| 61 | - continue 2; |
|
| 62 | - } |
|
| 58 | + $rule_validator = $this->rule_validators_registry->get_rule_validator( $rule_field_one ); |
|
| 59 | + // Skip to the next Rule Group if a rule isn't valid. |
|
| 60 | + if ( ! $rule_validator->is_valid( $identifier, $rule_logic_field, $rule_field_one, $rule_field_two, $type ) ) { |
|
| 61 | + continue 2; |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - } |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - // If we got here it means that all the rules have been validated (or the rules' group has no rules). |
|
| 67 | - return true; |
|
| 68 | - } |
|
| 66 | + // If we got here it means that all the rules have been validated (or the rules' group has no rules). |
|
| 67 | + return true; |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - return false; |
|
| 71 | - } |
|
| 70 | + return false; |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | 73 | } |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | * |
| 32 | 32 | * @param Rule_Validators_Registry $rule_validators_registry |
| 33 | 33 | */ |
| 34 | - public function __construct( $rule_validators_registry ) { |
|
| 34 | + public function __construct($rule_validators_registry) { |
|
| 35 | 35 | |
| 36 | 36 | $this->rule_validators_registry = $rule_validators_registry; |
| 37 | 37 | |
@@ -46,18 +46,18 @@ discard block |
||
| 46 | 46 | * |
| 47 | 47 | * @return bool Whether the post passes at least one rule group. |
| 48 | 48 | */ |
| 49 | - public function is_valid( $identifier, $rule_groups, $type ) { |
|
| 49 | + public function is_valid($identifier, $rule_groups, $type) { |
|
| 50 | 50 | |
| 51 | 51 | // Validate each group. Return true as soon as one group is validated (all rules). |
| 52 | - foreach ( (array) $rule_groups as $rule_group ) { |
|
| 53 | - foreach ( $rule_group['rules'] as $rule ) { |
|
| 52 | + foreach ((array) $rule_groups as $rule_group) { |
|
| 53 | + foreach ($rule_group['rules'] as $rule) { |
|
| 54 | 54 | $rule_field_one = $rule['rule_field_one']; |
| 55 | 55 | $rule_logic_field = $rule['rule_logic_field']; |
| 56 | 56 | $rule_field_two = $rule['rule_field_two']; |
| 57 | 57 | |
| 58 | - $rule_validator = $this->rule_validators_registry->get_rule_validator( $rule_field_one ); |
|
| 58 | + $rule_validator = $this->rule_validators_registry->get_rule_validator($rule_field_one); |
|
| 59 | 59 | // Skip to the next Rule Group if a rule isn't valid. |
| 60 | - if ( ! $rule_validator->is_valid( $identifier, $rule_logic_field, $rule_field_one, $rule_field_two, $type ) ) { |
|
| 60 | + if ( ! $rule_validator->is_valid($identifier, $rule_logic_field, $rule_field_one, $rule_field_two, $type)) { |
|
| 61 | 61 | continue 2; |
| 62 | 62 | } |
| 63 | 63 | |
@@ -15,53 +15,53 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Taxonomy_Term_Rule_Validator implements Rule_Validator { |
| 17 | 17 | |
| 18 | - const TAXONOMY = 'taxonomy'; |
|
| 18 | + const TAXONOMY = 'taxonomy'; |
|
| 19 | 19 | |
| 20 | - public function __construct() { |
|
| 21 | - add_filter( 'wl_mappings_rule_validators', array( $this, 'wl_mappings_rule_validators' ) ); |
|
| 22 | - } |
|
| 20 | + public function __construct() { |
|
| 21 | + add_filter( 'wl_mappings_rule_validators', array( $this, 'wl_mappings_rule_validators' ) ); |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Hook to `wl_mappings_rule_validators` to register ourselves. |
|
| 26 | - * |
|
| 27 | - * @param array $value An array with validators. |
|
| 28 | - * |
|
| 29 | - * @return array An array with validators plus ours. |
|
| 30 | - */ |
|
| 31 | - public function wl_mappings_rule_validators( $value ) { |
|
| 32 | - $value[ self::TAXONOMY ] = $this; |
|
| 24 | + /** |
|
| 25 | + * Hook to `wl_mappings_rule_validators` to register ourselves. |
|
| 26 | + * |
|
| 27 | + * @param array $value An array with validators. |
|
| 28 | + * |
|
| 29 | + * @return array An array with validators plus ours. |
|
| 30 | + */ |
|
| 31 | + public function wl_mappings_rule_validators( $value ) { |
|
| 32 | + $value[ self::TAXONOMY ] = $this; |
|
| 33 | 33 | |
| 34 | - return $value; |
|
| 35 | - } |
|
| 34 | + return $value; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | 37 | |
| 38 | - public function get_label() { |
|
| 39 | - return __( 'TaxonomyTerm', 'wordlift' ); |
|
| 40 | - } |
|
| 38 | + public function get_label() { |
|
| 39 | + return __( 'TaxonomyTerm', 'wordlift' ); |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - public function is_valid( $identifier, $operator, $operand_1, $taxonomy, $type ) { |
|
| 43 | - $current_term = get_term( $identifier ); |
|
| 44 | - // If it is not a term page, then return false for two operators. |
|
| 45 | - if ( ! $current_term instanceof \WP_Term ) { |
|
| 46 | - return false; |
|
| 47 | - } |
|
| 48 | - $terms = get_terms( $taxonomy, array( 'get' => 'all' ) ); |
|
| 49 | - $terms = array_map( function ( $term ) { |
|
| 50 | - /** |
|
| 51 | - * @var $term \WP_Term |
|
| 52 | - */ |
|
| 53 | - return $term->term_id; |
|
| 54 | - }, $terms ); |
|
| 55 | - if ( $operator === Rule_Validator::IS_EQUAL_TO ) { |
|
| 56 | - // if we dont have term id, then skip the flow. |
|
| 57 | - // If we are in term page, then we need to check if the current |
|
| 58 | - // term belongs to the taxonomy |
|
| 59 | - return in_array( $current_term->term_id, $terms ); |
|
| 60 | - } |
|
| 42 | + public function is_valid( $identifier, $operator, $operand_1, $taxonomy, $type ) { |
|
| 43 | + $current_term = get_term( $identifier ); |
|
| 44 | + // If it is not a term page, then return false for two operators. |
|
| 45 | + if ( ! $current_term instanceof \WP_Term ) { |
|
| 46 | + return false; |
|
| 47 | + } |
|
| 48 | + $terms = get_terms( $taxonomy, array( 'get' => 'all' ) ); |
|
| 49 | + $terms = array_map( function ( $term ) { |
|
| 50 | + /** |
|
| 51 | + * @var $term \WP_Term |
|
| 52 | + */ |
|
| 53 | + return $term->term_id; |
|
| 54 | + }, $terms ); |
|
| 55 | + if ( $operator === Rule_Validator::IS_EQUAL_TO ) { |
|
| 56 | + // if we dont have term id, then skip the flow. |
|
| 57 | + // If we are in term page, then we need to check if the current |
|
| 58 | + // term belongs to the taxonomy |
|
| 59 | + return in_array( $current_term->term_id, $terms ); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - if ( $operator === Rule_Validator::IS_NOT_EQUAL_TO ) { |
|
| 63 | - return ! in_array( $current_term->term_id, $terms ); |
|
| 64 | - } |
|
| 62 | + if ( $operator === Rule_Validator::IS_NOT_EQUAL_TO ) { |
|
| 63 | + return ! in_array( $current_term->term_id, $terms ); |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - } |
|
| 66 | + } |
|
| 67 | 67 | } |
@@ -18,7 +18,7 @@ discard block |
||
| 18 | 18 | const TAXONOMY = 'taxonomy'; |
| 19 | 19 | |
| 20 | 20 | public function __construct() { |
| 21 | - add_filter( 'wl_mappings_rule_validators', array( $this, 'wl_mappings_rule_validators' ) ); |
|
| 21 | + add_filter('wl_mappings_rule_validators', array($this, 'wl_mappings_rule_validators')); |
|
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | /** |
@@ -28,39 +28,39 @@ discard block |
||
| 28 | 28 | * |
| 29 | 29 | * @return array An array with validators plus ours. |
| 30 | 30 | */ |
| 31 | - public function wl_mappings_rule_validators( $value ) { |
|
| 32 | - $value[ self::TAXONOMY ] = $this; |
|
| 31 | + public function wl_mappings_rule_validators($value) { |
|
| 32 | + $value[self::TAXONOMY] = $this; |
|
| 33 | 33 | |
| 34 | 34 | return $value; |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | |
| 38 | 38 | public function get_label() { |
| 39 | - return __( 'TaxonomyTerm', 'wordlift' ); |
|
| 39 | + return __('TaxonomyTerm', 'wordlift'); |
|
| 40 | 40 | } |
| 41 | 41 | |
| 42 | - public function is_valid( $identifier, $operator, $operand_1, $taxonomy, $type ) { |
|
| 43 | - $current_term = get_term( $identifier ); |
|
| 42 | + public function is_valid($identifier, $operator, $operand_1, $taxonomy, $type) { |
|
| 43 | + $current_term = get_term($identifier); |
|
| 44 | 44 | // If it is not a term page, then return false for two operators. |
| 45 | - if ( ! $current_term instanceof \WP_Term ) { |
|
| 45 | + if ( ! $current_term instanceof \WP_Term) { |
|
| 46 | 46 | return false; |
| 47 | 47 | } |
| 48 | - $terms = get_terms( $taxonomy, array( 'get' => 'all' ) ); |
|
| 49 | - $terms = array_map( function ( $term ) { |
|
| 48 | + $terms = get_terms($taxonomy, array('get' => 'all')); |
|
| 49 | + $terms = array_map(function($term) { |
|
| 50 | 50 | /** |
| 51 | 51 | * @var $term \WP_Term |
| 52 | 52 | */ |
| 53 | 53 | return $term->term_id; |
| 54 | - }, $terms ); |
|
| 55 | - if ( $operator === Rule_Validator::IS_EQUAL_TO ) { |
|
| 54 | + }, $terms); |
|
| 55 | + if ($operator === Rule_Validator::IS_EQUAL_TO) { |
|
| 56 | 56 | // if we dont have term id, then skip the flow. |
| 57 | 57 | // If we are in term page, then we need to check if the current |
| 58 | 58 | // term belongs to the taxonomy |
| 59 | - return in_array( $current_term->term_id, $terms ); |
|
| 59 | + return in_array($current_term->term_id, $terms); |
|
| 60 | 60 | } |
| 61 | 61 | |
| 62 | - if ( $operator === Rule_Validator::IS_NOT_EQUAL_TO ) { |
|
| 63 | - return ! in_array( $current_term->term_id, $terms ); |
|
| 62 | + if ($operator === Rule_Validator::IS_NOT_EQUAL_TO) { |
|
| 63 | + return ! in_array($current_term->term_id, $terms); |
|
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | } |
@@ -15,63 +15,63 @@ |
||
| 15 | 15 | * @package Wordlift\Mappings\Validators |
| 16 | 16 | */ |
| 17 | 17 | class Post_Type_Rule_Validator implements Rule_Validator { |
| 18 | - /** |
|
| 19 | - * @since 3.25.0 |
|
| 20 | - * Enum for the post type rule validator. |
|
| 21 | - */ |
|
| 22 | - const POST_TYPE = 'post_type'; |
|
| 23 | - /** |
|
| 24 | - * Post_Type_Rule_Validator constructor. |
|
| 25 | - * |
|
| 26 | - * When initializing the class hooks to `wl_mappings_rule_validators`. |
|
| 27 | - */ |
|
| 28 | - public function __construct() { |
|
| 29 | - |
|
| 30 | - add_filter( 'wl_mappings_rule_validators', array( $this, 'wl_mappings_rule_validators' ) ); |
|
| 31 | - |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Hook to `wl_mappings_rule_validators` to register ourselves. |
|
| 36 | - * |
|
| 37 | - * @param array $value An array with validators. |
|
| 38 | - * |
|
| 39 | - * @return array An array with validators plus ours. |
|
| 40 | - */ |
|
| 41 | - public function wl_mappings_rule_validators( $value ) { |
|
| 42 | - |
|
| 43 | - $value[self::POST_TYPE] = $this; |
|
| 44 | - |
|
| 45 | - return $value; |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * {@inheritdoc} |
|
| 50 | - */ |
|
| 51 | - public function get_label() { |
|
| 52 | - return __( 'Post Type', 'wordlift' ); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * {@inheritdoc} |
|
| 57 | - */ |
|
| 58 | - public function is_valid( $identifier, $operator, $operand_1, $operand_2, $type ) { |
|
| 59 | - |
|
| 60 | - // Get the post type and then check whether it matches or not according to the operator. |
|
| 61 | - $post_type = get_post_type( $identifier ); |
|
| 62 | - |
|
| 63 | - switch ( $operator ) { |
|
| 64 | - case Rule_Validator::IS_NOT_EQUAL_TO: |
|
| 65 | - return $post_type !== $operand_2; |
|
| 66 | - |
|
| 67 | - case Rule_Validator::IS_EQUAL_TO: |
|
| 68 | - return $post_type === $operand_2; |
|
| 69 | - |
|
| 70 | - default: |
|
| 71 | - |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - return false; |
|
| 75 | - } |
|
| 18 | + /** |
|
| 19 | + * @since 3.25.0 |
|
| 20 | + * Enum for the post type rule validator. |
|
| 21 | + */ |
|
| 22 | + const POST_TYPE = 'post_type'; |
|
| 23 | + /** |
|
| 24 | + * Post_Type_Rule_Validator constructor. |
|
| 25 | + * |
|
| 26 | + * When initializing the class hooks to `wl_mappings_rule_validators`. |
|
| 27 | + */ |
|
| 28 | + public function __construct() { |
|
| 29 | + |
|
| 30 | + add_filter( 'wl_mappings_rule_validators', array( $this, 'wl_mappings_rule_validators' ) ); |
|
| 31 | + |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Hook to `wl_mappings_rule_validators` to register ourselves. |
|
| 36 | + * |
|
| 37 | + * @param array $value An array with validators. |
|
| 38 | + * |
|
| 39 | + * @return array An array with validators plus ours. |
|
| 40 | + */ |
|
| 41 | + public function wl_mappings_rule_validators( $value ) { |
|
| 42 | + |
|
| 43 | + $value[self::POST_TYPE] = $this; |
|
| 44 | + |
|
| 45 | + return $value; |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * {@inheritdoc} |
|
| 50 | + */ |
|
| 51 | + public function get_label() { |
|
| 52 | + return __( 'Post Type', 'wordlift' ); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * {@inheritdoc} |
|
| 57 | + */ |
|
| 58 | + public function is_valid( $identifier, $operator, $operand_1, $operand_2, $type ) { |
|
| 59 | + |
|
| 60 | + // Get the post type and then check whether it matches or not according to the operator. |
|
| 61 | + $post_type = get_post_type( $identifier ); |
|
| 62 | + |
|
| 63 | + switch ( $operator ) { |
|
| 64 | + case Rule_Validator::IS_NOT_EQUAL_TO: |
|
| 65 | + return $post_type !== $operand_2; |
|
| 66 | + |
|
| 67 | + case Rule_Validator::IS_EQUAL_TO: |
|
| 68 | + return $post_type === $operand_2; |
|
| 69 | + |
|
| 70 | + default: |
|
| 71 | + |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + return false; |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | 77 | } |
@@ -27,7 +27,7 @@ discard block |
||
| 27 | 27 | */ |
| 28 | 28 | public function __construct() { |
| 29 | 29 | |
| 30 | - add_filter( 'wl_mappings_rule_validators', array( $this, 'wl_mappings_rule_validators' ) ); |
|
| 30 | + add_filter('wl_mappings_rule_validators', array($this, 'wl_mappings_rule_validators')); |
|
| 31 | 31 | |
| 32 | 32 | } |
| 33 | 33 | |
@@ -38,7 +38,7 @@ discard block |
||
| 38 | 38 | * |
| 39 | 39 | * @return array An array with validators plus ours. |
| 40 | 40 | */ |
| 41 | - public function wl_mappings_rule_validators( $value ) { |
|
| 41 | + public function wl_mappings_rule_validators($value) { |
|
| 42 | 42 | |
| 43 | 43 | $value[self::POST_TYPE] = $this; |
| 44 | 44 | |
@@ -49,18 +49,18 @@ discard block |
||
| 49 | 49 | * {@inheritdoc} |
| 50 | 50 | */ |
| 51 | 51 | public function get_label() { |
| 52 | - return __( 'Post Type', 'wordlift' ); |
|
| 52 | + return __('Post Type', 'wordlift'); |
|
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | /** |
| 56 | 56 | * {@inheritdoc} |
| 57 | 57 | */ |
| 58 | - public function is_valid( $identifier, $operator, $operand_1, $operand_2, $type ) { |
|
| 58 | + public function is_valid($identifier, $operator, $operand_1, $operand_2, $type) { |
|
| 59 | 59 | |
| 60 | 60 | // Get the post type and then check whether it matches or not according to the operator. |
| 61 | - $post_type = get_post_type( $identifier ); |
|
| 61 | + $post_type = get_post_type($identifier); |
|
| 62 | 62 | |
| 63 | - switch ( $operator ) { |
|
| 63 | + switch ($operator) { |
|
| 64 | 64 | case Rule_Validator::IS_NOT_EQUAL_TO: |
| 65 | 65 | return $post_type !== $operand_2; |
| 66 | 66 | |
@@ -16,28 +16,28 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class Taxonomy_Rule_Validator implements Rule_Validator { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * {@inheritdoc} |
|
| 21 | - */ |
|
| 22 | - public function is_valid( $identifier, $operator, $operand_1, $operand_2, $type ) { |
|
| 23 | - |
|
| 24 | - $taxonomy = $operand_1; |
|
| 25 | - $term_slug = $operand_2; |
|
| 26 | - |
|
| 27 | - $is_object_in_term = is_object_in_term( $identifier, $taxonomy, $term_slug ); |
|
| 28 | - |
|
| 29 | - if ( is_wp_error( $is_object_in_term ) ) { |
|
| 30 | - return false; |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - return ( $is_object_in_term && self::IS_EQUAL_TO === $operator ) |
|
| 34 | - || ( ! $is_object_in_term && self::IS_NOT_EQUAL_TO === $operator ); |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * {@inheritdoc} |
|
| 39 | - */ |
|
| 40 | - public function get_label() { |
|
| 41 | - return __( 'Taxonomy', 'wordlift' ); |
|
| 42 | - } |
|
| 19 | + /** |
|
| 20 | + * {@inheritdoc} |
|
| 21 | + */ |
|
| 22 | + public function is_valid( $identifier, $operator, $operand_1, $operand_2, $type ) { |
|
| 23 | + |
|
| 24 | + $taxonomy = $operand_1; |
|
| 25 | + $term_slug = $operand_2; |
|
| 26 | + |
|
| 27 | + $is_object_in_term = is_object_in_term( $identifier, $taxonomy, $term_slug ); |
|
| 28 | + |
|
| 29 | + if ( is_wp_error( $is_object_in_term ) ) { |
|
| 30 | + return false; |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + return ( $is_object_in_term && self::IS_EQUAL_TO === $operator ) |
|
| 34 | + || ( ! $is_object_in_term && self::IS_NOT_EQUAL_TO === $operator ); |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * {@inheritdoc} |
|
| 39 | + */ |
|
| 40 | + public function get_label() { |
|
| 41 | + return __( 'Taxonomy', 'wordlift' ); |
|
| 42 | + } |
|
| 43 | 43 | } |
@@ -19,25 +19,25 @@ |
||
| 19 | 19 | /** |
| 20 | 20 | * {@inheritdoc} |
| 21 | 21 | */ |
| 22 | - public function is_valid( $identifier, $operator, $operand_1, $operand_2, $type ) { |
|
| 22 | + public function is_valid($identifier, $operator, $operand_1, $operand_2, $type) { |
|
| 23 | 23 | |
| 24 | 24 | $taxonomy = $operand_1; |
| 25 | 25 | $term_slug = $operand_2; |
| 26 | 26 | |
| 27 | - $is_object_in_term = is_object_in_term( $identifier, $taxonomy, $term_slug ); |
|
| 27 | + $is_object_in_term = is_object_in_term($identifier, $taxonomy, $term_slug); |
|
| 28 | 28 | |
| 29 | - if ( is_wp_error( $is_object_in_term ) ) { |
|
| 29 | + if (is_wp_error($is_object_in_term)) { |
|
| 30 | 30 | return false; |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | - return ( $is_object_in_term && self::IS_EQUAL_TO === $operator ) |
|
| 34 | - || ( ! $is_object_in_term && self::IS_NOT_EQUAL_TO === $operator ); |
|
| 33 | + return ($is_object_in_term && self::IS_EQUAL_TO === $operator) |
|
| 34 | + || ( ! $is_object_in_term && self::IS_NOT_EQUAL_TO === $operator); |
|
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | /** |
| 38 | 38 | * {@inheritdoc} |
| 39 | 39 | */ |
| 40 | 40 | public function get_label() { |
| 41 | - return __( 'Taxonomy', 'wordlift' ); |
|
| 41 | + return __('Taxonomy', 'wordlift'); |
|
| 42 | 42 | } |
| 43 | 43 | } |
@@ -11,45 +11,45 @@ |
||
| 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 | - /** |
|
| 45 | - * @return Data_Source_Factory |
|
| 46 | - */ |
|
| 47 | - public static function get_instance() { |
|
| 48 | - if ( self::$instance === null ) { |
|
| 49 | - self::$instance = new Data_Source_Factory(); |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - return self::$instance; |
|
| 53 | - } |
|
| 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 | + /** |
|
| 45 | + * @return Data_Source_Factory |
|
| 46 | + */ |
|
| 47 | + public static function get_instance() { |
|
| 48 | + if ( self::$instance === null ) { |
|
| 49 | + self::$instance = new Data_Source_Factory(); |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + return self::$instance; |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | 55 | } |
@@ -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 | } |
@@ -45,7 +45,7 @@ discard block |
||
| 45 | 45 | * @return Data_Source_Factory |
| 46 | 46 | */ |
| 47 | 47 | public static function get_instance() { |
| 48 | - if ( self::$instance === null ) { |
|
| 48 | + if (self::$instance === null) { |
|
| 49 | 49 | self::$instance = new Data_Source_Factory(); |
| 50 | 50 | } |
| 51 | 51 | |
@@ -13,6 +13,6 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | interface Abstract_Data_Source { |
| 15 | 15 | |
| 16 | - public function get_data( $identifier, $property, $type ); |
|
| 16 | + public function get_data( $identifier, $property, $type ); |
|
| 17 | 17 | |
| 18 | 18 | } |
@@ -13,6 +13,6 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | interface Abstract_Data_Source { |
| 15 | 15 | |
| 16 | - public function get_data( $identifier, $property, $type ); |
|
| 16 | + public function get_data($identifier, $property, $type); |
|
| 17 | 17 | |
| 18 | 18 | } |