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