Table of Contents
1 Core data operation class
2 Plug-in entity class
3 Server-side authentication operations
4 Server-side Weibo operation
5 Server data providing interface
6 Server-side callback operation
7 Server-side application entrance
7 Client application entrance
8 Summary
Home CMS Tutorial WordPress Share a WordPress Weibo wall plug-in based on Yar

Share a WordPress Weibo wall plug-in based on Yar

Jul 05, 2021 pm 04:25 PM
php wordpress

In the current mobile Internet era, Weibo has become an indispensable social tool in everyone’s life. WordPress is the most popular blogging system in the world. Connecting your blog to Sina Weibo and leveraging Weibo’s strong user base can not only provide your website with huge traffic, but also bring Come of immeasurable value.

WordPress Weibo Wall is such a tool. This is not an ordinary plug-in. It is a plug-in built on SAE and based on Yar. It is very lightweight, unlike other plug-ins that provide a lot of gorgeous but impractical functions, which are not only bloated but also slow down. This is a plug-in based on Yar, developed in the underlying C language and has excellent performance. And it is very scalable and can provide you with the following functions:

1. Personal Weibo wall
2. Publish articles and synchronize them to Sina Weibo
3. Synchronize article comments to Sina Weibo
Next, let’s introduce the basic structure:

1 Core data operation class

This class is in the Dao.class.php file. It is the core of the plug-in and is responsible for obtaining data from the server

/**
*
*	用户数据获取类
*	@author 夏天
*	@date 2015年6月28日
*	@site http://www.xtwind.com
*
*/
class Dao{
	/**
	*	微博RPC操作对象
	*/
	private $client;

	/**
	*	用户标识
	*/
	private $mark;

	/**
	*	构造函数设置用户标识
	*/
	function __construct($state);

	/**
	*	返回用户标识
	*/
	public function getMark();
 
	/**
	*	启用插件
	*	@return 成功返回true,失败返回认证地址 
	*/
	public function run();

	/**
	*	获取授权情况
	* 	@return string 返回过期时间,未登录或者过期返回false
	*/
	public function getAuthOver();

	/**
	*	删除授权
	*	@return boolean
	*/
	public function delAuth();

	/**
	*	获取认证跳转url
	*	@return string
	*/
	public function getAuthUrl();

	/**
	*	获取用户微博列表
	* 	@return array
	*/
	public function getWeibo();

	/**
	*	获取用户基本信息
	* 	@return array
	*/
	public function getUser();

	/**
	*	发布微博
	*	@return Array 返回微博数据数组
	*/
	public function weiboPub($content,$imgUrl=null);

	/**
	*	删除微博
	*	@param  int  微博ID
	*	@return Array 返回被删除微博数据数组
	*/
	public function weiboDel($weiboID);

	/**
	*	发布一条评论过
	*	@param  int  微博ID
	*	@param 	string  评论内容
	*	@return array 评论相关数组
	*/
	public function sendComment($id,$comment);

	/**
	*	关注一个用户
	*	@param 用户ID或者名字
	*	@return 返回关注者信息
	*/
	public function followUser($user);

	/**
	*	转发微博
	*	@param int 微博id
	*	@param string 添加的内容
	*/
	public function forwardWeibo($id,$text=null);
}
Copy after login

2 Plug-in entity class

This class is the entity of the plug-in, defined in Plugins.class.php, and is responsible for calling the Dao class to implement various functions, including input and output, user configuration, and authorization. Management

/**
*	插件实体类
*	@Author:Summer
*	@data:  2015-06-28
*	@site:  http://www.xtwind.com
*/
class Plugins{
	/**
	*	数据获取类对象
	*/
	private $dao;

	/**
	*	插件显示别名
	*/
	private $slug = 'weibo-wall';

	/**
	*	插件页url
	*/
	private $plugUrl ;

	/*
	*	插件构造
	* 	@param 用户数据操作类
	*/
	public function __construct(Dao $obj);

	/**
	*	启用插件,注册钩子,调用用户函数,删除授权,发表微博
	* 	@param array 插件设置选项关联数组,key必须为对应的操作方法,该数组中的键会被注册为wordpress相应钩子
	* 	@param array 需要过滤的动作,该数组中键不会被注册为钩子,但是会作为方法被调用,值为方法的参数
	*/
	public function run($arr1=null,$arr2=null);

	/**
	*	插件主页显示
	*/
	public function display_function();

	/**
	*	新文章同步发布微博
	* 	@param 	int 文章ID
	*/
	public function publish_post($postID);

	/**
	*	删除文章同步删除微博
	* 	@param int 文章ID 
	*/
	public function before_delete_post($postID);

	/**
	*	收到评论同步到微博评论
	*	@param id 评论id
	*/
	public function comment_post($commentID);

	/**
	*	关注作者
	*/
	public function follow_author($userid);

	/**
	*	用户微博数据获取
	*/
	public function weiboOuput( $atts=null, $content = null );
	/**
	*	数据页面输出
	*/
	public function showWeibo();

	/**
	*	图片URL处理
	* 	@param string
	*/
	private function getOriginalUrl($url);

	/**
	*	时间转换
	* 	@param string
	*/
	private function Sec2Time($time);

	/**
	*	插件设置key获取
	* 	@param string 	需要设置的key
	*/
	private function setting_key($key,$func=false);

	/**
	*	插件设置value获取
	* 	@param string 	需要获取的value
	*/
	private function get_setting($key,$func=false);

	/**
	*	插件设置删除
	*/	
	private function del_setting();

	/**
	*	提示信息
	*	@param string
	*/
	private function noticeMsg($msg);
}
Copy after login

3 Server-side authentication operations

This interface defines all operations required for user authentication, including obtaining authorization, deleting authorization, checking authorization, etc., defined in AuthDao.php

/**
*	认证操作类接口
*	@author 夏天
*	@date 2015年6月18日
*	@site http://www.xtwind.com
*/
interface AuthDao{
	/**
	*	设置用户AccessToken
	*	@return boolean
	*/
	public function setAccessToken();

	/**
	*	获取用户AccessToken
	*	@return String 
	*/
	public function getAccessToken();

	/**
	*	删除用户AccessToken
	*	@return boolean
	*/
	public function delAccessToken();

	/**
	*	判断用户AccessToken是否存在
	*	@return boolean
	*/
	public function isLogin();

	/**
	*	获取认证跳转url
	*	@return string
	*/
	public function getAuthUrl();

	/**
	*	授权过期时间
	*	@return string
	*/
	public function getAuthOver();
}
Copy after login

4 Server-side Weibo operation

This interface defines all methods related to user Weibo operations, including publishing Weibo, reading Weibo, reading information, deleting Weibo, etc., in WeiboDao. PHP definition

/**
*	微博操作类接口
*	@author 夏天
*	@date 2015年6月18日
*	@site http://www.xtwind.com
*/

interface WeiboDao {

	/**
	*	获取用户微博信息列表
	*	@param  int 	获取数量
	*	@param  int 	类型过滤  0:全部、1:原创、2:图片、3:视频、4:音乐,默认为0。
	*	@return String
	*/
	public function getWeibo();

	/**
	*	获取用户基本信息
	*	@return Array
	*/
	public function getUser();

	/**
	*	发布微博
	*	@return Array 返回微博数据数组
	*/
	public function weiboPub($content,$imgUrl);

	/**
	*	删除微博
	*	@return Array 返回被删除微博数据数组
	*/
	public function weiboDel($weiboID);

	/**
	*	发布一条评论
	*	@param integer 微博ID
	*	@param string  评论内容
	*/
	public function sendComment($id,$comment);

	/**
	*	关注一个用户
	*	@param 用户ID或者名字
	*	@return 返回关注者信息
	*/
	public function followUser($user);

	/**
	*	转发微博
	*	@param int 微博id
	*	@param string 添加的信息
	*/
	public function forwardWeibo($id,$text=null);
}
Copy after login

5 Server data providing interface

This interface is responsible for providing data to the client, as well as some operations required by the client. It is inherited from the Weibo operation interface and is in APIDao.php Definition

/**
*	对外提供服务类接口,继承于微博操作接口
*	@author 夏天
*	@date 2015年6月18日
*	@site http://www.xtwind.com
*/
interface DaoAPI extends WeiboDao{
	/**
	*	删除用户AccessToken
	*	@return boolean
	*/
	public function delAccessToken();

	/**
	*	判断用户AccessToken是否存在
	*	@return boolean
	*/
	public function isLogin();

	/**
	*	获取认证跳转url
	*	@return string
	*/
	public function getAuthUrl();

	/**
	*	授权过期时间
	*	@return string
	*/
	public function getAuthOver();
}
Copy after login

6 Server-side callback operation

This class encapsulates the callback operation after communicating with the Weibo open platform to obtain the user AccessToken

class Callback {
	/**
	*	微博认证类对象
	*/
	private $authObj;

	/**
	*	构造函数
	*	@param AuthDaoImpl 微博认证对象
	*/
	public function __construct(AuthDaoImpl $obj);

	/**
	*	认证回调操作,保存AccessToken
	* 	@return boolean
	*/
	public function callback();
}
Copy after login

7 Server-side application entrance

This entrance is mainly used to distribute callback requests and create RPC instances

if($_GET['code']){
	$keys = array(
			'code' => $_GET['code'],
			'redirect_uri' => APP_CALLBACK
		);
	$back = new Callback(new AuthDaoImpl($_GET['state'],$keys));
	if($back->callback()){
		header('Location: '.$_GET['state'].'/wp-admin/options-general.php?page=weibo-wall');
	}
	exit;
}
if($_GET['user']){
	$server = new Yar_Server(new API($_GET['user']));
	try{
		$server->handle();
	}catch(Exception $e){
		echo "感谢您使用微博墙!";
	}
}
Copy after login

7 Client application entrance

This entrance instantiates the plug-in entity class and enables the plug-in

$plu = new Plugins(new Dao(get_bloginfo( 'url' )));
$plu -> run(get_option('weibo_wall'),get_option('weibo_func'));
Copy after login

8 Summary

The whole process is like this. The business logic is very simple and the code is easy to understand. During the use process, I found that Yar is really simple and practical, and it can be parallelized. However, it is not reflected here, and some optimization can be done. The client of this plug-in relies on the Yar framework, which is an extension developed based on the C language. But it doesn’t matter if you don’t have this framework extension. We have already provided a pure PHP implementation of Yar. You can use it without paying attention to it, but it is still recommended that you use Yar.

The plug-in only provides a few functions when designed, but some people need other functions, so what should we do? We have also considered this aspect, so the plug-in is very scalable when designing, but you must have some PHP programming skills.

How to expand its functions?

1. Contact the author and tell you the functions you need
2. The author develops the corresponding data API
3. You call the API in local Dao.class.php
4. In Plugins. Get data in class.php and execute corresponding business logic

The above is the detailed content of Share a WordPress Weibo wall plug-in based on Yar. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles