Initial commit xmarticaims
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
namespace AIOSEO\TableOfContents\Traits\Helpers;
|
||||
|
||||
// Exit if accessed directly.
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Contains API specific helper methods.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
trait Api {
|
||||
/**
|
||||
* Request the remote URL via wp_remote_post and return a json decoded response.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param array $body The content to retrieve from the remote URL.
|
||||
* @param array $headers The headers to send to the remote URL.
|
||||
* @return string|null JSON decoded response on success, false on failure.
|
||||
*/
|
||||
public function sendRequest( $url, $body = [], $headers = [] ) {
|
||||
$body = wp_json_encode( $body );
|
||||
|
||||
// Build the headers of the request.
|
||||
$headers = wp_parse_args(
|
||||
$headers,
|
||||
[
|
||||
'Content-Type' => 'application/json'
|
||||
]
|
||||
);
|
||||
|
||||
// Setup variable for wp_remote_post.
|
||||
$requestArgs = [
|
||||
'headers' => $headers,
|
||||
'body' => $body,
|
||||
'timeout' => 20
|
||||
];
|
||||
|
||||
// Perform the query and retrieve the response.
|
||||
$response = $this->wpRemotePost( $url, $requestArgs );
|
||||
$responseBody = wp_remote_retrieve_body( $response );
|
||||
|
||||
// Bail out early if there are any errors.
|
||||
if ( ! $responseBody ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Return the json decoded content.
|
||||
return json_decode( $responseBody );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a request using wp_remote_post.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $url The URL to send the request to.
|
||||
* @param array $args The args to use in the request.
|
||||
* @return array|\WP_Error The response as an array or WP_Error on failure.
|
||||
*/
|
||||
public function wpRemotePost( $url, $args = [] ) {
|
||||
return wp_remote_post( $url, array_replace_recursive( $this->getWpApiRequestDefaults(), $args ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a request using wp_remote_get.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $url The URL to send the request to.
|
||||
* @param array $args The args to use in the request.
|
||||
* @return array|\WP_Error The response as an array or WP_Error on failure.
|
||||
*/
|
||||
public function wpRemoteGet( $url, $args = [] ) {
|
||||
return wp_remote_get( $url, array_replace_recursive( $this->getWpApiRequestDefaults(), $args ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Default arguments for wp_remote_get and wp_remote_post.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return array An array of default arguments for the request.
|
||||
*/
|
||||
private function getWpApiRequestDefaults() {
|
||||
return [
|
||||
'timeout' => 10,
|
||||
'headers' => aioseoTableOfContents()->helpers->getApiHeaders(),
|
||||
'user-agent' => aioseoTableOfContents()->helpers->getApiUserAgent()
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the headers for internal API requests.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return array An array of headers.
|
||||
*/
|
||||
private function getApiHeaders() {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the User Agent for internal API requests.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return string The User Agent.
|
||||
*/
|
||||
private function getApiUserAgent() {
|
||||
return 'WordPress/' . get_bloginfo( 'version' ) . '; ' . get_bloginfo( 'url' ) . '; AIOSEO/TableOfContents/' . AIOSEO_TABLE_OF_CONTENTS_VERSION;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
namespace AIOSEO\TableOfContents\Traits\Helpers;
|
||||
|
||||
// Exit if accessed directly.
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Contains array specific helper methods.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
trait Arrays {
|
||||
/**
|
||||
* Checks whether the given array is associative.
|
||||
* Arrays that only have consecutive, sequential numeric keys are numeric.
|
||||
* Otherwise they are associative.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param array $array The array.
|
||||
* @return bool Whether the array is associative.
|
||||
*/
|
||||
public function isArrayAssociative( $array ) {
|
||||
return 0 < count( array_filter( array_keys( $array ), 'is_string' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the given array is numeric.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param array $array The array.
|
||||
* @return bool Whether the array is numeric.
|
||||
*/
|
||||
public function isArrayNumeric( $array ) {
|
||||
return ! $this->isArrayAssociative( $array );
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively replaces the values from one array with the ones from another.
|
||||
* This function should act identical to the built-in array_replace_recursive(), with the exception that it also replaces array values with empty arrays.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param array $targetArray The target array
|
||||
* @param array $replacementArray The array with values to replace in the target array.
|
||||
* @return array The modified array.
|
||||
*/
|
||||
public function arrayReplaceRecursive( $targetArray, $replacementArray ) {
|
||||
// In some cases the target array isn't an array yet (due to e.g. race conditions in InternalOptions), so in that case we can just return the replacement array.
|
||||
if ( ! is_array( $targetArray ) ) {
|
||||
return $replacementArray;
|
||||
}
|
||||
|
||||
foreach ( $replacementArray as $k => $v ) {
|
||||
// If the key does not exist yet on the target array, add it.
|
||||
if ( ! isset( $targetArray[ $k ] ) ) {
|
||||
$targetArray[ $k ] = $replacementArray[ $k ];
|
||||
continue;
|
||||
}
|
||||
|
||||
// If the value is an array, only try to recursively replace it if the value isn't empty.
|
||||
// Otherwise empty arrays will be ignored and won't override the existing value of the target array.
|
||||
if ( is_array( $v ) && ! empty( $v ) ) {
|
||||
$targetArray[ $k ] = $this->arrayReplaceRecursive( $targetArray[ $k ], $v );
|
||||
continue;
|
||||
}
|
||||
|
||||
// Replace with non-array value or empty array.
|
||||
$targetArray[ $k ] = $v;
|
||||
}
|
||||
|
||||
return $targetArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively intersects the two given arrays.
|
||||
* You can pass in an optional argument (allowedKey) to restrict the intersect to arrays with a specific key.
|
||||
* This is needed when we are e.g. sanitizing array values before setting/saving them to an option.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param array $array1 The first array.
|
||||
* @param array $array2 The second array.
|
||||
* @param string $allowedKey The only key the method should run for (optional).
|
||||
* @param string $parentKey The parent key.
|
||||
* @return array The intersected array.
|
||||
*/
|
||||
public function arrayIntersectRecursive( $array1, $array2, $allowedKey = '', $parentKey = '' ) {
|
||||
if ( ! $allowedKey || $allowedKey === $parentKey ) {
|
||||
$array1 = array_intersect_assoc( $array1, $array2 );
|
||||
}
|
||||
|
||||
if ( empty( $array1 ) ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
foreach ( $array1 as $k => $v ) {
|
||||
if ( is_array( $v ) && isset( $array2[ $k ] ) ) {
|
||||
$array1[ $k ] = $this->arrayIntersectRecursive( $array1[ $k ], $array2[ $k ], $allowedKey, $k );
|
||||
}
|
||||
}
|
||||
|
||||
if ( $this->isArrayNumeric( $array1 ) ) {
|
||||
$array1 = array_values( $array1 );
|
||||
}
|
||||
|
||||
return $array1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
namespace AIOSEO\TableOfContents\Traits\Helpers;
|
||||
|
||||
// Exit if accessed directly.
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Contains constant specific helper methods.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
trait Constants {
|
||||
/**
|
||||
* Returns the plugin menu icon.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return string The icon as a string.
|
||||
*/
|
||||
public function icon() {
|
||||
return '<svg width="32" height="32" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg"><circle cx="16" cy="6" r="2.8" stroke="white" stroke-width="2.4"/><path d="M16 8.8V26.6M10.4 12.2H21.6" stroke="white" stroke-width="2.6" stroke-linecap="round"/><path d="M7 16.4H4.3a11.7 11.7 0 0 0 23.4 0H25" stroke="white" stroke-width="2.6" stroke-linecap="round" stroke-linejoin="round"/></svg>'; // phpcs:ignore Generic.Files.LineLength.MaxExceeded
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
namespace AIOSEO\TableOfContents\Traits\Helpers;
|
||||
|
||||
// Exit if accessed directly.
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Contains date/time specific helper methods.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
trait DateTime {
|
||||
/**
|
||||
* Returns a MySQL formatted date.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param int|string $time Any format accepted by strtotime.
|
||||
* @return false|string The MySQL formatted string.
|
||||
*/
|
||||
public function timeToMysql( $time ) {
|
||||
$time = is_string( $time ) ? strtotime( $time ) : $time;
|
||||
|
||||
return date( 'Y-m-d H:i:s', $time ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,284 @@
|
||||
<?php
|
||||
namespace AIOSEO\TableOfContents\Traits\Helpers;
|
||||
|
||||
// Exit if accessed directly.
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Contains string specific helper methods.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
trait Strings {
|
||||
/**
|
||||
* Escapes special regex characters.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $string The string.
|
||||
* @param string $delimiter The delimiter character.
|
||||
* @return string The escaped string.
|
||||
*/
|
||||
public function escapeRegex( $string, $delimiter = '/' ) {
|
||||
static $escapeRegex = [];
|
||||
if ( isset( $escapeRegex[ $string ] ) ) {
|
||||
return $escapeRegex[ $string ];
|
||||
}
|
||||
$escapeRegex[ $string ] = preg_quote( (string) $string, $delimiter );
|
||||
|
||||
return $escapeRegex[ $string ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Escapes special regex characters inside the replacement string.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $string The string.
|
||||
* @return string The escaped string.
|
||||
*/
|
||||
public function escapeRegexReplacement( $string ) {
|
||||
static $escapeRegexReplacement = [];
|
||||
if ( isset( $escapeRegexReplacement[ $string ] ) ) {
|
||||
return $escapeRegexReplacement[ $string ];
|
||||
}
|
||||
|
||||
$escapeRegexReplacement[ $string ] = str_replace( '$', '\$', $string );
|
||||
|
||||
return $escapeRegexReplacement[ $string ];
|
||||
}
|
||||
|
||||
/**
|
||||
* preg_replace but with the replacement escaped.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $pattern The pattern to search for.
|
||||
* @param string $replacement The replacement string.
|
||||
* @param string $subject The subject to search in.
|
||||
* @return string The subject with matches replaced.
|
||||
*/
|
||||
public function pregReplace( $pattern, $replacement, $subject ) {
|
||||
$key = $pattern . $replacement . $subject;
|
||||
|
||||
static $pregReplace = [];
|
||||
if ( isset( $pregReplace[ $key ] ) ) {
|
||||
return $pregReplace[ $key ];
|
||||
}
|
||||
|
||||
// We can use the following pattern for this - (?<!\\)([\/.^$*+?|()[{}\]]{1})
|
||||
// The pattern above will only escape special characters if they're not escaped yet, which makes it compatible with all our patterns that are already escaped.
|
||||
// The caveat is that we'd need to first trim off slash delimiters and add them back later - otherwise they'd be escaped as well.
|
||||
|
||||
$replacement = $this->escapeRegexReplacement( $replacement );
|
||||
$pregReplace[ $key ] = preg_replace( $pattern, $replacement, (string) $subject );
|
||||
|
||||
return $pregReplace[ $key ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the index of a substring in a string.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $stack The stack.
|
||||
* @param string $needle The needle.
|
||||
* @param int $offset The offset.
|
||||
* @return int|bool The index where the string starts or false if it does not exist.
|
||||
*/
|
||||
public function stringIndex( $stack, $needle, $offset = 0 ) {
|
||||
$key = $stack . $needle . $offset;
|
||||
|
||||
static $stringIndex = [];
|
||||
if ( isset( $stringIndex[ $key ] ) ) {
|
||||
return $stringIndex[ $key ];
|
||||
}
|
||||
|
||||
$stringIndex[ $key ] = function_exists( 'mb_strpos' ) ? mb_strpos( $stack, $needle, $offset, get_option( 'blog_charset' ) ) : strpos( $stack, $needle, $offset );
|
||||
|
||||
return $stringIndex[ $key ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given string contains the given substring.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $stack The stack.
|
||||
* @param string $needle The needle.
|
||||
* @param int $offset The offset.
|
||||
* @return bool Whether the substring occurs in the main string.
|
||||
*/
|
||||
public function stringContains( $stack, $needle, $offset = 0 ) {
|
||||
$key = $stack . $needle . $offset;
|
||||
|
||||
static $stringContains = [];
|
||||
if ( isset( $stringContains[ $key ] ) ) {
|
||||
return $stringContains[ $key ];
|
||||
}
|
||||
|
||||
$stringContains[ $key ] = false !== $this->stringIndex( $stack, $needle, $offset );
|
||||
|
||||
return $stringContains[ $key ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a string is JSON encoded or not.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $string The string to check.
|
||||
* @return bool True if it is JSON or false if not.
|
||||
*/
|
||||
public function isJsonString( $string ) {
|
||||
if ( ! is_string( $string ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
json_decode( $string );
|
||||
|
||||
// Return a boolean whether or not the last error matches.
|
||||
return json_last_error() === JSON_ERROR_NONE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the string after all HTML entities have been decoded.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $string The string to decode.
|
||||
* @return string The decoded string.
|
||||
*/
|
||||
public function decodeHtmlEntities( $string ) {
|
||||
static $decodeHtmlEntities = [];
|
||||
if ( isset( $decodeHtmlEntities[ $string ] ) ) {
|
||||
return $decodeHtmlEntities[ $string ];
|
||||
}
|
||||
|
||||
// We must manually decode non-breaking spaces since html_entity_decode doesn't do this.
|
||||
$string = $this->pregReplace( '/ /', ' ', $string );
|
||||
$decodeHtmlEntities[ $string ] = html_entity_decode( (string) $string, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 );
|
||||
|
||||
return $decodeHtmlEntities[ $string ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the string with script tags stripped.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $string The string.
|
||||
* @return string The modified string.
|
||||
*/
|
||||
public function stripScriptTags( $string ) {
|
||||
static $stripScriptTags = [];
|
||||
if ( isset( $stripScriptTags[ $string ] ) ) {
|
||||
return $stripScriptTags[ $string ];
|
||||
}
|
||||
|
||||
$stripScriptTags[ $string ] = $this->pregReplace( '/<script(.*?)>(.*?)<\/script>/is', '', $string );
|
||||
|
||||
return $stripScriptTags[ $string ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the string with incomplete HTML tags stripped.
|
||||
* Incomplete tags are not unopened/unclosed pairs but rather single tags that aren't properly formed.
|
||||
* e.g. <a href='something'
|
||||
* e.g. href='something' >
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $string The string.
|
||||
* @return string The modified string.
|
||||
*/
|
||||
public function stripIncompleteHtmlTags( $string ) {
|
||||
static $stripIncompleteHtmlTags = [];
|
||||
if ( isset( $stripIncompleteHtmlTags[ $string ] ) ) {
|
||||
return $stripIncompleteHtmlTags[ $string ];
|
||||
}
|
||||
|
||||
$stripIncompleteHtmlTags[ $string ] = $this->pregReplace( '/(^(?!<).*?(\/>)|<[^>]*?(?!\/>)$)/is', '', $string );
|
||||
|
||||
return $stripIncompleteHtmlTags[ $string ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Trims HTML paragraph from the start/end of the given string.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $string The string.
|
||||
* @return string The modified string.
|
||||
*/
|
||||
public function trimParagraphTags( $string ) {
|
||||
$string = preg_replace( '/^<p[^>]*>/', '', (string) $string );
|
||||
$string = preg_replace( '/<\/p>/', '', (string) $string );
|
||||
|
||||
return trim( $string );
|
||||
}
|
||||
|
||||
/**
|
||||
* Implodes an array into a WHEREIN clause useable string.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param array $array The array.
|
||||
* @param bool $outerQuotes Whether outer quotes should be added.
|
||||
* @return string The imploded array.
|
||||
*/
|
||||
public function implodeWhereIn( $array, $outerQuotes = false ) {
|
||||
// Reset the keys first in case there is no 0 index.
|
||||
$array = array_values( $array );
|
||||
|
||||
if ( ! isset( $array[0] ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if ( is_numeric( $array[0] ) ) {
|
||||
return implode( ', ', $array );
|
||||
}
|
||||
|
||||
return $outerQuotes ? "'" . implode( "', '", $array ) . "'" : implode( "', '", $array );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns string after converting it to lowercase.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $string The original string.
|
||||
* @return string The string converted to lowercase.
|
||||
*/
|
||||
public function toLowerCase( $string ) {
|
||||
static $lowerCased = [];
|
||||
if ( isset( $lowerCased[ $string ] ) ) {
|
||||
return $lowerCased[ $string ];
|
||||
}
|
||||
$lowerCased[ $string ] = function_exists( 'mb_strtolower' ) ? mb_strtolower( $string, $this->getCharset() ) : strtolower( $string );
|
||||
|
||||
return $lowerCased[ $string ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert to camelCase.
|
||||
*
|
||||
* @since 1.1.0
|
||||
*
|
||||
* @param string $string The string to convert.
|
||||
* @param bool $capitalize Whether to capitalize the first letter.
|
||||
* @return string The converted string.
|
||||
*/
|
||||
public function toCamelCase( $string, $capitalize = false ) {
|
||||
$string[0] = strtolower( $string[0] );
|
||||
if ( $capitalize ) {
|
||||
$string[0] = strtoupper( $string[0] );
|
||||
}
|
||||
|
||||
return preg_replace_callback( '/_([a-z0-9])/', function ( $value ) {
|
||||
return strtoupper( $value[1] );
|
||||
}, $string );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
namespace AIOSEO\TableOfContents\Traits\Helpers;
|
||||
|
||||
// Exit if accessed directly.
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Contains helper methods for other plugins/themes.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
trait ThirdParty {
|
||||
/**
|
||||
* Checks whether WooCommerce is active.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return boolean Whether WooCommerce is active.
|
||||
*/
|
||||
public function isWooCommerceActive() {
|
||||
return class_exists( 'woocommerce' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the queried object is the WooCommerce shop page.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param int $id The post ID to check against (optional).
|
||||
* @return bool Whether the current page is the WooCommerce shop page.
|
||||
*/
|
||||
public function isWooCommerceShopPage( $id = 0 ) {
|
||||
if ( ! $this->isWooCommerceActive() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( ! is_admin() && ! aioseoTableOfContents()->helpers->isAjaxCronRestRequest() && function_exists( 'is_shop' ) ) {
|
||||
return is_shop();
|
||||
}
|
||||
|
||||
$id = ! $id && ! empty( $_GET['post'] ) ? absint( wp_unslash( $_GET['post'] ) ) : (int) $id; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
|
||||
|
||||
return $id && wc_get_page_id( 'shop' ) === $id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
<?php
|
||||
namespace AIOSEO\TableOfContents\Traits\Helpers;
|
||||
|
||||
// Exit if accessed directly.
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
use AIOSEO\TableOfContents\Api;
|
||||
use AIOSEO\TableOfContents\Models;
|
||||
|
||||
/**
|
||||
* Generates the data we need for Vue.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
trait Vue {
|
||||
/**
|
||||
* The data to pass to Vue.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $vueData = [];
|
||||
|
||||
/**
|
||||
* Returns the data for Vue.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $currentPage The current page.
|
||||
* @return array The data.
|
||||
*/
|
||||
public function getVueData( $currentPage = null ) {
|
||||
global $wp_version;
|
||||
|
||||
static $showNotificationsDrawer = null;
|
||||
if ( null === $showNotificationsDrawer ) {
|
||||
$showNotificationsDrawer = aioseoTableOfContents()->core->cache->get( 'show_notifications_drawer' ) ? true : false;
|
||||
|
||||
// IF this is set to true, let's disable it now so it doesn't pop up again.
|
||||
if ( $showNotificationsDrawer ) {
|
||||
aioseoTableOfContents()->core->cache->delete( 'show_notifications_drawer' );
|
||||
}
|
||||
}
|
||||
|
||||
$this->vueData = [
|
||||
// The following data is needed on all screens.
|
||||
'wpVersion' => $wp_version,
|
||||
'page' => $currentPage,
|
||||
'screen' => aioseoTableOfContents()->helpers->getCurrentScreen(),
|
||||
'internalOptions' => aioseoTableOfContents()->internalOptions->all(),
|
||||
'options' => aioseoTableOfContents()->options->all(),
|
||||
'settings' => aioseoTableOfContents()->vueSettings->all(),
|
||||
'tocSettings' => aioseoTableOfContents()->standalone->frontend->get_options(),
|
||||
'tocPostTypes' => Api\TocSettings::getPostTypes(),
|
||||
'notifications' => array_merge( Models\Notification::getNotifications( false ), [ 'force' => $showNotificationsDrawer ] ),
|
||||
'helpPanel' => [],
|
||||
'urls' => [
|
||||
'domain' => $this->getSiteDomain(),
|
||||
'mainSiteUrl' => $this->getSiteUrl(),
|
||||
'home' => home_url(),
|
||||
'restUrl' => rest_url(),
|
||||
'editScreen' => admin_url( 'edit.php' ),
|
||||
'publicPath' => aioseoTableOfContents()->core->assets->normalizeAssetsHost( plugin_dir_url( AIOSEO_TABLE_OF_CONTENTS_FILE ) ),
|
||||
'assetsPath' => aioseoTableOfContents()->core->assets->getAssetsPath(),
|
||||
'marketingSite' => $this->getMarketingSiteUrl(),
|
||||
'connect' => admin_url( 'index.php?page=table-of-contents-connect' )
|
||||
],
|
||||
'isDev' => $this->isDev(),
|
||||
'isAioseoActive' => function_exists( 'aioseo' ),
|
||||
'isAioseoInstalled' => file_exists( WP_PLUGIN_DIR . '/all-in-one-seo-pack/all_in_one_seo_pack.php' )
|
||||
|| file_exists( WP_PLUGIN_DIR . '/all-in-one-seo-pack-pro/all_in_one_seo_pack.php' ),
|
||||
'isBrokenLinkCheckerActive' => function_exists( 'aioseoBrokenLinkChecker' ),
|
||||
'isBrokenLinkCheckerInstalled' => file_exists( WP_PLUGIN_DIR . '/broken-link-checker-seo/aioseo-broken-link-checker.php' )
|
||||
|| file_exists( WP_PLUGIN_DIR . '/aioseo-broken-link-checker/aioseo-broken-link-checker.php' ),
|
||||
'isSsl' => is_ssl(),
|
||||
'isMultisite' => is_multisite(),
|
||||
'isNetworkAdmin' => is_network_admin(),
|
||||
'mainSite' => is_main_site(),
|
||||
'hasUrlTrailingSlash' => '/' === user_trailingslashit( '' ),
|
||||
'nonce' => wp_create_nonce( 'wp_rest' ),
|
||||
'translations' => $this->getJedLocaleData( 'table-of-contents-plus' )
|
||||
];
|
||||
|
||||
// In multisite, super admins may not have explicit roles on subsites.
|
||||
// Ensure they have administrator role and capabilities for proper access.
|
||||
$userData = wp_get_current_user();
|
||||
$roles = $userData->roles;
|
||||
$capabilities = $userData->allcaps;
|
||||
|
||||
// If the user is a network admin, and doesn't have a user on the subsite, give him admin role/caps.
|
||||
if ( is_multisite() && is_super_admin() && empty( $roles ) ) {
|
||||
$roles = [ 'administrator' ];
|
||||
$adminRole = get_role( 'administrator' );
|
||||
if ( is_a( $adminRole, 'WP_Role' ) ) {
|
||||
$capabilities = $adminRole->capabilities;
|
||||
}
|
||||
}
|
||||
|
||||
$this->vueData['user'] = [
|
||||
'canManage' => aioseoTableOfContents()->access->canManage(),
|
||||
'roles' => $roles,
|
||||
'capabilities' => $capabilities,
|
||||
'locale' => function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale()
|
||||
];
|
||||
|
||||
switch ( $currentPage ) {
|
||||
case 'about':
|
||||
$this->addAboutData();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return $this->vueData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the data for the About Us screen.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function addAboutData() {
|
||||
$this->vueData['plugins'] = $this->getPluginData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Jed-formatted localization data.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $domain The text domain.
|
||||
* @return array The information of the locale.
|
||||
*/
|
||||
private function getJedLocaleData( $domain ) {
|
||||
$translations = get_translations_for_domain( $domain );
|
||||
|
||||
$locale = [
|
||||
'' => [
|
||||
'domain' => $domain,
|
||||
'lang' => is_admin() && function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale(),
|
||||
],
|
||||
];
|
||||
|
||||
if ( ! empty( $translations->headers['Plural-Forms'] ) ) {
|
||||
$locale['']['plural_forms'] = $translations->headers['Plural-Forms'];
|
||||
}
|
||||
|
||||
foreach ( $translations->entries as $msgid => $entry ) {
|
||||
if ( empty( $entry->translations ) || ! is_array( $entry->translations ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$locale[ $msgid ] = $entry->translations;
|
||||
}
|
||||
|
||||
return $locale;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the marketing site URL.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return string The marketing site URL.
|
||||
*/
|
||||
private function getMarketingSiteUrl() {
|
||||
if ( defined( 'AIOSEO_MARKETING_SITE_URL' ) && AIOSEO_MARKETING_SITE_URL ) {
|
||||
return AIOSEO_MARKETING_SITE_URL;
|
||||
}
|
||||
|
||||
return 'https://aioseo.com/';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,470 @@
|
||||
<?php
|
||||
namespace AIOSEO\TableOfContents\Traits\Helpers;
|
||||
|
||||
use AIOSEO\TableOfContents\Utils;
|
||||
|
||||
// Exit if accessed directly.
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Contains all WP related helper methods.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
trait Wp {
|
||||
/**
|
||||
* Returns all registered post statuses.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param boolean $statusesOnly Whether or not to only return statuses.
|
||||
* @return array List of post statuses.
|
||||
*/
|
||||
public function getPublicPostStatuses( $statusesOnly = false ) {
|
||||
$allStatuses = get_post_stati( [ 'show_in_admin_all_list' => true ], 'objects' );
|
||||
|
||||
$postStatuses = [];
|
||||
foreach ( $allStatuses as $status => $data ) {
|
||||
if (
|
||||
! $data->public &&
|
||||
! $data->protected &&
|
||||
! $data->private
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( $statusesOnly ) {
|
||||
$postStatuses[] = $status;
|
||||
continue;
|
||||
}
|
||||
|
||||
$postStatuses[] = [
|
||||
'label' => $data->label,
|
||||
'status' => $status
|
||||
];
|
||||
}
|
||||
|
||||
return $postStatuses;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of public post types with slugs/icons.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param boolean $namesOnly Whether only the names should be returned.
|
||||
* @param boolean $hasArchivesOnly Whether or not to only include post types which have archives.
|
||||
* @param boolean $rewriteType Whether or not to rewrite the type slugs.
|
||||
* @return array List of public post types.
|
||||
*/
|
||||
public function getPublicPostTypes( $namesOnly = false, $hasArchivesOnly = false, $rewriteType = false ) {
|
||||
$postTypes = [];
|
||||
$postTypeObjects = get_post_types( [ 'public' => true ], 'objects' );
|
||||
$woocommerce = class_exists( 'woocommerce' );
|
||||
foreach ( $postTypeObjects as $postTypeObject ) {
|
||||
if ( empty( $postTypeObject->label ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// We don't want to include archives for the WooCommerce shop page.
|
||||
if (
|
||||
$hasArchivesOnly &&
|
||||
(
|
||||
! $postTypeObject->has_archive ||
|
||||
( 'product' === $postTypeObject->name && $woocommerce )
|
||||
)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( $namesOnly ) {
|
||||
$postTypes[] = $postTypeObject->name;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( 'attachment' === $postTypeObject->name ) {
|
||||
// We have to check if the 'init' action has been fired to avoid a PHP notice
|
||||
// in WP 6.7+ due to loading translations too early.
|
||||
if ( did_action( 'init' ) ) {
|
||||
$postTypeObject->label = __( 'Attachments', 'table-of-contents-plus' );
|
||||
}
|
||||
}
|
||||
|
||||
if ( 'product' === $postTypeObject->name && $woocommerce ) {
|
||||
$postTypeObject->menu_icon = 'dashicons-products';
|
||||
}
|
||||
|
||||
$name = $postTypeObject->name;
|
||||
if ( 'type' === $postTypeObject->name && $rewriteType ) {
|
||||
$name = '_aioseo_type';
|
||||
}
|
||||
|
||||
$postTypes[] = [
|
||||
'name' => $name,
|
||||
'label' => ucwords( $postTypeObject->label ),
|
||||
'singular' => ucwords( $postTypeObject->labels->singular_name ),
|
||||
'icon' => $postTypeObject->menu_icon,
|
||||
'hasExcerpt' => post_type_supports( $postTypeObject->name, 'excerpt' ),
|
||||
'hasArchive' => $postTypeObject->has_archive,
|
||||
'hierarchical' => $postTypeObject->hierarchical,
|
||||
'taxonomies' => get_object_taxonomies( $name ),
|
||||
'slug' => isset( $postTypeObject->rewrite['slug'] ) ? $postTypeObject->rewrite['slug'] : $name
|
||||
];
|
||||
}
|
||||
|
||||
return apply_filters( 'aioseo_table_of_contents_public_post_types', $postTypes, $namesOnly, $hasArchivesOnly );
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the current user can edit posts of the given post type.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $postType The name of the post type.
|
||||
* @return bool Whether the user can edit posts of the given post type.
|
||||
*/
|
||||
public function canEditPostType( $postType ) {
|
||||
$capabilities = $this->getPostTypeCapabilities( $postType );
|
||||
|
||||
return current_user_can( $capabilities['edit_posts'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of capabilities for the given post type.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $postType The name of the post type.
|
||||
* @return array The capabilities.
|
||||
*/
|
||||
public function getPostTypeCapabilities( $postType ) {
|
||||
static $capabilities = [];
|
||||
if ( isset( $capabilities[ $postType ] ) ) {
|
||||
return $capabilities[ $postType ];
|
||||
}
|
||||
|
||||
$postTypeObject = get_post_type_object( $postType );
|
||||
if ( ! is_a( $postTypeObject, 'WP_Post_Type' ) ) {
|
||||
$capabilities[ $postType ] = [];
|
||||
|
||||
return $capabilities[ $postType ];
|
||||
}
|
||||
|
||||
$capabilities[ $postType ] = (array) $postTypeObject->cap;
|
||||
|
||||
return $capabilities[ $postType ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current post object.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param int|null $postId The post ID.
|
||||
* @return \WP_Post|null The post object.
|
||||
*/
|
||||
public function getPost( $postId = null ) {
|
||||
$postId = is_a( $postId, 'WP_Post' ) ? $postId->ID : $postId;
|
||||
|
||||
if ( $this->isWooCommerceShopPage( $postId ) ) {
|
||||
return get_post( wc_get_page_id( 'shop' ) );
|
||||
}
|
||||
|
||||
if ( is_front_page() || is_home() ) {
|
||||
$showOnFront = 'page' === get_option( 'show_on_front' );
|
||||
if ( $showOnFront ) {
|
||||
if ( is_front_page() ) {
|
||||
$pageOnFront = (int) get_option( 'page_on_front' );
|
||||
|
||||
return get_post( $pageOnFront );
|
||||
} elseif ( is_home() ) {
|
||||
$pageForPosts = (int) get_option( 'page_for_posts' );
|
||||
|
||||
return get_post( $pageForPosts );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// We need to check these conditions and cannot always return get_post() because we'll return the first post on archive pages (dynamic homepage, term pages, etc.).
|
||||
// https://github.com/awesomemotive/aioseo/issues/2419
|
||||
if (
|
||||
$this->isScreenBase( 'post' ) ||
|
||||
$postId ||
|
||||
is_singular()
|
||||
) {
|
||||
return get_post( $postId );
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the request is a non-legacy REST API request.
|
||||
* This function was copied from WooCommerce and improved.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return bool True if this is a REST API request.
|
||||
*/
|
||||
public function isRestApiRequest() {
|
||||
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
global $wp_rewrite;
|
||||
|
||||
if ( empty( $wp_rewrite ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( empty( $_SERVER['REQUEST_URI'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$restUrl = wp_parse_url( get_rest_url() );
|
||||
$restUrl = $restUrl['path'] . ( ! empty( $restUrl['query'] ) ? '?' . $restUrl['query'] : '' );
|
||||
|
||||
$isRestApiRequest = ( 0 === strpos( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ), $restUrl ) );
|
||||
|
||||
return apply_filters( 'aioseo_is_rest_api_request', $isRestApiRequest );
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the current request is an AJAX, CRON or REST request.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return bool Wether the request is an AJAX, CRON or REST request.
|
||||
*/
|
||||
public function isAjaxCronRestRequest() {
|
||||
return wp_doing_ajax() || wp_doing_cron() || $this->isRestApiRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the post passed in is a valid post, not a revision or autosave.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param \WP_Post $post The Post object to check.
|
||||
* @param array $allowedPostStatuses Allowed post statuses.
|
||||
* @return bool True if valid, false if not.
|
||||
*/
|
||||
public function isValidPost( $post, $allowedPostStatuses = [ 'publish' ] ) {
|
||||
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( ! is_object( $post ) ) {
|
||||
$post = get_post( $post );
|
||||
}
|
||||
|
||||
// In order to prevent recursion, we are skipping scheduled-action posts.
|
||||
if (
|
||||
! is_object( $post ) ||
|
||||
'scheduled-action' === $post->post_type ||
|
||||
'revision' === $post->post_type ||
|
||||
! in_array( $post->post_status, $allowedPostStatuses, true )
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of plugins with the active status.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return array List of plugins with active status.
|
||||
*/
|
||||
public function getPluginData() {
|
||||
static $pluginData = [];
|
||||
if ( ! empty( $pluginData ) ) {
|
||||
return $pluginData;
|
||||
}
|
||||
|
||||
$pluginUpgrader = new Utils\PluginUpgraderSilentAjax();
|
||||
$installedPlugins = array_keys( get_plugins() );
|
||||
|
||||
foreach ( $pluginUpgrader->pluginSlugs as $key => $slug ) {
|
||||
$pluginData[ $key ] = [
|
||||
'basename' => $slug,
|
||||
'installed' => in_array( $slug, $installedPlugins, true ),
|
||||
'activated' => is_plugin_active( $slug ),
|
||||
'adminUrl' => admin_url( $pluginUpgrader->pluginAdminUrls[ $key ] ),
|
||||
'canInstall' => $this->canInstall(),
|
||||
'canActivate' => $this->canActivate(),
|
||||
'canUpdate' => $this->canUpdate(),
|
||||
'wpLink' => ! empty( $pluginUpgrader->wpPluginLinks[ $key ] ) ? $pluginUpgrader->wpPluginLinks[ $key ] : null
|
||||
];
|
||||
}
|
||||
|
||||
return $pluginData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Installs and activates a given addon or plugin.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $name The addon name/SKU.
|
||||
* @param bool $network Whether or not we are in a network environment.
|
||||
* @return bool Whether or not the installation was succesful.
|
||||
*/
|
||||
public function installAddon( $name, $network = false ) {
|
||||
if ( ! $this->canInstall() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
require_once ABSPATH . 'wp-admin/includes/file.php';
|
||||
require_once ABSPATH . 'wp-admin/includes/template.php';
|
||||
require_once ABSPATH . 'wp-admin/includes/class-wp-screen.php';
|
||||
require_once ABSPATH . 'wp-admin/includes/screen.php';
|
||||
|
||||
// Set the current screen to avoid undefined notices.
|
||||
set_current_screen( 'toplevel_page_table-of-contents' );
|
||||
|
||||
// Prepare variables.
|
||||
$url = esc_url_raw(
|
||||
add_query_arg(
|
||||
[
|
||||
'page' => 'table-of-contents-links'
|
||||
],
|
||||
admin_url( 'admin.php' )
|
||||
)
|
||||
);
|
||||
|
||||
// Do not allow WordPress to search/download translations, as this will break JS output.
|
||||
remove_action( 'upgrader_process_complete', [ 'Language_Pack_Upgrader', 'async_upgrade' ], 20 );
|
||||
|
||||
// Create the plugin upgrader with our custom skin.
|
||||
$installer = new Utils\PluginUpgraderSilentAjax( new Utils\PluginUpgraderSkin() );
|
||||
|
||||
// Activate the plugin silently.
|
||||
$pluginUrl = ! empty( $installer->pluginSlugs[ $name ] ) ? $installer->pluginSlugs[ $name ] : $name;
|
||||
$activated = activate_plugin( $pluginUrl, '', $network );
|
||||
|
||||
if ( ! is_wp_error( $activated ) ) {
|
||||
return $name;
|
||||
}
|
||||
|
||||
// Using output buffering to prevent the FTP form from being displayed in the screen.
|
||||
ob_start();
|
||||
$creds = request_filesystem_credentials( $url, '', false, false, null );
|
||||
ob_end_clean();
|
||||
|
||||
// Check for file system permissions.
|
||||
$fs = aioseoTableOfContents()->core->fs->noConflict();
|
||||
$fs->init( $creds );
|
||||
if ( false === $creds || ! $fs->isWpfsValid() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Error check.
|
||||
if ( ! method_exists( $installer, 'install' ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$installLink = ! empty( $installer->pluginLinks[ $name ] ) ? $installer->pluginLinks[ $name ] : null;
|
||||
// Check if this is an addon and if we have a download link.
|
||||
if ( empty( $installLink ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$installer->install( $installLink );
|
||||
|
||||
// Flush the cache and return the newly installed plugin basename.
|
||||
wp_cache_flush();
|
||||
|
||||
$pluginBasename = $installer->plugin_info();
|
||||
if ( ! $pluginBasename ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Activate the plugin silently.
|
||||
$activated = activate_plugin( $pluginBasename, '', $network );
|
||||
|
||||
if ( is_wp_error( $activated ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $pluginBasename;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if plugins can be installed.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return bool Whether the plugin can be installed.
|
||||
*/
|
||||
public function canInstall() {
|
||||
if ( ! current_user_can( 'install_plugins' ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Determine whether file modifications are allowed.
|
||||
if ( ! wp_is_file_mod_allowed( 'aioseo_table_of_contents_can_install' ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if plugins can be updated.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return bool Whether the plugin can be updated.
|
||||
*/
|
||||
public function canUpdate() {
|
||||
if ( ! current_user_can( 'update_plugins' ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Determine whether file modifications are allowed.
|
||||
if ( ! wp_is_file_mod_allowed( 'aioseo_table_of_contents_can_update' ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if plugins can be activated.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return bool Whether the plugin can be activated.
|
||||
*/
|
||||
public function canActivate() {
|
||||
if ( ! current_user_can( 'activate_plugins' ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the charset for the site.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return string The name of the charset.
|
||||
*/
|
||||
public function getCharset() {
|
||||
static $charset = null;
|
||||
if ( null !== $charset ) {
|
||||
return $charset;
|
||||
}
|
||||
|
||||
$charset = get_option( 'blog_charset' );
|
||||
$charset = $charset ? $charset : 'UTF-8';
|
||||
|
||||
return $charset;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
namespace AIOSEO\TableOfContents\Traits\Helpers;
|
||||
|
||||
// Exit if accessed directly.
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Contains all context related helper methods.
|
||||
* This includes methods to check the context of the current request, but also get WP objects.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
trait WpContext {
|
||||
/**
|
||||
* Checks whether we're on the given screen.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $screenName The screen name.
|
||||
* @return boolean Whether we're on the given screen.
|
||||
*/
|
||||
public function isScreenBase( $screenName ) {
|
||||
$screen = $this->getCurrentScreen();
|
||||
if ( ! $screen || ! isset( $screen->base ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $screen->base === $screenName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets current admin screen
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return false|\WP_Screen|null
|
||||
*/
|
||||
public function getCurrentScreen() {
|
||||
if ( ! is_admin() || ! function_exists( 'get_current_screen' ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return get_current_screen();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
namespace AIOSEO\TableOfContents\Traits\Helpers;
|
||||
|
||||
// Exit if accessed directly.
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Contains methods related to multisites.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
trait WpMultisite {
|
||||
/**
|
||||
* Returns the current site.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return \WP_Site|Object A WP_Site instance of the current site or an object representing the same.
|
||||
*/
|
||||
public function getSite() {
|
||||
if ( is_multisite() ) {
|
||||
return get_site();
|
||||
}
|
||||
|
||||
return (object) [
|
||||
'domain' => $this->getSiteDomain(),
|
||||
'path' => $this->getHomePath()
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the network ID.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return int The integer of the blog/site id.
|
||||
*/
|
||||
public function getNetworkId() {
|
||||
if ( is_multisite() ) {
|
||||
return get_network()->site_id;
|
||||
}
|
||||
|
||||
return get_current_blog_id();
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper for switch_to_blog especially for non-multisite setups.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param int $blogId The blog ID to switch to.
|
||||
* @return bool True in all cases.
|
||||
*/
|
||||
public function switchToBlog( $blogId ) {
|
||||
if ( ! is_multisite() ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return switch_to_blog( $blogId );
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper for restore_current_blog especially for non-multisite setups.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return bool Whether we're already on the current blog or not in a multisite environment.
|
||||
*/
|
||||
public function restoreCurrentBlog() {
|
||||
if ( ! is_multisite() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return restore_current_blog();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
namespace AIOSEO\TableOfContents\Traits\Helpers;
|
||||
|
||||
// Exit if accessed directly.
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Contains all WordPress related URL, URI, path, slug, etc. related helper methods.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
trait WpUri {
|
||||
/**
|
||||
* Returns the site domain.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param bool $unfiltered Whether to get the unfiltered value.
|
||||
* @return string The site's domain.
|
||||
*/
|
||||
public function getSiteDomain( $unfiltered = false ) {
|
||||
return wp_parse_url( $this->getHomeUrl( $unfiltered ), PHP_URL_HOST );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the site URL.
|
||||
* NOTE: For multisites inside a sub-directory, this returns the URL for the main site.
|
||||
* This is intentional.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param bool $unfiltered Whether to get the unfiltered value.
|
||||
* @return string The site's domain.
|
||||
*/
|
||||
public function getSiteUrl( $unfiltered = false ) {
|
||||
$homeUrl = $this->getHomeUrl( $unfiltered );
|
||||
|
||||
return wp_parse_url( $homeUrl, PHP_URL_SCHEME ) . '://' . wp_parse_url( $homeUrl, PHP_URL_HOST );
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the home path.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param bool $unfiltered Whether to get the unfiltered value.
|
||||
* @return string The home path.
|
||||
*/
|
||||
public function getHomePath( $unfiltered = false ) {
|
||||
$path = wp_parse_url( $this->getHomeUrl( $unfiltered ), PHP_URL_PATH );
|
||||
|
||||
return $path ? trailingslashit( $path ) : '/';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the home URL.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param bool $unfiltered Whether to get the unfiltered value.
|
||||
* @return string The home URL.
|
||||
*/
|
||||
private function getHomeUrl( $unfiltered = false ) {
|
||||
$homeUrl = home_url();
|
||||
if ( $unfiltered ) {
|
||||
// We want to get this value straight from the DB to prevent plugins like WPML from filtering it.
|
||||
// This will otherwise mess with things like license activation requests and redirects.
|
||||
$homeUrl = get_option( 'home' );
|
||||
}
|
||||
|
||||
return $homeUrl;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user