web/wp-admin/includes/plugin.php
branchwordpress
changeset 132 4d4862461b8d
parent 109 03b0d1493584
equal deleted inserted replaced
131:a4642baaf829 132:4d4862461b8d
    64  * @param bool $markup If the returned data should have HTML markup applied
    64  * @param bool $markup If the returned data should have HTML markup applied
    65  * @param bool $translate If the returned data should be translated
    65  * @param bool $translate If the returned data should be translated
    66  * @return array See above for description.
    66  * @return array See above for description.
    67  */
    67  */
    68 function get_plugin_data( $plugin_file, $markup = true, $translate = true ) {
    68 function get_plugin_data( $plugin_file, $markup = true, $translate = true ) {
    69 	// We don't need to write to the file, so just open for reading.
    69 
    70 	$fp = fopen($plugin_file, 'r');
    70 	$default_headers = array( 
    71 
    71 		'Name' => 'Plugin Name', 
    72 	// Pull only the first 8kiB of the file in.
    72 		'PluginURI' => 'Plugin URI', 
    73 	$plugin_data = fread( $fp, 8192 );
    73 		'Version' => 'Version', 
    74 
    74 		'Description' => 'Description', 
    75 	// PHP will close file handle, but we are good citizens.
    75 		'Author' => 'Author', 
    76 	fclose($fp);
    76 		'AuthorURI' => 'Author URI', 
    77 
    77 		'TextDomain' => 'Text Domain', 
    78 	preg_match( '|Plugin Name:(.*)$|mi', $plugin_data, $name );
    78 		'DomainPath' => 'Domain Path' 
    79 	preg_match( '|Plugin URI:(.*)$|mi', $plugin_data, $uri );
    79 		);
    80 	preg_match( '|Version:(.*)|i', $plugin_data, $version );
    80 
    81 	preg_match( '|Description:(.*)$|mi', $plugin_data, $description );
    81 	$plugin_data = get_file_data( $plugin_file, $default_headers, 'plugin' );
    82 	preg_match( '|Author:(.*)$|mi', $plugin_data, $author_name );
    82 
    83 	preg_match( '|Author URI:(.*)$|mi', $plugin_data, $author_uri );
    83 	//For backward compatibility by default Title is the same as Name.
    84 	preg_match( '|Text Domain:(.*)$|mi', $plugin_data, $text_domain );
    84 	$plugin_data['Title'] = $plugin_data['Name'];
    85 	preg_match( '|Domain Path:(.*)$|mi', $plugin_data, $domain_path );
    85 
    86 
       
    87 	foreach ( array( 'name', 'uri', 'version', 'description', 'author_name', 'author_uri', 'text_domain', 'domain_path' ) as $field ) {
       
    88 		if ( !empty( ${$field} ) )
       
    89 			${$field} = _cleanup_header_comment(${$field}[1]);
       
    90 		else
       
    91 			${$field} = '';
       
    92 	}
       
    93 
       
    94 	$plugin_data = array(
       
    95 				'Name' => $name, 'Title' => $name, 'PluginURI' => $uri, 'Description' => $description,
       
    96 				'Author' => $author_name, 'AuthorURI' => $author_uri, 'Version' => $version,
       
    97 				'TextDomain' => $text_domain, 'DomainPath' => $domain_path
       
    98 				);
       
    99 	if ( $markup || $translate )
    86 	if ( $markup || $translate )
   100 		$plugin_data = _get_plugin_data_markup_translate($plugin_file, $plugin_data, $markup, $translate);
    87 		$plugin_data = _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup, $translate );
   101 
    88 
   102 	return $plugin_data;
    89 	return $plugin_data;
   103 }
    90 }
   104 
    91 
   105 function _get_plugin_data_markup_translate($plugin_file, $plugin_data, $markup = true, $translate = true) {
    92 function _get_plugin_data_markup_translate($plugin_file, $plugin_data, $markup = true, $translate = true) {
   106 
    93 
   107 	//Translate fields
    94 	//Translate fields
   108 	if( $translate && ! empty($plugin_data['TextDomain']) ) {
    95 	if( $translate && ! empty($plugin_data['TextDomain']) ) {
   109 		if( ! empty( $plugin_data['DomainPath'] ) )
    96 		if( ! empty( $plugin_data['DomainPath'] ) )
   110 			load_plugin_textdomain($plugin_data['TextDomain'], dirname($plugin_file). $plugin_data['DomainPath']);
    97 			load_plugin_textdomain($plugin_data['TextDomain'], false, dirname($plugin_file). $plugin_data['DomainPath']);
   111 		else
    98 		else
   112 			load_plugin_textdomain($plugin_data['TextDomain'], dirname($plugin_file));
    99 			load_plugin_textdomain($plugin_data['TextDomain'], false, dirname($plugin_file));
   113 
   100 
   114 		foreach ( array('Name', 'PluginURI', 'Description', 'Author', 'AuthorURI', 'Version') as $field )
   101 		foreach ( array('Name', 'PluginURI', 'Description', 'Author', 'AuthorURI', 'Version') as $field )
   115 			$plugin_data[ $field ] = translate($plugin_data[ $field ], $plugin_data['TextDomain']);
   102 			$plugin_data[ $field ] = translate($plugin_data[ $field ], $plugin_data['TextDomain']);
   116 	}
   103 	}
   117 
   104 
   270  *
   257  *
   271  * @param string $plugin Base plugin path from plugins directory.
   258  * @param string $plugin Base plugin path from plugins directory.
   272  * @return bool True, if in the active plugins list. False, not in the list.
   259  * @return bool True, if in the active plugins list. False, not in the list.
   273  */
   260  */
   274 function is_plugin_active($plugin) {
   261 function is_plugin_active($plugin) {
   275 	return in_array($plugin, get_option('active_plugins'));
   262 	return in_array( $plugin, apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) );
   276 }
   263 }
   277 
   264 
   278 /**
   265 /**
   279  * Attempts activation of plugin in a "sandbox" and redirects on success.
   266  * Attempts activation of plugin in a "sandbox" and redirects on success.
   280  *
   267  *
   312 			wp_redirect(add_query_arg('_error_nonce', wp_create_nonce('plugin-activation-error_' . $plugin), $redirect)); // we'll override this later if the plugin can be included without fatal error
   299 			wp_redirect(add_query_arg('_error_nonce', wp_create_nonce('plugin-activation-error_' . $plugin), $redirect)); // we'll override this later if the plugin can be included without fatal error
   313 		ob_start();
   300 		ob_start();
   314 		@include(WP_PLUGIN_DIR . '/' . $plugin);
   301 		@include(WP_PLUGIN_DIR . '/' . $plugin);
   315 		$current[] = $plugin;
   302 		$current[] = $plugin;
   316 		sort($current);
   303 		sort($current);
       
   304 		do_action( 'activate_plugin', trim( $plugin) );
   317 		update_option('active_plugins', $current);
   305 		update_option('active_plugins', $current);
   318 		do_action('activate_' . $plugin);
   306 		do_action( 'activate_' . trim( $plugin ) );
       
   307 		do_action( 'activated_plugin', trim( $plugin) );
   319 		ob_end_clean();
   308 		ob_end_clean();
   320 	}
   309 	}
   321 
   310 
   322 	return null;
   311 	return null;
   323 }
   312 }
   341 
   330 
   342 	foreach ( $plugins as $plugin ) {
   331 	foreach ( $plugins as $plugin ) {
   343 		$plugin = plugin_basename($plugin);
   332 		$plugin = plugin_basename($plugin);
   344 		if( ! is_plugin_active($plugin) )
   333 		if( ! is_plugin_active($plugin) )
   345 			continue;
   334 			continue;
   346 		array_splice($current, array_search( $plugin, $current), 1 ); // Fixed Array-fu!
   335 		if ( ! $silent )
   347 		if ( ! $silent ) //Used by Plugin updater to internally deactivate plugin, however, not to notify plugins of the fact to prevent plugin output.
   336 			do_action( 'deactivate_plugin', trim( $plugin ) );
   348 			do_action('deactivate_' . trim( $plugin ));
   337 
       
   338 		$key = array_search( $plugin, (array) $current );
       
   339 
       
   340 		if ( false !== $key )
       
   341 			array_splice( $current, $key, 1 );
       
   342 
       
   343 		//Used by Plugin updater to internally deactivate plugin, however, not to notify plugins of the fact to prevent plugin output.
       
   344 		if ( ! $silent ) {
       
   345 			do_action( 'deactivate_' . trim( $plugin ) );
       
   346 			do_action( 'deactivated_plugin', trim( $plugin ) );
       
   347 		}
   349 	}
   348 	}
   350 
   349 
   351 	update_option('active_plugins', $current);
   350 	update_option('active_plugins', $current);
   352 }
   351 }
   353 
   352 
   475 
   474 
   476 	return true;
   475 	return true;
   477 }
   476 }
   478 
   477 
   479 function validate_active_plugins() {
   478 function validate_active_plugins() {
   480 	$check_plugins = get_option('active_plugins');
   479 	$check_plugins = apply_filters( 'active_plugins', get_option('active_plugins') );
   481 
   480 
   482 	// Sanity check.  If the active plugin list is not an array, make it an
   481 	// Sanity check.  If the active plugin list is not an array, make it an
   483 	// empty array.
   482 	// empty array.
   484 	if ( !is_array($check_plugins) ) {
   483 	if ( !is_array($check_plugins) ) {
   485 		update_option('active_plugins', array());
   484 		update_option('active_plugins', array());
   582 
   581 
   583 //
   582 //
   584 // Menu
   583 // Menu
   585 //
   584 //
   586 
   585 
   587 function add_menu_page( $page_title, $menu_title, $access_level, $file, $function = '', $icon_url = '' ) {
   586 function add_menu_page( $page_title, $menu_title, $access_level, $file, $function = '', $icon_url = '', $position = NULL ) {
   588 	global $menu, $admin_page_hooks, $_registered_pages;
   587 	global $menu, $admin_page_hooks, $_registered_pages;
   589 
   588 
   590 	$file = plugin_basename( $file );
   589 	$file = plugin_basename( $file );
   591 
   590 
   592 	$admin_page_hooks[$file] = sanitize_title( $menu_title );
   591 	$admin_page_hooks[$file] = sanitize_title( $menu_title );
   593 
   592 
   594 	$hookname = get_plugin_page_hookname( $file, '' );
   593 	$hookname = get_plugin_page_hookname( $file, '' );
   595 	if (!empty ( $function ) && !empty ( $hookname ))
   594 	if (!empty ( $function ) && !empty ( $hookname ))
   596 		add_action( $hookname, $function );
   595 		add_action( $hookname, $function );
   597 
   596 
   598 	if ( empty($icon_url) )
   597 	if ( empty($icon_url) ) {
   599 		$icon_url = 'images/generic.png';
   598 		$icon_url = 'images/generic.png';
   600 	elseif ( is_ssl() && 0 === strpos($icon_url, 'http://') )
   599 	} elseif ( is_ssl() && 0 === strpos($icon_url, 'http://') ) {
   601 		$icon_url = 'https://' . substr($icon_url, 7);
   600 		$icon_url = 'https://' . substr($icon_url, 7);
   602 
   601 	}
   603 	$menu[] = array ( $menu_title, $access_level, $file, $page_title, 'menu-top ' . $hookname, $hookname, $icon_url );
   602 
       
   603 	$new_menu = array ( $menu_title, $access_level, $file, $page_title, 'menu-top ' . $hookname, $hookname, $icon_url );
       
   604 
       
   605 	if ( NULL === $position  ) {
       
   606 		$menu[] = $new_menu;
       
   607 	} else {
       
   608 		$menu[$position] = $new_menu;
       
   609 	}
   604 
   610 
   605 	$_registered_pages[$hookname] = true;
   611 	$_registered_pages[$hookname] = true;
   606 
   612 
   607 	return $hookname;
   613 	return $hookname;
   608 }
   614 }
   609 
   615 
   610 function add_object_page( $page_title, $menu_title, $access_level, $file, $function = '', $icon_url = '') {
   616 function add_object_page( $page_title, $menu_title, $access_level, $file, $function = '', $icon_url = '') {
   611 	global $menu, $admin_page_hooks, $_wp_last_object_menu, $_registered_pages;
   617 	global $_wp_last_object_menu;
   612 
       
   613 	$file = plugin_basename( $file );
       
   614 
       
   615 	$admin_page_hooks[$file] = sanitize_title( $menu_title );
       
   616 
       
   617 	$hookname = get_plugin_page_hookname( $file, '' );
       
   618 	if (!empty ( $function ) && !empty ( $hookname ))
       
   619 		add_action( $hookname, $function );
       
   620 
       
   621 	if ( empty($icon_url) )
       
   622 		$icon_url = 'images/generic.png';
       
   623 
   618 
   624 	$_wp_last_object_menu++;
   619 	$_wp_last_object_menu++;
   625 
   620 
   626 	$menu[$_wp_last_object_menu] = array ( $menu_title, $access_level, $file, $page_title, 'menu-top ' . $hookname, $hookname, $icon_url );
   621 	return add_menu_page($page_title, $menu_title, $access_level, $file, $function, $icon_url, $_wp_last_object_menu);
   627 
       
   628 	$_registered_pages[$hookname] = true;
       
   629 
       
   630 	return $hookname;
       
   631 }
   622 }
   632 
   623 
   633 function add_utility_page( $page_title, $menu_title, $access_level, $file, $function = '', $icon_url = '') {
   624 function add_utility_page( $page_title, $menu_title, $access_level, $file, $function = '', $icon_url = '') {
   634 	global $menu, $admin_page_hooks, $_wp_last_utility_menu, $_registered_pages;
   625 	global $_wp_last_utility_menu;
   635 
       
   636 	$file = plugin_basename( $file );
       
   637 
       
   638 	$admin_page_hooks[$file] = sanitize_title( $menu_title );
       
   639 
       
   640 	$hookname = get_plugin_page_hookname( $file, '' );
       
   641 	if (!empty ( $function ) && !empty ( $hookname ))
       
   642 		add_action( $hookname, $function );
       
   643 
       
   644 	if ( empty($icon_url) )
       
   645 		$icon_url = 'images/generic.png';
       
   646 	elseif ( is_ssl() && 0 === strpos($icon_url, 'http://') )
       
   647 		$icon_url = 'https://' . substr($icon_url, 7);
       
   648 
   626 
   649 	$_wp_last_utility_menu++;
   627 	$_wp_last_utility_menu++;
   650 
   628 
   651 	$menu[$_wp_last_utility_menu] = array ( $menu_title, $access_level, $file, $page_title, 'menu-top ' . $hookname, $hookname, $icon_url );
   629 	return add_menu_page($page_title, $menu_title, $access_level, $file, $function, $icon_url, $_wp_last_utility_menu);
   652 
       
   653 	$_registered_pages[$hookname] = true;
       
   654 
       
   655 	return $hookname;
       
   656 }
   630 }
   657 
   631 
   658 function add_submenu_page( $parent, $page_title, $menu_title, $access_level, $file, $function = '' ) {
   632 function add_submenu_page( $parent, $page_title, $menu_title, $access_level, $file, $function = '' ) {
   659 	global $submenu;
   633 	global $submenu;
   660 	global $menu;
   634 	global $menu;
   690 	if (!empty ( $function ) && !empty ( $hookname ))
   664 	if (!empty ( $function ) && !empty ( $hookname ))
   691 		add_action( $hookname, $function );
   665 		add_action( $hookname, $function );
   692 
   666 
   693 	$_registered_pages[$hookname] = true;
   667 	$_registered_pages[$hookname] = true;
   694 	// backwards-compatibility for plugins using add_management page.  See wp-admin/admin.php for redirect from edit.php to tools.php
   668 	// backwards-compatibility for plugins using add_management page.  See wp-admin/admin.php for redirect from edit.php to tools.php
   695 	if ( 'tools.php' == $parent ) 
   669 	if ( 'tools.php' == $parent )
   696 		$_registered_pages[get_plugin_page_hookname( $file, 'edit.php')] = true;
   670 		$_registered_pages[get_plugin_page_hookname( $file, 'edit.php')] = true;
   697 
   671 
   698 	return $hookname;
   672 	return $hookname;
   699 }
   673 }
   700 
   674 
  1001 /**
   975 /**
  1002  * Register a setting and its sanitization callback
   976  * Register a setting and its sanitization callback
  1003  *
   977  *
  1004  * @since 2.7.0
   978  * @since 2.7.0
  1005  *
   979  *
  1006  * @param string $option_group A settings group name.  Can be anything.
   980  * @param string $option_group A settings group name.  Should correspond to a whitelisted option key name.
       
   981  * 	Default whitelisted option key names include "general," "discussion," and "reading," among others.
  1007  * @param string $option_name The name of an option to sanitize and save.
   982  * @param string $option_name The name of an option to sanitize and save.
  1008  * @param unknown_type $sanitize_callback A callback function that sanitizes the option's value.
   983  * @param unknown_type $sanitize_callback A callback function that sanitizes the option's value.
  1009  * @return unknown
   984  * @return unknown
  1010  */
   985  */
  1011 function register_setting($option_group, $option_name, $sanitize_callback = '') {
   986 function register_setting($option_group, $option_name, $sanitize_callback = '') {