1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Gestion des méthodes communes |
4
|
|
|
* |
5
|
|
|
* @package Evarisk\Plugin |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace eoxia; |
9
|
|
|
|
10
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
11
|
|
|
exit; |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
if ( ! class_exists( '\eoxia\Common_Util' ) ) { |
15
|
|
|
/** |
16
|
|
|
* Gestion des méthodes communes |
17
|
|
|
* |
18
|
|
|
* @author Jimmy Latour <[email protected]> |
19
|
|
|
* @version 1.1.0.0 |
20
|
|
|
*/ |
21
|
|
|
class Common_Util extends \eoxia\Singleton_Util { |
22
|
|
|
/** |
23
|
|
|
* Le constructeur obligatoirement pour utiliser la classe \eoxia\Singleton_Util |
24
|
|
|
* |
25
|
|
|
* @return void nothing |
26
|
|
|
*/ |
27
|
|
|
protected function construct() {} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Renvoie la dernière clé unique selon le type de l'élement |
31
|
|
|
* |
32
|
|
|
* @param string $controller Le controller. |
33
|
|
|
* @return int L'identifiant unique |
34
|
|
|
*/ |
35
|
|
|
public static function get_last_unique_key( $controller ) { |
36
|
|
|
$element_type = $controller::g()->get_post_type(); |
37
|
|
|
$wp_type = $controller::g()->get_identifier_helper(); |
38
|
|
|
if ( empty( $wp_type ) || empty( $element_type ) || ! is_string( $wp_type ) || ! is_string( $element_type ) ) { |
39
|
|
|
return false; |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
global $wpdb; |
43
|
|
|
switch ( $wp_type ) { |
44
|
|
|
case 'post': |
45
|
|
|
$query = $wpdb->prepare( |
46
|
|
|
"SELECT max( PM.meta_value + 0 ) |
47
|
|
|
FROM {$wpdb->postmeta} AS PM |
48
|
|
|
INNER JOIN {$wpdb->posts} AS P ON ( P.ID = PM.post_id ) |
49
|
|
|
WHERE PM.meta_key = %s |
50
|
|
|
AND P.post_type = %s", '_wpdigi_unique_key', $element_type ); |
51
|
|
|
break; |
52
|
|
|
|
53
|
|
|
case 'comment': |
54
|
|
|
$query = $wpdb->prepare( |
55
|
|
|
"SELECT max( CM.meta_value + 0 ) |
56
|
|
|
FROM {$wpdb->commentmeta} AS CM |
57
|
|
|
INNER JOIN {$wpdb->comments} AS C ON ( C.comment_ID = CM.comment_id ) |
58
|
|
|
WHERE CM.meta_key = %s |
59
|
|
|
AND C.comment_type = %s", '_wpdigi_unique_key', $element_type ); |
60
|
|
|
break; |
61
|
|
|
|
62
|
|
|
case 'user': |
63
|
|
|
$query = $wpdb->prepare( |
64
|
|
|
"SELECT max( UM.meta_value + 0 ) |
65
|
|
|
FROM {$wpdb->usermeta} AS UM |
66
|
|
|
WHERE UM.meta_key = %s", '_wpdigi_unique_key' ); |
67
|
|
|
break; |
68
|
|
|
|
69
|
|
|
case 'term': |
70
|
|
|
$query = $wpdb->prepare( |
71
|
|
|
"SELECT max( TM.meta_value + 0 ) |
72
|
|
|
FROM {$wpdb->term_taxonomy} AS T |
73
|
|
|
INNER JOIN {$wpdb->termmeta} AS TM ON ( T.term_id = TM.term_id ) |
74
|
|
|
WHERE TM.meta_key = %s AND T.taxonomy=%s", '_wpdigi_unique_key', $element_type ); |
75
|
|
|
break; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if ( ! empty( $query ) ) { |
79
|
|
|
$last_unique_key = $wpdb->get_var( $query ); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if ( empty( $last_unique_key ) ) { |
83
|
|
|
return 0; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $last_unique_key; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} // End if(). |
|
|
|
|
90
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.