| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
if ( ! class_exists( 'MywpNotice' ) ) : |
| 8 |
|
| 9 |
final class MywpNotice { |
| 10 |
|
| 11 |
private $user_id = false; |
| 12 |
|
| 13 |
private $notice_key = 'mywp_notice'; |
| 14 |
|
| 15 |
public function __construct( $user_id = false ) { |
| 16 |
|
| 17 |
if( $user_id === false ) { |
| 18 |
|
| 19 |
$user_id = get_current_user_id(); |
| 20 |
|
| 21 |
} else { |
| 22 |
|
| 23 |
$user_id = (int) $user_id; |
| 24 |
|
| 25 |
} |
| 26 |
|
| 27 |
if( empty( $user_id ) ) { |
| 28 |
|
| 29 |
$called_text = sprintf( 'new %s( %s )' , __CLASS__ , '$user_id' ); |
| 30 |
|
| 31 |
MywpHelper::error_require_message( '$user_id' , $called_text ); |
| 32 |
|
| 33 |
return false; |
| 34 |
|
| 35 |
} |
| 36 |
|
| 37 |
$this->user_id = $user_id; |
| 38 |
|
| 39 |
} |
| 40 |
|
| 41 |
public function get_notices() { |
| 42 |
|
| 43 |
if( $this->user_id === false ) { |
| 44 |
|
| 45 |
$called_text = sprintf( '(object) %s->%s()' , __CLASS__ , __FUNCTION__ ); |
| 46 |
|
| 47 |
MywpHelper::error_not_found_message( 'user_id' , $called_text ); |
| 48 |
|
| 49 |
return false; |
| 50 |
|
| 51 |
} |
| 52 |
|
| 53 |
$notices = get_user_meta( $this->user_id , $this->notice_key , true ); |
| 54 |
|
| 55 |
if( empty( $notices ) ) { |
| 56 |
|
| 57 |
return array(); |
| 58 |
|
| 59 |
} |
| 60 |
|
| 61 |
return $notices; |
| 62 |
|
| 63 |
} |
| 64 |
|
| 65 |
public function get_notice() { |
| 66 |
|
| 67 |
$notices = $this->get_notices(); |
| 68 |
|
| 69 |
if( empty( $notices ) ) { |
| 70 |
|
| 71 |
return false; |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
$get_notice = false; |
| 76 |
|
| 77 |
foreach( $notices as $notice ) { |
| 78 |
|
| 79 |
$get_notice = $notice; |
| 80 |
|
| 81 |
break; |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
return $get_notice; |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
public function add_notice( $notice = false , $type = 'update' ) { |
| 90 |
|
| 91 |
$called_text = sprintf( '(object) %s->%s( %s )' , __CLASS__ , __FUNCTION__ , '$notice' ); |
| 92 |
|
| 93 |
if( $this->user_id === false ) { |
| 94 |
|
| 95 |
MywpHelper::error_not_found_message( 'user_id' , $called_text ); |
| 96 |
|
| 97 |
return false; |
| 98 |
|
| 99 |
} |
| 100 |
|
| 101 |
if( $notice === false ) { |
| 102 |
|
| 103 |
MywpHelper::error_not_found_message( '$notice' , $called_text ); |
| 104 |
|
| 105 |
return false; |
| 106 |
|
| 107 |
} |
| 108 |
|
| 109 |
$type = strip_tags( $type ); |
| 110 |
|
| 111 |
$notices = $this->get_notices(); |
| 112 |
|
| 113 |
$notices[ $type ][] = $notice; |
| 114 |
|
| 115 |
return $this->update_notice( $notices ); |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
public function add_notice_update( $notice = false ) { |
| 120 |
|
| 121 |
return $this->add_notice( $notice , 'update' ); |
| 122 |
|
| 123 |
} |
| 124 |
|
| 125 |
public function add_notice_error( $notice = false ) { |
| 126 |
|
| 127 |
return $this->add_notice( $notice , 'error' ); |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
public function update_notice( $notices = false ) { |
| 132 |
|
| 133 |
$called_text = sprintf( '(object) %s->%s( %s )' , __CLASS__ , __FUNCTION__ , '$notices' ); |
| 134 |
|
| 135 |
if( $this->user_id === false ) { |
| 136 |
|
| 137 |
MywpHelper::error_not_found_message( 'user_id' , $called_text ); |
| 138 |
|
| 139 |
return false; |
| 140 |
|
| 141 |
} |
| 142 |
|
| 143 |
if( $notices === false ) { |
| 144 |
|
| 145 |
MywpHelper::error_not_found_message( '$notices' , $called_text ); |
| 146 |
|
| 147 |
return false; |
| 148 |
|
| 149 |
} |
| 150 |
|
| 151 |
update_user_meta( $this->user_id , $this->notice_key , $notices ); |
| 152 |
|
| 153 |
return true; |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
public function delete_notice() { |
| 158 |
|
| 159 |
if( $this->user_id === false ) { |
| 160 |
|
| 161 |
$called_text = sprintf( '(object) %s->%s()' , __CLASS__ , __FUNCTION__ ); |
| 162 |
|
| 163 |
MywpHelper::error_not_found_message( 'user_id' , $called_text ); |
| 164 |
|
| 165 |
return false; |
| 166 |
|
| 167 |
} |
| 168 |
|
| 169 |
update_user_meta( $this->user_id , $this->notice_key , false ); |
| 170 |
|
| 171 |
} |
| 172 |
|
| 173 |
} |
| 174 |
|
| 175 |
endif; |
| 176 |
|