Last active
April 16, 2023 15:32
-
-
Save brigleb/0338325b7b69158859f382226aa10bfe to your computer and use it in GitHub Desktop.
This plugin helps manage other WordPress plugins by tracking who installed them, the installation date, and a description. Just put in a folder of the same name, in the Plugins directory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Plugin Manager | |
Description: This plugin helps manage other plugins by tracking who installed them, the installation date, and a description. | |
Version: 1.0 | |
Author: Needmore Designs | |
*/ | |
// Create custom table | |
function pm_create_table() | |
{ | |
global $wpdb; | |
$table_name = $wpdb->prefix . 'pm_plugin_data'; | |
if ($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) { | |
$charset_collate = $wpdb->get_charset_collate(); | |
$sql = "CREATE TABLE $table_name ( | |
id mediumint(9) NOT NULL AUTO_INCREMENT, | |
plugin_name varchar(255) NOT NULL, | |
user_name varchar(255) NOT NULL, | |
install_date date NOT NULL, | |
description text NOT NULL, | |
PRIMARY KEY (id) | |
) $charset_collate;"; | |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); | |
dbDelta($sql); | |
} | |
} | |
register_activation_hook(__FILE__, 'pm_create_table'); | |
// Save prompt data to the custom table | |
function pm_save_prompt_data($plugin_name, $user_name, $install_date, $description) | |
{ | |
global $wpdb; | |
$table_name = $wpdb->prefix . 'pm_plugin_data'; | |
$wpdb->insert( | |
$table_name, | |
array( | |
'plugin_name' => $plugin_name, | |
'user_name' => $user_name, | |
'install_date' => $install_date, | |
'description' => $description | |
) | |
); | |
} | |
// Hook to show prompt after plugin installation | |
function pm_prompt_on_plugin_install($plugin) | |
{ | |
if (current_user_can('install_plugins')) { | |
update_option('pm_show_prompt', $plugin); | |
} | |
} | |
add_action('activated_plugin', 'pm_prompt_on_plugin_install', 10, 1); | |
// Prompt user for description | |
function pm_prompt() | |
{ | |
$plugin = get_option('pm_show_prompt'); | |
if ($plugin) { | |
delete_option('pm_show_prompt'); | |
echo <<<HTML | |
<script> | |
function pm_submit_description() { | |
var description = document.getElementById('pm-description').value; | |
if (description) { | |
var data = { | |
'action': 'pm_save_prompt_data', | |
'plugin': '{$plugin}', | |
'description': description | |
}; | |
jQuery.post(ajaxurl, data, function() { | |
alert('Plugin data saved successfully.'); | |
}); | |
} else { | |
alert('Please enter a description for the plugin.'); | |
} | |
} | |
window.addEventListener('load', function() { | |
var description = prompt('Please enter a description for the plugin:'); | |
if (description !== null) { | |
var textarea = document.createElement('textarea'); | |
textarea.id = 'pm-description'; | |
textarea.value = description; | |
textarea.style.display = 'none'; | |
document.body.appendChild(textarea); | |
pm_submit_description(); | |
} | |
}); | |
</script> | |
HTML; | |
} | |
} | |
add_action('admin_footer', 'pm_prompt'); | |
// Save prompt data via AJAX | |
function pm_save_prompt_data_ajax() | |
{ | |
$plugin = $_POST['plugin']; | |
$description = $_POST['description']; | |
$user_name = wp_get_current_user()->user_login; | |
$install_date = date('Y-m-d'); | |
pm_save_prompt_data($plugin, $user_name, $install_date, $description); | |
wp_die(); | |
} | |
add_action('wp_ajax_pm_save_prompt_data', 'pm_save_prompt_data_ajax'); | |
// Add custom columns | |
function pm_plugin_column($columns) | |
{ | |
$columns['pm_user_name'] = __('Installed by', 'plugin-manager'); | |
$columns['pm_install_date'] = __('Install date', 'plugin-manager'); | |
$columns['pm_description'] = __('Description', 'plugin-manager'); | |
return $columns; | |
} | |
add_filter('manage_plugins_columns', 'pm_plugin_column'); | |
// Display custom column data | |
function pm_plugin_column_data($column_name, $plugin_file, $plugin_data) | |
{ | |
if (!in_array($column_name, ['pm_user_name', 'pm_install_date', 'pm_description'])) { | |
return; | |
} | |
global $wpdb; | |
$table_name = $wpdb->prefix . 'pm_plugin_data'; | |
$row = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name WHERE plugin_name = %s", $plugin_file)); | |
if ($row) { | |
switch ($column_name) { | |
case 'pm_user_name': | |
$user = get_user_by('login', $row->user_name); | |
if ($user) { | |
$user_avatar = get_avatar($user->ID, 20); | |
$user_name = esc_html($row->user_name); | |
echo $user_avatar . ' ' . $user_name; | |
} else { | |
echo esc_html($row->user_name); | |
} | |
break; | |
case 'pm_install_date': | |
$install_date = esc_html($row->install_date); | |
echo $install_date; | |
break; | |
case 'pm_description': | |
$description = wp_kses_post($row->description); | |
echo $description; | |
break; | |
} | |
} else { | |
if ($column_name === 'pm_install_date') { | |
$plugin_file_path = WP_PLUGIN_DIR . '/' . $plugin_file; | |
if (file_exists($plugin_file_path)) { | |
$last_modified = filemtime($plugin_file_path); | |
$install_date_guess = date('Y-m-d', $last_modified); | |
echo $install_date_guess; | |
} else { | |
echo "Unknown"; | |
} | |
} else { | |
echo "-"; | |
} | |
} | |
} | |
add_action('manage_plugins_custom_column', 'pm_plugin_column_data', 10, 3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment