|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Gestion des meta |
|
4
|
|
|
* |
|
5
|
|
|
* @author Jimmy Latour <[email protected]> |
|
6
|
|
|
* @since 1.0.0.0 |
|
7
|
|
|
* @version 1.3.0.0 |
|
8
|
|
|
* @copyright 2015-2017 |
|
9
|
|
|
* @package wpeo_model |
|
10
|
|
|
* @subpackage class |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace eoxia; |
|
14
|
|
|
|
|
15
|
|
|
if ( ! defined( 'ABSPATH' ) ) { exit; } |
|
16
|
|
|
|
|
17
|
|
|
if ( ! class_exists( '\eoxia\Save_Meta_Class' ) ) { |
|
18
|
|
|
/** |
|
19
|
|
|
* Gestion des meta |
|
20
|
|
|
*/ |
|
21
|
|
|
class Save_Meta_Class extends Singleton_Util { |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Le constructeur |
|
25
|
|
|
* |
|
26
|
|
|
* @since 1.0.0.0 |
|
27
|
|
|
* @version 1.3.0.0 |
|
28
|
|
|
*/ |
|
29
|
|
|
protected function construct() {} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Apelle la méthode selon si la définition du champ est en meta "single" ou "multiple". |
|
33
|
|
|
* |
|
34
|
|
|
* @param object $object L'objet courant. |
|
35
|
|
|
* @param string $function La méthode a appeler. |
|
36
|
|
|
* @param string $meta_key Le nom de la meta key. |
|
37
|
|
|
* |
|
38
|
|
|
* @since 1.0.0.0 |
|
39
|
|
|
* @ersion 1.3.0.0 |
|
40
|
|
|
*/ |
|
41
|
|
|
public static function save_meta_data( $object, $function, $meta_key ) { |
|
42
|
|
|
$schema = $object->get_model(); |
|
43
|
|
|
|
|
44
|
|
|
$list_meta_json = array(); |
|
45
|
|
|
|
|
46
|
|
|
if ( ! empty( $object->id ) ) { |
|
47
|
|
|
foreach ( $schema as $field_name => $field_def ) { |
|
48
|
|
|
if ( ! empty( $field_def['meta_type'] ) && isset( $object->$field_name ) ) { |
|
49
|
|
|
if ( 'single' === $field_def['meta_type'] ) { |
|
50
|
|
|
self::g()->save_single_meta_data( $object->id, $object->$field_name, $function, $field_def['field'] ); |
|
|
|
|
|
|
51
|
|
|
} else { |
|
52
|
|
|
$list_meta_json[ $field_name ] = $object->$field_name; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
self::g()->save_multiple_meta_data( $object->id, $list_meta_json, $function, $meta_key ); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Sauvegarde la valeur dans une meta seul. |
|
63
|
|
|
* |
|
64
|
|
|
* @param int $id L'ID de l'élément. |
|
65
|
|
|
* @param mixed $value La valeur a enregistrer. |
|
66
|
|
|
* @param string $function La function a appeler. |
|
67
|
|
|
* @param string $meta_key Le nom de la meta. |
|
68
|
|
|
* |
|
69
|
|
|
* @since 1.0.0.0 |
|
70
|
|
|
* @version 1.3.0.0 |
|
71
|
|
|
*/ |
|
72
|
|
|
private function save_single_meta_data( $id, $value, $function, $meta_key ) { |
|
73
|
|
|
$data = $value; |
|
74
|
|
|
|
|
75
|
|
|
if ( is_array( $data ) ) { |
|
76
|
|
|
$data = \wp_json_encode( $data ); |
|
77
|
|
|
$data = addslashes( $data ); |
|
78
|
|
|
$data = preg_replace_callback( '/\\\\u([0-9a-f]{4})/i', function ( $matches ) { |
|
79
|
|
|
$sym = mb_convert_encoding( pack( 'H*', $matches[1] ), 'UTF-8', 'UTF-16' ); |
|
80
|
|
|
return $sym; |
|
81
|
|
|
}, $data ); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
call_user_func( $function, $id, $meta_key, $data ); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Sauvegarde les valeurs dans une meta. |
|
89
|
|
|
* |
|
90
|
|
|
* @param int $id L'ID de l'élément. |
|
91
|
|
|
* @param mixed $array_value Les valeurs a enregistrer. |
|
92
|
|
|
* @param string $function La function a appeler. |
|
93
|
|
|
* @param string $meta_key Le nom de la meta. |
|
94
|
|
|
* |
|
95
|
|
|
* @since 1.0.0.0 |
|
96
|
|
|
* @version 1.3.0.0 |
|
97
|
|
|
*/ |
|
98
|
|
|
private function save_multiple_meta_data( $id, $array_value, $function, $meta_key ) { |
|
99
|
|
|
$data = \wp_json_encode( $array_value ); |
|
100
|
|
|
$data = addslashes( $data ); |
|
101
|
|
|
$data = preg_replace_callback( '/\\\\u([0-9a-f]{4})/i', function ( $matches ) { |
|
102
|
|
|
$sym = mb_convert_encoding( pack( 'H*', $matches[1] ), 'UTF-8', 'UTF-16' ); |
|
103
|
|
|
return $sym; |
|
104
|
|
|
}, $data ); |
|
105
|
|
|
|
|
106
|
|
|
call_user_func( $function, $id, $meta_key, $data ); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} // End if(). |
|
|
|
|
|
|
110
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: