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)

How to adjust the wordpress article list How to adjust the wordpress article list Apr 20, 2025 am 10:48 AM

There are four ways to adjust the WordPress article list: use theme options, use plugins (such as Post Types Order, WP Post List, Boxy Stuff), use code (add settings in the functions.php file), or modify the WordPress database directly.

How to build a website for wordpress host How to build a website for wordpress host Apr 20, 2025 am 11:12 AM

To build a website using WordPress hosting, you need to: select a reliable hosting provider. Buy a domain name. Set up a WordPress hosting account. Select a topic. Add pages and articles. Install the plug-in. Customize your website. Publish your website.

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

How to cancel the editing date of wordpress How to cancel the editing date of wordpress Apr 20, 2025 am 10:54 AM

WordPress editing dates can be canceled in three ways: 1. Install the Enable Post Date Disable plug-in; 2. Add code in the functions.php file; 3. Manually edit the post_modified column in the wp_posts table.

How to change the head image of the wordpress theme How to change the head image of the wordpress theme Apr 20, 2025 am 10:00 AM

A step-by-step guide to replacing a header image of WordPress: Log in to the WordPress dashboard and navigate to Appearance >Theme. Select the topic you want to edit and click Customize. Open the Theme Options panel and look for the Site Header or Header Image options. Click the Select Image button and upload a new head image. Crop the image and click Save and Crop. Click the Save and Publish button to update the changes.

How to write a header of a wordpress How to write a header of a wordpress Apr 20, 2025 pm 12:09 PM

The steps to create a custom header in WordPress are as follows: Edit the theme file "header.php". Add your website name and description. Create a navigation menu. Add a search bar. Save changes and view your custom header.

What to do if there is an error in wordpress What to do if there is an error in wordpress Apr 20, 2025 am 11:57 AM

WordPress Error Resolution Guide: 500 Internal Server Error: Disable the plug-in or check the server error log. 404 Page not found: Check permalink and make sure the page link is correct. White Screen of Death: Increase the server PHP memory limit. Database connection error: Check the database server status and WordPress configuration. Other tips: enable debug mode, check error logs, and seek support. Prevent errors: regularly update WordPress, install only necessary plugins, regularly back up your website, and optimize website performance.

How to display wordpress comments How to display wordpress comments Apr 20, 2025 pm 12:06 PM

Enable comments in WordPress website: 1. Log in to the admin panel, go to "Settings" - "Discussions", and check "Allow comments"; 2. Select a location to display comments; 3. Customize comments; 4. Manage comments, approve, reject or delete; 5. Use <?php comments_template(); ?> tags to display comments; 6. Enable nested comments; 7. Adjust comment shape; 8. Use plugins and verification codes to prevent spam comments; 9. Encourage users to use Gravatar avatar; 10. Create comments to refer to

See all articles