| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
if ( ! class_exists( 'MywpLogger' ) ) : |
| 8 |
|
| 9 |
final class MywpLogger { |
| 10 |
|
| 11 |
private $post_type = 'mywp_logger'; |
| 12 |
|
| 13 |
private $id = false; |
| 14 |
private $blog_id = false; |
| 15 |
private $user_id = false; |
| 16 |
private $log = false; |
| 17 |
|
| 18 |
public function __construct( $logger_id = false ) { |
| 19 |
|
| 20 |
if( $logger_id === false ) { |
| 21 |
|
| 22 |
return false; |
| 23 |
|
| 24 |
} else { |
| 25 |
|
| 26 |
$logger_id = (int) $logger_id; |
| 27 |
|
| 28 |
$post = get_post( $logger_id ); |
| 29 |
|
| 30 |
if( empty( $post ) ) { |
| 31 |
|
| 32 |
$called_text = sprintf( 'new %s( %s )' , __CLASS__ , '$logger_id' ); |
| 33 |
|
| 34 |
MywpHelper::error_not_found_message( 'get_post( $logger_id )' , $called_text ); |
| 35 |
|
| 36 |
return false; |
| 37 |
|
| 38 |
} |
| 39 |
|
| 40 |
if( $post->post_type !== $this->post_type ) { |
| 41 |
|
| 42 |
$called_text = sprintf( 'new %s( %s )' , __CLASS__ , '$logger_id' ); |
| 43 |
|
| 44 |
MywpHelper::error_require_message( 'post_type is need ' . $this->post_type , $called_text ); |
| 45 |
|
| 46 |
return false; |
| 47 |
|
| 48 |
} |
| 49 |
|
| 50 |
$this->id = $logger_id; |
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
public function get_id() { |
| 57 |
|
| 58 |
return $this->id; |
| 59 |
|
| 60 |
} |
| 61 |
|
| 62 |
public function get_blog_id() { |
| 63 |
|
| 64 |
if( ! empty( $this->blog_id ) ) { |
| 65 |
|
| 66 |
return $this->blog_id; |
| 67 |
|
| 68 |
} |
| 69 |
|
| 70 |
$id = $this->get_id(); |
| 71 |
|
| 72 |
if( empty( $id ) ) { |
| 73 |
|
| 74 |
return false; |
| 75 |
|
| 76 |
} |
| 77 |
|
| 78 |
$this->blog_id = MywpPostType::get_post_meta( $id , 'blog_id' ); |
| 79 |
|
| 80 |
return $this->blog_id; |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
public function get_user_id() { |
| 85 |
|
| 86 |
if( ! empty( $this->user_id ) ) { |
| 87 |
|
| 88 |
return $this->user_id; |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
$id = $this->get_id(); |
| 93 |
|
| 94 |
if( empty( $id ) ) { |
| 95 |
|
| 96 |
return false; |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
$this->user_id = MywpPostType::get_post_meta( $id , 'user_id' ); |
| 101 |
|
| 102 |
return $this->user_id; |
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
public function get_log() { |
| 107 |
|
| 108 |
if( ! empty( $this->log ) ) { |
| 109 |
|
| 110 |
return $this->log; |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
$id = $this->get_id(); |
| 115 |
|
| 116 |
if( empty( $id ) ) { |
| 117 |
|
| 118 |
return false; |
| 119 |
|
| 120 |
} |
| 121 |
|
| 122 |
$this->log = MywpPostType::get_post_meta( $id , 'log' ); |
| 123 |
|
| 124 |
return $this->log; |
| 125 |
|
| 126 |
} |
| 127 |
|
| 128 |
public function delete_log() { |
| 129 |
|
| 130 |
$id = $this->get_id(); |
| 131 |
|
| 132 |
if( empty( $id ) ) { |
| 133 |
|
| 134 |
return false; |
| 135 |
|
| 136 |
} |
| 137 |
|
| 138 |
wp_delete_post( $id , true ); |
| 139 |
|
| 140 |
do_action( 'mywp_logger_deleted_log' , $id ); |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
public function add_log( $content = array() , $blog_id = false , $user_id = false ) { |
| 145 |
|
| 146 |
$id = $this->get_id(); |
| 147 |
|
| 148 |
if( ! empty( $id ) ) { |
| 149 |
|
| 150 |
return false; |
| 151 |
|
| 152 |
} |
| 153 |
|
| 154 |
if( empty( $blog_id ) ) { |
| 155 |
|
| 156 |
$blog_id = get_current_blog_id(); |
| 157 |
|
| 158 |
} |
| 159 |
|
| 160 |
if( empty( $user_id ) ) { |
| 161 |
|
| 162 |
$user_id = get_current_user_id(); |
| 163 |
|
| 164 |
} |
| 165 |
|
| 166 |
$request_uri = false; |
| 167 |
|
| 168 |
if( ! empty( $_SERVER['REQUEST_URI'] ) ) { |
| 169 |
|
| 170 |
$request_uri = strip_tags( $_SERVER['REQUEST_URI'] ); |
| 171 |
|
| 172 |
} |
| 173 |
|
| 174 |
$args = array( |
| 175 |
'post_status' => 'private', |
| 176 |
'post_type' => $this->post_type, |
| 177 |
); |
| 178 |
|
| 179 |
$post_id = wp_insert_post( $args ); |
| 180 |
|
| 181 |
if( empty( $post_id ) ) { |
| 182 |
|
| 183 |
return $post_id; |
| 184 |
|
| 185 |
} |
| 186 |
|
| 187 |
if( is_wp_error( $post_id ) ) { |
| 188 |
|
| 189 |
return is_wp_error( $post_id ); |
| 190 |
|
| 191 |
} |
| 192 |
|
| 193 |
add_post_meta( $post_id , 'log' , $content , true ); |
| 194 |
|
| 195 |
add_post_meta( $post_id , 'blog_id' , $blog_id , true ); |
| 196 |
|
| 197 |
add_post_meta( $post_id , 'user_id' , $user_id , true ); |
| 198 |
|
| 199 |
add_post_meta( $post_id , 'request_uri' , $request_uri , true ); |
| 200 |
|
| 201 |
do_action( 'mywp_logger_added_log' , $content , $post_id ); |
| 202 |
|
| 203 |
} |
| 204 |
|
| 205 |
} |
| 206 |
|
| 207 |
endif; |
| 208 |
|