Commit inicial powerservice
This commit is contained in:
@@ -0,0 +1,201 @@
|
||||
<?php
|
||||
namespace AIOSEO\TableOfContents\Api;
|
||||
|
||||
// Exit if accessed directly.
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the registration, validation and authorization of API routes.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class Api {
|
||||
/**
|
||||
* The REST API namespace.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $namespace = 'aioseoTableOfContents/v1';
|
||||
|
||||
/**
|
||||
* The routes we use in the rest API.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $routes = [
|
||||
// phpcs:disable WordPress.Arrays.ArrayDeclarationSpacing.AssociativeArrayFound
|
||||
'GET' => [
|
||||
'options' => [ 'callback' => [ 'VueSettings', 'getOptions' ], 'access' => 'any' ],
|
||||
'toc-settings' => [ 'callback' => [ 'TocSettings', 'getSettings' ], 'access' => 'aioseo_table_of_contents_page' ],
|
||||
'ping' => [ 'callback' => [ 'Ping', 'ping' ], 'access' => 'any' ]
|
||||
],
|
||||
'POST' => [
|
||||
'notifications/dismiss' => [ 'callback' => [ 'Notifications', 'dismissNotifications' ], 'access' => 'any' ],
|
||||
'objects' => [ 'callback' => [ 'PostsTerms', 'searchForObjects' ], 'access' => [ 'aioseo_table_of_contents_page' ] ],
|
||||
'options' => [ 'callback' => [ 'VueSettings', 'saveChanges' ], 'access' => 'aioseo_table_of_contents_page' ],
|
||||
'plugins/deactivate' => [ 'callback' => [ 'Plugins', 'deactivatePlugins' ], 'access' => 'install_plugins' ],
|
||||
'plugins/install' => [ 'callback' => [ 'Plugins', 'installPlugins' ], 'access' => 'install_plugins' ],
|
||||
'toc-settings' => [ 'callback' => [ 'TocSettings', 'saveSettings' ], 'access' => 'aioseo_table_of_contents_page' ],
|
||||
'settings/toggle-radio' => [ 'callback' => [ 'VueSettings', 'toggleRadio' ], 'access' => 'aioseo_table_of_contents_page' ],
|
||||
'settings/items-per-page' => [ 'callback' => [ 'VueSettings', 'changeItemsPerPage' ], 'access' => 'aioseo_table_of_contents_page' ]
|
||||
]
|
||||
// phpcs:enable WordPress.Arrays.ArrayDeclarationSpacing.AssociativeArrayFound
|
||||
];
|
||||
|
||||
/**
|
||||
* Class contructor.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function __construct() {
|
||||
add_filter( 'rest_allowed_cors_headers', [ $this, 'allowedHeaders' ] );
|
||||
add_action( 'rest_api_init', [ $this, 'registerRoutes' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the API routes.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function registerRoutes() {
|
||||
$class = new \ReflectionClass( get_called_class() );
|
||||
foreach ( $this->routes as $method => $data ) {
|
||||
foreach ( $data as $route => $options ) {
|
||||
register_rest_route(
|
||||
$this->namespace,
|
||||
$route,
|
||||
[
|
||||
'methods' => $method,
|
||||
'permission_callback' => empty( $options['permissions'] ) ? [ $this, 'validRequest' ] : [ $this, $options['permissions'] ],
|
||||
'callback' => is_array( $options['callback'] )
|
||||
? [
|
||||
(
|
||||
! empty( $options['callback'][2] )
|
||||
? $options['callback'][2] . '\\' . $options['callback'][0]
|
||||
: (
|
||||
class_exists( $class->getNamespaceName() . '\\' . $options['callback'][0] )
|
||||
? $class->getNamespaceName() . '\\' . $options['callback'][0]
|
||||
: __NAMESPACE__ . '\\' . $options['callback'][0]
|
||||
)
|
||||
),
|
||||
$options['callback'][1]
|
||||
]
|
||||
: [ $this, $options['callback'] ]
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets headers that are allowed for our API routes.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param array $allowHeaders The allowed request headers.
|
||||
* @return array The allowed request headers.
|
||||
*/
|
||||
public function allowedHeaders( $allowHeaders ) {
|
||||
if ( ! array_search( 'X-WP-Nonce', $allowHeaders, true ) ) {
|
||||
$allowHeaders[] = 'X-WP-Nonce';
|
||||
}
|
||||
|
||||
return $allowHeaders;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the user is logged in and has the proper permissions.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param \WP_REST_Request $request The REST Request.
|
||||
* @return bool Whether the user is allowed access to the route.
|
||||
*/
|
||||
public function validRequest( $request ) {
|
||||
return is_user_logged_in() && $this->validateAccess( $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates access for the routes.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param \WP_REST_Request $request The REST Request.
|
||||
* @return bool Whether the user is allowed access to the route.
|
||||
*/
|
||||
private function validateAccess( $request ) {
|
||||
$routeData = $this->getRouteData( $request );
|
||||
if ( empty( $routeData ) || empty( $routeData['access'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Admins users always have access.
|
||||
if ( aioseoTableOfContents()->access->isAdmin() ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
switch ( $routeData['access'] ) {
|
||||
case 'any':
|
||||
// Users with any Table of Contents Plus permission can access the route.
|
||||
$user = wp_get_current_user();
|
||||
foreach ( $user->get_role_caps() as $capability => $enabled ) {
|
||||
if ( $enabled && preg_match( '/^aioseo_table_of_contents_/', (string) $capability ) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
default:
|
||||
// The user has access if he has any of the required capabilities.
|
||||
if ( ! is_array( $routeData['access'] ) ) {
|
||||
$routeData['access'] = [ $routeData['access'] ];
|
||||
}
|
||||
|
||||
foreach ( $routeData['access'] as $access ) {
|
||||
if ( current_user_can( $access ) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the data for the route that is being accessed.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param \WP_REST_Request $request The REST Request.
|
||||
* @return array The route data.
|
||||
*/
|
||||
private function getRouteData( $request ) {
|
||||
// NOTE: Since WordPress uses case-insensitive patterns to match routes,
|
||||
// we are forcing everything to lower case to ensure we have the proper route.
|
||||
// This prevents users with lower privileges from accessing routes they shouldn't.
|
||||
$route = aioseoTableOfContents()->helpers->toLowercase( $request->get_route() );
|
||||
$route = untrailingslashit( str_replace( '/' . $this->namespace . '/', '', $route ) );
|
||||
$routeData = isset( $this->routes[ $request->get_method() ][ $route ] ) ? $this->routes[ $request->get_method() ][ $route ] : [];
|
||||
|
||||
// No direct route name, let's try the regexes.
|
||||
if ( empty( $routeData ) ) {
|
||||
foreach ( $this->routes[ $request->get_method() ] as $routeRegex => $routeInfo ) {
|
||||
$routeRegex = str_replace( '@', '\@', $routeRegex );
|
||||
if ( preg_match( "@{$routeRegex}@", (string) $route ) ) {
|
||||
$routeData = $routeInfo;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $routeData;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
namespace AIOSEO\TableOfContents\Api;
|
||||
|
||||
// Exit if accessed directly.
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
use AIOSEO\TableOfContents\Models;
|
||||
|
||||
/**
|
||||
* Handles notification related routes.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class Notifications {
|
||||
/**
|
||||
* This allows us to not repeat code over and over.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $slug The slug of the reminder.
|
||||
* @return \WP_REST_Response The response.
|
||||
*/
|
||||
public static function reminder( $slug ) {
|
||||
aioseoTableOfContents()->notifications->remindMeLater( $slug );
|
||||
|
||||
return new \WP_REST_Response( [
|
||||
'success' => true,
|
||||
'notifications' => Models\Notification::getNotifications()
|
||||
], 200 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Dismiss notifications.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param \WP_REST_Request $request The REST Request
|
||||
* @return \WP_REST_Response The response.
|
||||
*/
|
||||
public static function dismissNotifications( $request ) {
|
||||
$slugs = $request->get_json_params();
|
||||
|
||||
$notifications = aioseoTableOfContents()->core->db
|
||||
->start( 'aioseo_table_of_contents_notifications' )
|
||||
->whereIn( 'slug', $slugs )
|
||||
->run()
|
||||
->models( 'AIOSEO\\TableOfContents\\Models\\Notification' );
|
||||
|
||||
foreach ( $notifications as $notification ) {
|
||||
$notification->dismissed = 1;
|
||||
$notification->save();
|
||||
}
|
||||
|
||||
// Dismiss static notifications.
|
||||
if ( in_array( 'notification-review', $slugs, true ) ) {
|
||||
update_user_meta( get_current_user_id(), '_aioseo_table_of_contents_notification_plugin_review_dismissed', '3' );
|
||||
}
|
||||
|
||||
if ( in_array( 'notification-review-delay', $slugs, true ) ) {
|
||||
update_user_meta( get_current_user_id(), '_aioseo_table_of_contents_notification_plugin_review_dismissed', strtotime( '+1 week' ) );
|
||||
}
|
||||
|
||||
return new \WP_REST_Response( [
|
||||
'success' => true,
|
||||
'notifications' => Models\Notification::getNotifications()
|
||||
], 200 );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
namespace AIOSEO\TableOfContents\Api;
|
||||
|
||||
// Exit if accessed directly.
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles plugin install/deinstall.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class Plugins {
|
||||
/**
|
||||
* Installs plugins.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param \WP_REST_Request $request The REST Request
|
||||
* @return \WP_REST_Response The response.
|
||||
*/
|
||||
public static function installPlugins( $request ) {
|
||||
$body = $request->get_json_params();
|
||||
$plugins = ! empty( $body['plugins'] ) ? $body['plugins'] : [];
|
||||
$network = ! empty( $body['network'] ) ? $body['network'] : false;
|
||||
$error = esc_html__( 'Installation failed. Please check permissions and try again.', 'table-of-contents-plus' );
|
||||
|
||||
if ( ! is_array( $plugins ) ) {
|
||||
return new \WP_REST_Response( [
|
||||
'success' => false,
|
||||
'message' => $error
|
||||
], 400 );
|
||||
}
|
||||
|
||||
if ( ! aioseoTableOfContents()->helpers->canInstall() ) {
|
||||
return new \WP_REST_Response( [
|
||||
'success' => false,
|
||||
'message' => $error
|
||||
], 400 );
|
||||
}
|
||||
|
||||
require_once ABSPATH . 'wp-admin/includes/plugin.php';
|
||||
|
||||
$failed = [];
|
||||
$completed = [];
|
||||
foreach ( $plugins as $plugin ) {
|
||||
if ( empty( $plugin['plugin'] ) ) {
|
||||
return new \WP_REST_Response( [
|
||||
'success' => false,
|
||||
'message' => $error
|
||||
], 400 );
|
||||
}
|
||||
|
||||
$result = aioseoTableOfContents()->helpers->installAddon( $plugin['plugin'], $network );
|
||||
if ( ! $result ) {
|
||||
$failed[] = $plugin['plugin'];
|
||||
} else {
|
||||
$completed[ $plugin['plugin'] ] = $result;
|
||||
}
|
||||
}
|
||||
|
||||
return new \WP_REST_Response( [
|
||||
'success' => true,
|
||||
'completed' => $completed,
|
||||
'failed' => $failed
|
||||
], 200 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Deactivates plugins.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param \WP_REST_Request $request The REST Request
|
||||
* @return \WP_REST_Response The response.
|
||||
*/
|
||||
public static function deactivatePlugins( $request ) {
|
||||
$body = $request->get_json_params();
|
||||
$plugins = ! empty( $body['plugins'] ) ? $body['plugins'] : [];
|
||||
$network = ! empty( $body['network'] ) ? $body['network'] : false;
|
||||
$error = esc_html__( 'Deactivation failed. Please check permissions and try again.', 'table-of-contents-plus' );
|
||||
|
||||
if ( ! is_array( $plugins ) ) {
|
||||
return new \WP_REST_Response( [
|
||||
'success' => false,
|
||||
'message' => $error
|
||||
], 400 );
|
||||
}
|
||||
|
||||
if ( ! current_user_can( 'install_plugins' ) ) {
|
||||
return new \WP_REST_Response( [
|
||||
'success' => false,
|
||||
'message' => $error
|
||||
], 400 );
|
||||
}
|
||||
|
||||
require_once ABSPATH . 'wp-admin/includes/plugin.php';
|
||||
|
||||
$failed = [];
|
||||
$completed = [];
|
||||
foreach ( $plugins as $plugin ) {
|
||||
if ( empty( $plugin['plugin'] ) ) {
|
||||
return new \WP_REST_Response( [
|
||||
'success' => false,
|
||||
'message' => $error
|
||||
], 400 );
|
||||
}
|
||||
|
||||
$deactivated = deactivate_plugins( $plugin['plugin'], false, $network );
|
||||
if ( is_wp_error( $deactivated ) ) {
|
||||
$failed[] = $plugin['plugin'];
|
||||
}
|
||||
|
||||
$completed[] = $plugin['plugin'];
|
||||
}
|
||||
|
||||
return new \WP_REST_Response( [
|
||||
'success' => true,
|
||||
'completed' => $completed,
|
||||
'failed' => $failed
|
||||
], 200 );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
namespace AIOSEO\TableOfContents\Api;
|
||||
|
||||
// Exit if accessed directly.
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
use AIOSEO\TableOfContents\Models;
|
||||
|
||||
/**
|
||||
* Handles post/term lookups.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class PostsTerms {
|
||||
/**
|
||||
* Returns posts by ID/name.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param \WP_REST_Request $request The REST Request
|
||||
* @return \WP_REST_Response The response.
|
||||
*/
|
||||
public static function searchForObjects( $request ) {
|
||||
$body = $request->get_json_params();
|
||||
$searchTerm = ! empty( $body['query'] ) ? sanitize_text_field( $body['query'] ) : null;
|
||||
$type = ! empty( $body['type'] ) ? sanitize_text_field( $body['type'] ) : null;
|
||||
if ( empty( $searchTerm ) || empty( $type ) ) {
|
||||
return new \WP_REST_Response( [
|
||||
'success' => false,
|
||||
'message' => 'No search term or object type was provided.'
|
||||
], 400 );
|
||||
}
|
||||
|
||||
$wpdb = aioseoTableOfContents()->core->db->db;
|
||||
$like = '%' . $wpdb->esc_like( $searchTerm ) . '%';
|
||||
|
||||
$objects = [];
|
||||
if ( 'posts' === $body['type'] ) {
|
||||
$postTypes = aioseoTableOfContents()->helpers->getPublicPostTypes( true );
|
||||
$objects = aioseoTableOfContents()->core->db
|
||||
->start( 'posts' )
|
||||
->select( 'ID, post_type, post_title, post_name' )
|
||||
->whereRaw( $wpdb->prepare( '( post_title LIKE %s OR post_name LIKE %s OR ID = %d )', $like, $like, $searchTerm ) )
|
||||
->whereIn( 'post_type', $postTypes )
|
||||
->whereIn( 'post_status', [ 'publish', 'draft', 'future', 'pending' ] )
|
||||
->orderBy( 'post_title' )
|
||||
->limit( 10 )
|
||||
->run()
|
||||
->result();
|
||||
}
|
||||
|
||||
if ( empty( $objects ) ) {
|
||||
return new \WP_REST_Response( [
|
||||
'success' => true,
|
||||
'objects' => []
|
||||
], 200 );
|
||||
}
|
||||
|
||||
$parsedObjects = [];
|
||||
foreach ( $objects as $object ) {
|
||||
if ( 'posts' === $type ) {
|
||||
$parsedObjects[] = [
|
||||
'value' => (int) $object->ID,
|
||||
'slug' => $object->post_name,
|
||||
'label' => $object->post_title,
|
||||
'type' => $object->post_type,
|
||||
'link' => get_permalink( $object->ID )
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return new \WP_REST_Response( [
|
||||
'success' => true,
|
||||
'objects' => $parsedObjects
|
||||
], 200 );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,213 @@
|
||||
<?php
|
||||
namespace AIOSEO\TableOfContents\Api;
|
||||
|
||||
// Exit if accessed directly.
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads and writes the table of contents settings.
|
||||
*
|
||||
* The frontend engine still stores its settings in the legacy 'toc-options'
|
||||
* option, so this controller bridges the Vue settings UI to that store and
|
||||
* mirrors the sanitization the legacy admin page used.
|
||||
*
|
||||
* Follow-up (tech debt): migrate these user settings onto the modern Options
|
||||
* framework the other AIOSEO plugins use. That store persists on activation and
|
||||
* version-tracks reliably, which would remove the existing-install detection
|
||||
* gymnastics in {@see \AIOSEO\TableOfContents\Main\Updates::migrateAppearanceDefaults()}.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class TocSettings {
|
||||
/**
|
||||
* Returns the current settings merged with the defaults, plus the data the UI needs.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return \WP_REST_Response The response.
|
||||
*/
|
||||
public static function getSettings() {
|
||||
$engine = aioseoTableOfContents()->standalone->frontend;
|
||||
|
||||
return new \WP_REST_Response( [
|
||||
'success' => true,
|
||||
'settings' => $engine->get_options(),
|
||||
'postTypes' => self::getPostTypes()
|
||||
], 200 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the settings to the legacy 'toc-options' option.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param \WP_REST_Request $request The REST request.
|
||||
* @return \WP_REST_Response The response.
|
||||
*/
|
||||
public static function saveSettings( $request ) {
|
||||
$engine = aioseoTableOfContents()->standalone->frontend;
|
||||
$defaults = $engine->get_defaults();
|
||||
$body = $request->get_json_params();
|
||||
|
||||
if ( empty( $body ) || ! is_array( $body ) ) {
|
||||
return new \WP_REST_Response( [ 'success' => false ], 400 );
|
||||
}
|
||||
|
||||
$string = function ( $key ) use ( $body, $defaults ) {
|
||||
return isset( $body[ $key ] ) ? trim( sanitize_text_field( wp_unslash( $body[ $key ] ) ) ) : $defaults[ $key ];
|
||||
};
|
||||
$bool = function ( $key ) use ( $body ) {
|
||||
return ! empty( $body[ $key ] );
|
||||
};
|
||||
$int = function ( $key ) use ( $body, $defaults ) {
|
||||
return isset( $body[ $key ] ) ? intval( $body[ $key ] ) : $defaults[ $key ];
|
||||
};
|
||||
$color = function ( $key, $default ) use ( $engine, $body ) {
|
||||
return ! empty( $body[ $key ] ) ? $engine->hex_value( trim( wp_unslash( $body[ $key ] ) ), $default ) : $default;
|
||||
};
|
||||
$enum = function ( $key, $allowed ) use ( $string, $defaults ) {
|
||||
$value = $string( $key );
|
||||
|
||||
return in_array( $value, $allowed, true ) ? $value : $defaults[ $key ];
|
||||
};
|
||||
|
||||
$restrictPath = $string( 'restrict_path' );
|
||||
if ( $restrictPath && 0 !== strpos( $restrictPath, '/' ) ) {
|
||||
// The restrict path must start with a forward slash, otherwise clear it.
|
||||
$restrictPath = '';
|
||||
}
|
||||
|
||||
$sanitized = [
|
||||
'fragment_prefix' => $string( 'fragment_prefix' ),
|
||||
'position' => $int( 'position' ),
|
||||
'start' => $int( 'start' ),
|
||||
'show_heading_text' => $bool( 'show_heading_text' ),
|
||||
'heading_text' => $string( 'heading_text' ),
|
||||
'heading_alignment' => $enum( 'heading_alignment', [ 'left', 'center', 'right' ] ),
|
||||
'auto_insert_post_types' => self::sanitizeStringArray( $body, 'auto_insert_post_types' ),
|
||||
'show_heirarchy' => $bool( 'show_heirarchy' ),
|
||||
'ordered_list' => $bool( 'ordered_list' ),
|
||||
'numbering_style' => $enum( 'numbering_style', [ 'hierarchical', 'decimal', 'upper-roman', 'lower-roman', 'upper-alpha', 'lower-alpha' ] ),
|
||||
'smooth_scroll' => $bool( 'smooth_scroll' ),
|
||||
'smooth_scroll_offset' => $int( 'smooth_scroll_offset' ),
|
||||
'visibility' => $bool( 'visibility' ),
|
||||
'visibility_show' => $string( 'visibility_show' ),
|
||||
'visibility_hide' => $string( 'visibility_hide' ),
|
||||
'toggle_style' => $enum( 'toggle_style', [ 'plus_minus', 'caret', 'brackets' ] ),
|
||||
'visibility_hide_by_default' => $bool( 'visibility_hide_by_default' ),
|
||||
'width' => $string( 'width' ),
|
||||
'width_custom' => isset( $body['width_custom'] ) ? floatval( $body['width_custom'] ) : $defaults['width_custom'],
|
||||
'width_custom_units' => $string( 'width_custom_units' ),
|
||||
'wrapping' => $int( 'wrapping' ),
|
||||
'font_size' => isset( $body['font_size'] ) ? floatval( $body['font_size'] ) : $defaults['font_size'],
|
||||
'font_size_units' => $string( 'font_size_units' ),
|
||||
'theme' => $int( 'theme' ),
|
||||
'custom_background_colour' => $color( 'custom_background_colour', TOC_DEFAULT_BACKGROUND_COLOUR ),
|
||||
'custom_border_colour' => $color( 'custom_border_colour', TOC_DEFAULT_BORDER_COLOUR ),
|
||||
'custom_title_colour' => $color( 'custom_title_colour', TOC_DEFAULT_TITLE_COLOUR ),
|
||||
'custom_links_colour' => $color( 'custom_links_colour', TOC_DEFAULT_LINKS_COLOUR ),
|
||||
'custom_links_hover_colour' => $color( 'custom_links_hover_colour', TOC_DEFAULT_LINKS_HOVER_COLOUR ),
|
||||
'custom_links_visited_colour' => $color( 'custom_links_visited_colour', TOC_DEFAULT_LINKS_VISITED_COLOUR ),
|
||||
'lowercase' => $bool( 'lowercase' ),
|
||||
'hyphenate' => $bool( 'hyphenate' ),
|
||||
'bullet_spacing' => $bool( 'bullet_spacing' ),
|
||||
'include_homepage' => $bool( 'include_homepage' ),
|
||||
'exclude_css' => $bool( 'exclude_css' ),
|
||||
'heading_levels' => self::sanitizeIntArray( $body, 'heading_levels' ),
|
||||
'exclude' => $string( 'exclude' ), // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude
|
||||
'restrict_path' => $restrictPath,
|
||||
'css_container_class' => $string( 'css_container_class' ),
|
||||
'sitemap_show_page_listing' => $bool( 'sitemap_show_page_listing' ),
|
||||
'sitemap_show_category_listing' => $bool( 'sitemap_show_category_listing' ),
|
||||
'sitemap_heading_type' => $int( 'sitemap_heading_type' ),
|
||||
'sitemap_pages' => $string( 'sitemap_pages' ),
|
||||
'sitemap_categories' => $string( 'sitemap_categories' ),
|
||||
'show_toc_in_widget_only' => $bool( 'show_toc_in_widget_only' ),
|
||||
'show_toc_in_widget_only_post_types' => self::sanitizeStringArray( $body, 'show_toc_in_widget_only_post_types' ),
|
||||
'rest_toc_output' => $bool( 'rest_toc_output' )
|
||||
];
|
||||
|
||||
$options = array_merge( $engine->get_options(), $sanitized );
|
||||
update_option( 'toc-options', $options );
|
||||
$engine->set_option( $options );
|
||||
|
||||
return new \WP_REST_Response( [
|
||||
'success' => true,
|
||||
'settings' => $options
|
||||
], 200 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitizes an array of strings from the request body.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param array $body The request body.
|
||||
* @param string $key The key to sanitize.
|
||||
* @return array The sanitized array.
|
||||
*/
|
||||
private static function sanitizeStringArray( $body, $key ) {
|
||||
if ( empty( $body[ $key ] ) || ! is_array( $body[ $key ] ) ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return array_values( array_map( 'sanitize_text_field', array_map( 'wp_unslash', $body[ $key ] ) ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitizes an array of integers from the request body.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param array $body The request body.
|
||||
* @param string $key The key to sanitize.
|
||||
* @return array The sanitized array.
|
||||
*/
|
||||
private static function sanitizeIntArray( $body, $key ) {
|
||||
if ( empty( $body[ $key ] ) || ! is_array( $body[ $key ] ) ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return array_values( array_map( 'intval', $body[ $key ] ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the public post types the table of contents can be enabled for.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return array The post types as { name, label, icon } objects.
|
||||
*/
|
||||
public static function getPostTypes() {
|
||||
$exclude = [ 'attachment', 'revision', 'nav_menu_item', 'safecss' ];
|
||||
$builtinIcons = [
|
||||
'post' => 'dashicons-admin-post',
|
||||
'page' => 'dashicons-admin-page'
|
||||
];
|
||||
|
||||
$postTypes = [];
|
||||
foreach ( get_post_types( [ 'public' => true ], 'objects' ) as $postType ) {
|
||||
if ( in_array( $postType->name, $exclude, true ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( isset( $builtinIcons[ $postType->name ] ) ) {
|
||||
$icon = $builtinIcons[ $postType->name ];
|
||||
} elseif ( is_string( $postType->menu_icon ) && 0 === strpos( $postType->menu_icon, 'dashicons-' ) ) {
|
||||
$icon = $postType->menu_icon;
|
||||
} else {
|
||||
$icon = 'dashicons-admin-post';
|
||||
}
|
||||
|
||||
$postTypes[] = [
|
||||
'name' => $postType->name,
|
||||
'label' => $postType->labels->name,
|
||||
'icon' => $icon
|
||||
];
|
||||
}
|
||||
|
||||
return $postTypes;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
namespace AIOSEO\TableOfContents\Api;
|
||||
|
||||
// Exit if accessed directly.
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
use AIOSEO\TableOfContents\Models;
|
||||
|
||||
/**
|
||||
* Handles the user Vue settings (toggled radios, table pagination, etc.).
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class VueSettings {
|
||||
/**
|
||||
* Returns the settings.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return \WP_REST_Response The response.
|
||||
*/
|
||||
public static function getOptions() {
|
||||
return new \WP_REST_Response( [
|
||||
'success' => true,
|
||||
'options' => aioseoTableOfContents()->options->all(),
|
||||
'internalOptions' => aioseoTableOfContents()->internalOptions->all(),
|
||||
'settings' => aioseoTableOfContents()->vueSettings->all()
|
||||
], 200 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggles a radio in the settings.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param \WP_REST_Request $request The REST Request
|
||||
* @return \WP_REST_Response The response.
|
||||
*/
|
||||
public static function toggleRadio( $request ) {
|
||||
$body = $request->get_json_params();
|
||||
$radio = ! empty( $body['radio'] ) ? sanitize_text_field( $body['radio'] ) : null;
|
||||
$value = ! empty( $body['value'] ) ? sanitize_text_field( $body['value'] ) : null;
|
||||
|
||||
$radios = aioseoTableOfContents()->vueSettings->toggledRadio;
|
||||
if ( ! array_key_exists( $radio, $radios ) ) {
|
||||
return new \WP_REST_Response( [
|
||||
'success' => false
|
||||
], 400 );
|
||||
}
|
||||
|
||||
$radios[ $radio ] = $value;
|
||||
aioseoTableOfContents()->vueSettings->toggledRadio = $radios;
|
||||
|
||||
return new \WP_REST_Response( [
|
||||
'success' => true
|
||||
], 200 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggles a table's items per page setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param \WP_REST_Request $request The REST Request
|
||||
* @return \WP_REST_Response The response.
|
||||
*/
|
||||
public static function changeItemsPerPage( $request ) {
|
||||
$body = $request->get_json_params();
|
||||
$table = ! empty( $body['table'] ) ? sanitize_text_field( $body['table'] ) : null;
|
||||
$value = ! empty( $body['value'] ) ? intval( $body['value'] ) : null;
|
||||
|
||||
$tables = aioseoTableOfContents()->vueSettings->tablePagination;
|
||||
if ( ! array_key_exists( $table, $tables ) ) {
|
||||
return new \WP_REST_Response( [
|
||||
'success' => false
|
||||
], 400 );
|
||||
}
|
||||
|
||||
$tables[ $table ] = $value;
|
||||
aioseoTableOfContents()->vueSettings->tablePagination = $tables;
|
||||
|
||||
return new \WP_REST_Response( [
|
||||
'success' => true
|
||||
], 200 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Save options from the frontend.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param \WP_REST_Request $request The REST Request
|
||||
* @return \WP_REST_Response The response.
|
||||
*/
|
||||
public static function saveChanges( $request ) {
|
||||
$body = $request->get_json_params();
|
||||
$options = ! empty( $body['options'] ) ? $body['options'] : []; // The options class will sanitize them.
|
||||
|
||||
aioseoTableOfContents()->options->sanitizeAndSave( $options );
|
||||
|
||||
// Re-initialize the notices.
|
||||
aioseoTableOfContents()->notifications->init();
|
||||
|
||||
return new \WP_REST_Response( [
|
||||
'success' => true,
|
||||
'notifications' => Models\Notification::getNotifications()
|
||||
], 200 );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user