2021-04-26 22:57:04 +01:00
< ? php // phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
2021-02-23 16:00:59 +00:00
/**
2021-05-01 21:34:46 +01:00
* Admin . Layouts : Init class
2021-02-23 16:00:59 +00:00
*
2021-05-01 21:34:46 +01:00
* The Admin . Layouts subpackage is composed of the { @ see Engine }
* abstract class , which is extended by the { @ see Settings }
2021-05-01 19:22:41 +01:00
* sub - class . The subpackage is initialised at runtime by the { @ see
2021-05-01 21:34:46 +01:00
* Init } class .
2021-04-27 08:31:37 +01:00
*
2021-05-01 21:34:46 +01:00
* @ package footnotes
2021-04-30 18:03:15 +01:00
* @ since 1.5 . 0
* @ since 2.8 . 0 Rename file from `init.php` to `class-footnotes-layout-init.php` ,
2021-05-01 19:22:41 +01:00
* rename `dashboard/` sub - directory to `layout/` .
2021-02-23 16:00:59 +00:00
*/
2021-05-01 21:34:46 +01:00
namespace footnotes\admin\layout ;
use footnotes\includes as Includes ;
2021-02-23 16:00:59 +00:00
/**
2021-04-30 18:03:15 +01:00
* Class to initialise all defined page layouts .
2021-02-23 16:00:59 +00:00
*
2021-05-01 21:34:46 +01:00
* @ package footnotes
2021-02-23 16:00:59 +00:00
* @ since 1.5 . 0
*/
2021-05-01 21:34:46 +01:00
class Init {
2021-02-23 16:00:59 +00:00
2021-04-27 09:30:53 +01:00
/**
* The ID of this plugin .
*
2021-04-30 18:03:15 +01:00
* @ access private
* @ var string $plugin_name The ID of this plugin .
*
* @ since 2.8 . 0
2021-04-27 09:30:53 +01:00
*/
private $plugin_name ;
2021-02-23 16:00:59 +00:00
/**
* Slug for the Plugin main menu .
*
2021-04-30 18:03:15 +01:00
* @ var string
*
* @ since 1.5 . 0
2021-02-23 16:00:59 +00:00
*/
2021-04-19 12:15:17 +01:00
const C_STR_MAIN_MENU_SLUG = 'footnotes' ;
2021-02-23 16:00:59 +00:00
/**
2021-04-30 18:03:15 +01:00
* Contains the settings page .
2021-02-23 16:00:59 +00:00
*
2021-05-01 21:34:46 +01:00
* @ var Settings
2021-04-30 18:03:15 +01:00
*
* @ since 1.5 . 0
2021-02-23 16:00:59 +00:00
*/
2021-04-16 03:10:03 +01:00
private $settings_page ;
2021-02-23 16:00:59 +00:00
/**
2021-04-30 18:03:15 +01:00
* Initializes all WordPress hooks for the Plugin Settings .
2021-02-23 16:00:59 +00:00
*
2021-05-01 19:22:41 +01:00
* @ param string $plugin_name The name of the plugin .
2021-04-27 09:54:07 +01:00
*
2021-02-23 16:00:59 +00:00
* @ since 1.5 . 0
2021-04-30 18:03:15 +01:00
* @ since 2.8 . 0 Added `$plugin_name` parameter .
2021-02-23 16:00:59 +00:00
*/
2021-04-27 09:30:53 +01:00
public function __construct ( $plugin_name ) {
$this -> plugin_name = $plugin_name ;
2021-04-27 09:54:07 +01:00
2021-04-27 08:33:28 +01:00
$this -> load_dependencies ();
2021-04-27 08:42:21 +01:00
2021-05-01 21:34:46 +01:00
$this -> settings_page = new Settings ( $this -> plugin_name );
2021-02-23 16:48:53 +00:00
// Register hooks/actions.
2021-04-16 03:10:03 +01:00
add_action ( 'admin_menu' , array ( $this , 'register_options_submenu' ) );
2021-02-23 16:48:53 +00:00
add_action ( 'admin_init' , array ( $this , 'initialize_settings' ) );
2021-02-23 23:45:01 +01:00
// Register AJAX callbacks for Plugin information.
2021-02-23 16:48:53 +00:00
add_action ( 'wp_ajax_nopriv_footnotes_get_plugin_info' , array ( $this , 'get_plugin_meta_information' ) );
add_action ( 'wp_ajax_footnotes_get_plugin_info' , array ( $this , 'get_plugin_meta_information' ) );
2021-02-23 16:00:59 +00:00
}
2021-05-01 19:22:41 +01:00
2021-04-27 08:33:28 +01:00
/**
2021-04-30 18:03:15 +01:00
* Load the required dependencies for the layouts pages .
2021-04-27 08:33:28 +01:00
*
* Include the following files that make up the plugin :
*
2021-05-01 21:34:46 +01:00
* - { @ see Includes\Config } : defines plugin constants ;
* - { @ see Includes\Settings } : defines configurable plugin settings ; and
* - { @ see Settings } : defines the plugin settings page .
2021-04-27 08:33:28 +01:00
*
2021-04-30 18:03:15 +01:00
* @ access private
2021-04-27 08:33:28 +01:00
*
2021-04-30 18:03:15 +01:00
* @ since 2.8 . 0
2021-04-27 08:33:28 +01:00
*/
private function load_dependencies () {
/**
2021-04-30 18:03:15 +01:00
* Defines plugin constants .
2021-04-27 08:33:28 +01:00
*/
2021-05-01 21:34:46 +01:00
require_once plugin_dir_path ( dirname ( __FILE__ , 2 ) ) . 'includes/class-config.php' ;
2021-04-27 08:42:21 +01:00
2021-04-27 08:33:28 +01:00
/**
2021-04-30 18:03:15 +01:00
* Defines configurable plugin settings .
2021-04-27 08:33:28 +01:00
*/
2021-05-01 21:34:46 +01:00
require_once plugin_dir_path ( dirname ( __FILE__ , 2 ) ) . 'includes/class-settings.php' ;
2021-04-27 08:42:21 +01:00
2021-04-27 08:33:28 +01:00
/**
2021-04-30 18:03:15 +01:00
* Represents the plugin settings dashboard page .
2021-04-27 08:33:28 +01:00
*/
2021-05-01 21:34:46 +01:00
require_once plugin_dir_path ( dirname ( __FILE__ ) ) . 'layout/class-settings.php' ;
2021-04-27 08:33:28 +01:00
}
2021-02-23 16:00:59 +00:00
/**
2021-04-16 03:10:03 +01:00
* Registers the settings and initialises the settings page .
2021-02-23 16:00:59 +00:00
*
* @ since 1.5 . 0
*/
2021-02-23 16:48:53 +00:00
public function initialize_settings () {
2021-05-01 21:34:46 +01:00
Includes\Settings :: instance () -> register_settings ();
2021-04-16 03:10:03 +01:00
$this -> settings_page -> register_sections ();
2021-02-23 16:00:59 +00:00
}
/**
2021-04-16 03:10:03 +01:00
* Registers the footnotes submenu page .
2021-02-23 16:00:59 +00:00
*
* @ since 1.5 . 0
* @ see http :// codex . wordpress . org / Function_Reference / add_menu_page
*/
2021-04-16 03:10:03 +01:00
public function register_options_submenu () {
add_submenu_page (
'options-general.php' ,
'footnotes Settings' ,
2021-05-01 21:34:46 +01:00
Includes\Config :: C_STR_PLUGIN_PUBLIC_NAME ,
2021-04-16 03:10:03 +01:00
'manage_options' ,
2021-04-27 08:29:19 +01:00
self :: C_STR_MAIN_MENU_SLUG ,
2021-04-16 03:10:03 +01:00
array ( $this -> settings_page , 'display_content' )
2021-02-23 16:00:59 +00:00
);
2021-04-16 03:10:03 +01:00
$this -> settings_page -> register_sub_page ();
2021-02-23 16:00:59 +00:00
}
2021-02-23 17:59:12 +00:00
// phpcs:disable WordPress.Security.NonceVerification.Missing
2021-02-23 16:00:59 +00:00
/**
* AJAX call . returns a JSON string containing meta information about a specific WordPress Plugin .
*
* @ since 1.5 . 0
*/
2021-02-23 16:48:53 +00:00
public function get_plugin_meta_information () {
// TODO: add nonce verification.
2021-02-23 23:45:01 +01:00
// Get plugin internal name from POST data.
2021-02-23 17:59:12 +00:00
if ( isset ( $_POST [ 'plugin' ] ) ) {
2021-04-19 12:15:17 +01:00
$l_str_plugin_name = wp_unslash ( $_POST [ 'plugin' ] );
2021-02-23 16:48:53 +00:00
}
2021-04-19 12:15:17 +01:00
if ( empty ( $l_str_plugin_name ) ) {
2021-02-23 16:48:53 +00:00
echo wp_json_encode ( array ( 'error' => 'Plugin name invalid.' ) );
2021-02-23 16:00:59 +00:00
exit ;
}
2021-04-19 12:15:17 +01:00
$l_str_url = 'https://api.wordpress.org/plugins/info/1.0/' . $l_str_plugin_name . '.json' ;
2021-02-23 23:45:01 +01:00
// Call URL and collect data.
2021-04-19 12:15:17 +01:00
$l_arr_response = wp_remote_get ( $l_str_url );
2021-02-23 23:45:01 +01:00
// Check if response is valid.
2021-04-19 12:15:17 +01:00
if ( is_wp_error ( $l_arr_response ) ) {
2021-02-23 16:48:53 +00:00
echo wp_json_encode ( array ( 'error' => 'Error receiving Plugin Information from WordPress.' ) );
2021-02-23 16:00:59 +00:00
exit ;
}
2021-04-19 12:15:17 +01:00
if ( ! array_key_exists ( 'body' , $l_arr_response ) ) {
2021-02-23 16:48:53 +00:00
echo wp_json_encode ( array ( 'error' => 'Error reading WordPress API response message.' ) );
2021-02-23 16:00:59 +00:00
exit ;
}
2021-02-23 23:45:01 +01:00
// Get the body of the response.
2021-04-19 12:15:17 +01:00
$l_str_response = $l_arr_response [ 'body' ];
2021-02-23 23:45:01 +01:00
// Get plugin object.
2021-04-19 12:15:17 +01:00
$l_arr_plugin = json_decode ( $l_str_response , true );
if ( empty ( $l_arr_plugin ) ) {
echo wp_json_encode ( array ( 'error' => 'Error reading Plugin meta information.<br/>URL: ' . $l_str_url . '<br/>Response: ' . $l_str_response ) );
2021-02-23 16:00:59 +00:00
exit ;
}
2021-04-19 12:15:17 +01:00
$l_int_num_ratings = array_key_exists ( 'num_ratings' , $l_arr_plugin ) ? intval ( $l_arr_plugin [ 'num_ratings' ] ) : 0 ;
$l_int_rating = array_key_exists ( 'rating' , $l_arr_plugin ) ? floatval ( $l_arr_plugin [ 'rating' ] ) : 0.0 ;
$l_int_stars = round ( 5 * $l_int_rating / 100.0 , 1 );
2021-02-23 16:00:59 +00:00
2021-02-23 23:45:01 +01:00
// Return Plugin information as JSON encoded string.
2021-02-23 16:48:53 +00:00
echo wp_json_encode (
2021-02-23 16:00:59 +00:00
array (
2021-02-23 16:48:53 +00:00
'error' => '' ,
2021-04-19 12:15:17 +01:00
'PluginDescription' => array_key_exists ( 'short_description' , $l_arr_plugin ) ? html_entity_decode ( $l_arr_plugin [ 'short_description' ] ) : 'Error reading Plugin information' ,
'PluginAuthor' => array_key_exists ( 'author' , $l_arr_plugin ) ? html_entity_decode ( $l_arr_plugin [ 'author' ] ) : 'unknown' ,
'PluginRatingText' => $l_int_stars . ' ' . __ ( 'rating based on' , 'footnotes' ) . ' ' . $l_int_num_ratings . ' ' . __ ( 'ratings' , 'footnotes' ),
'PluginRating1' => $l_int_stars >= 0.5 ? 'star-full' : 'star-empty' ,
'PluginRating2' => $l_int_stars >= 1.5 ? 'star-full' : 'star-empty' ,
'PluginRating3' => $l_int_stars >= 2.5 ? 'star-full' : 'star-empty' ,
'PluginRating4' => $l_int_stars >= 3.5 ? 'star-full' : 'star-empty' ,
'PluginRating5' => $l_int_stars >= 4.5 ? 'star-full' : 'star-empty' ,
'PluginRating' => $l_int_num_ratings ,
'PluginLastUpdated' => array_key_exists ( 'last_updated' , $l_arr_plugin ) ? $l_arr_plugin [ 'last_updated' ] : 'unknown' ,
'PluginDownloads' => array_key_exists ( 'downloaded' , $l_arr_plugin ) ? $l_arr_plugin [ 'downloaded' ] : '---' ,
2021-02-23 16:00:59 +00:00
)
);
exit ;
}
2021-02-27 08:49:08 +00:00
// phpcs:enable WordPress.Security.NonceVerification.Missing
2021-02-23 16:00:59 +00:00
}