2015-10-15 22:52:37 +10:00
< ? php
2020-05-20 23:16:54 -04:00
/**
* MainWP WordPress SEO
*
* The code is used for the MainWP WordPress SEO Extension .
*/
2020-04-21 21:39:24 +02:00
/**
2019-02-19 23:52:21 +07:00
* Credits
*
2020-06-05 00:45:26 +07:00
* Plugin - Name : Yoast SEO
* Plugin - URI : https :// yoast . com / wordpress / plugins / seo / #utm_source=wpadmin&utm_medium=plugin&utm_campaign=wpseoplugin
2019-02-19 23:52:21 +07:00
* Author : Team Yoast
* Author URI : https :// yoast . com /
* Licence : GPL v3
*
* The code is used for the MainWP WordPress SEO Extension
* Extension URL : https :// mainwp . com / extension / wordpress - seo /
2020-04-21 21:39:24 +02:00
*/
2019-02-19 23:52:21 +07:00
2020-05-20 20:15:29 +07:00
use MainWP\Child\MainWP_Helper ;
2020-05-21 20:10:15 +07:00
// phpcs:disable PSR1.Classes.ClassDeclaration, WordPress.WP.AlternativeFunctions -- to use external code, third party credit.
2020-05-05 20:13:38 +07:00
2020-05-20 23:16:54 -04:00
/**
* Class MainWP_WordPress_SEO
*/
2020-05-07 23:22:05 +07:00
class MainWP_WordPress_SEO {
2020-05-21 04:40:14 +00:00
/**
* @ static
* @ var null Public static instance of MainWP_WordPress_SEO .
*/
2015-10-15 22:52:37 +10:00
public static $instance = null ;
2020-05-21 04:40:14 +00:00
/**
* Create public static instance of MainWP_WordPress_SEO .
*
* @ return MainWP_WordPress_SEO | null
*/
2020-05-06 20:22:11 +07:00
public static function instance () {
2020-03-26 14:05:04 +00:00
if ( null === self :: $instance ) {
2020-05-07 01:03:56 +07:00
self :: $instance = new self ();
2015-10-15 22:52:37 +10:00
}
2020-03-26 14:05:04 +00:00
return self :: $instance ;
2015-10-15 22:52:37 +10:00
}
2020-05-21 04:40:14 +00:00
/**
* MainWP_WordPress_SEO constructor .
*/
2015-10-15 22:52:37 +10:00
public function __construct () {
2020-05-20 23:16:54 -04:00
2020-05-21 04:40:14 +00:00
/** @var global $wpdb wbdb */
2015-10-15 22:52:37 +10:00
global $wpdb ;
2020-05-20 23:16:54 -04:00
2015-10-15 22:52:37 +10:00
add_action ( 'mainwp_child_deactivation' , array ( $this , 'child_deactivation' ) );
}
2020-05-21 04:40:14 +00:00
/**
* Removes option by name .
*
* @ return bool True , if option is successfully deleted . False on failure .
*/
2015-10-15 22:52:37 +10:00
public function child_deactivation () {
$dell_all = array ();
foreach ( $dell_all as $opt ) {
delete_option ( $opt );
}
}
2020-05-21 04:40:14 +00:00
/**
* MainWP WordPress SEO import_settings $_POST action .
*/
2015-10-15 22:52:37 +10:00
public function action () {
if ( ! class_exists ( 'WPSEO_Admin' ) ) {
$information [ 'error' ] = 'NO_WPSEO' ;
2020-05-20 20:15:29 +07:00
MainWP_Helper :: write ( $information );
2015-10-15 22:52:37 +10:00
}
$result = array ();
switch ( $_POST [ 'action' ] ) {
case 'import_settings' :
$information = $this -> import_settings ();
break ;
}
2020-05-20 20:15:29 +07:00
MainWP_Helper :: write ( $information );
2015-10-15 22:52:37 +10:00
}
2020-05-21 04:40:14 +00:00
/**
* Import settings .
*
* @ return array $information success [] | error []
*/
2015-10-15 22:52:37 +10:00
public function import_settings () {
2020-04-21 21:39:24 +02:00
if ( isset ( $_POST [ 'file_url' ] ) ) {
2020-05-22 20:20:02 +07:00
$file_url = base64_decode ( $_POST [ 'file_url' ] ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for http encode compatible..
2020-03-27 15:13:11 +00:00
$temporary_file = '' ;
2020-05-11 13:33:45 +00:00
2020-03-27 15:13:11 +00:00
try {
2020-04-21 21:39:24 +02:00
include_once ABSPATH . 'wp-admin/includes/file.php' ;
2020-05-11 20:30:56 +07:00
add_filter ( 'http_request_args' , array ( MainWP_Helper :: get_class_name (), 'reject_unsafe_urls' ), 99 , 2 );
2020-03-27 15:13:11 +00:00
$temporary_file = download_url ( $file_url );
2020-05-11 20:30:56 +07:00
remove_filter ( 'http_request_args' , array ( MainWP_Helper :: get_class_name (), 'reject_unsafe_urls' ), 99 , 2 );
2020-03-27 15:13:11 +00:00
if ( is_wp_error ( $temporary_file ) ) {
2020-05-07 01:03:56 +07:00
throw new \Exception ( 'Error: ' . $temporary_file -> get_error_message () );
2020-03-27 15:13:11 +00:00
} else {
if ( $this -> import_seo_settings ( $temporary_file ) ) {
$information [ 'success' ] = true ;
} else {
2020-05-07 01:03:56 +07:00
throw new \Exception ( __ ( 'Settings could not be imported.' , 'mainwp-child' ) );
2020-03-27 15:13:11 +00:00
}
}
2020-05-07 01:03:56 +07:00
} catch ( \Exception $e ) {
2020-03-27 15:13:11 +00:00
$information [ 'error' ] = $e -> getMessage ();
}
if ( file_exists ( $temporary_file ) ) {
unlink ( $temporary_file );
}
2020-03-27 14:11:21 +00:00
} elseif ( isset ( $_POST [ 'settings' ] ) ) {
2020-03-27 15:13:11 +00:00
try {
2020-05-22 20:20:02 +07:00
$settings = base64_decode ( $_POST [ 'settings' ] ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for http encode compatible..
2020-04-21 19:39:31 +00:00
$options = parse_ini_string ( $settings , true , INI_SCANNER_RAW );
2020-03-27 15:13:11 +00:00
if ( is_array ( $options ) && array () !== $options ) {
2019-03-25 22:11:41 +07:00
2020-04-21 21:39:24 +02:00
$old_wpseo_version = null ;
2020-03-26 19:51:58 +00:00
if ( isset ( $options [ 'wpseo' ][ 'version' ] ) && '' !== $options [ 'wpseo' ][ 'version' ] ) {
$old_wpseo_version = $options [ 'wpseo' ][ 'version' ];
}
foreach ( $options as $name => $optgroup ) {
if ( 'wpseo_taxonomy_meta' === $name ) {
$optgroup = json_decode ( urldecode ( $optgroup [ 'wpseo_taxonomy_meta' ] ), true );
}
$option_instance = WPSEO_Options :: get_option_instance ( $name );
if ( is_object ( $option_instance ) && method_exists ( $option_instance , 'import' ) ) {
$optgroup = $option_instance -> import ( $optgroup , $old_wpseo_version , $options );
}
}
2020-04-21 21:39:24 +02:00
$information [ 'success' ] = true ;
2019-03-25 22:11:41 +07:00
2020-03-26 19:51:58 +00:00
} else {
2020-05-07 01:03:56 +07:00
throw new \Exception ( __ ( 'Settings could not be imported:' , 'mainwp-child' ) );
2020-03-26 19:51:58 +00:00
}
2020-05-07 01:03:56 +07:00
} catch ( \Exception $e ) {
2020-03-27 15:13:11 +00:00
$information [ 'error' ] = $e -> getMessage ();
2020-03-27 14:11:21 +00:00
}
2020-03-27 15:13:11 +00:00
}
2015-10-15 22:52:37 +10:00
2020-05-20 20:15:29 +07:00
MainWP_Helper :: write ( $information );
2015-10-15 22:52:37 +10:00
}
2020-05-21 04:40:14 +00:00
/**
* Import SEO settings .
*
* @ param $file settings . ini file to import .
* @ return bool Return TRUE on success .
* @ throws Exception Trow an exception and return error message .
*/
2015-10-15 22:52:37 +10:00
public function import_seo_settings ( $file ) {
if ( ! empty ( $file ) ) {
$upload_dir = wp_upload_dir ();
if ( ! defined ( 'DIRECTORY_SEPARATOR' ) ) {
define ( 'DIRECTORY_SEPARATOR' , '/' );
}
$p_path = $upload_dir [ 'basedir' ] . DIRECTORY_SEPARATOR . 'wpseo-import' . DIRECTORY_SEPARATOR ;
if ( ! isset ( $GLOBALS [ 'wp_filesystem' ] ) || ! is_object ( $GLOBALS [ 'wp_filesystem' ] ) ) {
WP_Filesystem ();
}
$unzipped = unzip_file ( $file , $p_path );
if ( ! is_wp_error ( $unzipped ) ) {
$filename = $p_path . 'settings.ini' ;
2020-04-23 19:53:22 +02:00
if ( is_file ( $filename ) && is_readable ( $filename ) ) {
2015-10-15 22:52:37 +10:00
$options = parse_ini_file ( $filename , true );
if ( is_array ( $options ) && array () !== $options ) {
$old_wpseo_version = null ;
if ( isset ( $options [ 'wpseo' ][ 'version' ] ) && '' !== $options [ 'wpseo' ][ 'version' ] ) {
$old_wpseo_version = $options [ 'wpseo' ][ 'version' ];
}
foreach ( $options as $name => $optgroup ) {
if ( 'wpseo_taxonomy_meta' === $name ) {
$optgroup = json_decode ( urldecode ( $optgroup [ 'wpseo_taxonomy_meta' ] ), true );
}
$option_instance = WPSEO_Options :: get_option_instance ( $name );
if ( is_object ( $option_instance ) && method_exists ( $option_instance , 'import' ) ) {
$optgroup = $option_instance -> import ( $optgroup , $old_wpseo_version , $options );
}
}
return true ;
} else {
2020-05-07 01:03:56 +07:00
throw new \Exception ( __ ( 'Settings could not be imported:' , 'mainwp-child' ) );
2015-10-15 22:52:37 +10:00
}
unset ( $options , $name , $optgroup );
} else {
2020-05-07 01:03:56 +07:00
throw new \Exception ( __ ( 'Settings could not be imported:' , 'mainwp-child' ) );
2015-10-15 22:52:37 +10:00
}
2020-04-23 19:53:22 +02:00
unlink ( $filename );
unlink ( $p_path );
2015-10-15 22:52:37 +10:00
} else {
2020-05-07 01:03:56 +07:00
throw new \Exception ( __ ( 'Settings could not be imported:' , 'mainwp-child' ) . ' ' . sprintf ( __ ( 'Unzipping failed with error "%s".' , 'mainwp-child' ), $unzipped -> get_error_message () ) );
2015-10-15 22:52:37 +10:00
}
unset ( $zip , $unzipped );
2020-04-23 19:53:22 +02:00
unlink ( $file );
2015-10-15 22:52:37 +10:00
} else {
2020-05-07 01:03:56 +07:00
throw new \Exception ( __ ( 'Settings could not be imported:' , 'mainwp-child' ) . ' ' . __ ( 'Upload failed.' , 'mainwp-child' ) );
2015-10-15 22:52:37 +10:00
}
return false ;
}
2017-08-24 20:41:12 +02:00
2020-05-21 04:40:14 +00:00
/**
* Parse Column Score .
*
* @ param $post_id Post ID .
* @ return string SEO Score .
*/
2017-08-24 20:41:12 +02:00
public function parse_column_score ( $post_id ) {
if ( '1' === WPSEO_Meta :: get_value ( 'meta-robots-noindex' , $post_id ) ) {
$rank = new WPSEO_Rank ( WPSEO_Rank :: NO_INDEX );
2020-04-21 21:39:24 +02:00
$title = __ ( 'Post is set to noindex.' , 'mainwp-child' );
2017-08-24 20:41:12 +02:00
WPSEO_Meta :: set_value ( 'linkdex' , 0 , $post_id );
2020-03-26 17:03:00 +00:00
} elseif ( '' === WPSEO_Meta :: get_value ( 'focuskw' , $post_id ) ) {
2017-08-24 20:41:12 +02:00
$rank = new WPSEO_Rank ( WPSEO_Rank :: NO_FOCUS );
2020-04-21 21:39:24 +02:00
$title = __ ( 'Focus keyword not set.' , 'mainwp-child' );
2020-03-26 17:03:00 +00:00
} else {
2017-08-24 20:41:12 +02:00
$score = ( int ) WPSEO_Meta :: get_value ( 'linkdex' , $post_id );
$rank = WPSEO_Rank :: from_numeric_score ( $score );
$title = $rank -> get_label ();
}
return $this -> render_score_indicator ( $rank , $title );
}
2020-05-21 04:40:14 +00:00
/**
* Parse readability score .
*
* @ param $post_id Post ID .
* @ return string Redability Score .
*/
2017-08-24 20:41:12 +02:00
public function parse_column_score_readability ( $post_id ) {
$score = ( int ) WPSEO_Meta :: get_value ( 'content_score' , $post_id );
2020-03-26 19:45:07 +00:00
$rank = WPSEO_Rank :: from_numeric_score ( $score );
2017-08-24 20:41:12 +02:00
return $this -> render_score_indicator ( $rank );
}
2020-05-21 04:40:14 +00:00
/**
* Render Score Rank .
*
* @ param $rank SEO Rank Score .
* @ param string $title Rank title .
* @ return string Return SEO Score html .
*/
2017-08-24 20:41:12 +02:00
private function render_score_indicator ( $rank , $title = '' ) {
if ( empty ( $title ) ) {
$title = $rank -> get_label ();
}
return '<div aria-hidden="true" title="' . esc_attr ( $title ) . '" class="wpseo-score-icon ' . esc_attr ( $rank -> get_css_class () ) . '"></div><span class="screen-reader-text">' . $title . '</span>' ;
}
2015-10-15 22:52:37 +10:00
}