public function testRun() { $aliyunOpenSearch = new AliyunOpenSearch('AliYun Open Search test kit', '0.1.0'); $admin = m::mock('AliyunOpenSearchAdmin'); $admin->shouldReceive('run')->once(); $frontend = m::mock('AliyunOpenSearchFrontend'); $frontend->shouldReceive('run')->once(); $aliyunOpenSearch->run($admin, $frontend); }
/** * Delete post from aliyun after posts deleted. * * @param int $postId ID of the post which has been deleted. * * @return void */ public function afterDeletePost($postId) { $post = get_post($postId); if (!$post || !in_array($post->post_type, $this->acceptedSyncTypes) || $post->post_parent != 0) { return; } try { $this->getOpenSearchClient()->deletePosts(array($post)); } catch (AliyunOpenSearchException $e) { wp_die(sprintf('从阿里云删除文章时发生错误:%s,请检查您的配置是否有误。<a href="%s" target="_blank">查看错误码说明</a>', $e->getMessage(), AliyunOpenSearch::getErrorCodeReferencesUrl())); } }
/** * Filter `pre_get_posts`. * * @param WP_Query $wp_the_query * @return WP_Query */ public function preGetPosts($wp_the_query) { /** @var WP_Query $wp_the_query */ if ($wp_the_query->is_search() && !$wp_the_query->is_admin && $wp_the_query->is_main_query()) { $this->keyword = $wp_the_query->query['s']; $this->limit = isset($wp_the_query->query_vars['posts_per_page']) ? $wp_the_query->query_vars['posts_per_page'] : 10; if (isset($wp_the_query->query_vars['paged'])) { $this->paged = intval($wp_the_query->query_vars['paged']); $this->paged = $this->paged > 0 ? $this->paged : 1; } $offset = ($this->paged - 1) * $this->limit; try { $ret = $this->getOpenSearchClient()->search("default:'{$this->keyword}' AND post_status:'publish'", $offset, $this->limit); } catch (AliyunOpenSearchException $e) { wp_die(sprintf('搜索文章时发生错误:%s,请检查您的配置是否有误。<a href="%s" target="_blank">查看错误码说明</a>', $e->getMessage(), AliyunOpenSearch::getErrorCodeReferencesUrl())); } $this->posts = $ret['posts']; $this->postCount = $ret['total']; $this->pageCount = ceil($this->postCount / $this->limit); foreach ($this->posts as $post) { $this->ids[] = $post->ID; } $wp_the_query->query = array(); $wp_the_query->query_vars['post_in'] = $this->ids; $wp_the_query->query_vars['post_type'] = null; $wp_the_query->query_vars['s'] = null; $wp_the_query->query_vars['paged'] = null; // set_query_var('post__in', $this->ids); // set_query_var('post_type', null); // set_query_var('s', null); // set_query_var('paged', null); } return $wp_the_query; }
/* Plugin Name: AliYun Open Search Plugin URI: http://www.aliyun.com/product/opensearch/ Description: Aliyun Open Search is a hosting service for structured data searching. Supporting data structures, sorting and data processing freedom to customize. Aliyun Open Search provides a simple, low cost, stable and efficient search solution for your sites or applications. Author: Aliyun Version: dev Author URI: http://www.aliyun.com/product/opensearch/ */ // If this file is called directly, abort. if (!defined('WPINC')) { header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found.'); exit('404 Not Found.'); } /** * The core plugin class that is used to define internationalization, * admin-specific hooks, and public-facing site hooks. */ require plugin_dir_path(__FILE__) . 'includes/AliyunOpenSearch.php'; define('ALI_OPENSEARCH_PLUGIN_NAME', 'aliyun-open-search'); $aos_version_file = plugin_dir_path(__FILE__) . 'VERSION'; if (is_file($aos_version_file)) { define('ALI_OPENSEARCH_PLUGIN_VERSION', file_get_contents($aos_version_file)); } else { define('ALI_OPENSEARCH_PLUGIN_VERSION', 'dev'); } $aliyun_opensearch = new AliyunOpenSearch(ALI_OPENSEARCH_PLUGIN_NAME, ALI_OPENSEARCH_PLUGIN_VERSION); $aliyun_opensearch->initialize(); $frontend = new AliyunOpenSearchFrontend($aliyun_opensearch->getPluginName(), $aliyun_opensearch->getVersion(), AliyunOpenSearchClient::autoload()); $admin = new AliyunOpenSearchAdmin($aliyun_opensearch->getPluginName(), $aliyun_opensearch->getVersion(), AliyunOpenSearchClient::autoload()); $aliyun_opensearch->run($admin, $frontend);