函数文档

wp_load_press_this()

💡 云策文档标注

概述

wp_load_press_this() 函数用于加载 Press This 插件,处理用户权限检查、插件激活状态和安装流程。它确保只有具备相应权限的用户才能使用该功能,并在插件未激活或未安装时提供适当的错误消息和操作链接。

关键要点

  • 函数首先检查当前用户是否具有编辑文章和创建文章的权限,否则通过 wp_die() 显示错误并终止执行。
  • 如果 Press This 插件已激活,则包含相关类文件并实例化 WP_Press_This_Plugin 以输出 HTML。
  • 若插件未激活但用户有 activate_plugins 权限,则根据插件文件是否存在,生成激活或安装链接,否则显示安装提示。
  • 对于非主站点,如果插件未安装,会提示从主站点安装 Press This。
  • 如果用户无足够权限,函数会显示联系站点管理员的错误信息。

代码示例

if ( ! current_user_can( 'edit_posts' ) || ! current_user_can( get_post_type_object( 'post' )->cap->create_posts ) ) {
    wp_die(
        __( 'Sorry, you are not allowed to create posts as this user.' ),
        __( 'You need a higher level of permission.' ),
        403
    );
}

注意事项

  • 函数依赖于多个 WordPress 核心函数,如 current_user_can()、is_plugin_active() 和 wp_die(),确保在开发时正确引入相关文件。
  • 在生成非安全 URL 时使用 wp_nonce_url() 和 esc_url() 以防止安全漏洞。
  • 错误处理通过 wp_die() 实现,提供用户友好的消息和适当的 HTTP 状态码。

📄 原文内容

Source

function wp_load_press_this() {
	$plugin_slug = 'press-this';
	$plugin_file = 'press-this/press-this-plugin.php';

	if ( ! current_user_can( 'edit_posts' ) || ! current_user_can( get_post_type_object( 'post' )->cap->create_posts ) ) {
		wp_die(
			__( 'Sorry, you are not allowed to create posts as this user.' ),
			__( 'You need a higher level of permission.' ),
			403
		);
	} elseif ( is_plugin_active( $plugin_file ) ) {
		include WP_PLUGIN_DIR . '/press-this/class-wp-press-this-plugin.php';
		$wp_press_this = new WP_Press_This_Plugin();
		$wp_press_this->html();
	} elseif ( current_user_can( 'activate_plugins' ) ) {
		if ( file_exists( WP_PLUGIN_DIR . '/' . $plugin_file ) ) {
			$url    = wp_nonce_url(
				add_query_arg(
					array(
						'action' => 'activate',
						'plugin' => $plugin_file,
						'from'   => 'press-this',
					),
					admin_url( 'plugins.php' )
				),
				'activate-plugin_' . $plugin_file
			);
			$action = sprintf(
				'<a href="%1$s" aria-label="%2$s">%2$s</a>',
				esc_url( $url ),
				__( 'Activate Press This' )
			);
		} else {
			if ( is_main_site() ) {
				$url    = wp_nonce_url(
					add_query_arg(
						array(
							'action' => 'install-plugin',
							'plugin' => $plugin_slug,
							'from'   => 'press-this',
						),
						self_admin_url( 'update.php' )
					),
					'install-plugin_' . $plugin_slug
				);
				$action = sprintf(
					'<a href="%1$s" class="install-now" data-slug="%2$s" data-name="%2$s" aria-label="%3$s">%3$s</a>',
					esc_url( $url ),
					esc_attr( $plugin_slug ),
					_x( 'Install Now', 'plugin' )
				);
			} else {
				$action = sprintf(
					/* translators: %s: URL to Press This bookmarklet on the main site. */
					__( 'Press This is not installed. Please install Press This from <a href="%s">the main site</a>.' ),
					get_admin_url( get_current_network_id(), 'press-this.php' )
				);
			}
		}
		wp_die(
			__( 'The Press This plugin is required.' ) . '<br />' . $action,
			__( 'Installation Required' ),
			200
		);
	} else {
		wp_die(
			__( 'Press This is not available. Please contact your site administrator.' ),
			__( 'Installation Required' ),
			200
		);
	}
}