| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* A class that handles the notes for each plugin. |
| 5 |
* Defines functions to add, edit, and delete plugin notes. |
| 6 |
* Also defines functions to retrieve note(s) for a specific plugin. |
| 7 |
* |
| 8 |
* @link https://jamiebergen.com/ |
| 9 |
* @since 1.0.0 |
| 10 |
* |
| 11 |
* @package Plugin_Notes_Plus |
| 12 |
* @subpackage Plugin_Notes_Plus/admin |
| 13 |
* @author Jamie Bergen <jamie.bergen@gmail.com> |
| 14 |
*/ |
| 15 |
|
| 16 |
class Plugin_Notes_Plus_The_Note { |
| 17 |
|
| 18 |
/** |
| 19 |
* The unique ID of the plugin associated with the note. |
| 20 |
* |
| 21 |
* @since 1.0.0 |
| 22 |
* @access private |
| 23 |
* @var string $plugin_unique_id The unique ID of the plugin associated with the note. |
| 24 |
*/ |
| 25 |
private $plugin_unique_id; |
| 26 |
|
| 27 |
/** |
| 28 |
* A list of allowed tags. |
| 29 |
* |
| 30 |
* @since 1.0.0 |
| 31 |
* @access private |
| 32 |
* @var array $allowed_tags |
| 33 |
*/ |
| 34 |
private $allowed_tags = array( |
| 35 |
'a' => array( |
| 36 |
'href' => array(), |
| 37 |
'title' => array(), |
| 38 |
'target' => array(), |
| 39 |
), |
| 40 |
'br' => array(), |
| 41 |
'p' => array(), |
| 42 |
'b' => array(), |
| 43 |
'strong' => array(), |
| 44 |
'i' => array(), |
| 45 |
'em' => array(), |
| 46 |
'u' => array(), |
| 47 |
'hr' => array(), |
| 48 |
); |
| 49 |
|
| 50 |
/** |
| 51 |
* Initialize the class and set its properties. |
| 52 |
* |
| 53 |
* @since 1.0.0 |
| 54 |
* @param string $plugin_unique_id The name of the plugin associated with the note. |
| 55 |
*/ |
| 56 |
public function __construct( $plugin_unique_id ) { |
| 57 |
|
| 58 |
$this->plugin_unique_id = $plugin_unique_id; |
| 59 |
$this->allowed_tags = apply_filters( 'plugin-notes-plus_allowed_html', $this->allowed_tags ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Retrieve the plugin note or notes from the options table |
| 64 |
* Used for migration from options to custom table |
| 65 |
* |
| 66 |
* @since 1.1.0 |
| 67 |
*/ |
| 68 |
public function retrieve_notes_from_options() { |
| 69 |
$notes_array = get_option( $this->plugin_unique_id ); |
| 70 |
|
| 71 |
$notes_output_array = array(); |
| 72 |
|
| 73 |
if ( is_array($notes_array) ) { |
| 74 |
foreach( $notes_array as $index => $note_array ) { |
| 75 |
$notes_output_array[$index]['note'] = $this->process_plugin_note( $note_array['note'] ); |
| 76 |
$notes_output_array[$index]['icon'] = $note_array['icon']; |
| 77 |
$notes_output_array[$index]['user'] = $note_array['user']; |
| 78 |
$notes_output_array[$index]['time'] = $note_array['time']; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
return $notes_output_array; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Get a specific plugin note by id. |
| 87 |
* |
| 88 |
* @since 1.1.0 |
| 89 |
* |
| 90 |
*/ |
| 91 |
public function get_plugin_note_by_id( $note_id ) { |
| 92 |
|
| 93 |
global $wpdb; |
| 94 |
$table_name = $wpdb->prefix . Plugin_Notes_Plus::get_table_name(); |
| 95 |
|
| 96 |
$result = $wpdb->get_row( $wpdb->prepare( |
| 97 |
"SELECT * FROM $table_name WHERE ID = %d;", |
| 98 |
$note_id |
| 99 |
) ); |
| 100 |
|
| 101 |
$note_array = array(); |
| 102 |
$note_array['note'] = $this->process_plugin_note( $result->note_content ); |
| 103 |
$note_array['icon'] = $result->note_icon; |
| 104 |
$note_array['user'] = $result->user_name; |
| 105 |
$note_array['time'] = $result->time; |
| 106 |
|
| 107 |
return $note_array; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Get the plugin note or notes from the custom db table |
| 112 |
* |
| 113 |
* @since 1.1.0 |
| 114 |
*/ |
| 115 |
public function get_plugin_notes() { |
| 116 |
|
| 117 |
$notes_output_array = array(); |
| 118 |
|
| 119 |
global $wpdb; |
| 120 |
$table_name = $wpdb->prefix . Plugin_Notes_Plus::get_table_name(); |
| 121 |
|
| 122 |
$results = $wpdb->get_results( $wpdb->prepare( |
| 123 |
"SELECT * FROM $table_name WHERE plugin_id LIKE %s;", |
| 124 |
$this->plugin_unique_id |
| 125 |
) ); |
| 126 |
|
| 127 |
foreach( $results as $key => $row ) { |
| 128 |
|
| 129 |
$index = $row->id; |
| 130 |
$note = $row->note_content; |
| 131 |
$icon = $row->note_icon; |
| 132 |
$user = $row->user_name; |
| 133 |
$time = $row->time; |
| 134 |
|
| 135 |
$notes_output_array[$index]['note'] = $this->process_plugin_note( $note ); |
| 136 |
$notes_output_array[$index]['icon'] = $icon; |
| 137 |
$notes_output_array[$index]['user'] = $user; |
| 138 |
$notes_output_array[$index]['time'] = $time; |
| 139 |
} |
| 140 |
|
| 141 |
return $notes_output_array; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Migrate plugin note from options to new database table. |
| 146 |
* Assign a new id based on normalized filepath |
| 147 |
* |
| 148 |
* @since 1.1.0 |
| 149 |
*/ |
| 150 |
public function migrate_plugin_note( $new_id, $note_text, $icon_class, $username, $note_time ) { |
| 151 |
|
| 152 |
global $wpdb; |
| 153 |
$table_name = $wpdb->prefix . Plugin_Notes_Plus::get_table_name(); |
| 154 |
|
| 155 |
$wpdb->insert( |
| 156 |
$table_name, |
| 157 |
array( |
| 158 |
'plugin_id' => $new_id, |
| 159 |
'note_content' => $this->process_plugin_note( $note_text ), |
| 160 |
'note_icon' => $icon_class, |
| 161 |
'user_name' => $username, |
| 162 |
'time' => $note_time, |
| 163 |
) |
| 164 |
); |
| 165 |
|
| 166 |
return $wpdb->insert_id; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Create a new database entry for a plugin note. |
| 171 |
* |
| 172 |
* @since 1.1.0 |
| 173 |
*/ |
| 174 |
public function add_plugin_note( $note_text, $icon_class, $username ) { |
| 175 |
|
| 176 |
$note_time = time(); |
| 177 |
|
| 178 |
$processed_note = $this->process_plugin_note( $note_text ); |
| 179 |
|
| 180 |
global $wpdb; |
| 181 |
$table_name = $wpdb->prefix . Plugin_Notes_Plus::get_table_name(); |
| 182 |
|
| 183 |
$wpdb->insert( |
| 184 |
$table_name, |
| 185 |
array( |
| 186 |
'plugin_id' => $this->plugin_unique_id, |
| 187 |
'note_content' => $processed_note, |
| 188 |
'note_icon' => $icon_class, |
| 189 |
'user_name' => $username, |
| 190 |
'time' => $note_time, |
| 191 |
) |
| 192 |
); |
| 193 |
|
| 194 |
return $wpdb->insert_id; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Edit an existing plugin note. |
| 199 |
* |
| 200 |
* @since 1.1.0 |
| 201 |
*/ |
| 202 |
public function edit_plugin_note( $note_text, $icon_class, $username, $note_id ) { |
| 203 |
|
| 204 |
$note_time = time(); // update time for edited note |
| 205 |
|
| 206 |
$processed_note = $this->process_plugin_note( $note_text ); |
| 207 |
|
| 208 |
global $wpdb; |
| 209 |
$table_name = $wpdb->prefix . Plugin_Notes_Plus::get_table_name(); |
| 210 |
|
| 211 |
$wpdb->update( |
| 212 |
$table_name, |
| 213 |
array( |
| 214 |
'note_content' => $processed_note, // string |
| 215 |
'note_icon' => $icon_class, // string |
| 216 |
'user_name' => $username, // string |
| 217 |
'time' => $note_time, // int |
| 218 |
), |
| 219 |
array( 'id' => $note_id ), |
| 220 |
array( |
| 221 |
'%s', // note_content |
| 222 |
'%s', // note_icon |
| 223 |
'%s', // user_name |
| 224 |
'%d', // time |
| 225 |
), |
| 226 |
array( '%d' ) |
| 227 |
); |
| 228 |
|
| 229 |
return $note_id; |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Sanitize the plugin note and convert any urls to links. |
| 234 |
* |
| 235 |
* @since 1.0.0 |
| 236 |
*/ |
| 237 |
protected function process_plugin_note( $note ) { |
| 238 |
|
| 239 |
$sanitized_note = stripslashes( force_balance_tags( wp_kses( $note, $this->allowed_tags ) ) ); |
| 240 |
$note_with_links = $this->convert_urls_to_links( $sanitized_note ); |
| 241 |
|
| 242 |
return $note_with_links; |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Turn all URLs in clickable links. |
| 247 |
* |
| 248 |
* @param string $input |
| 249 |
* @return string |
| 250 |
* |
| 251 |
* @since 1.0.0 |
| 252 |
*/ |
| 253 |
protected function convert_urls_to_links( $input ) { |
| 254 |
|
| 255 |
$url_without_tags_regex = "/<a.*?<\/a>(*SKIP)(*F)|https?:\/\/\S*[^\s`!()\[\]{};:'\".,<>?«»“”‘’]/"; |
| 256 |
$replacement_pattern = '<a href="$0">$0</a>'; |
| 257 |
|
| 258 |
return preg_replace( $url_without_tags_regex, $replacement_pattern, $input ); |
| 259 |
} |
| 260 |
} |