1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File for service log definition |
4
|
|
|
* |
5
|
|
|
* @package wpeo_logs |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace eoxia; |
9
|
|
|
|
10
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
11
|
|
|
exit; |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
if ( ! class_exists( '\eoxia\log_service_class' ) ) { |
15
|
|
|
/** |
16
|
|
|
* Class for service log definition |
17
|
|
|
*/ |
18
|
|
|
class log_service_class extends \eoxia\Singleton_Util { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Instanciate the log service |
22
|
|
|
*/ |
23
|
|
|
protected function construct() {} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Crée un service par défaut et l'ajoutes au tableau JSON _wpeo_log_settings. |
27
|
|
|
* Renvoie un message de type updated en transient pour afficher qu'un |
28
|
|
|
* nouveau service à été crée. |
29
|
|
|
* |
30
|
|
|
* @param string $name Le nom du service. |
31
|
|
|
*/ |
32
|
|
|
public function add( $name ) { |
33
|
|
|
$data_service = array( |
34
|
|
|
'active' => true, |
35
|
|
|
'name' => ! empty( $name ) ? $name : 'new_log', |
36
|
|
|
'size' => '1000000', |
37
|
|
|
'format' => 'ko', |
38
|
|
|
'rotate' => false, |
39
|
|
|
'number' => 0, |
40
|
|
|
'created_date' => current_time( 'mysql' ), |
41
|
|
|
); |
42
|
|
|
$array_current_settings = get_option( '_wpeo_log_settings' ); |
43
|
|
|
if ( ! empty( $array_current_settings ) && ! is_array( $array_current_settings ) ) { |
44
|
|
|
$array_current_settings = json_decode( $array_current_settings, true ); |
45
|
|
|
} else { |
46
|
|
|
$array_current_settings = array(); |
47
|
|
|
} |
48
|
|
|
$array_current_settings[] = $data_service; |
49
|
|
|
$success = update_option( '_wpeo_log_settings', wp_json_encode( $array_current_settings ) ); |
|
|
|
|
50
|
|
|
if ( $success ) { |
51
|
|
|
set_transient( 'log_message', wp_json_encode( array( 'type' => 'updated', 'message' => __( 'A new service has been created!', 'digirisk' ) ) ) ); |
52
|
|
|
} |
53
|
|
|
if ( ! empty( $name ) ) { |
54
|
|
|
return $data_service; |
55
|
|
|
} else { |
56
|
|
|
wp_safe_redirect( wp_get_referer() ); |
57
|
|
|
die(); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get a service definition by id |
63
|
|
|
* |
64
|
|
|
* @param integer $id Service identifier to get complete definition for. |
65
|
|
|
* @return array The service definition |
66
|
|
|
*/ |
67
|
|
|
public function get_service_by_id( $id ) { |
68
|
|
|
$array_service = get_option( '_wpeo_log_settings', array() ); |
|
|
|
|
69
|
|
|
$getted_service = null; |
70
|
|
|
if ( ! empty( $array_service ) ) { |
71
|
|
|
$array_service = json_decode( $array_service, true ); |
72
|
|
|
foreach ( $array_service as $key => $service ) { |
73
|
|
|
if ( $key == $id ) { |
74
|
|
|
$getted_service = $service; |
75
|
|
|
break; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
return $getted_service; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Récupères le service selon son nom. |
84
|
|
|
* |
85
|
|
|
* @param string $name Le nom du service. |
86
|
|
|
* |
87
|
|
|
* @return array | null |
88
|
|
|
*/ |
89
|
|
|
public static function get_service_by_name( $name ) { |
90
|
|
|
$array_service = get_option( '_wpeo_log_settings', array() ); |
|
|
|
|
91
|
|
|
$getted_service = null; |
92
|
|
|
if ( ! empty( $array_service ) ) { |
93
|
|
|
$array_service = ! is_array( $array_service ) ? json_decode( $array_service, true ) : $array_service; |
94
|
|
|
foreach ( $array_service as $service ) { |
95
|
|
|
if ( ! empty( $service ) && ! empty( $service['name'] ) && ( $service['name'] === $name ) ) { |
96
|
|
|
$getted_service = $service; |
97
|
|
|
break; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
return $getted_service; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Create a new service in database in case it does not exists |
106
|
|
|
* |
107
|
|
|
* @param string $service_name The service name to check if it exists or to create if it is not the case. |
108
|
|
|
* |
109
|
|
|
* @return array The service definition |
110
|
|
|
*/ |
111
|
|
|
public function create_service( $service_name ) { |
112
|
|
|
$service = self::get_service_by_name( $service_name ); |
113
|
|
|
|
114
|
|
|
if ( null === $service ) { |
115
|
|
|
/** Créer le service s'il n'existe pas */ |
116
|
|
|
$service = self::add( $service_name ); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $service; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.