| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* The admin-specific functionality of the plugin. |
| 5 |
* Defines the plugin name, version, and icon options. |
| 6 |
* Enqueues admin-specific styles and scripts. |
| 7 |
* Defines functions for adding a custom column and rendering plugin notes. |
| 8 |
* Defines ajax handlers for adding and deleting notes. |
| 9 |
* |
| 10 |
* @link https://jamiebergen.com/ |
| 11 |
* @since 1.0.0 |
| 12 |
* |
| 13 |
* @package Plugin_Notes_Plus |
| 14 |
* @subpackage Plugin_Notes_Plus/admin |
| 15 |
* @author Jamie Bergen <jamie.bergen@gmail.com> |
| 16 |
*/ |
| 17 |
|
| 18 |
class Plugin_Notes_Plus_Admin { |
| 19 |
|
| 20 |
/** |
| 21 |
* A reference to the Plugin_Notes_Plus object. |
| 22 |
* |
| 23 |
* @since 1.0.0 |
| 24 |
* @access private |
| 25 |
* @var Plugin_Notes_Plus $plugin References the Plugin_Notes_Plus object for this plugin. |
| 26 |
*/ |
| 27 |
private $plugin; |
| 28 |
|
| 29 |
/** |
| 30 |
* A list of icon options. See definition of $icon_options after |
| 31 |
* the end of the class. |
| 32 |
* |
| 33 |
* @since 1.0.0 |
| 34 |
* @access public |
| 35 |
* @var array $icon_options |
| 36 |
*/ |
| 37 |
public static $icon_options; |
| 38 |
|
| 39 |
/** |
| 40 |
* Initialize the class and set its properties. |
| 41 |
* |
| 42 |
* @since 1.0.0 |
| 43 |
* |
| 44 |
*/ |
| 45 |
public function __construct( $plugin ) { |
| 46 |
|
| 47 |
$this->plugin = $plugin; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Register the stylesheets for the admin area. |
| 52 |
* |
| 53 |
* @since 1.0.0 |
| 54 |
*/ |
| 55 |
public function enqueue_styles() { |
| 56 |
|
| 57 |
/** |
| 58 |
* |
| 59 |
* An instance of this class should be passed to the run() function |
| 60 |
* defined in Plugin_Notes_Plus_Loader as all of the hooks are defined |
| 61 |
* in that particular class. |
| 62 |
* |
| 63 |
* The Plugin_Notes_Plus_Loader will then create the relationship |
| 64 |
* between the defined hooks and the functions defined in this |
| 65 |
* class. |
| 66 |
*/ |
| 67 |
|
| 68 |
wp_enqueue_style( $this->plugin->get_plugin_name(), plugin_dir_url( __FILE__ ) . 'css/plugin-notes-plus-admin.css', array(), $this->plugin->get_version(), 'all' ); |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Register the JavaScript for the admin area. |
| 74 |
* |
| 75 |
* @since 1.0.0 |
| 76 |
*/ |
| 77 |
public function enqueue_scripts() { |
| 78 |
|
| 79 |
/** |
| 80 |
* |
| 81 |
* An instance of this class should be passed to the run() function |
| 82 |
* defined in Plugin_Notes_Plus_Loader as all of the hooks are defined |
| 83 |
* in that particular class. |
| 84 |
* |
| 85 |
* The Plugin_Notes_Plus_Loader will then create the relationship |
| 86 |
* between the defined hooks and the functions defined in this |
| 87 |
* class. |
| 88 |
*/ |
| 89 |
|
| 90 |
$params = array ( |
| 91 |
'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 92 |
'ajax_nonce' => wp_create_nonce( 'pnp_add_plugin_note_form_nonce' ), // this is a unique token to prevent form hijacking |
| 93 |
'edit_text' => esc_html__( 'Edit', $this->plugin->get_plugin_name() ), |
| 94 |
'delete_text' => esc_html__( 'Delete', $this->plugin->get_plugin_name() ), |
| 95 |
'confirm_delete' => esc_html__( 'Are you sure you want to delete this note?', $this->plugin->get_plugin_name() ), |
| 96 |
'needs_content' => esc_html__( 'The note must contain content.', $this->plugin->get_plugin_name() ), |
| 97 |
); |
| 98 |
wp_enqueue_script( 'pnp_ajax_handle', plugin_dir_url( __FILE__ ) . 'js/plugin-notes-plus-admin.js', array( 'jquery' ), $this->plugin->get_version(), false ); |
| 99 |
wp_localize_script( 'pnp_ajax_handle', 'params', $params ); |
| 100 |
|
| 101 |
/** |
| 102 |
* Retrieve notes for plugins on updates page and send to JavaScript file |
| 103 |
* |
| 104 |
* @since 1.2.0 |
| 105 |
*/ |
| 106 |
|
| 107 |
// Only run this code on update-core.php admin page |
| 108 |
global $hook_suffix; |
| 109 |
|
| 110 |
if ( $hook_suffix == 'update-core.php' ) { |
| 111 |
$updates = $this->get_notes_for_plugin_updates_page(); |
| 112 |
$updates_json_str = json_encode( $updates ); |
| 113 |
$labels = array ( |
| 114 |
'col_title' => esc_html__( 'Plugin Notes', $this->plugin->get_plugin_name() ), |
| 115 |
'no_note' => esc_html__( 'No Plugin Notes', $this->plugin->get_plugin_name() ), |
| 116 |
); |
| 117 |
wp_enqueue_script( 'pnp_updates_script', plugin_dir_url( __FILE__ ) . 'js/plugin-notes-plus-updates.js', array( 'jquery' ), $this->plugin->get_version(), false ); |
| 118 |
wp_localize_script( 'pnp_updates_script', 'updates', $updates_json_str ); |
| 119 |
wp_localize_script( 'pnp_updates_script', 'labels', $labels ); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Add a custom column on the plugins page for notes. |
| 125 |
* |
| 126 |
* @since 1.0.0 |
| 127 |
*/ |
| 128 |
public function add_plugin_notes_column( $columns ) { |
| 129 |
$columns['pnp_plugin_notes_col'] = esc_html__( 'Plugin Notes', $this->plugin->get_plugin_name() ); |
| 130 |
return $columns; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Generate a unique id for a plugin based on the plugin's filepath. |
| 135 |
* Fixed to account for backslash in Windows paths |
| 136 |
* |
| 137 |
* @since 1.0.0 |
| 138 |
*/ |
| 139 |
public function get_plugin_unique_id( $plugin_file ) { |
| 140 |
return wp_normalize_path( $plugin_file ); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
/** |
| 145 |
* Display the plugin note(s) for a given plugin. |
| 146 |
* |
| 147 |
* @since 1.0.0 |
| 148 |
*/ |
| 149 |
public function display_plugin_note( $column_name, $plugin_file, $plugin_data ) { |
| 150 |
|
| 151 |
if ( 'pnp_plugin_notes_col' == $column_name ) { |
| 152 |
|
| 153 |
$plugin_unique_id = $this->get_plugin_unique_id( $plugin_file ); |
| 154 |
$plugin_note_obj = new Plugin_Notes_Plus_The_Note( $plugin_unique_id ); |
| 155 |
|
| 156 |
$the_plugin_notes = $plugin_note_obj->get_plugin_notes(); |
| 157 |
ksort($the_plugin_notes); |
| 158 |
|
| 159 |
$icon_options_array = apply_filters( 'plugin-notes-plus_icon_options', self::$icon_options ); |
| 160 |
$plugin_unique_id_sanitized = preg_replace( '/[^a-zA-Z0-9_-]/', '-', 'pnp_' . $plugin_unique_id ); |
| 161 |
|
| 162 |
include( 'partials/plugin-note-markup.php' ); |
| 163 |
|
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Display the plugin note(s) for a given plugin beneath the plugin description |
| 169 |
* |
| 170 |
* @since 1.2.4 |
| 171 |
*/ |
| 172 |
public function display_plugin_note_desc( $plugin_meta, $plugin_file, $plugin_data, $context ) { |
| 173 |
|
| 174 |
$plugin_unique_id = $this->get_plugin_unique_id( $plugin_file ); |
| 175 |
$plugin_note_obj = new Plugin_Notes_Plus_The_Note( $plugin_unique_id ); |
| 176 |
|
| 177 |
$the_plugin_notes = $plugin_note_obj->get_plugin_notes(); |
| 178 |
ksort($the_plugin_notes); |
| 179 |
|
| 180 |
$icon_options_array = apply_filters( 'plugin-notes-plus_icon_options', self::$icon_options ); |
| 181 |
$plugin_unique_id_sanitized = preg_replace( '/[^a-zA-Z0-9_-]/', '-', 'pnp_' . $plugin_unique_id ); |
| 182 |
|
| 183 |
include( 'partials/plugin-note-markup.php' ); |
| 184 |
|
| 185 |
return $plugin_meta; |
| 186 |
|
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Ajax handler for adding a plugin note. |
| 191 |
* |
| 192 |
* @since 1.0.0 |
| 193 |
*/ |
| 194 |
public function pnp_add_response() { |
| 195 |
|
| 196 |
// The $_REQUEST contains all the data sent via ajax |
| 197 |
if ( isset($_REQUEST) ) { |
| 198 |
|
| 199 |
// Check nonce and die if any funny business is detected |
| 200 |
check_ajax_referer( 'pnp_add_plugin_note_form_nonce', 'security' ); |
| 201 |
|
| 202 |
$note = $_REQUEST['note']; |
| 203 |
$icon = $_REQUEST['icon']; |
| 204 |
$pluginId = $_REQUEST['pluginId']; |
| 205 |
|
| 206 |
$noteId = $_REQUEST['noteId']; |
| 207 |
|
| 208 |
$user = wp_get_current_user()->display_name; |
| 209 |
|
| 210 |
// Create object and create_plugin_note |
| 211 |
$plugin_note_obj = new Plugin_Notes_Plus_The_Note( $pluginId ); |
| 212 |
|
| 213 |
if ( $noteId !== '' ) { |
| 214 |
$new_note_id = $plugin_note_obj->edit_plugin_note( $note, $icon, $user, $noteId ); |
| 215 |
} else { |
| 216 |
$new_note_id = $plugin_note_obj->add_plugin_note( $note, $icon, $user ); |
| 217 |
} |
| 218 |
|
| 219 |
$processed_note = $plugin_note_obj->get_plugin_note_by_id( $new_note_id ); |
| 220 |
|
| 221 |
$return = array( |
| 222 |
'new_note_id' => $new_note_id, |
| 223 |
'note_icon' => $processed_note['icon'], |
| 224 |
'processed_note' => $processed_note['note'], |
| 225 |
'note_user' => $processed_note['user'], |
| 226 |
'note_time' => $processed_note['time'] |
| 227 |
); |
| 228 |
wp_send_json($return); |
| 229 |
|
| 230 |
} |
| 231 |
// Always die in functions echoing ajax content |
| 232 |
die(); |
| 233 |
|
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Delete the plugin note by ID. |
| 238 |
* |
| 239 |
* @since 1.1.0 |
| 240 |
*/ |
| 241 |
public function delete_plugin_note_by_id( $index ) { |
| 242 |
|
| 243 |
global $wpdb; |
| 244 |
$table_name = $wpdb->prefix . $this->plugin->get_table_name(); |
| 245 |
|
| 246 |
$wpdb->delete( $table_name, array( 'id' => $index ) ); |
| 247 |
|
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Ajax handler for deleting a plugin note. |
| 252 |
* |
| 253 |
* @since 1.0.0 |
| 254 |
*/ |
| 255 |
public function pnp_delete_response() { |
| 256 |
|
| 257 |
// The $_REQUEST contains all the data sent via ajax |
| 258 |
if ( isset($_REQUEST) ) { |
| 259 |
|
| 260 |
// Check nonce and die if any funny business is detected |
| 261 |
check_ajax_referer( 'pnp_add_plugin_note_form_nonce', 'security' ); |
| 262 |
|
| 263 |
$note_id = $_REQUEST['noteId']; |
| 264 |
$this->delete_plugin_note_by_id( $note_id ); |
| 265 |
} |
| 266 |
// Always die in functions echoing ajax content |
| 267 |
die(); |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* Function that retrieves plugin notes for plugins that need updating |
| 272 |
* |
| 273 |
* @since 1.2.0 |
| 274 |
*/ |
| 275 |
public function get_notes_for_plugin_updates_page() { |
| 276 |
|
| 277 |
$notes_array = array(); |
| 278 |
|
| 279 |
require_once( ABSPATH . 'wp-admin/includes/plugin-install.php' ); |
| 280 |
$plugins = get_plugin_updates(); |
| 281 |
|
| 282 |
if ( empty( $plugins ) ) { |
| 283 |
return $notes_array; |
| 284 |
} |
| 285 |
|
| 286 |
foreach ( $plugins as $plugin_file => $plugin_data ) { |
| 287 |
|
| 288 |
$plugin_unique_id = $this->get_plugin_unique_id( $plugin_file ); |
| 289 |
$plugin_note_obj = new Plugin_Notes_Plus_The_Note( $plugin_unique_id ); |
| 290 |
$the_plugin_notes = $plugin_note_obj->get_plugin_notes(); |
| 291 |
|
| 292 |
array_push( $notes_array, $the_plugin_notes ); |
| 293 |
} |
| 294 |
|
| 295 |
return $notes_array; |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
Plugin_Notes_Plus_Admin::$icon_options = array( |
| 300 |
'dashicons-clipboard' => esc_html__( 'Note', Plugin_Notes_Plus::get_plugin_name() ), |
| 301 |
'dashicons-info' => esc_html__( 'Info', Plugin_Notes_Plus::get_plugin_name() ), |
| 302 |
'dashicons-admin-links' => esc_html__( 'Link', Plugin_Notes_Plus::get_plugin_name() ), |
| 303 |
'dashicons-warning' => esc_html__( 'Warning', Plugin_Notes_Plus::get_plugin_name() ), |
| 304 |
'dashicons-admin-network' => esc_html__( 'Key', Plugin_Notes_Plus::get_plugin_name() ), |
| 305 |
'dashicons-yes' => esc_html__( 'Checkmark', Plugin_Notes_Plus::get_plugin_name() ), |
| 306 |
'dashicons-money' => esc_html__( 'Money', Plugin_Notes_Plus::get_plugin_name() ), |
| 307 |
); |
| 308 |
|