search
HomeCMS TutorialWordPressShare a WordPress Weibo wall plug-in based on Yar

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);
}

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);
}

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();
}

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);
}

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();
}

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();
}

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 "感谢您使用微博墙!";
	}
}

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'));

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
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete
Can I learn WordPress in 3 days?Can I learn WordPress in 3 days?Apr 09, 2025 am 12:16 AM

Can learn WordPress within three days. 1. Master basic knowledge, such as themes, plug-ins, etc. 2. Understand the core functions, including installation and working principles. 3. Learn basic and advanced usage through examples. 4. Understand debugging techniques and performance optimization suggestions.

Is WordPress a CMS?Is WordPress a CMS?Apr 08, 2025 am 12:02 AM

WordPress is a Content Management System (CMS). It provides content management, user management, themes and plug-in capabilities to support the creation and management of website content. Its working principle includes database management, template systems and plug-in architecture, suitable for a variety of needs from blogs to corporate websites.

What is the WordPress good for?What is the WordPress good for?Apr 07, 2025 am 12:06 AM

WordPressisgoodforvirtuallyanywebprojectduetoitsversatilityasaCMS.Itexcelsin:1)user-friendliness,allowingeasywebsitesetup;2)flexibilityandcustomizationwithnumerousthemesandplugins;3)SEOoptimization;and4)strongcommunitysupport,thoughusersmustmanageper

Should I use Wix or WordPress?Should I use Wix or WordPress?Apr 06, 2025 am 12:11 AM

Wix is ​​suitable for users who have no programming experience, and WordPress is suitable for users who want more control and expansion capabilities. 1) Wix provides drag-and-drop editors and rich templates, making it easy to quickly build a website. 2) As an open source CMS, WordPress has a huge community and plug-in ecosystem, supporting in-depth customization and expansion.

How much does WordPress cost?How much does WordPress cost?Apr 05, 2025 am 12:13 AM

WordPress itself is free, but it costs extra to use: 1. WordPress.com offers a package ranging from free to paid, with prices ranging from a few dollars per month to dozens of dollars; 2. WordPress.org requires purchasing a domain name (10-20 US dollars per year) and hosting services (5-50 US dollars per month); 3. Most plug-ins and themes are free, and the paid price ranges from tens to hundreds of dollars; by choosing the right hosting service, using plug-ins and themes reasonably, and regularly maintaining and optimizing, the cost of WordPress can be effectively controlled and optimized.

Is WordPress still free?Is WordPress still free?Apr 04, 2025 am 12:06 AM

The core version of WordPress is free, but other fees may be incurred during use. 1. Domain names and hosting services require payment. 2. Advanced themes and plug-ins may be charged. 3. Professional services and advanced features may be charged.

Is WordPress easy for beginners?Is WordPress easy for beginners?Apr 03, 2025 am 12:02 AM

WordPress is easy for beginners to get started. 1. After logging into the background, the user interface is intuitive and the simple dashboard provides all the necessary function links. 2. Basic operations include creating and editing content. The WYSIWYG editor simplifies content creation. 3. Beginners can expand website functions through plug-ins and themes, and the learning curve exists but can be mastered through practice.

Why would anyone use WordPress?Why would anyone use WordPress?Apr 02, 2025 pm 02:57 PM

People choose to use WordPress because of its power and flexibility. 1) WordPress is an open source CMS with strong ease of use and scalability, suitable for various website needs. 2) It has rich themes and plugins, a huge ecosystem and strong community support. 3) The working principle of WordPress is based on themes, plug-ins and core functions, and uses PHP and MySQL to process data, and supports performance optimization.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),