PluginProbe
Plugin Notes Plus / 1.2.3
Plugin Notes Plus v1.2.3
trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.10 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9
plugin-notes-plus / admin / class-plugin-notes-plus-admin.php

class-plugin-notes-plus-admin.php in Plugin Notes Plus 1.2.3, at admin/class-plugin-notes-plus-admin.php

286 lines 8.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * Ajax handler for adding a plugin note.
169 *
170 * @since 1.0.0
171 */
172 public function pnp_add_response() {
173
174 // The $_REQUEST contains all the data sent via ajax
175 if ( isset($_REQUEST) ) {
176
177 // Check nonce and die if any funny business is detected
178 check_ajax_referer( 'pnp_add_plugin_note_form_nonce', 'security' );
179
180 $note = $_REQUEST['note'];
181 $icon = $_REQUEST['icon'];
182 $pluginId = $_REQUEST['pluginId'];
183
184 $noteId = $_REQUEST['noteId'];
185
186 $user = wp_get_current_user()->display_name;
187
188 // Create object and create_plugin_note
189 $plugin_note_obj = new Plugin_Notes_Plus_The_Note( $pluginId );
190
191 if ( $noteId !== '' ) {
192 $new_note_id = $plugin_note_obj->edit_plugin_note( $note, $icon, $user, $noteId );
193 } else {
194 $new_note_id = $plugin_note_obj->add_plugin_note( $note, $icon, $user );
195 }
196
197 $processed_note = $plugin_note_obj->get_plugin_note_by_id( $new_note_id );
198
199 $return = array(
200 'new_note_id' => $new_note_id,
201 'note_icon' => $processed_note['icon'],
202 'processed_note' => $processed_note['note'],
203 'note_user' => $processed_note['user'],
204 'note_time' => $processed_note['time']
205 );
206 wp_send_json($return);
207
208 }
209 // Always die in functions echoing ajax content
210 die();
211
212 }
213
214 /**
215 * Delete the plugin note by ID.
216 *
217 * @since 1.1.0
218 */
219 public function delete_plugin_note_by_id( $index ) {
220
221 global $wpdb;
222 $table_name = $wpdb->prefix . $this->plugin->get_table_name();
223
224 $wpdb->delete( $table_name, array( 'id' => $index ) );
225
226 }
227
228 /**
229 * Ajax handler for deleting a plugin note.
230 *
231 * @since 1.0.0
232 */
233 public function pnp_delete_response() {
234
235 // The $_REQUEST contains all the data sent via ajax
236 if ( isset($_REQUEST) ) {
237
238 // Check nonce and die if any funny business is detected
239 check_ajax_referer( 'pnp_add_plugin_note_form_nonce', 'security' );
240
241 $note_id = $_REQUEST['noteId'];
242 $this->delete_plugin_note_by_id( $note_id );
243 }
244 // Always die in functions echoing ajax content
245 die();
246 }
247
248 /**
249 * Function that retrieves plugin notes for plugins that need updating
250 *
251 * @since 1.2.0
252 */
253 public function get_notes_for_plugin_updates_page() {
254
255 $notes_array = array();
256
257 require_once( ABSPATH . 'wp-admin/includes/plugin-install.php' );
258 $plugins = get_plugin_updates();
259
260 if ( empty( $plugins ) ) {
261 return $notes_array;
262 }
263
264 foreach ( $plugins as $plugin_file => $plugin_data ) {
265
266 $plugin_unique_id = $this->get_plugin_unique_id( $plugin_file );
267 $plugin_note_obj = new Plugin_Notes_Plus_The_Note( $plugin_unique_id );
268 $the_plugin_notes = $plugin_note_obj->get_plugin_notes();
269
270 array_push( $notes_array, $the_plugin_notes );
271 }
272
273 return $notes_array;
274 }
275 }
276
277 Plugin_Notes_Plus_Admin::$icon_options = array(
278 'dashicons-clipboard' => esc_html__( 'Note', Plugin_Notes_Plus::get_plugin_name() ),
279 'dashicons-info' => esc_html__( 'Info', Plugin_Notes_Plus::get_plugin_name() ),
280 'dashicons-admin-links' => esc_html__( 'Link', Plugin_Notes_Plus::get_plugin_name() ),
281 'dashicons-warning' => esc_html__( 'Warning', Plugin_Notes_Plus::get_plugin_name() ),
282 'dashicons-admin-network' => esc_html__( 'Key', Plugin_Notes_Plus::get_plugin_name() ),
283 'dashicons-yes' => esc_html__( 'Checkmark', Plugin_Notes_Plus::get_plugin_name() ),
284 'dashicons-money' => esc_html__( 'Money', Plugin_Notes_Plus::get_plugin_name() ),
285 );
286