PHP Cross Reference of WordPress Subversion HEAD

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]

title

Body

[close]

/wp-admin/includes/ -> plugin.php (source)

   1  <?php
   2  
   3  function get_plugin_data( $plugin_file ) {
   4      $plugin_data = implode( '', file( $plugin_file ));
   5      preg_match( '|Plugin Name:(.*)$|mi', $plugin_data, $plugin_name );
   6      preg_match( '|Plugin URI:(.*)$|mi', $plugin_data, $plugin_uri );
   7      preg_match( '|Description:(.*)$|mi', $plugin_data, $description );
   8      preg_match( '|Author:(.*)$|mi', $plugin_data, $author_name );
   9      preg_match( '|Author URI:(.*)$|mi', $plugin_data, $author_uri );
  10  
  11      if ( preg_match( "|Version:(.*)|i", $plugin_data, $version ))
  12          $version = trim( $version[1] );
  13      else
  14          $version = '';
  15  
  16      $description = wptexturize( trim( $description[1] ));
  17  
  18      $name = $plugin_name[1];
  19      $name = trim( $name );
  20      $plugin = $name;
  21      if ('' != trim($plugin_uri[1]) && '' != $name ) {
  22          $plugin = '<a href="' . trim( $plugin_uri[1] ) . '" title="'.__( 'Visit plugin homepage' ).'">'.$plugin.'</a>';
  23      }
  24  
  25      if ('' == $author_uri[1] ) {
  26          $author = trim( $author_name[1] );
  27      } else {
  28          $author = '<a href="' . trim( $author_uri[1] ) . '" title="'.__( 'Visit author homepage' ).'">' . trim( $author_name[1] ) . '</a>';
  29      }
  30  
  31      return array('Name' => $name, 'Title' => $plugin, 'Description' => $description, 'Author' => $author, 'Version' => $version);
  32  }
  33  
  34  function get_plugins() {
  35      global $wp_plugins;
  36  
  37      if ( isset( $wp_plugins ) ) {
  38          return $wp_plugins;
  39      }
  40  
  41      $wp_plugins = array ();
  42      $plugin_root = ABSPATH . PLUGINDIR;
  43  
  44      // Files in wp-content/plugins directory
  45      $plugins_dir = @ opendir( $plugin_root);
  46      if ( $plugins_dir ) {
  47          while (($file = readdir( $plugins_dir ) ) !== false ) {
  48              if ( substr($file, 0, 1) == '.' )
  49                  continue;
  50              if ( is_dir( $plugin_root.'/'.$file ) ) {
  51                  $plugins_subdir = @ opendir( $plugin_root.'/'.$file );
  52                  if ( $plugins_subdir ) {
  53                      while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
  54                          if ( substr($subfile, 0, 1) == '.' )
  55                              continue;
  56                          if ( substr($subfile, -4) == '.php' )
  57                              $plugin_files[] = "$file/$subfile";
  58                      }
  59                  }
  60              } else {
  61                  if ( substr($file, -4) == '.php' )
  62                      $plugin_files[] = $file;
  63              }
  64          }
  65      }
  66      @closedir( $plugins_dir );
  67      @closedir( $plugins_subdir );
  68  
  69      if ( !$plugins_dir || !$plugin_files )
  70          return $wp_plugins;
  71  
  72      foreach ( $plugin_files as $plugin_file ) {
  73          if ( !is_readable( "$plugin_root/$plugin_file" ) )
  74              continue;
  75  
  76          $plugin_data = get_plugin_data( "$plugin_root/$plugin_file" );
  77  
  78          if ( empty ( $plugin_data['Name'] ) )
  79              continue;
  80  
  81          $wp_plugins[plugin_basename( $plugin_file )] = $plugin_data;
  82      }
  83  
  84      uasort( $wp_plugins, create_function( '$a, $b', 'return strnatcasecmp( $a["Name"], $b["Name"] );' ));
  85  
  86      return $wp_plugins;
  87  }
  88  
  89  function activate_plugin($plugin) {
  90          $current = get_option('active_plugins');
  91          $plugin = trim($plugin);
  92  
  93          if ( validate_file($plugin) )
  94              return new WP_Error('plugin_invalid', __('Invalid plugin.'));
  95          if ( ! file_exists(ABSPATH . PLUGINDIR . '/' . $plugin) )
  96              return new WP_Error('plugin_not_found', __('Plugin file does not exist.'));
  97  
  98          if (!in_array($plugin, $current)) {
  99              wp_redirect(add_query_arg('_error_nonce', wp_create_nonce('plugin-activation-error_' . $plugin), 'plugins.php?error=true&plugin=' . $plugin)); // we'll override this later if the plugin can be included without fatal error
 100              ob_start();
 101              @include(ABSPATH . PLUGINDIR . '/' . $plugin);
 102              $current[] = $plugin;
 103              sort($current);
 104              update_option('active_plugins', $current);
 105              do_action('activate_' . $plugin);
 106              ob_end_clean();
 107          }
 108  
 109          return null;
 110  }
 111  
 112  function deactivate_plugins($plugins) {
 113      $current = get_option('active_plugins');
 114  
 115      if(!is_array($plugins))
 116          $plugins = array($plugins);
 117  
 118      foreach($plugins as $plugin) {
 119          array_splice($current, array_search( $plugin, $current), 1 ); // Array-fu!
 120          do_action('deactivate_' . trim( $plugin ));
 121      }
 122  
 123      update_option('active_plugins', $current);
 124  }
 125  
 126  function deactivate_all_plugins() {
 127      $current = get_option('active_plugins');
 128      deactivate_plugins($current);
 129  }
 130  
 131  //
 132  // Menu
 133  //
 134  
 135  function add_menu_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
 136      global $menu, $admin_page_hooks;
 137  
 138      $file = plugin_basename( $file );
 139  
 140      $menu[] = array ( $menu_title, $access_level, $file, $page_title );
 141  
 142      $admin_page_hooks[$file] = sanitize_title( $menu_title );
 143  
 144      $hookname = get_plugin_page_hookname( $file, '' );
 145      if (!empty ( $function ) && !empty ( $hookname ))
 146          add_action( $hookname, $function );
 147  
 148      return $hookname;
 149  }
 150  
 151  function add_submenu_page( $parent, $page_title, $menu_title, $access_level, $file, $function = '' ) {
 152      global $submenu;
 153      global $menu;
 154      global $_wp_real_parent_file;
 155      global $_wp_submenu_nopriv;
 156  
 157      $file = plugin_basename( $file );
 158  
 159      $parent = plugin_basename( $parent);
 160      if ( isset( $_wp_real_parent_file[$parent] ) )
 161          $parent = $_wp_real_parent_file[$parent];
 162  
 163      if ( !current_user_can( $access_level ) ) {
 164          $_wp_submenu_nopriv[$parent][$file] = true;
 165          return false;
 166      }
 167  
 168      // If the parent doesn't already have a submenu, add a link to the parent
 169      // as the first item in the submenu.  If the submenu file is the same as the
 170      // parent file someone is trying to link back to the parent manually.  In
 171      // this case, don't automatically add a link back to avoid duplication.
 172      if (!isset( $submenu[$parent] ) && $file != $parent  ) {
 173          foreach ( $menu as $parent_menu ) {
 174              if ( $parent_menu[2] == $parent && current_user_can( $parent_menu[1] ) )
 175                  $submenu[$parent][] = $parent_menu;
 176          }
 177      }
 178  
 179      $submenu[$parent][] = array ( $menu_title, $access_level, $file, $page_title );
 180  
 181      $hookname = get_plugin_page_hookname( $file, $parent);
 182      if (!empty ( $function ) && !empty ( $hookname ))
 183          add_action( $hookname, $function );
 184  
 185      return $hookname;
 186  }
 187  
 188  function add_management_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
 189      return add_submenu_page( 'edit.php', $page_title, $menu_title, $access_level, $file, $function );
 190  }
 191  
 192  function add_options_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
 193      return add_submenu_page( 'options-general.php', $page_title, $menu_title, $access_level, $file, $function );
 194  }
 195  
 196  function add_theme_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
 197      return add_submenu_page( 'themes.php', $page_title, $menu_title, $access_level, $file, $function );
 198  }
 199  
 200  function add_users_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
 201      if ( current_user_can('edit_users') )
 202          $parent = 'users.php';
 203      else
 204          $parent = 'profile.php';
 205      return add_submenu_page( $parent, $page_title, $menu_title, $access_level, $file, $function );
 206  }
 207  
 208  //
 209  // Pluggable Menu Support -- Private
 210  //
 211  
 212  function get_admin_page_parent() {
 213      global $parent_file;
 214      global $menu;
 215      global $submenu;
 216      global $pagenow;
 217      global $plugin_page;
 218      global $_wp_real_parent_file;
 219      global $_wp_menu_nopriv;
 220      global $_wp_submenu_nopriv;
 221  
 222      if ( !empty ( $parent_file ) ) {
 223          if ( isset( $_wp_real_parent_file[$parent_file] ) )
 224              $parent_file = $_wp_real_parent_file[$parent_file];
 225  
 226          return $parent_file;
 227      }
 228  
 229      if ( $pagenow == 'admin.php' && isset( $plugin_page ) ) {
 230          foreach ( $menu as $parent_menu ) {
 231              if ( $parent_menu[2] == $plugin_page ) {
 232                  $parent_file = $plugin_page;
 233                  if ( isset( $_wp_real_parent_file[$parent_file] ) )
 234                      $parent_file = $_wp_real_parent_file[$parent_file];
 235                  return $parent_file;
 236              }
 237          }
 238          if ( isset( $_wp_menu_nopriv[$plugin_page] ) ) {
 239              $parent_file = $plugin_page;
 240              if ( isset( $_wp_real_parent_file[$parent_file] ) )
 241                      $parent_file = $_wp_real_parent_file[$parent_file];
 242              return $parent_file;
 243          }
 244      }
 245  
 246      if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) ) {
 247          $parent_file = $pagenow;
 248          if ( isset( $_wp_real_parent_file[$parent_file] ) )
 249              $parent_file = $_wp_real_parent_file[$parent_file];
 250          return $parent_file;
 251      }
 252  
 253      foreach (array_keys( $submenu ) as $parent) {
 254          foreach ( $submenu[$parent] as $submenu_array ) {
 255              if ( isset( $_wp_real_parent_file[$parent] ) )
 256                  $parent = $_wp_real_parent_file[$parent];
 257              if ( $submenu_array[2] == $pagenow ) {
 258                  $parent_file = $parent;
 259                  return $parent;
 260              } else
 261                  if ( isset( $plugin_page ) && ($plugin_page == $submenu_array[2] ) ) {
 262                      $parent_file = $parent;
 263                      return $parent;
 264                  }
 265          }
 266      }
 267  
 268      $parent_file = '';
 269      return '';
 270  }
 271  
 272  function get_admin_page_title() {
 273      global $title;
 274      global $menu;
 275      global $submenu;
 276      global $pagenow;
 277      global $plugin_page;
 278  
 279      if ( isset( $title ) && !empty ( $title ) ) {
 280          return $title;
 281      }
 282  
 283      $hook = get_plugin_page_hook( $plugin_page, $pagenow );
 284  
 285      $parent = $parent1 = get_admin_page_parent();
 286      if ( empty ( $parent) ) {
 287          foreach ( $menu as $menu_array ) {
 288              if ( isset( $menu_array[3] ) ) {
 289                  if ( $menu_array[2] == $pagenow ) {
 290                      $title = $menu_array[3];
 291                      return $menu_array[3];
 292                  } else
 293                      if ( isset( $plugin_page ) && ($plugin_page == $menu_array[2] ) && ($hook == $menu_array[3] ) ) {
 294                          $title = $menu_array[3];
 295                          return $menu_array[3];
 296                      }
 297              } else {
 298                  $title = $menu_array[0];
 299                  return $title;
 300              }
 301          }
 302      } else {
 303          foreach (array_keys( $submenu ) as $parent) {
 304              foreach ( $submenu[$parent] as $submenu_array ) {
 305                  if ( isset( $plugin_page ) &&
 306                      ($plugin_page == $submenu_array[2] ) &&
 307                      (($parent == $pagenow ) || ($parent == $plugin_page ) || ($plugin_page == $hook ) || (($pagenow == 'admin.php' ) && ($parent1 != $submenu_array[2] ) ) )
 308                      ) {
 309                          $title = $submenu_array[3];
 310                          return $submenu_array[3];
 311                      }
 312  
 313                  if ( $submenu_array[2] != $pagenow || isset( $_GET['page'] ) ) // not the current page
 314                      continue;
 315  
 316                  if ( isset( $submenu_array[3] ) ) {
 317                      $title = $submenu_array[3];
 318                      return $submenu_array[3];
 319                  } else {
 320                      $title = $submenu_array[0];
 321                      return $title;
 322                  }
 323              }
 324          }
 325      }
 326  
 327      return $title;
 328  }
 329  
 330  function get_plugin_page_hook( $plugin_page, $parent_page ) {
 331      $hook = get_plugin_page_hookname( $plugin_page, $parent_page );
 332      if ( has_action($hook) )
 333          return $hook;
 334      else
 335          return null;
 336  }
 337  
 338  function get_plugin_page_hookname( $plugin_page, $parent_page ) {
 339      global $admin_page_hooks;
 340  
 341      $parent = get_admin_page_parent();
 342  
 343      if ( empty ( $parent_page ) || 'admin.php' == $parent_page ) {
 344          if ( isset( $admin_page_hooks[$plugin_page] ))
 345              $page_type = 'toplevel';
 346          else
 347              if ( isset( $admin_page_hooks[$parent] ))
 348                  $page_type = $admin_page_hooks[$parent];
 349      } else
 350          if ( isset( $admin_page_hooks[$parent_page] ) ) {
 351              $page_type = $admin_page_hooks[$parent_page];
 352          } else {
 353              $page_type = 'admin';
 354          }
 355  
 356      $plugin_name = preg_replace( '!\.php!', '', $plugin_page );
 357  
 358      return $page_type.'_page_'.$plugin_name;
 359  }
 360  
 361  function user_can_access_admin_page() {
 362      global $pagenow;
 363      global $menu;
 364      global $submenu;
 365      global $_wp_menu_nopriv;
 366      global $_wp_submenu_nopriv;
 367      global $plugin_page;
 368  
 369      $parent = get_admin_page_parent();
 370  
 371      if ( isset( $_wp_submenu_nopriv[$parent][$pagenow] ) )
 372          return false;
 373  
 374      if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$parent][$plugin_page] ) )
 375          return false;
 376  
 377      if ( empty( $parent) ) {
 378          if ( isset( $_wp_menu_nopriv[$pagenow] ) )
 379              return false;
 380          if ( isset( $_wp_submenu_nopriv[$pagenow][$pagenow] ) )
 381              return false;
 382          if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) )
 383              return false;
 384          foreach (array_keys( $_wp_submenu_nopriv ) as $key ) {
 385              if ( isset( $_wp_submenu_nopriv[$key][$pagenow] ) )
 386                  return false;
 387              if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$key][$plugin_page] ) )
 388              return false;
 389          }
 390          return true;
 391      }
 392  
 393      if ( isset( $submenu[$parent] ) ) {
 394          foreach ( $submenu[$parent] as $submenu_array ) {
 395              if ( isset( $plugin_page ) && ( $submenu_array[2] == $plugin_page ) ) {
 396                  if ( current_user_can( $submenu_array[1] ))
 397                      return true;
 398                  else
 399                      return false;
 400              } else if ( $submenu_array[2] == $pagenow ) {
 401                  if ( current_user_can( $submenu_array[1] ))
 402                      return true;
 403                  else
 404                      return false;
 405              }
 406          }
 407      }
 408  
 409      foreach ( $menu as $menu_array ) {
 410          if ( $menu_array[2] == $parent) {
 411              if ( current_user_can( $menu_array[1] ))
 412                  return true;
 413              else
 414                  return false;
 415          }
 416      }
 417  
 418      return true;
 419  }
 420  
 421  ?>


Generated Thu Dec 6 06:47:08 2007 for RedAlt XRefs Cross-referenced by PHPXref 0.6 and RedAlt