*/ function plugmemo_get_all() { $notes = get_site_option( PLUGMEMO_OPTION, array() ); return is_array( $notes ) ? $notes : array(); } /** * Whether the current user may read notes. * * @return bool */ function plugmemo_user_can_view() { return current_user_can( 'activate_plugins' ); } /** * Whether the current user may add, edit and clear notes. * * On Multisite, notes are shared across the network, so only super admins may change them. * * @return bool */ function plugmemo_user_can_edit() { return current_user_can( is_multisite() ? 'manage_network_plugins' : 'activate_plugins' ); } /** * Add a "Note" link to each plugin's action links. * * @param string[] $actions Plugin action links. * @param string $plugin_file Plugin file relative to the plugins directory. * @return string[] */ function plugmemo_action_link( $actions, $plugin_file ) { if ( ! plugmemo_user_can_edit() ) { return $actions; } $notes = plugmemo_get_all(); $label = isset( $notes[ $plugin_file ] ) ? __( 'Edit note', 'plugmemo' ) : __( 'Add note', 'plugmemo' ); $actions['plugmemo'] = sprintf( '', esc_attr( $plugin_file ), esc_html( $label ) ); return $actions; } add_filter( 'plugin_action_links', 'plugmemo_action_link', 20, 2 ); add_filter( 'network_admin_plugin_action_links', 'plugmemo_action_link', 20, 2 ); /** * Print the note row (and its hidden editor) below each plugin. * * @param string $plugin_file Plugin file relative to the plugins directory. */ function plugmemo_row( $plugin_file ) { if ( ! plugmemo_user_can_view() ) { return; } $notes = plugmemo_get_all(); $note = isset( $notes[ $plugin_file ] ) ? $notes[ $plugin_file ] : ''; $can_edit = plugmemo_user_can_edit(); if ( '' === $note && ! $can_edit ) { return; } $wp_list_table = _get_list_table( 'WP_Plugins_List_Table', array( 'screen' => get_current_screen() ) ); $is_active = is_network_admin() ? is_plugin_active_for_network( $plugin_file ) : is_plugin_active( $plugin_file ); $editor_id = 'plugmemo-' . md5( $plugin_file ); ?> >

admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'plugmemo_save' ), 'i18n' => array( 'add' => __( 'Add note', 'plugmemo' ), 'edit' => __( 'Edit note', 'plugmemo' ), 'error' => __( 'The note could not be saved. Please try again.', 'plugmemo' ), ), ) ); } add_action( 'admin_enqueue_scripts', 'plugmemo_enqueue' ); /** * Save or clear a note via AJAX. */ function plugmemo_ajax_save() { check_ajax_referer( 'plugmemo_save', 'nonce' ); if ( ! plugmemo_user_can_edit() ) { wp_send_json_error( null, 403 ); } $plugin_file = isset( $_POST['plugin'] ) ? sanitize_text_field( wp_unslash( $_POST['plugin'] ) ) : ''; $note = isset( $_POST['note'] ) ? sanitize_textarea_field( wp_unslash( $_POST['note'] ) ) : ''; if ( ! array_key_exists( $plugin_file, get_plugins() ) ) { wp_send_json_error( null, 400 ); } $notes = plugmemo_get_all(); if ( '' === trim( $note ) ) { unset( $notes[ $plugin_file ] ); $note = ''; } else { $notes[ $plugin_file ] = $note; } update_site_option( PLUGMEMO_OPTION, $notes ); wp_send_json_success( array( 'note' => $note ) ); } add_action( 'wp_ajax_plugmemo_save', 'plugmemo_ajax_save' ); /** * Remove a plugin's note when the plugin is deleted. * * @param string $plugin_file Plugin file relative to the plugins directory. * @param bool $deleted Whether the plugin was deleted. */ function plugmemo_cleanup( $plugin_file, $deleted ) { if ( ! $deleted ) { return; } $notes = plugmemo_get_all(); if ( isset( $notes[ $plugin_file ] ) ) { unset( $notes[ $plugin_file ] ); update_site_option( PLUGMEMO_OPTION, $notes ); } } add_action( 'deleted_plugin', 'plugmemo_cleanup', 10, 2 );