Skip to content

Commit 8da6088

Browse files
authored
Merge pull request #21 from getshifter/feat/algolia/support-terms-and-users
feat: [Algolia integrations] Support wp_terms / wp_users to replace these links
2 parents 982350d + 0989d08 commit 8da6088

File tree

8 files changed

+96
-44
lines changed

8 files changed

+96
-44
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ version: 2
33
jobs:
44
build:
55
docker:
6-
- image: circleci/php:7.3-cli
6+
- image: circleci/php:7.4-cli
77

88
working_directory: ~/shifter-wp-mu
99

admin/class-shifter-admin.php

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function shifter_heartbert_on_sitepreview_write_script() {
115115
<script>
116116
function shifter_heartbert_getajax() {
117117
var xhr= new XMLHttpRequest();
118-
xhr.open("GET","<?php echo esc_url(add_query_arg('action','nopriv_heartbeat',site_url('/wp-admin/admin-ajax.php')));?>");
118+
xhr.open("GET","<?php echo esc_url( add_query_arg( 'action', 'nopriv_heartbeat', site_url( '/wp-admin/admin-ajax.php' ) ) ); ?>");
119119
xhr.send();
120120
}
121121
var shifterHB = setInterval("shifter_heartbert_getajax()", 30000);
@@ -137,28 +137,62 @@ public function shifter_mail_from( $email_address ) {
137137
}
138138

139139
/**
140-
* Integrations between Shifter and Algolia
140+
* Integrations between Shifter and Algolia Shared private function
141141
*
142-
* @param string $shared_attributes Shared attrs.
143-
* @param string $post Post.
142+
* @param string $url URL strings.
143+
* @return string Replaced URL from Shifter WP to Shifter CDN
144144
*
145-
* @since 1.0.0
145+
* @since 1.1.0
146146
*/
147-
public function shifter_replace_algolia_permalink( $shared_attributes, $post ) {
147+
private function replace_url_to_public_domain( $url ) {
148148
$replaced_domain = getenv( 'SHIFTER_DOMAIN' );
149149
if ( ! $replaced_domain ) {
150150
$replaced_domain = getenv( 'CF_DOMAIN' );
151151
}
152152
if ( $replaced_domain ) {
153-
$url = $shared_attributes['permalink'];
154153
$parsed_url = wp_parse_url( $url );
155154
$replace_target = $parsed_url['host'];
156155
if ( isset( $parsed_url['port'] ) && $parsed_url['port'] ) {
157156
$replace_target .= ":{$parsed_url['port']}";
158157
}
159-
$shared_attributes['permalink'] = preg_replace( "#{$replace_target}#i", $replaced_domain, $url );
158+
return preg_replace( "#{$replace_target}#i", $replaced_domain, $url );
160159
}
161-
return $shared_attributes;
160+
return $url;
161+
}
162+
163+
/**
164+
* Integrations between Shifter and Algolia for wp_posts items
165+
*
166+
* @param string $record Shared attrs.
167+
*
168+
* @since 1.0.0
169+
*/
170+
public function replace_algolia_posts_permalink( $record ) {
171+
$record['permalink'] = $this->replace_url_to_public_domain( $record['permalink'] );
172+
return $record;
173+
}
174+
/**
175+
* Integrations between Shifter and Algolia for wp_terms items
176+
*
177+
* @param string $record Shared attrs.
178+
*
179+
* @since 1.1.0
180+
*/
181+
public function replace_algolia_terms_permalink( $record ) {
182+
$record['permalink'] = $this->replace_url_to_public_domain( $record['permalink'] );
183+
return $record;
184+
}
185+
186+
/**
187+
* Integrations between Shifter and Algolia for wp_users items
188+
*
189+
* @param string $record Shared attrs.
190+
*
191+
* @since 1.1.0
192+
*/
193+
public function replace_algolia_users_posts_url( $record ) {
194+
$record['posts_url'] = $this->replace_url_to_public_domain( $record['posts_url'] );
195+
return $record;
162196
}
163197

164198
/**

api/class-shifter-api.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ private function refresh_token() {
154154
'body' => wp_json_encode( array( 'refreshToken' => $this->refresh_token ) ),
155155
);
156156
$response = wp_remote_request( $this->refresh_url, $args );
157-
$body = $response[ 'body' ];
157+
$body = $response['body'];
158158
$body_array = json_decode( $body );
159159
// phpcs:disable
160160
$this->access_token = $body_array->AccessToken;

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
"type": "wordpress-plugin",
44
"description": "",
55
"require": {
6-
"wp-coding-standards/wpcs": "^2.0",
7-
"squizlabs/php_codesniffer": "^3.4",
8-
"dealerdirect/phpcodesniffer-composer-installer": "^0.5.0"
6+
"wp-coding-standards/wpcs": "*",
7+
"squizlabs/php_codesniffer": "*",
8+
"dealerdirect/phpcodesniffer-composer-installer": "*"
99
},
1010
"scripts": {
1111
"post-install-cmd": [
@@ -15,10 +15,10 @@
1515
"\"vendor/bin/phpcs\" --config-set installed_paths vendor/wp-coding-standards/wpcs"
1616
],
1717
"cs-fix": [
18-
"phpcbf . -pwv --ignore=vendor,node_modules --extensions=php --standard=WordPress || git diff"
18+
"phpcbf . -pwv --ignore=vendor,node_modules,volume --extensions=php --standard=WordPress || git diff"
1919
],
2020
"cs-lint": [
21-
"phpcs . -p --ignore=vendor,node_modules --extensions=php --standard=WordPress"
21+
"phpcs . -p --ignore=vendor,node_modules,volume --extensions=php --standard=WordPress"
2222
]
2323
},
2424
"authors": [

composer.lock

Lines changed: 40 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

global/class-shifter-global.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public function shifter_admin_bar_items() {
154154
);
155155

156156
$wp_admin_bar->add_menu( $shifter_support_back_to_shifter_dashboard );
157-
if (!getenv( 'SHIFTER_DISABLE_GENERATE' )) {
157+
if ( ! getenv( 'SHIFTER_DISABLE_GENERATE' ) ) {
158158
$wp_admin_bar->add_menu( $shifter_support_generate );
159159
}
160160
$wp_admin_bar->add_menu( $shifter_support_terminate );

includes/class-shifter.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,10 @@ private function define_admin_hooks() {
182182
$this->loader->add_filter( 'wp_mail_from', $plugin_admin, 'shifter_mail_from' );
183183

184184
// Shifter Algolia Intergrations.
185-
$this->loader->add_filter( 'algolia_post_shared_attributes', $plugin_admin, 'shifter_replace_algolia_permalink', 10, 2 );
186-
$this->loader->add_filter( 'algolia_searchable_post_shared_attributes', $plugin_admin, 'shifter_replace_algolia_permalink', 10, 2 );
185+
$this->loader->add_filter( 'algolia_post_shared_attributes', $plugin_admin, 'replace_algolia_posts_permalink', 10, 1 );
186+
$this->loader->add_filter( 'algolia_searchable_post_shared_attributes', $plugin_admin, 'replace_algolia_posts_permalink', 10, 1 );
187+
$this->loader->add_filter( 'algolia_term_record', $plugin_admin, 'replace_algolia_terms_permalink', 10, 1 );
188+
$this->loader->add_filter( 'algolia_user_record', $plugin_admin, 'replace_algolia_users_posts_url', 10, 1 );
187189

188190
// Shifter Admin Page.
189191
$this->loader->add_action( 'admin_menu', $plugin_admin, 'shifter_mu_admin_page' );

shifter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* that starts the plugin.
99
*
1010
* @link https://www.getshifter.io
11-
* @since 1.0.0
11+
* @since 1.1.0
1212
* @package Shifter
1313
*
1414
* @wordpress-plugin

0 commit comments

Comments
 (0)