Skip to content

Commit

Permalink
Merge pull request #164 from 10up/fix/pages-not-scanned-161
Browse files Browse the repository at this point in the history
Fix: Pages are not being scanned by Language Processing
  • Loading branch information
jeffpaul authored Feb 26, 2020
2 parents 1d60931 + 10cf2ce commit ae141a5
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 27 deletions.
15 changes: 13 additions & 2 deletions includes/Classifai/Admin/SavePostHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,20 @@ public function show_error_if() {
* @return bool
*/
public function is_rest_route() {
if ( isset( $_SERVER['REQUEST_URI'] ) && false !== strpos( $_SERVER['REQUEST_URI'], 'wp-json/wp/v2/post' ) ) {
return true;

if ( ! isset( $_SERVER['REQUEST_URI'] ) ) {
return false;
}

// Support custom post types with custom rest base.
$rest_bases = apply_filters( 'classifai_rest_bases', array( 'posts', 'pages' ) );

foreach ( $rest_bases as $rest_base ) {
if ( false !== strpos( $_SERVER['REQUEST_URI'], 'wp-json/wp/v2/' . $rest_base ) ) {
return true;
}
}

return false;
}

Expand Down
26 changes: 1 addition & 25 deletions includes/Classifai/Providers/Watson/NLU.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,19 +174,10 @@ public function enqueue_editor_assets() {
CLASSIFAI_PLUGIN_VERSION,
true
);
if ( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() ) {
wp_enqueue_script(
'classifai-gutenberg-support',
CLASSIFAI_PLUGIN_URL . 'assets/js/classifai-gutenberg-support.js',
[ 'editor' ],
CLASSIFAI_PLUGIN_VERSION,
true
);
}
}

/**
* Adds ClassifAI Gutenberg Support if on the Gutenberg editor page
* Enqueue the admin scripts.
*/
public function enqueue_admin_assets() {
wp_enqueue_script(
Expand All @@ -208,21 +199,6 @@ public function enqueue_admin_assets() {
);
}

/**
* Adds ClassifAI Gutenberg Support if on the Gutenberg editor page
*/
public function init_admin_scripts() {
if ( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() ) {
wp_enqueue_script(
'classifai-gutenberg-support',
CLASSIFAI_PLUGIN_URL . 'assets/js/classifai-gutenberg-support.js',
[ 'editor' ],
CLASSIFAI_PLUGIN_VERSION,
true
);
}
}

/**
* Setup fields
*/
Expand Down
62 changes: 62 additions & 0 deletions tests/Classifai/Admin/SavePostHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Testing for the SavePostHandler class
*/

namespace Classifai\Tests\Admin;

use \WP_UnitTestCase;
use Classifai\Admin\SavePostHandler;

/**
* Class SavePostHandlerTest
* @package Classifai\Tests\Admin
*
* @group admin
*/
class SavePostHandlerTest extends WP_UnitTestCase {
protected $save_post_handler;

/**
* setup method
*/
function setUp() {
parent::setUp();

$this->save_post_handler = new SavePostHandler();
}

function test_is_rest_route() {
global $wp_filter;

$saved_filters = $wp_filter['classifai_rest_bases'] ?? null;
unset( $wp_filter['classifai_rest_bases'] );

$this->assertEquals( false, $this->save_post_handler->is_rest_route() );

$_SERVER['REQUEST_URI'] = '/wp-json/wp/v2/users/me';
$this->assertEquals( false, $this->save_post_handler->is_rest_route() );

$_SERVER['REQUEST_URI'] = '/wp-json/wp/v2/posts/1';
$this->assertEquals( true, $this->save_post_handler->is_rest_route() );

$_SERVER['REQUEST_URI'] = '/wp-json/wp/v2/pages/1';
$this->assertEquals( true, $this->save_post_handler->is_rest_route() );

$_SERVER['REQUEST_URI'] = '/wp-json/wp/v2/custom/1';
$this->assertEquals( false, $this->save_post_handler->is_rest_route() );

if ( ! is_null( $saved_filters ) ) {
$wp_filter['classifai_rest_bases'] = $saved_filters;
}

add_filter(
'classifai_rest_bases',
function( $bases ) {
$bases[] = 'custom';
return $bases;
}
);
$this->assertEquals( true, $this->save_post_handler->is_rest_route() );
}
}

0 comments on commit ae141a5

Please sign in to comment.