PDF rausgenommen

This commit is contained in:
aschwarz
2023-01-23 11:03:31 +01:00
parent 82d562a322
commit a6523903eb
28078 changed files with 4247552 additions and 2 deletions

View File

@ -0,0 +1,106 @@
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Admin\Services
*/
/**
* Represents the file size service.
*/
class WPSEO_File_Size_Service {
/**
* Retrieves an indexable.
*
* @param WP_REST_Request $request The request object.
*
* @return WP_REST_Response The response.
*/
public function get( WP_REST_Request $request ) {
try {
$file_url = $this->get_file_url( $request );
return new WP_REST_Response(
array(
'type' => 'success',
'size_in_bytes' => $this->get_file_size( $file_url ),
),
404
);
}
catch ( WPSEO_File_Size_Exception $exception ) {
return new WP_REST_Response(
array(
'type' => 'failure',
'response' => $exception->getMessage(),
),
404
);
}
}
/**
* Retrieves the file url.
*
* @param WP_REST_Request $request The request to retrieve file url from.
*
* @return string The file url.
* @throws WPSEO_File_Size_Exception The file is hosted externally.
*/
protected function get_file_url( WP_REST_Request $request ) {
$file_url = rawurldecode( $request->get_param( 'url' ) );
if ( ! $this->is_externally_hosted( $file_url ) ) {
return $file_url;
}
throw WPSEO_File_Size_Exception::externally_hosted( $file_url );
}
/**
* Checks if the file is hosted externally.
*
* @param string $file_url The file url.
*
* @return bool True if it is hosted externally.
*/
protected function is_externally_hosted( $file_url ) {
return wp_parse_url( home_url(), PHP_URL_HOST ) !== wp_parse_url( $file_url, PHP_URL_HOST );
}
/**
* Returns the file size.
*
* @param string $file_url The file url to get the size for.
*
* @return int The file size.
* @throws WPSEO_File_Size_Exception Retrieval of file size went wrong for unknown reasons.
*/
protected function get_file_size( $file_url ) {
$file_config = wp_upload_dir();
$file_url = str_replace( $file_config['baseurl'], '', $file_url );
$file_size = $this->calculate_file_size( $file_url );
if ( ! $file_size ) {
throw WPSEO_File_Size_Exception::unknown_error( $file_url );
}
return $file_size;
}
/**
* Calculates the file size using the Utils class.
*
* @param string $file_url The file to retrieve the size for.
*
* @return int|bool The file size or False if it could not be retrieved.
*/
protected function calculate_file_size( $file_url ) {
return WPSEO_Image_Utils::get_file_size(
array(
'path' => $file_url,
)
);
}
}

View File

@ -0,0 +1,232 @@
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Admin\Services
*/
/**
* Represents the indexable post service.
*/
class WPSEO_Indexable_Service_Post_Provider extends WPSEO_Indexable_Provider {
/**
* List of fields that need to be renamed.
*
* @var array
*/
protected $renameable_fields = array(
'description' => 'metadesc',
'breadcrumb_title' => 'bctitle',
'og_title' => 'opengraph-title',
'og_description' => 'opengraph-description',
'og_image' => 'opengraph-image',
'twitter_title' => 'twitter-title',
'twitter_description' => 'twitter-description',
'twitter_image' => 'twitter-image',
'is_robots_noindex' => 'meta-robots-noindex',
'is_robots_nofollow' => 'meta-robots-nofollow',
'primary_focus_keyword' => 'focuskw',
'primary_focus_keyword_score' => 'linkdex',
'readability_score' => 'content_score',
);
/**
* Returns an array with data for the target object.
*
* @param integer $object_id The target object id.
* @param bool $as_object Optional. Whether or not to return the indexable
* as an object. Defaults to false.
*
* @return array|WPSEO_Post_Indexable The retrieved data. Defaults to an array format.
*
* @throws WPSEO_Invalid_Argument_Exception The invalid argument exception.
*/
public function get( $object_id, $as_object = false ) {
if ( ! $this->is_indexable( $object_id ) ) {
return array();
}
$indexable = WPSEO_Post_Indexable::from_object( $object_id );
if ( $as_object === true ) {
return $indexable;
}
return $indexable->to_array();
}
/**
* Handles the patching of values for an existing indexable.
*
* @param int $object_id The ID of the object.
* @param array $requestdata The request data to store.
*
* @return array The patched indexable.
*
* @throws WPSEO_Invalid_Indexable_Exception The invalid argument exception.
* @throws WPSEO_REST_Request_Exception Exception that is thrown if patching the object has failed.
*/
public function patch( $object_id, $requestdata ) {
$indexable = $this->get( $object_id, true );
if ( $indexable === array() ) {
throw WPSEO_Invalid_Indexable_Exception::non_existing_indexable( $object_id );
}
$new_indexable = $indexable->update( $requestdata );
$stored_indexable = $this->store_indexable( $new_indexable );
if ( $stored_indexable === true ) {
return $new_indexable->to_array();
}
throw WPSEO_REST_Request_Exception::patch( 'Post', $object_id );
}
/**
* Stores the indexable object.
*
* @param WPSEO_Indexable $indexable The indexable object to store.
*
* @return bool True if saving was successful.
*/
protected function store_indexable( WPSEO_Indexable $indexable ) {
$values = $this->convert_indexable_data( $indexable->to_array() );
$renamed_values = $this->rename_indexable_data( $values );
foreach ( $renamed_values as $key => $item ) {
WPSEO_Meta::set_value( $key, $item, $values['object_id'] );
}
return true;
}
/**
* Checks if the given object id belongs to an indexable.
*
* @param int $object_id The object id.
*
* @return bool Whether the object id is indexable.
*/
public function is_indexable( $object_id ) {
if ( get_post( $object_id ) === null ) {
return false;
}
if ( wp_is_post_autosave( $object_id ) ) {
return false;
}
if ( wp_is_post_revision( $object_id ) ) {
return false;
}
return true;
}
/**
* Converts some of the indexable data to its database variant.
*
* @param array $indexable_data The indexable data to convert.
*
* @return array The converted indexable data.
*/
protected function convert_indexable_data( $indexable_data ) {
if ( WPSEO_Validator::key_exists( $indexable_data, 'is_robots_nofollow' ) ) {
$indexable_data['is_robots_nofollow'] = $this->convert_nofollow( $indexable_data['is_robots_nofollow'] );
}
if ( WPSEO_Validator::key_exists( $indexable_data, 'is_robots_noindex' ) ) {
$indexable_data['is_robots_noindex'] = $this->convert_noindex( $indexable_data['is_robots_noindex'] );
}
if ( WPSEO_Validator::key_exists( $indexable_data, 'is_cornerstone' ) ) {
$indexable_data['is_cornerstone'] = $this->convert_cornerstone( $indexable_data['is_cornerstone'] );
}
$indexable_data['meta-robots-adv'] = $this->convert_advanced( $indexable_data );
return $indexable_data;
}
/**
* Converts the cornerstone value to its database variant.
*
* @param string $cornerstone_value The cornerstone value.
*
* @return string The converted indexable cornerstone value.
*/
protected function convert_cornerstone( $cornerstone_value ) {
if ( $cornerstone_value === 'true' ) {
return '1';
}
return null;
}
/**
* Converts the advanced meta settings to its database variant.
*
* @param array $indexable_data The indexable data to convert the advanced meta settings from.
*
* @return string The converted advanced meta settings.
*/
protected function convert_advanced( &$indexable_data ) {
$translated_advanced_data = array();
if ( WPSEO_Validator::key_exists( $indexable_data, 'is_robots_nosnippet' ) && (bool) $indexable_data['is_robots_nosnippet'] === true ) {
$translated_advanced_data[] = 'nosnippet';
unset( $indexable_data['is_robots_nosnippet'] );
}
if ( WPSEO_Validator::key_exists( $indexable_data, 'is_robots_noarchive' ) && (bool) $indexable_data['is_robots_noarchive'] === true ) {
$translated_advanced_data[] = 'noarchive';
unset( $indexable_data['is_robots_noarchive'] );
}
if ( WPSEO_Validator::key_exists( $indexable_data, 'is_robots_noimageindex' ) && (bool) $indexable_data['is_robots_noimageindex'] === true ) {
$translated_advanced_data[] = 'noimageindex';
unset( $indexable_data['is_robots_noimageindex'] );
}
return implode( ',', $translated_advanced_data );
}
/**
* Converts the nofollow value to a database compatible one.
*
* @param bool $nofollow The current nofollow value.
*
* @return string The converted value.
*/
protected function convert_nofollow( $nofollow ) {
if ( $nofollow === 'true' ) {
return '1';
}
return '0';
}
/**
* Converts the noindex value to a database compatible one.
*
* @param string $noindex The current noindex value.
*
* @return string|null The converted value.
*/
protected function convert_noindex( $noindex ) {
if ( $noindex === 'false' ) {
return '2';
}
if ( $noindex === 'true' ) {
return '1';
}
return null;
}
}

View File

@ -0,0 +1,38 @@
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Admin\Services
*/
/**
* Represents the indexable service.
*/
abstract class WPSEO_Indexable_Provider implements WPSEO_Indexable_Service_Provider {
/**
* List of fields that need to be renamed.
*
* @var array
*/
protected $renameable_fields = array();
/**
* Renames and converts some of the indexable data to its database variant.
*
* @param array $indexable_data The indexable data to rename and convert.
*
* @return array The renamed and converted indexable data.
*/
protected function rename_indexable_data( &$indexable_data ) {
foreach ( $this->renameable_fields as $old_key => $new_key ) {
if ( WPSEO_Validator::key_exists( $indexable_data, $old_key ) ) {
$indexable_data[ $new_key ] = $indexable_data[ $old_key ];
unset( $indexable_data[ $old_key ] );
}
}
return $indexable_data;
}
}

View File

@ -0,0 +1,168 @@
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Admin\Services
*/
/**
* Represents the indexable term service.
*/
class WPSEO_Indexable_Service_Term_Provider extends WPSEO_Indexable_Provider {
/**
* List of fields that need to be renamed.
*
* @var array
*/
protected $renameable_fields = array(
'description' => 'desc',
'breadcrumb_title' => 'bctitle',
'og_title' => 'opengraph-title',
'og_description' => 'opengraph-description',
'og_image' => 'opengraph-image',
'twitter_title' => 'twitter-title',
'twitter_description' => 'twitter-description',
'twitter_image' => 'twitter-image',
'is_robots_noindex' => 'noindex',
'primary_focus_keyword' => 'focuskw',
'primary_focus_keyword_score' => 'linkdex',
'readability_score' => 'content_score',
);
/**
* Returns an array with data for the target object.
*
* @param integer $object_id The target object id.
* @param bool $as_object Optional. Whether or not to return the indexable
* as an object. Defaults to false.
*
* @return array|WPSEO_Term_Indexable The retrieved data. Defaults to an array format.
*/
public function get( $object_id, $as_object = false ) {
if ( ! $this->is_indexable( $object_id ) ) {
return array();
}
$indexable = WPSEO_Term_Indexable::from_object( $object_id );
if ( $as_object === true ) {
return $indexable;
}
return $indexable->to_array();
}
/**
* Handles the patching of values for an existing indexable.
*
* @param int $object_id The ID of the object.
* @param array $requestdata The request data to store.
*
* @return array The patched indexable.
*
* @throws WPSEO_Invalid_Indexable_Exception The indexable exception.
* @throws WPSEO_REST_Request_Exception Exception that is thrown if patching the object has failed.
*/
public function patch( $object_id, $requestdata ) {
$indexable = $this->get( $object_id, true );
if ( $indexable === array() ) {
throw WPSEO_Invalid_Indexable_Exception::non_existing_indexable( $object_id );
}
$new_indexable = $indexable->update( $requestdata );
$stored_indexable = $this->store_indexable( $new_indexable );
if ( $stored_indexable === true ) {
return $new_indexable->to_array();
}
throw WPSEO_REST_Request_Exception::patch( 'Term', $object_id );
}
/**
* Stores the indexable object.
*
* @param WPSEO_Indexable $indexable The indexable object to store.
*
* @return bool True if the indexable object was successfully stored.
*/
protected function store_indexable( WPSEO_Indexable $indexable ) {
$values = $this->convert_indexable_data( $indexable->to_array() );
$renamed_values = $this->rename_indexable_data( $values );
$prefixed_values = $this->prefix_indexable_data( $renamed_values );
WPSEO_Taxonomy_Meta::set_values( $values['object_id'], $values['object_subtype'], $prefixed_values );
return true;
}
/**
* Prefixes the indexable data to make it compatible with the database.
*
* @param array $indexable_data The indexable data to prefix.
*
* @return array The compatible indexable data.
*/
protected function prefix_indexable_data( $indexable_data ) {
$converted_data = array();
foreach ( $indexable_data as $key => $item ) {
if ( substr( strtolower( $key ), 0, 6 ) !== 'wpseo_' ) {
$key = 'wpseo_' . $key;
}
$converted_data[ $key ] = $item;
}
return $converted_data;
}
/**
* Converts the indexable data to make it compatible with the database.
*
* @param array $indexable_data The indexable data to prepare.
*
* @return array The converted indexable data.
*/
protected function convert_indexable_data( $indexable_data ) {
if ( WPSEO_Validator::key_exists( $indexable_data, 'is_robots_noindex' ) ) {
$indexable_data['is_robots_noindex'] = $this->convert_noindex( $indexable_data['is_robots_noindex'] );
}
return $indexable_data;
}
/**
* Checks if the given object id belongs to an indexable.
*
* @param int $object_id The object id.
*
* @return bool Whether the object id is indexable.
*/
public function is_indexable( $object_id ) {
$term = get_term( $object_id );
return ( $term !== null && ! is_wp_error( $term ) );
}
/**
* Converts the noindex value to a database compatible one.
*
* @param bool $noindex The current noindex value.
*
* @return string|null The converted value.
*/
protected function convert_noindex( $noindex ) {
if ( $noindex === 'false' ) {
return 'index';
}
if ( $noindex === 'true' ) {
return 'noindex';
}
return 'default';
}
}

View File

@ -0,0 +1,98 @@
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Admin\Services
*/
/**
* Represents the indexable service.
*/
class WPSEO_Indexable_Service {
/**
* Retrieves an indexable.
*
* @param WP_REST_Request $request The request object.
*
* @return WP_REST_Response The response.
*/
public function get_indexable( WP_REST_Request $request ) {
$object_type = $request->get_param( 'object_type' );
$object_id = $request->get_param( 'object_id' );
try {
$provider = $this->get_provider( $object_type );
$indexable = $provider->get( $object_id );
return new WP_REST_Response( $indexable );
}
catch ( Exception $exception ) {
return new WP_REST_Response( $exception->getMessage(), 500 );
}
}
/**
* Patches an indexable with the request parameters.
*
* @param WP_REST_Request $request The REST API request to process.
*
* @return WP_REST_Response The REST response.
*/
public function patch_indexable( WP_REST_Request $request ) {
$object_type = $request->get_param( 'object_type' );
$object_id = $request->get_param( 'object_id' );
try {
$provider = $this->get_provider( $object_type );
$patched_result = $provider->patch( $object_id, $request->get_params() );
return new WP_REST_Response( $patched_result );
}
catch ( Exception $exception ) {
return new WP_REST_Response( $exception->getMessage(), 500 );
}
}
/**
* Returns a provider based on the given object type.
*
* @param string $object_type The object type to get the provider for.
*
* @return WPSEO_Indexable_Service_Provider Instance of the service provider.
*
* @throws WPSEO_Invalid_Argument_Exception The invalid argument exception.
*/
protected function get_provider( $object_type ) {
$object_type = strtolower( $object_type );
if ( $object_type === 'post' ) {
return new WPSEO_Indexable_Service_Post_Provider();
}
if ( $object_type === 'term' ) {
return new WPSEO_Indexable_Service_Term_Provider();
}
throw WPSEO_Invalid_Argument_Exception::invalid_callable_parameter( $object_type, 'provider' );
}
/**
* Handles the situation when the object type is unknown.
*
* @param string $object_type The unknown object type.
*
* @return WP_REST_Response The response.
*/
protected function handle_unknown_object_type( $object_type ) {
return new WP_REST_Response(
sprintf(
/* translators: %1$s expands to the requested indexable type */
__( 'Unknown type %1$s', 'wordpress-seo' ),
$object_type
),
400
);
}
}

View File

@ -0,0 +1,42 @@
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Admin\Endpoints
*/
/**
* Dictates the required methods for an indexable service provider.
*/
interface WPSEO_Indexable_Service_Provider {
/**
* Returns an array with data for the target object.
*
* @param integer $object_id The target object id.
* @param bool $as_object Optional. Whether or not to return the indexable
* as an object. Defaults to false.
*
* @return array The retrieved data.
*/
public function get( $object_id, $as_object = false );
/**
* Handles the patching of values for an existing indexable.
*
* @param int $object_id The ID of the object.
* @param array $requestdata The request data to store.
*
* @return array The patched indexable.
*/
public function patch( $object_id, $requestdata );
/**
* Checks if the given object id belongs to an indexable.
*
* @param int $object_id The object id.
*
* @return bool Whether the object id is indexable.
*/
public function is_indexable( $object_id );
}