Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/private public #2

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions src/makeandship/elasticsearch/acf-elasticsearch-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ public function get_options( $fresh=false ) {
$this->options = array();

$this->get_option( $this->options, Constants::OPTION_SERVER);
$this->get_option( $this->options, Constants::OPTION_PRIMARY_INDEX);
$this->get_option( $this->options, Constants::OPTION_SECONDARY_INDEX);
$this->get_option( $this->options, Constants::OPTION_PUBLIC_PRIMARY_INDEX);
$this->get_option( $this->options, Constants::OPTION_PUBLIC_SECONDARY_INDEX);
$this->get_option( $this->options, Constants::OPTION_PRIVATE_PRIMARY_INDEX);
$this->get_option( $this->options, Constants::OPTION_PRIVATE_SECONDARY_INDEX);
$this->get_option( $this->options, Constants::OPTION_READ_TIMEOUT);
$this->get_option( $this->options, Constants::OPTION_WRITE_TIMEOUT);
$this->get_option( $this->options, Constants::OPTION_INDEX_STATUS);
Expand Down Expand Up @@ -100,12 +102,17 @@ public function initialise_index_hooks() {
* Index Administration
* -------------------
*/
function create_mappings() {
function create_mappings($private = false) {
error_log('create_mappings()');
$options = $this->get_options();
$index_key = Constants::OPTION_PUBLIC_PRIMARY_INDEX;

if (isset($options) && array_key_exists(Constants::OPTION_PRIMARY_INDEX, $options)) {
$primary_index = $options[Constants::OPTION_PRIMARY_INDEX];
if($private) {
$index_key = Constants::OPTION_PRIVATE_PRIMARY_INDEX;
}

if (isset($options) && array_key_exists($index_key, $options)) {
$primary_index = $options[$index_key];

// (re)create the index
$indexer = new Indexer( $options );
Expand Down Expand Up @@ -149,13 +156,23 @@ function index_posts( ) {
}

function index_taxonomies() {
error_log('index_taxonomies()');
$options = $this->get_options();

if (isset($options)) {
$indexer = new Indexer( $options );
$status = $indexer->index_taxonomies();
}

$json = json_encode(array(
'message' => 'Taxonomies were indexed successfully'
));
die($json);
}

function clear_index() {
$this->create_mappings(false);

$json = json_encode(array(
'message' => 'Index was cleared successfully'
));
Expand Down
42 changes: 32 additions & 10 deletions src/makeandship/elasticsearch/admin/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,31 @@

// save incoming options
$server = esc_url($_POST['acf_elasticsearch_server']);
$primary_index = trim($_POST['acf_elasticsearch_primary_index']);
$secondary_index = trim($_POST['acf_elasticsearch_secondary_index']);
$public_primary_index = trim($_POST['acf_elasticsearch_public_primary_index']);
$public_secondary_index = trim($_POST['acf_elasticsearch_public_secondary_index']);
$private_primary_index = trim($_POST['acf_elasticsearch_private_primary_index']);
$private_secondary_index = trim($_POST['acf_elasticsearch_private_secondary_index']);
$read_timeout = intval(trim($_POST['acf_elasticsearch_read_timeout']));
$write_timeout = intval(trim($_POST['acf_elasticsearch_write_timeout']));

if (is_multisite()) { // && is_plugin_active_for_network(plugin_basename(__FILE__))
error_log('save multisite');
// store at network level
update_site_option('acf_elasticsearch_server', $server);
update_site_option('acf_elasticsearch_primary_index', $primary_index);
update_site_option('acf_elasticsearch_secondary_index', $secondary_index);
update_site_option('acf_elasticsearch_public_primary_index', $public_primary_index);
update_site_option('acf_elasticsearch_public_secondary_index', $public_secondary_index);
update_site_option('acf_elasticsearch_private_primary_index', $private_primary_index);
update_site_option('acf_elasticsearch_private_secondary_index', $private_secondary_index);
update_site_option('acf_elasticsearch_read_timeout', $read_timeout);
update_site_option('acf_elasticsearch_write_timeout', $write_timeout);
}
else {
// store at site level
update_option('acf_elasticsearch_server', $server);
update_option('acf_elasticsearch_primary_index', $primary_index);
update_option('acf_elasticsearch_secondary_index', $secondary_index);
update_option('acf_elasticsearch_public_primary_index', $public_primary_index);
update_option('acf_elasticsearch_public_secondary_index', $public_secondary_index);
update_option('acf_elasticsearch_private_primary_index', $private_primary_index);
update_option('acf_elasticsearch_private_secondary_index', $private_secondary_index);
update_option('acf_elasticsearch_read_timeout', $read_timeout);
update_option('acf_elasticsearch_write_timeout', $write_timeout);
}
Expand All @@ -46,16 +52,32 @@
)
);
echo HtmlUtils::render_field(
'Primary Index',
'acf_elasticsearch_primary_index',
'Public Primary Index',
'acf_elasticsearch_public_primary_index',
array(
'class' => '',
'placeholder' => ''
)
);
echo HtmlUtils::render_field(
'Secondary Index',
'acf_elasticsearch_secondary_index',
'Public Secondary Index',
'acf_elasticsearch_public_secondary_index',
array(
'class' => '',
'placeholder' => ''
)
);
echo HtmlUtils::render_field(
'Private Primary Index',
'acf_elasticsearch_private_primary_index',
array(
'class' => '',
'placeholder' => ''
)
);
echo HtmlUtils::render_field(
'Private Secondary Index',
'acf_elasticsearch_private_secondary_index',
array(
'class' => '',
'placeholder' => ''
Expand Down
60 changes: 46 additions & 14 deletions src/makeandship/elasticsearch/admin/user-interface-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ public function initialise_options() {
add_site_option(Constants::DB_VERSION, $this->db_version);

add_site_option(Constants::OPTION_SERVER, '');
add_site_option(Constants::OPTION_PRIMARY_INDEX, '');
add_site_option(Constants::OPTION_SECONDARY_INDEX, '');
add_site_option(Constants::OPTION_PUBLIC_PRIMARY_INDEX, '');
add_site_option(Constants::OPTION_PUBLIC_SECONDARY_INDEX, '');
add_site_option(Constants::OPTION_PRIVATE_PRIMARY_INDEX, '');
add_site_option(Constants::OPTION_PRIVATE_SECONDARY_INDEX, '');

add_site_option(Constants::OPTION_READ_TIMEOUT, 30);
add_site_option(Constants::OPTION_WRITE_TIMEOUT, 30);
Expand All @@ -47,8 +49,10 @@ public function initialise_options() {
add_option(Constants::DB_VERSION, $this->db_version);

add_option(Constants::OPTION_SERVER, '');
add_option(Constants::OPTION_PRIMARY_INDEX, '');
add_option(Constants::OPTION_SECONDARY_INDEX, '');
add_option(Constants::OPTION_PUBLIC_PRIMARY_INDEX, '');
add_option(Constants::OPTION_PUBLIC_SECONDARY_INDEX, '');
add_option(Constants::OPTION_PRIVATE_PRIMARY_INDEX, '');
add_option(Constants::OPTION_PRIVATE_SECONDARY_INDEX, '');

add_option(Constants::OPTION_READ_TIMEOUT, 30);
add_option(Constants::OPTION_WRITE_TIMEOUT, 30);
Expand Down Expand Up @@ -76,16 +80,30 @@ public function initialise_settings() {

);
add_settings_field(
'acf_elasticsearch_primary_index',
'Primary Index',
array($this, 'render_option_primary_index'),
'acf_elasticsearch_public_primary_index',
'Public Primary Index',
array($this, 'render_option_public_primary_index'),
'acf_elasticsearch_settings_page',
'acf_elasticsearch_settings'
);
add_settings_field(
'acf_elasticsearch_secondary_index',
'Secondary Index',
array($this, 'render_option_secondary_index'),
'acf_elasticsearch_public_secondary_index',
'Public Secondary Index',
array($this, 'render_option_public_secondary_index'),
'acf_elasticsearch_settings_page',
'acf_elasticsearch_settings'
);
add_settings_field(
'acf_elasticsearch_private_primary_index',
'Private Primary Index',
array($this, 'render_option_private_primary_index'),
'acf_elasticsearch_settings_page',
'acf_elasticsearch_settings'
);
add_settings_field(
'acf_elasticsearch_private_secondary_index',
'Private Secondary Index',
array($this, 'render_option_private_secondary_index'),
'acf_elasticsearch_settings_page',
'acf_elasticsearch_settings'
);
Expand Down Expand Up @@ -327,15 +345,29 @@ public function render_option_server() {
));
}

public function render_option_primary_index() {
$this->render_option( 'text', 'primary_index', array(
public function render_option_public_primary_index() {
$this->render_option( 'text', 'public_primary_index', array(
'placeholder' => '',
'class' => 'regular-text'
));
}

public function render_option_public_secondary_index() {
$this->render_option( 'text', 'public_secondary_index', array(
'placeholder' => '',
'class' => 'regular-text'
));
}

public function render_option_private_primary_index() {
$this->render_option( 'text', 'private_primary_index', array(
'placeholder' => '',
'class' => 'regular-text'
));
}

public function render_option_secondary_index() {
$this->render_option( 'text', 'secondary_index', array(
public function render_option_private_secondary_index() {
$this->render_option( 'text', 'private_secondary_index', array(
'placeholder' => '',
'class' => 'regular-text'
));
Expand Down
8 changes: 6 additions & 2 deletions src/makeandship/elasticsearch/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Constants {
// elastica
const SETTING_URL = 'url';
const SETTING_TIMEOUT = 'timeout';
const SETTING_USERNAME = 'username';
const SETTING_PASSWORD = 'password';

const DEFAULT_WRITE_TIMEOUT = 30;
const DEFAULT_READ_TIMEOUT = 30;
Expand All @@ -20,8 +22,10 @@ class Constants {
const DB_VERSION = 1;

const OPTION_SERVER = 'acf_elasticsearch_server';
const OPTION_PRIMARY_INDEX = 'acf_elasticsearch_primary_index';
const OPTION_SECONDARY_INDEX = 'acf_elasticsearch_secondary_index';
const OPTION_PUBLIC_PRIMARY_INDEX = 'acf_elasticsearch_public_primary_index';
const OPTION_PUBLIC_SECONDARY_INDEX = 'acf_elasticsearch_public_secondary_index';
const OPTION_PRIVATE_PRIMARY_INDEX = 'acf_elasticsearch_private_primary_index';
const OPTION_PRIVATE_SECONDARY_INDEX = 'acf_elasticsearch_private_secondary_index';
const OPTION_READ_TIMEOUT = 'acf_elasticsearch_read_timeout';
const OPTION_WRITE_TIMEOUT = 'acf_elasticsearch_write_timeout';
const OPTION_INDEX_STATUS = 'acf_elasticsearch_index_status';
Expand Down
32 changes: 32 additions & 0 deletions src/makeandship/elasticsearch/domain/taxonomies-manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace makeandship\elasticsearch\domain;

class TaxonomiesManager {
function __construct() {

}

/**
* Get all terms from the db
*
* @return array of WP_Site
*/
public function get_taxonomies() {
global $wpdb;
$sql = $wpdb->prepare("
select
*
from
$wpdb->terms wt,
$wpdb->term_taxonomy wtt
where
wt.term_id = wtt.term_id
AND 1 = %s ", "1");
//$terms = $wpdb->get_results($sql, OBJECT);
$terms = get_terms([]);


return $terms;
}
}
44 changes: 39 additions & 5 deletions src/makeandship/elasticsearch/indexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use makeandship\elasticsearch\domain\OptionsManager;
use makeandship\elasticsearch\domain\SitesManager;
use makeandship\elasticsearch\domain\PostsManager;
use makeandship\elasticsearch\domain\TaxonomiesManager;

use \Elastica\Client;
use \Elastica\Exception\ResponseException;
Expand All @@ -33,7 +34,9 @@ public function create( $name ) {

// elastic client to the cluster/server
$settings = array(
'url' => $this->config[Constants::OPTION_SERVER]
Constants::SETTING_URL => $this->config[Constants::OPTION_SERVER],
Constants::SETTING_USERNAME => ES_PRIVATE_USERNAME,
Constants::SETTING_PASSWORD => ES_PRIVATE_PASSWORD
);
$client = new Client($settings);

Expand Down Expand Up @@ -115,6 +118,33 @@ public function create( $name ) {
}
}

/**
* Clear the index
*/
public function clear( $name ) {
$errors = array();

// elastic client to the cluster/server
$settings = array();
$client = new Client($settings);

// remove the current index
$index = $client->getIndex( $name );
try {
$index->delete();
}
catch (ResponseException $ex) {
$response = $ex->getResponse();
$error = $response->getFullError();

// ignore if there's no index as that's the state we want
$is_index_error = strpos($error, 'IndexMissingException');
if ($is_index_error === false) {
$errors = $ex;
}
}
}

public function index_posts( $fresh ) {
if (is_multisite()) {
$status = $this->index_posts_multisite( $fresh );
Expand Down Expand Up @@ -195,8 +225,10 @@ public function index_posts_singlesite( $fresh ) {
return $status;
}

public function index_taxonomies( $page, $per ) {

public function index_taxonomies() {
$taxonomies_manager = new TaxonomiesManager();
$terms = $taxonomies_manager->get_taxonomies();
$count = $this->add_or_update_documents( $terms );
}

public function index_sites( $page, $per ) {
Expand Down Expand Up @@ -240,13 +272,15 @@ public function add_or_update_document( $o ) {
$builder = $this->document_builder_factory->create( $o );
$document = $builder->build( $o );
$id = $builder->get_id( $o );
$doc_type = $builder->get_type( $o );

// ensure the document and id are valid before indexing
if (isset($document) && !empty($document) &&
isset($id) && !empty($id)) {

$type = $this->type_factory->create( $o->post_type );
$type->addDocument(new \Elastica\Document($o->ID, $document));
$type = $this->type_factory->create( $doc_type );

$type->addDocument(new \Elastica\Document($id, $document));

// response ?
}
Expand Down
7 changes: 7 additions & 0 deletions src/makeandship/elasticsearch/post-document-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,11 @@ private function transform_acf_value( $value, $type ) {
public function get_id( $post ) {
return $post->ID;
}

/**
*
*/
public function get_type( $post ) {
return $post->post_type;
}
}
Loading