Advertisement
Guest User

Plugin manager WP3.1

a guest
Mar 6th, 2011
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.85 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: WPMU Plugin Manager
  4. Plugin URI: http://uglyrobot.com
  5. Description: Plugin management for Wordpress MU that supports the native WPMU plugins page and the WPMUdev Premium Supporter plugin!
  6. Version: 2.0.1
  7. Author: Aaron Edwards / Mason James
  8. Author URI: http://uglyrobot.com
  9. License: GPL (see http://www.gnu.org/copyleft/gpl.html)
  10.  
  11. For WordPress 3.0+ ONLY! Will not function correctly on any earlier install. Thanks.
  12. This is not an official plugin release. It's provided as is with no guarantees for future upgrades.
  13. Instructions: Copy into the mu-plugins folder. If the WPMUdev Premium Supporter plugin is
  14. enabled it will detect that. Make sure the plugins menu is enabled under site options.
  15. This adds two admin screens to Site Admin: Plugin Management and a form under the individual blog's edit page.
  16. */
  17. define('PC_HOME','index.php');
  18. define('PC_CMD_BASE',PC_HOME."?page=plugin-management");
  19.  
  20. //declare hooks
  21. add_action( 'admin_menu', 'mp_pm_add_menu' );
  22. add_action( 'wpmu_new_blog','mp_pm_new_blog' ); //auto activation hook
  23. add_filter( 'all_plugins', 'mp_pm_remove_plugins' );
  24. add_filter( 'plugin_action_links', 'mp_pm_action_links', 10, 4 );
  25. add_filter( 'active_plugins', 'mp_pm_check_activated' );
  26. //add_action( 'pre_current_active_plugins', 'mp_pm_supporter_message' ); removed for 2.8 compatibiity
  27. add_action( 'admin_notices', 'mp_pm_supporter_message' );
  28. //individual blog options
  29. add_action('wpmueditblogaction', 'mp_pm_blog_options_form');
  30. add_action('wpmu_update_blog_options', 'mp_pm_blog_options_form_process');
  31.  
  32.  
  33. function mp_pm_add_menu() {
  34. //check for WPMUdev Premium Supporter plugin
  35. global $supporter;
  36. if (function_exists(is_supporter))
  37. $supporter = true;
  38. else
  39. $supporter = false;
  40.  
  41. $file = substr(__FILE__,strlen(ABSPATH));
  42. if (is_super_admin())
  43. add_submenu_page('index.php', 'Plugin Management', 'Plugin Management', '10', 'plugin-management', 'mp_pm_page' );
  44. }
  45.  
  46.  
  47. function mp_pm_page() {
  48. global $supporter;
  49. if (!is_super_admin()) return;
  50. mp_pm_process_form();
  51. ?>
  52. <div class='wrap'>
  53. <h2>Manage Plugins</h2>
  54.  
  55. <?php if ($_REQUEST['saved']) { ?>
  56. <div id="message" class="updated fade"><p>Settings Saved</p></div>
  57. <?php } ?>
  58. <?php if ($_REQUEST['mass_activate']) { ?>
  59. <div id="message" class="updated fade"><p>Update: <span style="color:#FF3300;"><?php echo $_REQUEST['mass_activate']; ?></span> has been <strong>MASS ACTIVATED</strong>.</p></div>
  60. <?php } ?>
  61. <?php if ($_REQUEST['mass_deactivate']) { ?>
  62. <div id="message" class="updated fade"><p>Update: <span style="color:#FF3300;"><?php echo $_REQUEST['mass_deactivate']; ?></span> has been <strong>MASS DEACTIVATED</strong>.</p></div>
  63. <?php }
  64.  
  65. if ($supporter == FALSE) {
  66. echo '<p style="border:1px gray solid;margin:10px;padding:10px;">If you want to limit certain plugins to paid Supporters only, you must have the <a href="http://premium.wpmudev.org/?ref=prayhumbly-3442">WPMU Dev Premium</a> Supporter plugin installed.
  67. <a href="http://premium.wpmudev.org/?ref=prayhumbly-3442"><img src="http://premium.wpmudev.org/banners/180x60-banner.png" alt="180x60-banner.png" title="Check Out WPMU DEV Premium" /></a></p>';
  68. }
  69. ?>
  70. <div class="donate-message" style="border:1px gray solid;margin:10px;padding:10px;">
  71. <table>
  72. <tr>
  73. <td><?php if ($supporter) { echo "You are making money with this plugin. ";} ?>Why not send me a small donation in honor of the time I put into this? Thanks!</td>
  74. <td>
  75. <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
  76. <input type="hidden" name="cmd" value="_s-xclick">
  77. <input type="hidden" name="hosted_button_id" value="5935926">
  78. <input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
  79. <img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
  80. </form>
  81. </td>
  82. </tr>
  83. <tr>
  84. <td>I provide support and answer feature requests for this plugin exclusively through the forums at <a href="http://premium.wpmudev.org/?ref=prayhumbly-3442">WPMU Dev Premium</a>. If you are not a member, signup using this button and you can post your questions there.</td>
  85. <td><a href='http://premium.wpmudev.org/?ref=prayhumbly-3442'><img src='http://premium.wpmudev.org/banners/180x60-banner.png' alt='180x60-banner.png' title='Check Out WPMU DEV Premium' /></a></td>
  86. </tr>
  87. </table>
  88. </div>
  89. <h3>Help</h3>
  90. <p><strong>Auto Activation</strong><br/>
  91. When auto activation is on for a plugin, newly created blogs will have that plugin activated automatically. This does not affect existing blogs.</p>
  92. <p><strong>User Control</strong><br/>
  93. <?php if ($supporter) { ?>
  94. Choose if all users, supporters only, or no one will be able to activate/deactivate the plugin through the <cite>Plugins</cite> menu. When you turn it off, users that have the plugin activated are grandfathered in, and will continue to have access until they deactivate it.
  95. <?php } else { ?>
  96. When user control is enabled for a plugin, all users will be able to activate/deactivate the plugin through the <cite>Plugins</cite> menu. When you turn it off, users that have the plugin activated are grandfathered in, and will continue to have access until they deactivate it.
  97. <?php } ?>
  98. </p>
  99. <p><strong>Mass Activation/Deactivation</strong><br/>
  100. Mass activate and Mass deactivate buttons activate/deactivates the specified plugin for all blogs. This is different than the "Activate All" option on the WPMU plugins page, as users can later disable it and this only affects existing blogs. It also ignores the User Control option.</p>
  101. <form action="index.php?page=plugin-management&saved=1" method="post">
  102. <table class="widefat">
  103. <thead>
  104. <tr>
  105. <th>Name</th>
  106. <th>Version</th>
  107. <th>Author</th>
  108. <th title="Users may activate/deactivate">User Control</th>
  109. <th>Mass Activate</th>
  110. <th>Mass Deactivate</th>
  111. </tr>
  112. </thead>
  113. <?php
  114.  
  115. $plugins = get_plugins();
  116. $auto_activate = explode(',',get_site_option('mp_pm_auto_activate_list'));
  117. $user_control = explode(',',get_site_option('mp_pm_user_control_list'));
  118. $supporter_control = explode(',',get_site_option('mp_pm_supporter_control_list'));
  119. foreach($plugins as $file=>$p)
  120. {
  121. ?>
  122. <tr>
  123. <td><?php echo $p['Name']?></td>
  124. <td><?php echo $p['Version']?></td>
  125. <td><?php echo $p['Author']?></td>
  126. <td style="text-align:center;">
  127. <?php
  128. echo '<select name="control['.$file.']" />'."\n";
  129. $u_checked = in_array($file, $user_control);
  130. $s_checked = in_array($file, $supporter_control);
  131. $auto_checked = in_array($file, $auto_activate);
  132.  
  133. if ($u_checked) {
  134. $n_opt = '';
  135. $s_opt = '';
  136. $a_opt = ' selected="yes"';
  137. $auto_opt = '';
  138. } else if ($s_checked) {
  139. $n_opt = '';
  140. $s_opt = ' selected="yes"';
  141. $a_opt = '';
  142. $auto_opt = '';
  143. } else if ($auto_checked) {
  144. $n_opt = '';
  145. $s_opt = '';
  146. $a_opt = '';
  147. $auto_opt = ' selected="yes"';
  148. }else {
  149. $n_opt = ' selected="yes"';
  150. $s_opt = '';
  151. $a_opt = '';
  152. $auto_opt = '';
  153. }
  154.  
  155. $opts = '<option value="none"'.$n_opt.'>None</option>'."\n";
  156. if ($supporter)
  157. $opts .= '<option value="supporters"'.$s_opt.'>Supporters</option>'."\n";
  158. $opts .= '<option value="all"'.$a_opt.'>All Users</option>'."\n";
  159. $opts .= '<option value="auto"'.$auto_opt.'>Auto-Activate (All Users)</option>'."\n";
  160.  
  161. echo $opts.'</select>';
  162. ?>
  163. </td>
  164. <td><?php echo "<a href='".PC_CMD_BASE."&mass_activate=$file'>Activate All</a>" ?></td>
  165. <td><?php echo "<a href='".PC_CMD_BASE."&mass_deactivate=$file'>Deactivate All</a>" ?></td>
  166. </tr>
  167. <?php
  168. }
  169. ?>
  170. </table>
  171. <p class="submit">
  172. <input name="Submit" value="<?php _e('Update Options') ?>" type="submit">
  173. </p>
  174. </form>
  175. </div>
  176. <?php
  177. } //end mp_pm_page()
  178.  
  179.  
  180. function mp_pm_process_form() {
  181.  
  182. if (isset($_GET['mass_activate'])) {
  183. $plugins = get_plugins();
  184. $plugin = $_GET['mass_activate'];
  185. mp_pm_mass_activate($plugin);
  186. }
  187. if (isset($_GET['mass_deactivate'])) {
  188. $plugins = get_plugins();
  189. $plugin = $_GET['mass_deactivate'];
  190. mp_pm_mass_deactivate($plugin);
  191. }
  192.  
  193. if (isset($_POST['control'])) {
  194. //create blank arrays
  195. $supporter_control = array();
  196. $user_control = array();
  197. $auto_activate = array();
  198. foreach ($_POST['control'] as $plugin => $value) {
  199. if ($value == 'none') {
  200. //do nothing
  201. } else if ($value == 'supporters') {
  202. $supporter_control[] = $plugin;
  203. } else if ($value == 'all') {
  204. $user_control[] = $plugin;
  205. } else if ($value == 'auto') {
  206. $auto_activate[] = $plugin;
  207. }
  208. }
  209. update_site_option('mp_pm_supporter_control_list',implode(',',array_unique($supporter_control)));
  210. update_site_option('mp_pm_user_control_list',implode(',',array_unique($user_control)));
  211. update_site_option('mp_pm_auto_activate_list',implode(',',array_unique($auto_activate)));
  212.  
  213. //can't save blank value via update_site_option
  214. if (!$supporter_control)
  215. update_site_option('mp_pm_supporter_control_list','EMPTY');
  216. if (!$user_control)
  217. update_site_option('mp_pm_user_control_list','EMPTY');
  218. if (!$auto_activate)
  219. update_site_option('mp_pm_auto_activate_list','EMPTY');
  220. }
  221. }
  222.  
  223. //options added to ms-sites.php edit page. Overrides sitewide control settings for an individual blog.
  224. function mp_pm_blog_options_form($blog_id) {
  225.  
  226. $plugins = get_plugins();
  227. $override_plugins = (array)get_blog_option($blog_id, 'mp_pm_plugin_override_list');
  228. ?>
  229. <h3>Plugin Override Options</h3>
  230. <p style="padding:5px 10px 0 10px;margin:0;">
  231. Checked plugins here will be accessible to this blog, overriding the sitewide <a href="index.php?page=plugin-management">Plugin Management</a> settings. Uncheck to return to sitewide settings.
  232. </p>
  233. <table class="widefat" style="margin:10px;width:95%;">
  234. <thead>
  235. <tr>
  236. <th title="Blog users may activate/deactivate">User Control</th>
  237. <th>Name</th>
  238. </tr>
  239. </thead>
  240. <?php
  241. foreach($plugins as $file=>$p) {
  242. ?>
  243. <tr>
  244. <td>
  245. <?php
  246. $checked = (in_array($file, $override_plugins)) ? 'checked="checked"' : '';
  247. echo '<label><input name="plugins['.$file.']" type="checkbox" value="1" '.$checked.'/> Enable</label>';
  248. ?>
  249. </td>
  250. <td><?php echo $p['Name']?></td>
  251. </tr>
  252. <?php
  253. }
  254. echo '</table>';
  255. }
  256.  
  257. //process options from ms-sites.php edit page. Overrides sitewide control settings for an individual blog.
  258. function mp_pm_blog_options_form_process() {
  259. $override_plugins = array();
  260. if (is_array($_POST['plugins'])) {
  261. foreach ((array)$_POST['plugins'] as $plugin => $value) {
  262. $override_plugins[] = $plugin;
  263. }
  264. update_option( "mp_pm_plugin_override_list", $override_plugins );
  265. } else {
  266. update_option( "mp_pm_plugin_override_list", array('EMPTY') );
  267. }
  268. }
  269.  
  270. ///////activation functions (thanks plugin commander!)
  271. function mp_pm_new_blog($new_blog_id) {
  272. // a work around wpmu bug (http://trac.mu.wordpress.org/ticket/497)
  273. global $wpdb;
  274. if (!isset($wpdb->siteid)) $wpdb->siteid = 1;
  275. $auto_activate_list = get_site_option('mp_pm_auto_activate_list');
  276. $auto_activate = explode(',',$auto_activate_list);
  277. foreach($auto_activate as $plugin) {
  278. mp_pm_activate_plugin($new_blog_id, $plugin);
  279. }
  280. }
  281.  
  282. function mp_pm_activate_plugin($blog_id, $plugin) {
  283. if (empty($plugin)) return;
  284. if (validate_file($plugin)) return;
  285. if (!file_exists(ABSPATH . PLUGINDIR . '/' . $plugin)) return;
  286. switch_to_blog($blog_id);
  287. $current = get_option('active_plugins');
  288. ob_start();
  289. include_once(ABSPATH . PLUGINDIR . '/' . $plugin);
  290. $current[] = $plugin;
  291. sort($current);
  292. update_option('active_plugins', $current);
  293. do_action('activate_' . $plugin);
  294. $res = ob_get_clean();
  295. if (!empty($res)) echo __("Error activating $plugin for blog id=$blog_id: $res<br/>");
  296. restore_current_blog();
  297. }
  298.  
  299. function mp_pm_deactivate_plugin($blog_id, $plugin) {
  300. if (empty($plugin)) return;
  301. if (validate_file($plugin)) return;
  302. if (!file_exists(ABSPATH . PLUGINDIR . '/' . $plugin)) return;
  303.  
  304. switch_to_blog($blog_id);
  305. $current = get_option('active_plugins');
  306. array_splice($current, array_search($plugin, $current), 1 ); // Array-fu!
  307. update_option('active_plugins', $current);
  308. ob_start();
  309. do_action('deactivate_'.$plugin);
  310. $res = ob_get_clean();
  311. if (!empty($res)) echo "Error deactivating $plugin for blog id=$blog_id: $res<br/>";
  312. restore_current_blog();
  313. }
  314.  
  315. function mp_pm_mass_activate($plugin) {
  316. global $wpdb;
  317. $res = $wpdb->get_results("select blog_id from wp_blogs");
  318. if ($res === false)
  319. {
  320. echo "Failed to mass activate $plugin : error selecting blogs";
  321. return;
  322. }
  323.  
  324. foreach($res as $r)
  325. {
  326. mp_pm_activate_plugin($r->blog_id, $plugin);
  327. }
  328. }
  329.  
  330. function mp_pm_mass_deactivate($plugin) {
  331. global $wpdb;
  332. $res = $wpdb->get_results("select blog_id from wp_blogs");
  333. if ($res === false)
  334. {
  335. echo "Failed to mass deactivate $plugin : error selecting blogs";
  336. return;
  337. }
  338.  
  339. foreach($res as $r)
  340. {
  341. mp_pm_deactivate_plugin($r->blog_id, $plugin);
  342. }
  343. }
  344.  
  345. //remove plugins with no user control
  346. function mp_pm_remove_plugins($all_plugins) {
  347. if (is_super_admin()) //don't filter siteadmin
  348. return $all_plugins;
  349.  
  350. $auto_activate = explode(',',get_site_option('mp_pm_auto_activate_list'));
  351. $user_control = explode(',',get_site_option('mp_pm_user_control_list'));
  352. $supporter_control = explode(',',get_site_option('mp_pm_supporter_control_list'));
  353. $override_plugins = (array)get_option('mp_pm_plugin_override_list');
  354.  
  355. foreach ( (array)$all_plugins as $plugin_file => $plugin_data) {
  356. if (in_array($plugin_file, $user_control) || in_array($plugin_file, $auto_activate) || in_array($plugin_file, $supporter_control) || in_array($plugin_file, $override_plugins)) {
  357. //do nothing - leave it in
  358. } else {
  359. unset($all_plugins[$plugin_file]); //remove plugin
  360. }
  361. }
  362. return $all_plugins;
  363. }
  364.  
  365. //plugin activate links
  366. function mp_pm_action_links($action_links, $plugin_file, $plugin_data, $context) {
  367. global $supporter;
  368. if (is_super_admin()) //don't filter siteadmin
  369. return $action_links;
  370.  
  371. $auto_activate = explode(',',get_site_option('mp_pm_auto_activate_list'));
  372. $user_control = explode(',',get_site_option('mp_pm_user_control_list'));
  373. $supporter_control = explode(',',get_site_option('mp_pm_supporter_control_list'));
  374. $override_plugins = (array)get_option('mp_pm_plugin_override_list');
  375.  
  376. if ($context != 'active') {
  377. if (in_array($plugin_file, $user_control) || in_array($plugin_file, $auto_activate) || in_array($plugin_file, $override_plugins)) {
  378. return $action_links;
  379. } else if (in_array($plugin_file, $supporter_control)) {
  380. if ($supporter) {
  381. if (is_supporter()) {
  382. return $action_links;
  383. } else {
  384. add_action( "after_plugin_row_$plugin_file", "mp_pm_remove_checks" ); //add action to disable row's checkbox
  385. return array('<a style="color:red;" href="./supporter.php">Supporters Only</a>');
  386. }
  387. }
  388. }
  389. }
  390. return $action_links;
  391. }
  392.  
  393. //show supporter message if plugin exists
  394. function mp_pm_supporter_message() {
  395. global $supporter;
  396. if (is_super_admin()) //don't filter siteadmin
  397. return; // $action_links; 2.7.1
  398.  
  399. if ($supporter && substr( $_SERVER["PHP_SELF"], -11 ) == 'plugins.php') {
  400. if (!is_supporter()) {
  401. echo '<div class="error fade"><p style="font-weight:bold;padding:10px;">Premium plugins are only available to '.get_site_option('site_name').' Supporters. <a title="Become a Supporter" href="./supporter.php">Why not become a Supporter today?</a></p></div>';
  402. } else {
  403. echo '<div class="error" style="background-color:#F9F9F9;border:0;font-weight:bold;"><p>As a '.get_site_option('site_name')." Supporter, you now have access to all our premium plugins!</p></div>";
  404. }
  405. }
  406.  
  407. return;
  408. }
  409.  
  410. //use jquery to remove associated checkboxes to prevent mass activation (usability, not security)
  411. function mp_pm_remove_checks($plugin_file) {
  412. echo '
  413. <script type="text/javascript">
  414. ';
  415. echo " jQuery(\"input:checkbox[value='".attribute_escape($plugin_file)."']\").remove();\n";
  416. echo '
  417. </script>
  418. ';
  419. }
  420.  
  421. /*
  422. Removes activated plugins that should not have been activated (multi). Single activations
  423. are additionaly protected by a nonce field. Dirty hack in case someone uses firebug or
  424. something to hack the post and simulate a bulk activation. I'd rather prevent
  425. them from being activated in the first place, but there are no hooks for that! The
  426. display will show the activated status, but really they are not. Only hacking attempts
  427. will see this though! */
  428. function mp_pm_check_activated($active_plugins) {
  429. global $supporter, $_GET;
  430. if (is_super_admin()) //don't filter siteadmin
  431. return $active_plugins;
  432.  
  433. //only perform check right after activation hack attempt
  434. if (!isset($_GET['activate-multi']))
  435. return $active_plugins;
  436.  
  437. $auto_activate = explode(',',get_site_option('mp_pm_auto_activate_list'));
  438. $user_control = explode(',',get_site_option('mp_pm_user_control_list'));
  439. $supporter_control = explode(',',get_site_option('mp_pm_supporter_control_list'));
  440. $override_plugins = (array)get_option('mp_pm_plugin_override_list');
  441.  
  442. foreach ( (array)$active_plugins as $plugin_file => $plugin_data) {
  443. if (in_array($plugin_file, $user_control) || in_array($plugin_file, $auto_activate) || in_array($plugin_file, $supporter_control) || in_array($plugin_file, $override_plugins)) {
  444. //do nothing - leave it in
  445. } else {
  446. deactivate_plugins($plugin_file, true); //silently remove any plugins
  447. unset($active_plugins[$plugin_file]);
  448. }
  449. }
  450.  
  451. if ($supporter) {
  452. if (count($supporter_control) && !is_supporter()) {
  453. deactivate_plugins($supporter_control, true); //silently remove any plugins
  454. foreach ($supporter_control as $plugin_file)
  455. unset($active_plugins[$plugin_file]);
  456. }
  457. }
  458.  
  459. return $active_plugins;
  460. }
  461. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement