refactor: remove Hungarian notation

FINALLY close #34
This commit is contained in:
Ben Goldsworthy 2021-05-02 20:46:44 +01:00
parent 7c0b05b59e
commit 205764f91a
13 changed files with 1637 additions and 1637 deletions

View file

@ -30,15 +30,15 @@ class WYSIWYG {
/**
* Append a new Button to the WYSIWYG editor of Posts and Pages.
*
* @param string[] $p_arr_buttons Already-defined editor buttons.
* @param string[] $buttons Already-defined editor buttons.
* @return string[]
*
* @since 1.5.0
* @todo Should this be `static`?
*/
public static function new_visual_editor_button( array $p_arr_buttons ): array {
$p_arr_buttons[] = 'footnotes';
return $p_arr_buttons;
public static function new_visual_editor_button( array $buttons ): array {
$buttons[] = 'footnotes';
return $buttons;
}
/**
@ -47,24 +47,24 @@ class WYSIWYG {
* @since 1.5.0
*/
public static function new_plain_text_editor_button(): void {
$l_obj_template = new Includes\Template( \footnotes\includes\Template::C_STR_DASHBOARD, 'editor-button' );
$template = new Includes\Template( \footnotes\includes\Template::DASHBOARD, 'editor-button' );
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
echo $l_obj_template->get_content();
echo $template->get_content();
// phpcs:enable
}
/**
* Includes the Plugins WYSIWYG editor script.
*
* @param string[] $p_arr_plugins Scripts to be included by the editor.
* @param string[] $plugins Scripts to be included by the editor.
* @return string[]
*
* @since 1.5.0
* @todo Should this be `static`?
*/
public static function include_scripts( array $p_arr_plugins ): array {
$p_arr_plugins['footnotes'] = plugins_url( '/../admin/js/wysiwyg-editor' . ( ( PRODUCTION_ENV ) ? '.min' : '' ) . '.js', __FILE__ );
return $p_arr_plugins;
public static function include_scripts( array $plugins ): array {
$plugins['footnotes'] = plugins_url( '/../admin/js/wysiwyg-editor' . ( ( PRODUCTION_ENV ) ? '.min' : '' ) . '.js', __FILE__ );
return $plugins;
}
/**
@ -75,16 +75,16 @@ class WYSIWYG {
*/
public static function ajax_callback(): void {
// Get start and end tag for the footnotes short code.
$l_str_starting_tag = Includes\Settings::instance()->get( \footnotes\includes\Settings::C_STR_FOOTNOTES_SHORT_CODE_START );
$l_str_ending_tag = Includes\Settings::instance()->get( \footnotes\includes\Settings::C_STR_FOOTNOTES_SHORT_CODE_END );
if ( 'userdefined' === $l_str_starting_tag || 'userdefined' === $l_str_ending_tag ) {
$l_str_starting_tag = Includes\Settings::instance()->get( \footnotes\includes\Settings::C_STR_FOOTNOTES_SHORT_CODE_START_USER_DEFINED );
$l_str_ending_tag = Includes\Settings::instance()->get( \footnotes\includes\Settings::C_STR_FOOTNOTES_SHORT_CODE_END_USER_DEFINED );
$starting_tag = Includes\Settings::instance()->get( \footnotes\includes\Settings::FOOTNOTES_SHORT_CODE_START );
$ending_tag = Includes\Settings::instance()->get( \footnotes\includes\Settings::FOOTNOTES_SHORT_CODE_END );
if ( 'userdefined' === $starting_tag || 'userdefined' === $ending_tag ) {
$starting_tag = Includes\Settings::instance()->get( \footnotes\includes\Settings::FOOTNOTES_SHORT_CODE_START_USER_DEFINED );
$ending_tag = Includes\Settings::instance()->get( \footnotes\includes\Settings::FOOTNOTES_SHORT_CODE_END_USER_DEFINED );
}
echo wp_json_encode(
array(
'start' => htmlspecialchars( $l_str_starting_tag ),
'end' => htmlspecialchars( $l_str_ending_tag ),
'start' => htmlspecialchars( $starting_tag ),
'end' => htmlspecialchars( $ending_tag ),
)
);
exit;

View file

@ -49,7 +49,7 @@ abstract class Engine {
*
* @since 1.5.0
*/
protected ?string $a_str_sub_page_hook = null;
protected ?string $sub_page_hook = null;
/**
* Stores all Sections for the child sub-page.
@ -59,7 +59,7 @@ abstract class Engine {
*
* @since 1.5.0
*/
protected array $a_arr_sections = array();
protected array $sections = array();
/**
* Returns a Priority index. Lower numbers have a higher priority.
@ -114,10 +114,10 @@ abstract class Engine {
* Returns an array describing a sub-page section.
*
* @access protected
* @param string $p_str_id Unique ID suffix.
* @param string $p_str_title Title of the section.
* @param int $p_int_settings_container_index Settings Container index.
* @param bool $p_bool_has_submit_button Whether a Submit button should
* @param string $id Unique ID suffix.
* @param string $title Title of the section.
* @param int $settings_container_index Settings Container index.
* @param bool $has_submit_button Whether a Submit button should
* be displayed for this section. Default `true`.
* @return array {
* A dashboard section.
@ -131,12 +131,12 @@ abstract class Engine {
* @since 1.5.0
* @todo Refactor sections into their own class?
*/
protected function add_section( string $p_str_id, string $p_str_title, int $p_int_settings_container_index, bool $p_bool_has_submit_button = true ): array {
protected function add_section( string $id, string $title, int $settings_container_index, bool $has_submit_button = true ): array {
return array(
'id' => $this->plugin_name . '-' . $p_str_id,
'title' => $p_str_title,
'submit' => $p_bool_has_submit_button,
'container' => $p_int_settings_container_index,
'id' => $this->plugin_name . '-' . $id,
'title' => $title,
'submit' => $has_submit_button,
'container' => $settings_container_index,
);
}
@ -144,10 +144,10 @@ abstract class Engine {
* Returns an array describing a meta box.
*
* @access protected
* @param string $p_str_section_id Parent section ID.
* @param string $p_str_id Unique ID suffix.
* @param string $p_str_title Title for the meta box.
* @param string $p_str_callback_function_name Class method name for callback.
* @param string $section_id Parent section ID.
* @param string $id Unique ID suffix.
* @param string $title Title for the meta box.
* @param string $callback_function_name Class method name for callback.
* @return array {
* A dashboard meta box.
*
@ -161,12 +161,12 @@ abstract class Engine {
* @todo Refactor meta boxes into their own class?
* @todo Pass actual functions rather than strings?
*/
protected function add_meta_box( string $p_str_section_id, string $p_str_id, string $p_str_title, string $p_str_callback_function_name ): array {
protected function add_meta_box( string $section_id, string $id, string $title, string $callback_function_name ): array {
return array(
'parent' => $this->plugin_name . '-' . $p_str_section_id,
'id' => $p_str_id,
'title' => $p_str_title,
'callback' => $p_str_callback_function_name,
'parent' => $this->plugin_name . '-' . $section_id,
'id' => $id,
'title' => $title,
'callback' => $callback_function_name,
);
}
@ -178,20 +178,20 @@ abstract class Engine {
public function register_sub_page(): void {
global $submenu;
if ( array_key_exists( plugin_basename( Init::C_STR_MAIN_MENU_SLUG ), $submenu ) ) {
foreach ( $submenu[ plugin_basename( Init::C_STR_MAIN_MENU_SLUG ) ] as $l_arr_sub_menu ) {
if ( plugin_basename( Init::C_STR_MAIN_MENU_SLUG . $this->get_sub_page_slug() ) === $l_arr_sub_menu[2] ) {
remove_submenu_page( Init::C_STR_MAIN_MENU_SLUG, Init::C_STR_MAIN_MENU_SLUG . $this->get_sub_page_slug() );
if ( array_key_exists( plugin_basename( Init::MAIN_MENU_SLUG ), $submenu ) ) {
foreach ( $submenu[ plugin_basename( Init::MAIN_MENU_SLUG ) ] as $sub_menu ) {
if ( plugin_basename( Init::MAIN_MENU_SLUG . $this->get_sub_page_slug() ) === $sub_menu[2] ) {
remove_submenu_page( Init::MAIN_MENU_SLUG, Init::MAIN_MENU_SLUG . $this->get_sub_page_slug() );
}
}
}
$this->a_str_sub_page_hook = add_submenu_page(
Init::C_STR_MAIN_MENU_SLUG,
$this->sub_page_hook = add_submenu_page(
Init::MAIN_MENU_SLUG,
$this->get_sub_page_title(),
$this->get_sub_page_title(),
'manage_options',
Init::C_STR_MAIN_MENU_SLUG . $this->get_sub_page_slug(),
Init::MAIN_MENU_SLUG . $this->get_sub_page_slug(),
fn() => $this->display_content()
);
}
@ -202,16 +202,16 @@ abstract class Engine {
* @since 1.5.0
*/
public function register_sections(): void {
foreach ( $this->get_sections() as $l_arr_section ) {
foreach ( $this->get_sections() as $section ) {
// Append tab to the tab-array.
$this->a_arr_sections[ $l_arr_section['id'] ] = $l_arr_section;
$this->sections[ $section['id'] ] = $section;
add_settings_section(
$l_arr_section['id'],
$section['id'],
'',
fn() => $this->description(),
$l_arr_section['id']
$section['id']
);
$this->register_meta_boxes( $l_arr_section['id'] );
$this->register_meta_boxes( $section['id'] );
}
}
@ -219,21 +219,21 @@ abstract class Engine {
* Registers all Meta boxes for a sub-page.
*
* @access private
* @param string $p_str_parent_id Parent section unique ID.
* @param string $parent_id Parent section unique ID.
*
* @since 1.5.0
*/
private function register_meta_boxes( string $p_str_parent_id ): void {
private function register_meta_boxes( string $parent_id ): void {
// Iterate through each meta box.
foreach ( $this->get_meta_boxes() as $l_arr_meta_box ) {
if ( $p_str_parent_id !== $l_arr_meta_box['parent'] ) {
foreach ( $this->get_meta_boxes() as $meta_box ) {
if ( $parent_id !== $meta_box['parent'] ) {
continue;
}
add_meta_box(
$p_str_parent_id . '-' . $l_arr_meta_box['id'],
$l_arr_meta_box['title'],
array( $this, $l_arr_meta_box['callback'] ),
$p_str_parent_id,
$parent_id . '-' . $meta_box['id'],
$meta_box['title'],
array( $this, $meta_box['callback'] ),
$parent_id,
'main'
);
}
@ -262,33 +262,33 @@ abstract class Engine {
*/
public function display_content(): void {
$this->append_scripts();
$l_str_active_section_id = isset( $_GET['t'] ) ? wp_unslash( $_GET['t'] ) : array_key_first( $this->a_arr_sections );
$l_arr_active_section = $this->a_arr_sections[ $l_str_active_section_id ];
$active_section_id = isset( $_GET['t'] ) ? wp_unslash( $_GET['t'] ) : array_key_first( $this->sections );
$active_section = $this->sections[ $active_section_id ];
// Store settings.
$l_bool_settings_updated = false;
$settings_updated = false;
if ( array_key_exists( 'save-settings', $_POST ) && 'save' === $_POST['save-settings'] ) {
unset( $_POST['save-settings'] );
unset( $_POST['submit'] );
$l_bool_settings_updated = $this->save_settings();
$settings_updated = $this->save_settings();
}
// Display all sections and highlight the active section.
echo '<div class="wrap">';
echo '<h2 class="nav-tab-wrapper">';
// Iterate through all register sections.
foreach ( $this->a_arr_sections as $l_str_id => $l_arr_description ) {
foreach ( $this->sections as $id => $description ) {
echo sprintf(
'<a class="nav-tab%s" href="?page=%s&t=%s">%s</a>',
( $l_str_id === $l_arr_active_section['id'] ) ? ' nav-tab-active' : '',
Init::C_STR_MAIN_MENU_SLUG,
$l_str_id,
$l_arr_description['title']
( $id === $active_section['id'] ) ? ' nav-tab-active' : '',
Init::MAIN_MENU_SLUG,
$id,
$description['title']
);
}
echo '</h2><br/>';
if ( $l_bool_settings_updated ) {
if ( $settings_updated ) {
echo sprintf( '<div id="message" class="updated">%s</div>', __( 'Settings saved', 'footnotes' ) );
}
@ -296,11 +296,11 @@ abstract class Engine {
echo '<!--suppress HtmlUnknownTarget --><form method="post" action="">';
echo '<input type="hidden" name="save-settings" value="save" />';
// Outputs the settings field of the active section.
do_settings_sections( $l_arr_active_section['id'] );
do_meta_boxes( $l_arr_active_section['id'], 'main', null );
do_settings_sections( $active_section['id'] );
do_meta_boxes( $active_section['id'], 'main', null );
// Add submit button to active section if defined.
if ( $l_arr_active_section['submit'] ) {
if ( $active_section['submit'] ) {
submit_button();
}
echo '</form>';
@ -311,7 +311,7 @@ abstract class Engine {
echo 'jQuery(document).ready(function ($) {';
echo 'jQuery(".footnotes-color-picker").wpColorPicker();';
echo "jQuery('.if-js-closed').removeClass('if-js-closed').addClass('closed');";
echo "postboxes.add_postbox_toggles('" . $this->a_str_sub_page_hook . "');";
echo "postboxes.add_postbox_toggles('" . $this->sub_page_hook . "');";
echo '});';
echo '</script>';
}
@ -328,15 +328,15 @@ abstract class Engine {
* @todo Review nonce verification.
*/
private function save_settings(): bool {
$l_arr_new_settings = array();
$l_str_active_section_id = isset( $_GET['t'] ) ? wp_unslash( $_GET['t'] ) : array_key_first( $this->a_arr_sections );
$l_arr_active_section = $this->a_arr_sections[ $l_str_active_section_id ];
$new_settings = array();
$active_section_id = isset( $_GET['t'] ) ? wp_unslash( $_GET['t'] ) : array_key_first( $this->sections );
$active_section = $this->sections[ $active_section_id ];
foreach ( array_keys( Includes\Settings::instance()->get_defaults( $l_arr_active_section['container'] ) ) as $l_str_key ) {
$l_arr_new_settings[ $l_str_key ] = array_key_exists( $l_str_key, $_POST ) ? wp_unslash( $_POST[ $l_str_key ] ) : '';
foreach ( array_keys( Includes\Settings::instance()->get_defaults( $active_section['container'] ) ) as $key ) {
$new_settings[ $key ] = array_key_exists( $key, $_POST ) ? wp_unslash( $_POST[ $key ] ) : '';
}
// Update settings.
return Includes\Settings::instance()->save_options( $l_arr_active_section['container'], $l_arr_new_settings );
return Includes\Settings::instance()->save_options( $active_section['container'], $new_settings );
}
// phpcs:enable WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
@ -354,7 +354,7 @@ abstract class Engine {
* Loads a specified setting.
*
* @access protected
* @param string $p_str_setting_key_name Setting key.
* @param string $setting_key_name Setting key.
* @return array {
* A configurable setting.
*
@ -367,41 +367,41 @@ abstract class Engine {
* @since 2.5.11 Broken due to accidental removal of `esc_attr()` call.
* @since 2.6.1 Restore `esc_attr()` call.
*/
protected function load_setting( string $p_str_setting_key_name ): array {
protected function load_setting( string $setting_key_name ): array {
// Get current section.
reset( $this->a_arr_sections );
$p_arr_return = array();
$p_arr_return['id'] = $p_str_setting_key_name;
$p_arr_return['name'] = $p_str_setting_key_name;
$p_arr_return['value'] = esc_attr( Includes\Settings::instance()->get( $p_str_setting_key_name ) );
return $p_arr_return;
reset( $this->sections );
$return = array();
$return['id'] = $setting_key_name;
$return['name'] = $setting_key_name;
$return['value'] = esc_attr( Includes\Settings::instance()->get( $setting_key_name ) );
return $return;
}
/**
* Returns a simple text inside a 'span' element.
*
* @access protected
* @param string $p_str_text Message to be surrounded with `<span>` tags.
* @param string $text Message to be surrounded with `<span>` tags.
*
* @since 1.5.0
* @todo Refactor HTML generation.
*/
protected function add_text( string $p_str_text ): string {
return sprintf( '<span>%s</span>', $p_str_text );
protected function add_text( string $text ): string {
return sprintf( '<span>%s</span>', $text );
}
/**
* Returns the HTML tag for a 'label' element.
*
* @access protected
* @param string $p_str_setting_name Settings key.
* @param string $p_str_caption Label caption.
* @param string $setting_name Settings key.
* @param string $caption Label caption.
*
* @since 1.5.0
* @todo Refactor HTML generation.
*/
protected function add_label( string $p_str_setting_name, string $p_str_caption ): string {
if ( empty( $p_str_caption ) ) {
protected function add_label( string $setting_name, string $caption ): string {
if ( empty( $caption ) ) {
return '';
}
@ -416,36 +416,36 @@ abstract class Engine {
* {@link https://softwareengineering.stackexchange.com/questions/234546/colons-in-internationalized-ui
* style guides}.
*/
return sprintf( '<label for="%s">%s</label>', $p_str_setting_name, $p_str_caption );
return sprintf( '<label for="%s">%s</label>', $setting_name, $caption );
}
/**
* Constructs the HTML for a text 'input' element.
*
* @access protected
* @param string $p_str_setting_name Setting key.
* @param int $p_str_max_length Maximum length of the input. Default length 999 chars.
* @param bool $p_bool_readonly Set the input to be read only. Default `false`.
* @param bool $p_bool_hidden Set the input to be hidden. Default `false`.
* @param string $setting_name Setting key.
* @param int $max_length Maximum length of the input. Default length 999 chars.
* @param bool $readonly Set the input to be read only. Default `false`.
* @param bool $hidden Set the input to be hidden. Default `false`.
*
* @since 1.5.0
* @todo Refactor HTML generation.
*/
protected function add_text_box( string $p_str_setting_name, int $p_str_max_length = 999, bool $p_bool_readonly = false, bool $p_bool_hidden = false ): string {
$l_str_style = '';
protected function add_text_box( string $setting_name, int $max_length = 999, bool $readonly = false, bool $hidden = false ): string {
$style = '';
// Collect data for given settings field.
$l_arr_data = $this->load_setting( $p_str_setting_name );
if ( $p_bool_hidden ) {
$l_str_style .= 'display:none;';
$data = $this->load_setting( $setting_name );
if ( $hidden ) {
$style .= 'display:none;';
}
return sprintf(
'<input type="text" name="%s" id="%s" maxlength="%d" style="%s" value="%s" %s/>',
$l_arr_data['name'],
$l_arr_data['id'],
$p_str_max_length,
$l_str_style,
$l_arr_data['value'],
$p_bool_readonly ? 'readonly="readonly"' : ''
$data['name'],
$data['id'],
$max_length,
$style,
$data['value'],
$readonly ? 'readonly="readonly"' : ''
);
}
@ -453,19 +453,19 @@ abstract class Engine {
* Constructs the HTML for a checkbox 'input' element.
*
* @access protected
* @param string $p_str_setting_name Setting key.
* @param string $setting_name Setting key.
*
* @since 1.5.0
* @todo Refactor HTML generation.
*/
protected function add_checkbox( string $p_str_setting_name ): string {
protected function add_checkbox( string $setting_name ): string {
// Collect data for given settings field.
$l_arr_data = $this->load_setting( $p_str_setting_name );
$data = $this->load_setting( $setting_name );
return sprintf(
'<input type="checkbox" name="%s" id="%s" %s/>',
$l_arr_data['name'],
$l_arr_data['id'],
Includes\Convert::to_bool( $l_arr_data['value'] ) ? 'checked="checked"' : ''
$data['name'],
$data['id'],
Includes\Convert::to_bool( $data['value'] ) ? 'checked="checked"' : ''
);
}
@ -473,34 +473,34 @@ abstract class Engine {
* Constructs the HTML for a 'select' element.
*
* @access protected
* @param string $p_str_setting_name Setting key.
* @param array $p_arr_options Possible options.
* @param string $setting_name Setting key.
* @param array $options Possible options.
*
* @since 1.5.0
* @todo Refactor HTML generation.
*/
protected function add_select_box( string $p_str_setting_name, array $p_arr_options ): string {
protected function add_select_box( string $setting_name, array $options ): string {
// Collect data for given settings field.
$l_arr_data = $this->load_setting( $p_str_setting_name );
$l_str_options = '';
$data = $this->load_setting( $setting_name );
$options = '';
// Loop through all array keys.
foreach ( $p_arr_options as $l_str_value => $l_str_caption ) {
$l_str_options .= sprintf(
foreach ( $options as $value => $caption ) {
$options .= sprintf(
'<option value="%s" %s>%s</option>',
$l_str_value,
$value,
// Only check for equality, not identity, WRT backlink symbol arrows.
// phpcs:disable WordPress.PHP.StrictComparisons.LooseComparison
$l_str_value == $l_arr_data['value'] ? 'selected' : '',
$value == $data['value'] ? 'selected' : '',
// phpcs:enable WordPress.PHP.StrictComparisons.LooseComparison
$l_str_caption
$caption
);
}
return sprintf(
'<select name="%s" id="%s">%s</select>',
$l_arr_data['name'],
$l_arr_data['id'],
$l_str_options
$data['name'],
$data['id'],
$options
);
}
@ -508,19 +508,19 @@ abstract class Engine {
* Constructs the HTML for a 'textarea' element.
*
* @access protected
* @param string $p_str_setting_name Setting key.
* @param string $setting_name Setting key.
*
* @since 1.5.0
* @todo Refactor HTML generation.
*/
protected function add_textarea( $p_str_setting_name ): string {
protected function add_textarea( $setting_name ): string {
// Collect data for given settings field.
$l_arr_data = $this->load_setting( $p_str_setting_name );
$data = $this->load_setting( $setting_name );
return sprintf(
'<textarea name="%s" id="%s">%s</textarea>',
$l_arr_data['name'],
$l_arr_data['id'],
$l_arr_data['value']
$data['name'],
$data['id'],
$data['value']
);
}
@ -529,20 +529,20 @@ abstract class Engine {
* class.
*
* @access protected
* @param string $p_str_setting_name Setting key.
* @param string $setting_name Setting key.
*
* @since 1.5.6
* @todo Refactor HTML generation.
* @todo Use proper colorpicker element.
*/
protected function add_color_selection( string $p_str_setting_name ): string {
protected function add_color_selection( string $setting_name ): string {
// Collect data for given settings field.
$l_arr_data = $this->load_setting( $p_str_setting_name );
$data = $this->load_setting( $setting_name );
return sprintf(
'<input type="text" name="%s" id="%s" class="footnotes-color-picker" value="%s"/>',
$l_arr_data['name'],
$l_arr_data['id'],
$l_arr_data['value']
$data['name'],
$data['id'],
$data['value']
);
}
@ -550,36 +550,36 @@ abstract class Engine {
* Constructs the HTML for numeric 'input' element.
*
* @access protected
* @param string $p_str_setting_name Setting key.
* @param string $setting_name Setting key.
* @param int $p_in_min Minimum value.
* @param int $p_int_max Maximum value.
* @param bool $p_bool_deci `true` if float, `false` if integer. Default `false`.
* @param int $max Maximum value.
* @param bool $deci `true` if float, `false` if integer. Default `false`.
*
* @since 1.5.0
* @todo Refactor HTML generation.
*/
protected function add_num_box( string $p_str_setting_name, int $p_in_min, int $p_int_max, bool $p_bool_deci = false ): string {
protected function add_num_box( string $setting_name, int $p_in_min, int $max, bool $deci = false ): string {
// Collect data for given settings field.
$l_arr_data = $this->load_setting( $p_str_setting_name );
$data = $this->load_setting( $setting_name );
if ( $p_bool_deci ) {
$l_str_value = number_format( floatval( $l_arr_data['value'] ), 1 );
if ( $deci ) {
$value = number_format( floatval( $data['value'] ), 1 );
return sprintf(
'<input type="number" name="%s" id="%s" value="%s" step="0.1" min="%d" max="%d"/>',
$l_arr_data['name'],
$l_arr_data['id'],
$l_str_value,
$data['name'],
$data['id'],
$value,
$p_in_min,
$p_int_max
$max
);
}
return sprintf(
'<input type="number" name="%s" id="%s" value="%d" min="%d" max="%d"/>',
$l_arr_data['name'],
$l_arr_data['id'],
$l_arr_data['value'],
$data['name'],
$data['id'],
$data['value'],
$p_in_min,
$p_int_max
$max
);
}

View file

@ -34,7 +34,7 @@ class Init {
*
* @since 1.5.0
*/
const C_STR_MAIN_MENU_SLUG = 'footnotes';
const MAIN_MENU_SLUG = 'footnotes';
/**
* Contains the settings page.
@ -136,9 +136,9 @@ class Init {
add_submenu_page(
'options-general.php',
'footnotes Settings',
\footnotes\includes\Config::C_STR_PLUGIN_PUBLIC_NAME,
\footnotes\includes\Config::PLUGIN_PUBLIC_NAME,
'manage_options',
self::C_STR_MAIN_MENU_SLUG,
self::MAIN_MENU_SLUG,
fn() => $this->settings_page->display_content()
);
$this->settings_page->register_sub_page();
@ -151,58 +151,58 @@ class Init {
* @since 1.5.0
*/
public function get_plugin_meta_information(): void {
$l_str_plugin_name = null;
$plugin_name = null;
// TODO: add nonce verification?
// Get plugin internal name from POST data.
if ( isset( $_POST['plugin'] ) ) {
$l_str_plugin_name = wp_unslash( $_POST['plugin'] );
$plugin_name = wp_unslash( $_POST['plugin'] );
}
if ( empty( $l_str_plugin_name ) ) {
if ( empty( $plugin_name ) ) {
echo wp_json_encode( array( 'error' => 'Plugin name invalid.' ) );
exit;
}
$l_str_url = 'https://api.wordpress.org/plugins/info/1.0/' . $l_str_plugin_name . '.json';
$url = 'https://api.wordpress.org/plugins/info/1.0/' . $plugin_name . '.json';
// Call URL and collect data.
$l_arr_response = wp_remote_get( $l_str_url );
$response = wp_remote_get( $url );
// Check if response is valid.
if ( is_wp_error( $l_arr_response ) ) {
if ( is_wp_error( $response ) ) {
echo wp_json_encode( array( 'error' => 'Error receiving Plugin Information from WordPress.' ) );
exit;
}
if ( ! array_key_exists( 'body', $l_arr_response ) ) {
if ( ! array_key_exists( 'body', $response ) ) {
echo wp_json_encode( array( 'error' => 'Error reading WordPress API response message.' ) );
exit;
}
// Get the body of the response.
$l_str_response = $l_arr_response['body'];
$response = $response['body'];
// Get plugin object.
$l_arr_plugin = json_decode( $l_str_response, true, 512, JSON_THROW_ON_ERROR );
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 ) );
$plugin = json_decode( $response, true, 512, JSON_THROW_ON_ERROR );
if ( empty( $plugin ) ) {
echo wp_json_encode( array( 'error' => 'Error reading Plugin meta information.<br/>URL: ' . $url . '<br/>Response: ' . $response ) );
exit;
}
$l_int_num_ratings = array_key_exists( 'num_ratings', $l_arr_plugin ) ? (int) $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 );
$num_ratings = array_key_exists( 'num_ratings', $plugin ) ? (int) $plugin['num_ratings'] : 0;
$rating = array_key_exists( 'rating', $plugin ) ? floatval( $plugin['rating'] ) : 0.0;
$stars = round( 5 * $rating / 100.0, 1 );
// Return Plugin information as JSON encoded string.
echo wp_json_encode(
array(
'error' => '',
'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'] : '---',
'PluginDescription' => array_key_exists( 'short_description', $plugin ) ? html_entity_decode( $plugin['short_description'] ) : 'Error reading Plugin information',
'PluginAuthor' => array_key_exists( 'author', $plugin ) ? html_entity_decode( $plugin['author'] ) : 'unknown',
'PluginRatingText' => $stars . ' ' . __( 'rating based on', 'footnotes' ) . ' ' . $num_ratings . ' ' . __( 'ratings', 'footnotes' ),
'PluginRating1' => $stars >= 0.5 ? 'star-full' : 'star-empty',
'PluginRating2' => $stars >= 1.5 ? 'star-full' : 'star-empty',
'PluginRating3' => $stars >= 2.5 ? 'star-full' : 'star-empty',
'PluginRating4' => $stars >= 3.5 ? 'star-full' : 'star-empty',
'PluginRating5' => $stars >= 4.5 ? 'star-full' : 'star-empty',
'PluginRating' => $num_ratings,
'PluginLastUpdated' => array_key_exists( 'last_updated', $plugin ) ? $plugin['last_updated'] : 'unknown',
'PluginDownloads' => array_key_exists( 'downloaded', $plugin ) ? $plugin['downloaded'] : '---',
)
);
exit;

File diff suppressed because it is too large Load diff