Update layout files

This commit is contained in:
Ben Goldsworthy 2021-02-23 16:48:53 +00:00
parent 8ff0d9ee86
commit df6bd2c282
4 changed files with 1125 additions and 1105 deletions

View file

@ -1,12 +1,10 @@
<?php <?php // phpcs:disable WordPress.Files.FileName.InvalidClassFileName
/** /**
* Includes Layout Engine for the admin dashboard. * Includes Layout Engine for the admin dashboard.
* *
* @filesource * @filesource
* @author Stefan Herndler * @package footnotes
* @since 1.5.0 12.09.14 10:56 * @since 1.5.0 12.09.14 10:56
*
* @lastmodified 2021-02-18T2021+0100
* *
* @since 2.1.2 add versioning of settings.css for cache busting 2020-11-19T1456+0100 * @since 2.1.2 add versioning of settings.css for cache busting 2020-11-19T1456+0100
* @since 2.1.4 automate passing version number for cache busting 2020-11-30T0648+0100 * @since 2.1.4 automate passing version number for cache busting 2020-11-30T0648+0100
@ -16,182 +14,170 @@
* @since 2.5.5 Bugfix: Stylesheets: minify to shrink the carbon footprint, increase speed and implement best practice, thanks to @docteurfitness issue report. * @since 2.5.5 Bugfix: Stylesheets: minify to shrink the carbon footprint, increase speed and implement best practice, thanks to @docteurfitness issue report.
*/ */
/** /**
* Layout Engine for the administration dashboard. * Layout Engine for the administration dashboard.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
*/ */
abstract class MCI_Footnotes_LayoutEngine { abstract class MCI_Footnotes_Layout_Engine {
/** /**
* Stores the Hook connection string for the child sub page. * Stores the Hook connection string for the child sub page.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
* @var null|string * @var null|string
*/ */
protected $a_str_SubPageHook = null; protected $a_str_sub_page_hook = null;
/** /**
* Stores all Sections for the child sub page. * Stores all Sections for the child sub page.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
* @var array * @var array
*/ */
protected $a_arr_Sections = array(); protected $a_arr_sections = array();
/** /**
* Returns a Priority index. Lower numbers have a higher Priority. * Returns a Priority index. Lower numbers have a higher Priority.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
* @return int * @return int
*/ */
abstract public function getPriority(); abstract public function get_priority();
/** /**
* Returns the unique slug of the child sub page. * Returns the unique slug of the child sub page.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
* @return string * @return string
*/ */
abstract protected function getSubPageSlug(); abstract protected function get_sub_page_slug();
/** /**
* Returns the title of the child sub page. * Returns the title of the child sub page.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
* @return string * @return string
*/ */
abstract protected function getSubPageTitle(); abstract protected function get_sub_page_title();
/** /**
* Returns an array of all registered sections for a sub page. * Returns an array of all registered sections for a sub page.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
* @return array * @return array
*/ */
abstract protected function getSections(); abstract protected function get_sections();
/** /**
* Returns an array of all registered meta boxes. * Returns an array of all registered meta boxes.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
* @return array * @return array
*/ */
abstract protected function getMetaBoxes(); abstract protected function get_meta_boxes();
/** /**
* Returns an array describing a sub page section. * Returns an array describing a sub page section.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
* @param string $p_str_ID Unique ID suffix. * @param string $p_str_id Unique ID suffix.
* @param string $p_str_Title Title of the section. * @param string $p_str_title Title of the section.
* @param int $p_int_SettingsContainerIndex Settings Container Index. * @param int $p_int_settings_container_index Settings Container Index.
* @param bool $p_bool_hasSubmitButton Should a Submit Button be displayed for this section, default: true. * @param bool $p_bool_has_submit_button Should a Submit Button be displayed for this section, default: true.
* @return array Array describing the section. * @return array Array describing the section.
*/ */
protected function addSection($p_str_ID, $p_str_Title, $p_int_SettingsContainerIndex, $p_bool_hasSubmitButton = true) { protected function add_section( $p_str_id, $p_str_title, $p_int_settings_container_index, $p_bool_has_submit_button = true ) {
return array("id" => MCI_Footnotes_Config::C_STR_PLUGIN_NAME . "-" . $p_str_ID, "title" => $p_str_Title, "submit" => $p_bool_hasSubmitButton, "container" => $p_int_SettingsContainerIndex); return array(
'id' => MCI_Footnotes_Config::C_STR_PLUGIN_NAME . '-' . $p_str_id,
'title' => $p_str_title,
'submit' => $p_bool_has_submit_button,
'container' => $p_int_settings_container_index,
);
} }
/** /**
* Returns an array describing a meta box. * Returns an array describing a meta box.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
* @param string $p_str_SectionID Parent Section ID. * @param string $p_str_section_id Parent Section ID.
* @param string $p_str_ID Unique ID suffix. * @param string $p_str_id Unique ID suffix.
* @param string $p_str_Title Title for the meta box. * @param string $p_str_title Title for the meta box.
* @param string $p_str_CallbackFunctionName Class method name for callback. * @param string $p_str_callback_function_name Class method name for callback.
* @return array meta box description to be able to append a meta box to the output. * @return array meta box description to be able to append a meta box to the output.
*/ */
protected function addMetaBox($p_str_SectionID, $p_str_ID, $p_str_Title, $p_str_CallbackFunctionName) { protected function add_meta_box( $p_str_section_id, $p_str_id, $p_str_title, $p_str_callback_function_name ) {
return array( return array(
"parent" => MCI_Footnotes_Config::C_STR_PLUGIN_NAME . "-" . $p_str_SectionID, 'parent' => MCI_Footnotes_Config::C_STR_PLUGIN_NAME . '-' . $p_str_section_id,
"id" => $p_str_ID, 'id' => $p_str_id,
"title" => $p_str_Title, 'title' => $p_str_title,
"callback" => $p_str_CallbackFunctionName 'callback' => $p_str_callback_function_name,
); );
} }
/** /**
* Registers a sub page. * Registers a sub page.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
*/ */
public function registerSubPage() { public function register_sub_page() {
global $submenu; global $submenu;
// any sub menu for our main menu exists
if (array_key_exists(plugin_basename(MCI_Footnotes_Layout_Init::C_STR_MAIN_MENU_SLUG), $submenu)) { if ( array_key_exists( plugin_basename( MCI_Footnotes_Layout_Init::C_STR_MAIN_MENU_SLUG ), $submenu ) ) {
// iterate through all sub menu entries of the ManFisher main menu foreach ( $submenu[ plugin_basename( MCI_Footnotes_Layout_Init::C_STR_MAIN_MENU_SLUG ) ] as $l_arr_sub_menu ) {
foreach($submenu[plugin_basename(MCI_Footnotes_Layout_Init::C_STR_MAIN_MENU_SLUG)] as $l_arr_SubMenu) { if ( plugin_basename( MCI_Footnotes_Layout_Init::C_STR_MAIN_MENU_SLUG . $this->get_sub_page_slug() ) === $l_arr_sub_menu[2] ) {
if ($l_arr_SubMenu[2] == plugin_basename(MCI_Footnotes_Layout_Init::C_STR_MAIN_MENU_SLUG . $this->getSubPageSlug())) { remove_submenu_page( MCI_Footnotes_Layout_Init::C_STR_MAIN_MENU_SLUG, MCI_Footnotes_Layout_Init::C_STR_MAIN_MENU_SLUG . $this->get_sub_page_slug() );
// remove that sub menu and add it again to move it to the bottom
remove_submenu_page(MCI_Footnotes_Layout_Init::C_STR_MAIN_MENU_SLUG, MCI_Footnotes_Layout_Init::C_STR_MAIN_MENU_SLUG .$this->getSubPageSlug());
} }
} }
} }
$this->a_str_SubPageHook = add_submenu_page( $this->a_str_sub_page_hook = add_submenu_page(
MCI_Footnotes_Layout_Init::C_STR_MAIN_MENU_SLUG, // parent slug MCI_Footnotes_Layout_Init::C_STR_MAIN_MENU_SLUG,
$this->getSubPageTitle(), // page title $this->get_sub_page_title(),
$this->getSubPageTitle(), // menu title $this->get_sub_page_title(),
'manage_options', // capability 'manage_options',
MCI_Footnotes_Layout_Init::C_STR_MAIN_MENU_SLUG . $this->getSubPageSlug(), // menu slug MCI_Footnotes_Layout_Init::C_STR_MAIN_MENU_SLUG . $this->get_sub_page_slug(),
array($this, 'displayContent') // function array( $this, 'display_content' )
); );
} }
/** /**
* Registers all sections for a sub page. * Registers all sections for a sub page.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
*/ */
public function registerSections() { public function register_sections() {
// iterate through each section foreach ( $this->get_sections() as $l_arr_section ) {
foreach($this->getSections() as $l_arr_Section) { // Append tab to the tab-array.
// append tab to the tab-array $this->a_arr_sections[ $l_arr_section['id'] ] = $l_arr_section;
$this->a_arr_Sections[$l_arr_Section["id"]] = $l_arr_Section;
add_settings_section( add_settings_section(
$l_arr_Section["id"], // unique id $l_arr_section['id'],
"", //$l_arr_Section["title"], // title '',
array($this, 'Description'), // callback function for the description array( $this, 'Description' ),
$l_arr_Section["id"] // parent sub page slug $l_arr_section['id']
); );
$this->registerMetaBoxes($l_arr_Section["id"]); $this->register_meta_boxes( $l_arr_section['id'] );
} }
} }
/** /**
* Registers all Meta boxes for a sub page. * Registers all Meta boxes for a sub page.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
* @param string $p_str_ParentID Parent section unique id. * @param string $p_str_parent_id Parent section unique id.
*/ */
private function registerMetaBoxes($p_str_ParentID) { private function register_meta_boxes( $p_str_parent_id ) {
// iterate through each meta box // Iterate through each meta box.
foreach($this->getMetaBoxes() as $l_arr_MetaBox) { foreach ( $this->get_meta_boxes() as $l_arr_meta_box ) {
if ($l_arr_MetaBox["parent"] != $p_str_ParentID) { if ( $p_str_parent_id !== $l_arr_meta_box['parent'] ) {
continue; continue;
} }
add_meta_box( add_meta_box(
$p_str_ParentID. "-" . $l_arr_MetaBox["id"], // unique id $p_str_parent_id . '-' . $l_arr_meta_box['id'],
$l_arr_MetaBox["title"], // meta box title $l_arr_meta_box['title'],
array($this, $l_arr_MetaBox["callback"]), // callback function to display (echo) the content array( $this, $l_arr_meta_box['callback'] ),
$p_str_ParentID, // post type = parent section id $p_str_parent_id,
'main' // context 'main'
); );
} }
} }
@ -199,17 +185,12 @@ abstract class MCI_Footnotes_LayoutEngine {
/** /**
* Append javascript and css files for specific sub page. * Append javascript and css files for specific sub page.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
*/ */
private function appendScripts() { private function append_scripts() {
// enable meta boxes layout and close functionality wp_enqueue_script( 'postbox' );
wp_enqueue_script('postbox'); wp_enqueue_style( 'wp-color-picker' );
// add WordPress color picker layout wp_enqueue_script( 'wp-color-picker' );
wp_enqueue_style('wp-color-picker');
// add WordPress color picker function
wp_enqueue_script('wp-color-picker');
/** /**
* Registers and enqueues the dashboard stylesheet. * Registers and enqueues the dashboard stylesheet.
@ -239,314 +220,337 @@ abstract class MCI_Footnotes_LayoutEngine {
} }
wp_enqueue_style('mci-footnotes-admin'); wp_enqueue_style( 'mci-footnotes-admin' );
} }
/** /**
* Displays the content of specific sub page. * Displays the content of specific sub page.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
*/ */
public function displayContent() { public function display_content() {
// register and enqueue scripts and styling $this->append_scripts();
$this->appendScripts();
// get current section // TODO: add nonce verification.
reset($this->a_arr_Sections);
$l_str_ActiveSectionID = isset($_GET['t']) ? $_GET['t'] : key($this->a_arr_Sections); // Get the current section.
$l_arr_ActiveSection = $this->a_arr_Sections[$l_str_ActiveSectionID]; reset( $this->a_arr_sections );
// store settings $l_str_active_section_id = isset( $_GET['t'] ) ? sanitize_option( wp_unslash( $_GET['t'] ) ) : key( $this->a_arr_sections );
$l_bool_SettingsUpdated = false; $l_arr_active_section = $this->a_arr_sections[ $l_str_active_section_id ];
if (array_key_exists("save-settings", $_POST)) {
if ($_POST["save-settings"] == "save") { // Store settings.
unset($_POST["save-settings"]); $l_bool_settings_updated = false;
unset($_POST["submit"]); if ( array_key_exists( 'save-settings', $_POST ) ) {
$l_bool_SettingsUpdated = $this->saveSettings(); if ( 'save' === $_POST['save-settings'] ) {
unset( $_POST['save-settings'] );
unset( $_POST['submit'] );
$l_bool_settings_updated = $this->save_settings();
} }
} }
// display all sections and highlight the active section // Display all sections and highlight the active section.
echo '<div class="wrap">'; echo '<div class="wrap">';
echo '<h2 class="nav-tab-wrapper">'; echo '<h2 class="nav-tab-wrapper">';
// iterate through all register sections // Iterate through all register sections.
foreach ($this->a_arr_Sections as $l_str_ID => $l_arr_Description) { foreach ( $this->a_arr_sections as $l_str_id => $l_arr_description ) {
$l_str_tab_active = ( $l_str_id === $l_arr_active_section['id'] ) ? ' nav-tab-active' : '';
echo sprintf( echo sprintf(
'<a class="nav-tab%s" href="?page=%s&t=%s">%s</a>', '<a class="nav-tab%s" href="?page=%s&t=%s">%s</a>',
$l_arr_ActiveSection["id"] == $l_str_ID ? ' nav-tab-active' : '', esc_attr( ( $l_str_id === $l_arr_active_section['id'] ) ? ' nav-tab-active' : '' ),
MCI_Footnotes_Layout_Init::C_STR_MAIN_MENU_SLUG . $this->getSubPageSlug(), $l_str_ID, $l_arr_Description["title"] esc_attr( MCI_Footnotes_Layout_Init::C_STR_MAIN_MENU_SLUG . $this->get_sub_page_slug() ),
esc_attr( $l_str_id ),
esc_attr( $l_arr_description['title'] )
); );
} }
echo '</h2><br/>'; echo '</h2><br/>';
if ($l_bool_SettingsUpdated) { if ( $l_bool_settings_updated ) {
echo sprintf('<div id="message" class="updated">%s</div>', __("Settings saved", MCI_Footnotes_Config::C_STR_PLUGIN_NAME)); echo sprintf( '<div id="message" class="updated">%s</div>', esc_html( __( 'Settings saved', 'footnotes' ) ) );
} }
// form to submit the active section // Form to submit the active section.
echo '<!--suppress HtmlUnknownTarget --><form method="post" action="">'; echo '<!--suppress HtmlUnknownTarget --><form method="post" action="">';
//settings_fields($l_arr_ActiveSection["container"]);
echo '<input type="hidden" name="save-settings" value="save" />'; echo '<input type="hidden" name="save-settings" value="save" />';
// outputs the settings field of the active section // Outputs the settings field of the active section.
do_settings_sections($l_arr_ActiveSection["id"]); do_settings_sections( $l_arr_active_section['id'] );
do_meta_boxes($l_arr_ActiveSection["id"], 'main', NULL); do_meta_boxes( $l_arr_active_section['id'], 'main', null );
// add submit button to active section if defined // Add submit button to active section if defined.
if ($l_arr_ActiveSection["submit"]) { if ( $l_arr_active_section['submit'] ) {
submit_button(); submit_button();
} }
// close the form to submit data
echo '</form>'; echo '</form>';
// close container for the settings page
echo '</div>'; echo '</div>';
// output special javascript for the expand/collapse function of the meta boxes
// Echo JavaScript for the expand/collapse function of the meta boxes.
echo '<script type="text/javascript">'; echo '<script type="text/javascript">';
echo "jQuery(document).ready(function ($) {"; echo 'jQuery(document).ready(function ($) {';
echo 'jQuery(".mfmmf-color-picker").wpColorPicker();'; echo 'jQuery(".mfmmf-color-picker").wpColorPicker();';
echo "jQuery('.if-js-closed').removeClass('if-js-closed').addClass('closed');"; echo "jQuery('.if-js-closed').removeClass('if-js-closed').addClass('closed');";
echo "postboxes.add_postbox_toggles('" . $this->a_str_SubPageHook . "');"; echo "postboxes.add_postbox_toggles('" . esc_js( $this->a_str_sub_page_hook ) . "');";
echo "});"; echo '});';
echo '</script>'; echo '</script>';
} }
/** /**
* Save all Plugin settings. * Save all Plugin settings.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
* @return bool * @return bool
*/ */
private function saveSettings() { private function save_settings() {
$l_arr_newSettings = array(); $l_arr_new_settings = array();
// get current section
reset($this->a_arr_Sections);
$l_str_ActiveSectionID = isset($_GET['t']) ? $_GET['t'] : key($this->a_arr_Sections);
$l_arr_ActiveSection = $this->a_arr_Sections[$l_str_ActiveSectionID];
// iterate through each value that has to be in the specific container // TODO: add nonce verification.
foreach(MCI_Footnotes_Settings::instance()->getDefaults($l_arr_ActiveSection["container"]) as $l_str_Key => $l_mixed_Value) {
// setting is available in the POST array, use it // Get current section.
if (array_key_exists($l_str_Key, $_POST)) { reset( $this->a_arr_sections );
$l_arr_newSettings[$l_str_Key] = $_POST[$l_str_Key]; $l_str_active_section_id = isset( $_GET['t'] ) ? sanitize_option( wp_unslash( $_GET['t'] ) ) : key( $this->a_arr_sections );
$l_arr_active_section = $this->a_arr_sections[ $l_str_active_section_id ];
foreach ( MCI_Footnotes_Settings::instance()->get_defaults( $l_arr_active_section['container'] ) as $l_str_key => $l_mixed_value ) {
if ( array_key_exists( $l_str_key, $_POST ) ) {
$l_arr_new_settings[ $l_str_key ] = sanitize_option( wp_unslash( $_POST[ $l_str_key ] ) );
} else { } else {
// setting is not defined in the POST array, define it to avoid the Default value // Setting is not defined in the POST array, define it to avoid the Default value.
$l_arr_newSettings[$l_str_Key] = ""; $l_arr_new_settings[ $l_str_key ] = '';
} }
} }
// update settings // Update settings.
return MCI_Footnotes_Settings::instance()->saveOptions($l_arr_ActiveSection["container"], $l_arr_newSettings); return MCI_Footnotes_Settings::instance()->save_options( $l_arr_active_section['container'], $l_arr_new_settings );
} }
/** /**
* Output the Description of a section. May be overwritten in any section. * Output the Description of a section. May be overwritten in any section.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
*/ */
public function Description() { public function description() {
// default no description will be displayed // Default no description will be displayed.
} }
/** /**
* Loads specific setting and returns an array with the keys [id, name, value]. * Loads specific setting and returns an array with the keys [id, name, value].
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
* @param string $p_str_SettingKeyName Settings Array key name. * @param string $p_str_setting_key_name Settings Array key name.
* @return array Contains Settings ID, Settings Name and Settings Value. * @return array Contains Settings ID, Settings Name and Settings Value.
*/ */
protected function LoadSetting($p_str_SettingKeyName) { protected function load_setting( $p_str_setting_key_name ) {
// get current section // Get current section.
reset($this->a_arr_Sections); reset( $this->a_arr_sections );
$p_arr_Return = array(); $p_arr_return = array();
$p_arr_Return["id"] = sprintf('%s', $p_str_SettingKeyName); $p_arr_return['id'] = sprintf( '%s', $p_str_setting_key_name );
$p_arr_Return["name"] = sprintf('%s', $p_str_SettingKeyName); $p_arr_return['name'] = sprintf( '%s', $p_str_setting_key_name );
$p_arr_Return["value"] = esc_attr(MCI_Footnotes_Settings::instance()->get($p_str_SettingKeyName)); $p_arr_return['value'] = esc_attr( MCI_Footnotes_Settings::instance()->get( $p_str_setting_key_name ) );
return $p_arr_Return; return $p_arr_return;
} }
/** /**
* Returns a line break to start a new line. * Returns a line break to start a new line.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
* @return string * @return string
*/ */
protected function addNewline() { protected function add_newline() {
return '<br/>'; return '<br/>';
} }
/** /**
* Returns a line break to have a space between two lines. * Returns a line break to have a space between two lines.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
* @return string * @return string
*/ */
protected function addLineSpace() { protected function add_line_space() {
return '<br/><br/>'; return '<br/><br/>';
} }
/** /**
* Returns a simple text inside html <span> text. * Returns a simple text inside html <span> text.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
* @param string $p_str_Text Message to be surrounded with simple html tag (span). * @param string $p_str_text Message to be surrounded with simple html tag (span).
* @return string * @return string
*/ */
protected function addText($p_str_Text) { protected function add_text( $p_str_text ) {
return sprintf('<span>%s</span>', $p_str_Text); return sprintf( '<span>%s</span>', $p_str_text );
} }
/** /**
* Returns the html tag for an input/select label. * Returns the html tag for an input/select label.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
* @param string $p_str_SettingName Name of the Settings key to connect the Label with the input/select field. * @param string $p_str_setting_name Name of the Settings key to connect the Label with the input/select field.
* @param string $p_str_Caption Label caption. * @param string $p_str_caption Label caption.
* @return string * @return string
*
* Edited 2020-12-01T0159+0100..
* @since 2.1.6 no colon
*/ */
protected function addLabel($p_str_SettingName, $p_str_Caption) { protected function add_label( $p_str_setting_name, $p_str_caption ) {
if (empty($p_str_Caption)) { if ( empty( $p_str_caption ) ) {
return ""; return '';
} }
// remove the colon causing localization issues with French,
// and with languages not using punctuation at all, /*
// and with languages using other punctuation marks instead of colon, * Remove the colon causing localization issues with French, and with
// e.g. Greek using a raised dot. * languages not using punctuation at all, and with languages using other
// In French, colon is preceded by a space, forcibly non-breaking, * punctuation marks instead of colon, e.g. Greek using a raised dot.
// and narrow per new school. * In French, colon is preceded by a space, forcibly non-breaking, and
// Add colon to label strings for inclusion in localization. * narrow per new school.
// Colon after label is widely preferred best practice, mandatory per style guides. * Add colon to label strings for inclusion in localization. Colon after
// <https://softwareengineering.stackexchange.com/questions/234546/colons-in-internationalized-ui> * label is widely preferred best practice, mandatory per
return sprintf('<label for="%s">%s</label>', $p_str_SettingName, $p_str_Caption); * [style guides](https://softwareengineering.stackexchange.com/questions/234546/colons-in-internationalized-ui).
// ^ here deleted colon 2020-12-08T1546+0100 */
return sprintf( '<label for="%s">%s</label>', $p_str_setting_name, $p_str_caption );
} }
/** /**
* Returns the html tag for an input [type = text]. * Returns the html tag for an input [type = text].
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
* @param string $p_str_SettingName Name of the Settings key to pre load the input field. * @param string $p_str_setting_name Name of the Settings key to pre load the input field.
* @param int $p_str_MaxLength Maximum length of the input, default 999 characters. * @param int $p_str_max_length Maximum length of the input, default 999 characters.
* @param bool $p_bool_Readonly Set the input to be read only, default false. * @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 bool $p_bool_hidden Set the input to be hidden, default false.
* @return string * @return string
*/ */
protected function addTextBox($p_str_SettingName, $p_str_MaxLength = 999, $p_bool_Readonly = false, $p_bool_Hidden = false) { protected function add_text_box( $p_str_setting_name, $p_str_max_length = 999, $p_bool_readonly = false, $p_bool_hidden = false ) {
$l_str_Style = ""; $l_str_style = '';
// collect data for given settings field // Collect data for given settings field.
$l_arr_Data = $this->LoadSetting($p_str_SettingName); $l_arr_data = $this->load_setting( $p_str_setting_name );
if ($p_bool_Hidden) { if ( $p_bool_hidden ) {
$l_str_Style .= 'display:none;'; $l_str_style .= 'display:none;';
} }
return sprintf('<input type="text" name="%s" id="%s" maxlength="%d" style="%s" value="%s" %s/>', return sprintf(
$l_arr_Data["name"], $l_arr_Data["id"], $p_str_MaxLength, '<input type="text" name="%s" id="%s" maxlength="%d" style="%s" value="%s" %s/>',
$l_str_Style, $l_arr_Data["value"], $p_bool_Readonly ? 'readonly="readonly"' : ''); $l_arr_data['name'],
$l_arr_data['id'],
$p_str_max_length,
$l_str_style,
$l_arr_data['value'],
$p_bool_readonly ? 'readonly="readonly"' : ''
);
} }
/** /**
* Returns the html tag for an input [type = checkbox]. * Returns the html tag for an input [type = checkbox].
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
* @param string $p_str_SettingName Name of the Settings key to pre load the input field. * @param string $p_str_setting_name Name of the Settings key to pre load the input field.
* @return string * @return string
*/ */
protected function addCheckbox($p_str_SettingName) { protected function add_checkbox( $p_str_setting_name ) {
// collect data for given settings field // Collect data for given settings field.
$l_arr_Data = $this->LoadSetting($p_str_SettingName); $l_arr_data = $this->load_setting( $p_str_setting_name );
return sprintf('<input type="checkbox" name="%s" id="%s" %s/>', return sprintf(
$l_arr_Data["name"], $l_arr_Data["id"], '<input type="checkbox" name="%s" id="%s" %s/>',
MCI_Footnotes_Convert::toBool($l_arr_Data["value"]) ? 'checked="checked"' : ''); $l_arr_data['name'],
$l_arr_data['id'],
MCI_Footnotes_Convert::to_bool( $l_arr_data['value'] ) ? 'checked="checked"' : ''
);
} }
/** /**
* Returns the html tag for a select box. * Returns the html tag for a select box.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
* @param string $p_str_SettingName Name of the Settings key to pre select the current value. * @param string $p_str_setting_name Name of the Settings key to pre select the current value.
* @param array $p_arr_Options Possible options to be selected. * @param array $p_arr_options Possible options to be selected.
* @return string * @return string
*/ */
protected function addSelectBox($p_str_SettingName, $p_arr_Options) { protected function add_select_box( $p_str_setting_name, $p_arr_options ) {
// collect data for given settings field // Collect data for given settings field.
$l_arr_Data = $this->LoadSetting($p_str_SettingName); $l_arr_data = $this->load_setting( $p_str_setting_name );
$l_str_Options = ""; $l_str_options = '';
/* loop through all array keys */ foreach ( $p_arr_options as $l_str_value => $l_str_caption ) {
foreach ($p_arr_Options as $l_str_Value => $l_str_Caption) { $l_str_options .= sprintf(
$l_str_Options .= sprintf('<option value="%s" %s>%s</option>', '<option value="%s" %s>%s</option>',
$l_str_Value, $l_str_value,
$l_arr_Data["value"] == $l_str_Value ? "selected" : "", $l_str_value === $l_arr_data['value'] ? 'selected' : '',
$l_str_Caption); $l_str_caption
);
} }
return sprintf('<select name="%s" id="%s">%s</select>', return sprintf(
$l_arr_Data["name"], $l_arr_Data["id"], $l_str_Options); '<select name="%s" id="%s">%s</select>',
$l_arr_data['name'],
$l_arr_data['id'],
$l_str_options
);
} }
/** /**
* Returns the html tag for a text area. * Returns the html tag for a text area.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
* @param string $p_str_SettingName Name of the Settings key to pre fill the text area. * @param string $p_str_setting_name Name of the Settings key to pre fill the text area.
* @return string * @return string
*/ */
protected function addTextArea($p_str_SettingName) { protected function add_textarea( $p_str_setting_name ) {
// collect data for given settings field // Collect data for given settings field.
$l_arr_Data = $this->LoadSetting($p_str_SettingName); $l_arr_data = $this->load_setting( $p_str_setting_name );
return sprintf('<textarea name="%s" id="%s">%s</textarea>', return sprintf(
$l_arr_Data["name"], $l_arr_Data["id"], $l_arr_Data["value"]); '<textarea name="%s" id="%s">%s</textarea>',
$l_arr_data['name'],
$l_arr_data['id'],
$l_arr_data['value']
);
} }
/** /**
* Returns the html tag for an input [type = text] with color selection class. * Returns the html tag for an input [type = text] with color selection class.
* *
* @author Stefan Herndler
* @since 1.5.6 * @since 1.5.6
* @param string $p_str_SettingName Name of the Settings key to pre load the input field. * @param string $p_str_setting_name Name of the Settings key to pre load the input field.
* @return string * @return string
*/ */
protected function addColorSelection($p_str_SettingName) { protected function add_color_selection( $p_str_setting_name ) {
// collect data for given settings field // Collect data for given settings field.
$l_arr_Data = $this->LoadSetting($p_str_SettingName); $l_arr_data = $this->load_setting( $p_str_setting_name );
return sprintf('<input type="text" name="%s" id="%s" class="mfmmf-color-picker" value="%s"/>', return sprintf(
$l_arr_Data["name"], $l_arr_Data["id"], $l_arr_Data["value"]); '<input type="text" name="%s" id="%s" class="mfmmf-color-picker" value="%s"/>',
$l_arr_data['name'],
$l_arr_data['id'],
$l_arr_data['value']
);
} }
/** /**
* Returns the html tag for an input [type = num]. * Returns the html tag for an input [type = num].
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
* @param string $p_str_SettingName Name of the Settings key to pre load the input field. * @param string $p_str_setting_name Name of the Settings key to pre load the input field.
* @param int $p_in_Min Minimum value. * @param int $p_in_min Minimum value.
* @param int $p_int_Max Maximum value. * @param int $p_int_max Maximum value.
* @param bool $p_bool_Deci true if 0.1 steps and floating to string, false if integer (default) * @param bool $p_bool_deci true if 0.1 steps and floating to string, false if integer (default).
* @return string * @return string
* *
* Edited: * Edited:
* @since 2.1.4 step argument and number_format() to allow decimals 2020-12-03T0631+0100..2020-12-12T1110+0100 * @since 2.1.4 step argument and number_format() to allow decimals 2020-12-03T0631+0100..2020-12-12T1110+0100
*/ */
protected function addNumBox($p_str_SettingName, $p_in_Min, $p_int_Max, $p_bool_Deci = false ) { protected function add_num_box( $p_str_setting_name, $p_in_min, $p_int_max, $p_bool_deci = false ) {
// collect data for given settings field // Collect data for given settings field.
$l_arr_Data = $this->LoadSetting($p_str_SettingName); $l_arr_data = $this->load_setting( $p_str_setting_name );
if ($p_bool_Deci) { if ( $p_bool_deci ) {
$l_str_Value = number_format(floatval($l_arr_Data["value"]), 1); $l_str_value = number_format( floatval( $l_arr_data['value'] ), 1 );
return sprintf('<input type="number" name="%s" id="%s" value="%s" step="0.1" min="%d" max="%d"/>', return sprintf(
$l_arr_Data["name"], $l_arr_Data["id"], $l_str_Value, $p_in_Min, $p_int_Max); '<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,
$p_in_min,
$p_int_max
);
} else { } else {
return sprintf('<input type="number" name="%s" id="%s" value="%d" min="%d" max="%d"/>', return sprintf(
$l_arr_Data["name"], $l_arr_Data["id"], $l_arr_Data["value"], $p_in_Min, $p_int_Max); '<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'],
$p_in_min,
$p_int_max
);
} }
} }
} // end of class }

View file

@ -1,140 +1,146 @@
<?php <?php // phpcs:disable WordPress.Files.FileName.InvalidClassFileName
/** /**
* Includes the Plugin Class to display Diagnostics. * Includes the Plugin Class to display Diagnostics.
* *
* @filesource * @filesource
* @author Stefan Herndler * @package footnotes
* @since 1.5.0 14.09.14 14:47 * @since 1.5.0 14.09.14 14:47
*/ */
/** /**
* Displays Diagnostics of the web server, PHP and WordPress. * Displays Diagnostics of the web server, PHP and WordPress.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
*/ */
class MCI_Footnotes_Layout_Diagnostics extends MCI_Footnotes_LayoutEngine { class MCI_Footnotes_Layout_Diagnostics extends MCI_Footnotes_Layout_Engine {
/** /**
* Returns a Priority index. Lower numbers have a higher Priority. * Returns a Priority index. Lower numbers have a higher Priority.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
* @return int * @return int
*/ */
public function getPriority() { public function get_priority() {
return 999; return 999;
} }
/** /**
* Returns the unique slug of the sub page. * Returns the unique slug of the sub page.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
* @return string * @return string
*/ */
protected function getSubPageSlug() { protected function get_sub_page_slug() {
return "-diagnostics"; return '-diagnostics';
} }
/** /**
* Returns the title of the sub page. * Returns the title of the sub page.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
* @return string * @return string
*/ */
protected function getSubPageTitle() { protected function get_sub_page_title() {
return __("Diagnostics", MCI_Footnotes_Config::C_STR_PLUGIN_NAME); return __( 'Diagnostics', 'footnotes' );
} }
/** /**
* Returns an array of all registered sections for the sub page. * Returns an array of all registered sections for the sub page.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
* @return array * @return array
*/ */
protected function getSections() { protected function get_sections() {
return array( return array(
$this->addSection("diagnostics", __("Diagnostics", MCI_Footnotes_Config::C_STR_PLUGIN_NAME), null, false) $this->add_section( 'diagnostics', __( 'Diagnostics', 'footnotes' ), null, false ),
); );
} }
/** /**
* Returns an array of all registered meta boxes for each section of the sub page. * Returns an array of all registered meta boxes for each section of the sub page.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
* @return array * @return array
*/ */
protected function getMetaBoxes() { protected function get_meta_boxes() {
return array( return array(
$this->addMetaBox("diagnostics", "diagnostics", __("Displays information about the web server, PHP and WordPress", MCI_Footnotes_Config::C_STR_PLUGIN_NAME), "Diagnostics") $this->add_meta_box( 'diagnostics', 'diagnostics', __( 'Displays information about the web server, PHP and WordPress', 'footnotes' ), 'Diagnostics' ),
); );
} }
/** /**
* Displays a diagnostics about the web server, php and WordPress. * Displays a diagnostics about the web server, php and WordPress.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
*/ */
public function Diagnostics() { public function Diagnostics() {
global $wp_version; global $wp_version;
$l_str_PhpExtensions = ""; $l_str_php_extensions = '';
// iterate through each PHP extension // Iterate through each PHP extension.
foreach (get_loaded_extensions() as $l_int_Index => $l_str_Extension) { foreach ( get_loaded_extensions() as $l_int_index => $l_str_extension ) {
if ($l_int_Index > 0) { if ( $l_int_index > 0 ) {
$l_str_PhpExtensions .= ' | '; $l_str_php_extensions .= ' | ';
} }
$l_str_PhpExtensions .= $l_str_Extension . ' ' . phpversion($l_str_Extension); $l_str_php_extensions .= $l_str_extension . ' ' . phpversion( $l_str_extension );
} }
/** @var WP_Theme $l_obj_CurrentTheme */ $l_obj_current_theme = wp_get_theme();
$l_obj_CurrentTheme = wp_get_theme();
$l_str_WordPressPlugins = ""; $l_str_wordpress_plugins = '';
// iterate through each installed WordPress Plugin // Iterate through each installed WordPress Plugin.
foreach (get_plugins() as $l_arr_Plugin) { foreach ( get_plugins() as $l_arr_plugin ) {
$l_str_WordPressPlugins .= '<tr>'; $l_str_wordpress_plugins .= '<tr>';
$l_str_WordPressPlugins .= '<td>' . $l_arr_Plugin["Name"] . '</td>'; $l_str_wordpress_plugins .= '<td>' . $l_arr_plugin['Name'] . '</td>';
$l_str_WordPressPlugins .= '<td>' . $l_arr_Plugin["Version"] . ' [' . $l_arr_Plugin["PluginURI"] . ']' . '</td>'; // phpcs:disable Generic.Strings.UnnecessaryStringConcat.Found
$l_str_WordPressPlugins .= '</tr>'; $l_str_wordpress_plugins .= '<td>' . $l_arr_plugin['Version'] . ' [' . $l_arr_plugin['PluginURI'] . ']' . '</td>';
// phpcs:enable
$l_str_wordpress_plugins .= '</tr>';
} }
// load template file // Load template file.
$l_obj_Template = new MCI_Footnotes_Template(MCI_Footnotes_Template::C_STR_DASHBOARD, "diagnostics"); $l_obj_template = new MCI_Footnotes_Template( MCI_Footnotes_Template::C_STR_DASHBOARD, 'diagnostics' );
// replace all placeholders
$l_obj_Template->replace( if ( ! isset( $_SERVER['SERVER_NAME'] ) ) {
die;
} else {
$l_str_server_name = wp_kses_post( wp_unslash( $_SERVER['SERVER_NAME'] ) );
}
if ( ! isset( $_SERVER['HTTP_USER_AGENT'] ) ) {
die;
} else {
$l_str_http_user_agent = wp_kses_post( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) );
}
// Replace all placeholders.
$l_obj_template->replace(
array( array(
"label-server" => __("Server name", MCI_Footnotes_Config::C_STR_PLUGIN_NAME), 'label-server' => __( 'Server name', 'footnotes' ),
"server" => $_SERVER["SERVER_NAME"], 'server' => $l_str_server_name,
"label-php" => __("PHP version", MCI_Footnotes_Config::C_STR_PLUGIN_NAME), 'label-php' => __( 'PHP version', 'footnotes' ),
"php" => phpversion(), 'php' => phpversion(),
"label-user-agent" => __("User agent", MCI_Footnotes_Config::C_STR_PLUGIN_NAME), 'label-user-agent' => __( 'User agent', 'footnotes' ),
"user-agent" => $_SERVER["HTTP_USER_AGENT"], 'user-agent' => $l_str_http_user_agent,
"label-max-execution-time" => __("Max execution time", MCI_Footnotes_Config::C_STR_PLUGIN_NAME), 'label-max-execution-time' => __( 'Max execution time', 'footnotes' ),
"max-execution-time" => ini_get('max_execution_time') . ' ' . __('seconds', MCI_Footnotes_Config::C_STR_PLUGIN_NAME), 'max-execution-time' => ini_get( 'max_execution_time' ) . ' ' . __( 'seconds', 'footnotes' ),
"label-memory-limit" => __("Memory limit", MCI_Footnotes_Config::C_STR_PLUGIN_NAME), 'label-memory-limit' => __( 'Memory limit', 'footnotes' ),
"memory-limit" => ini_get('memory_limit'), 'memory-limit' => ini_get( 'memory_limit' ),
"label-php-extensions" => __("PHP extensions", MCI_Footnotes_Config::C_STR_PLUGIN_NAME), 'label-php-extensions' => __( 'PHP extensions', 'footnotes' ),
"php-extensions" => $l_str_PhpExtensions, 'php-extensions' => $l_str_php_extensions,
"label-wordpress" => __("WordPress version", MCI_Footnotes_Config::C_STR_PLUGIN_NAME), 'label-wordpress' => __( 'WordPress version', 'footnotes' ),
"wordpress" => $wp_version, 'wordpress' => $wp_version,
"label-theme" => __("Active Theme", MCI_Footnotes_Config::C_STR_PLUGIN_NAME), 'label-theme' => __( 'Active Theme', 'footnotes' ),
"theme" => $l_obj_CurrentTheme->get("Name") . " " . $l_obj_CurrentTheme->get("Version") . ", " . $l_obj_CurrentTheme->get("Author"). " [" . $l_obj_CurrentTheme->get("AuthorURI") . "]", 'theme' => $l_obj_current_theme->get( 'Name' ) . ' ' . $l_obj_current_theme->get( 'Version' ) . ', ' . $l_obj_current_theme->get( 'Author' ) . ' [' . $l_obj_current_theme->get( 'AuthorURI' ) . ']',
"plugins" => $l_str_WordPressPlugins 'plugins' => $l_str_wordpress_plugins,
) )
); );
// display template with replaced placeholders // Display template with replaced placeholders.
echo $l_obj_Template->getContent(); echo wp_kses_post( $l_obj_template->get_content() );
} }
} }

View file

@ -1,17 +1,15 @@
<?php <?php // phpcs:disable WordPress.Files.FileName.InvalidClassFileName
/** /**
* Includes the Plugin settings menu. * Includes the Plugin settings menu.
* *
* @filesource * @filesource
* @author Stefan Herndler * @package footnotes
* @since 1.5.0 12.09.14 10:26 * @since 1.5.0 12.09.14 10:26
*/ */
/** /**
* Handles the Settings interface of the Plugin. * Handles the Settings interface of the Plugin.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
*/ */
class MCI_Footnotes_Layout_Init { class MCI_Footnotes_Layout_Init {
@ -19,68 +17,60 @@ class MCI_Footnotes_Layout_Init {
/** /**
* Slug for the Plugin main menu. * Slug for the Plugin main menu.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
* @var string * @var string
*/ */
const C_STR_MAIN_MENU_SLUG = "mfmmf"; const C_STR_MAIN_MENU_SLUG = 'mfmmf';
/** /**
* Plugin main menu name. * Plugin main menu name.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
* @var string * @var string
*/ */
const C_STR_MAIN_MENU_TITLE = "ManFisher"; const C_STR_MAIN_MENU_TITLE = 'ManFisher';
/** /**
* Contains layout engine sub classes.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
* @var array * @var array
*/ */
private $a_arr_SubPageClasses = array(); private $a_arr_sub_page_classes = array();
/** /**
* Class Constructor. Initializes all WordPress hooks for the Plugin Settings. * Class Constructor. Initializes all WordPress hooks for the Plugin Settings.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
*/ */
public function __construct() { public function __construct() {
// iterate through each class define in the current script foreach ( get_declared_classes() as $l_str_class_name ) {
foreach(get_declared_classes() as $l_str_ClassName) { if ( is_subclass_of( $l_str_class_name, 'MCI_Footnotes_Layout_Engine' ) ) {
// accept only child classes of the layout engine $l_obj_class = new $l_str_class_name();
if(is_subclass_of($l_str_ClassName, 'MCI_Footnotes_LayoutEngine')) { // Append new instance of the layout engine sub class.
/** @var MCI_Footnotes_LayoutEngine $l_obj_Class */ $this->a_arr_sub_page_classes[ $l_obj_class->get_priority() ] = $l_obj_class;
$l_obj_Class = new $l_str_ClassName();
// append new instance of the layout engine sub class
$this->a_arr_SubPageClasses[$l_obj_Class->getPriority()] = $l_obj_Class;
} }
} }
ksort($this->a_arr_SubPageClasses); ksort( $this->a_arr_sub_page_classes );
// register hooks/actions // Register hooks/actions.
add_action('admin_init', array($this, 'initializeSettings')); add_action( 'admin_init', array( $this, 'initialize_settings' ) );
add_action('admin_menu', array($this, 'registerMainMenu')); add_action( 'admin_menu', array( $this, 'register_main_menu' ) );
// register AJAX callbacks for Plugin information // register AJAX callbacks for Plugin information.
add_action("wp_ajax_nopriv_footnotes_getPluginInfo", array($this, "getPluginMetaInformation")); add_action( 'wp_ajax_nopriv_footnotes_get_plugin_info', array( $this, 'get_plugin_meta_information' ) );
add_action("wp_ajax_footnotes_getPluginInfo", array($this, "getPluginMetaInformation")); add_action( 'wp_ajax_footnotes_get_plugin_info', array( $this, 'get_plugin_meta_information' ) );
} }
/** /**
* Initializes all sub pages and registers the settings. * Initializes all sub pages and registers the settings.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
*/ */
public function initializeSettings() { public function initialize_settings() {
MCI_Footnotes_Settings::instance()->RegisterSettings(); MCI_Footnotes_Settings::instance()->register_settings();
// iterate though each sub class of the layout engine and register their sections // iterate though each sub class of the layout engine and register their sections.
/** @var MCI_Footnotes_LayoutEngine $l_obj_LayoutEngineSubClass */ foreach ( $this->a_arr_sub_page_classes as $l_obj_layout_engine_sub_class ) {
foreach($this->a_arr_SubPageClasses as $l_obj_LayoutEngineSubClass) { $l_obj_layout_engine_sub_class->register_sections();
$l_obj_LayoutEngineSubClass->registerSections();
} }
} }
@ -88,123 +78,123 @@ class MCI_Footnotes_Layout_Init {
* Registers the new main menu for the WordPress dashboard. * Registers the new main menu for the WordPress dashboard.
* Registers all sub menu pages for the new main menu. * Registers all sub menu pages for the new main menu.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
* @see http://codex.wordpress.org/Function_Reference/add_menu_page * @see http://codex.wordpress.org/Function_Reference/add_menu_page
*/ */
public function registerMainMenu() { public function register_main_menu() {
global $menu; global $menu;
// iterate through each main menu // iterate through each main menu.
foreach($menu as $l_arr_MainMenu) { foreach ( $menu as $l_arr_main_menu ) {
// iterate through each main menu attribute // iterate through each main menu attribute.
foreach($l_arr_MainMenu as $l_str_Attribute) { foreach ( $l_arr_main_menu as $l_str_attribute ) {
// main menu already added, append sub pages and stop // main menu already added, append sub pages and stop.
if ($l_str_Attribute == self::C_STR_MAIN_MENU_SLUG) { if ( self::C_STR_MAIN_MENU_SLUG === $l_str_attribute ) {
$this->registerSubPages(); $this->register_sub_pages();
return; return;
} }
} }
} }
// add a new main menu page to the WordPress dashboard // add a new main menu page to the WordPress dashboard.
add_menu_page( add_menu_page(
self::C_STR_MAIN_MENU_TITLE, // page title self::C_STR_MAIN_MENU_TITLE, // page title.
self::C_STR_MAIN_MENU_TITLE, // menu title self::C_STR_MAIN_MENU_TITLE, // menu title.
'manage_options', // capability 'manage_options', // capability.
self::C_STR_MAIN_MENU_SLUG, // menu slug self::C_STR_MAIN_MENU_SLUG, // menu slug.
array($this, "displayOtherPlugins"), // function array( $this, 'display_other_plugins' ), // function.
plugins_url('footnotes/img/main-menu.png'), // icon url plugins_url( 'footnotes/img/main-menu.png' ), // icon url.
null // position null // position.
); );
$this->registerSubPages(); $this->register_sub_pages();
} }
/** /**
* Registers all SubPages for this Plugin. * Registers all SubPages for this Plugin.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
*/ */
private function registerSubPages() { private function register_sub_pages() {
// first registered sub menu page MUST NOT contain a unique slug suffix // first registered sub menu page MUST NOT contain a unique slug suffix.
// iterate though each sub class of the layout engine and register their sub page // iterate though each sub class of the layout engine and register their sub page.
/** @var MCI_Footnotes_LayoutEngine $l_obj_LayoutEngineSubClass */ foreach ( $this->a_arr_sub_page_classes as $l_obj_layout_engine_sub_class ) {
foreach($this->a_arr_SubPageClasses as $l_obj_LayoutEngineSubClass) { $l_obj_layout_engine_sub_class->register_sub_page();
$l_obj_LayoutEngineSubClass->registerSubPage();
} }
} }
/** /**
* Displays other Plugins from the developers. * Displays other Plugins from the developers.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
*/ */
public function displayOtherPlugins() { public function display_other_plugins() {
printf("<br/><br/>"); printf( '<br/><br/>' );
// load template file // load template file.
$l_obj_Template = new MCI_Footnotes_Template(MCI_Footnotes_Template::C_STR_DASHBOARD, "manfisher"); $l_obj_template = new MCI_Footnotes_Template( MCI_Footnotes_Template::C_STR_DASHBOARD, 'manfisher' );
echo $l_obj_Template->getContent(); echo wp_kses_post( $l_obj_template->get_content() );
printf('<em>visit <a href="https://cheret.de/plugins/footnotes-2/" target="_blank">Mark Cheret</a></em>'); printf( '<em>visit <a href="https://cheret.de/plugins/footnotes-2/" target="_blank">Mark Cheret</a></em>' );
printf("<br/><br/>"); printf( '<br/><br/>' );
printf('</div>'); printf( '</div>' );
} }
/** /**
* AJAX call. returns a JSON string containing meta information about a specific WordPress Plugin. * AJAX call. returns a JSON string containing meta information about a specific WordPress Plugin.
* *
* @author Stefan Herndler
* @since 1.5.0 * @since 1.5.0
*/ */
public function getPluginMetaInformation() { public function get_plugin_meta_information() {
// get plugin internal name from POST data // TODO: add nonce verification.
$l_str_PluginName = array_key_exists("plugin", $_POST) ? $_POST["plugin"] : null;
if (empty($l_str_PluginName)) { // get plugin internal name from POST data.
echo json_encode(array("error" => "Plugin name invalid.")); if ( isset( $_POST['plugin'] ) && 'true' === $_POST['plugin'] ) {
$l_str_plugin_name = santitize_option( wp_unslash( $_POST['plugin'] ) );
}
if ( empty( $l_str_plugin_name ) ) {
echo wp_json_encode( array( 'error' => 'Plugin name invalid.' ) );
exit; exit;
} }
$l_str_Url = "https://api.wordpress.org/plugins/info/1.0/".$l_str_PluginName.".json"; $l_str_url = 'https://api.wordpress.org/plugins/info/1.0/' . $l_str_plugin_name . '.json';
// call URL and collect data // call URL and collect data.
$l_arr_Response = wp_remote_get($l_str_Url); $l_arr_response = wp_remote_get( $l_str_url );
// check if response is valid // check if response is valid.
if (is_wp_error($l_arr_Response)) { if ( is_wp_error( $l_arr_response ) ) {
echo json_encode(array("error" => "Error receiving Plugin Information from WordPress.")); echo wp_json_encode( array( 'error' => 'Error receiving Plugin Information from WordPress.' ) );
exit; exit;
} }
if (!array_key_exists("body", $l_arr_Response)) { if ( ! array_key_exists( 'body', $l_arr_response ) ) {
echo json_encode(array("error" => "Error reading WordPress API response message.")); echo wp_json_encode( array( 'error' => 'Error reading WordPress API response message.' ) );
exit; exit;
} }
// get the body of the response // get the body of the response.
$l_str_Response = $l_arr_Response["body"]; $l_str_response = $l_arr_response['body'];
// get plugin object // get plugin object.
$l_arr_Plugin = json_decode($l_str_Response, true); $l_arr_plugin = json_decode( $l_str_response, true );
if (empty($l_arr_Plugin)) { if ( empty( $l_arr_plugin ) ) {
echo json_encode(array("error" => "Error reading Plugin meta information.<br/>URL: " . $l_str_Url . "<br/>Response: " . $l_str_Response)); echo wp_json_encode( array( 'error' => 'Error reading Plugin meta information.<br/>URL: ' . $l_str_url . '<br/>Response: ' . $l_str_response ) );
exit; exit;
} }
$l_int_NumRatings = array_key_exists("num_ratings", $l_arr_Plugin) ? intval($l_arr_Plugin["num_ratings"]) : 0; $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_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); $l_int_stars = round( 5 * $l_int_rating / 100.0, 1 );
// return Plugin information as JSON encoded string // return Plugin information as JSON encoded string.
echo json_encode( echo wp_json_encode(
array( array(
"error" => "", 'error' => '',
"PluginDescription" => array_key_exists("short_description", $l_arr_Plugin) ? html_entity_decode($l_arr_Plugin["short_description"]) : "Error reading Plugin information", '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", 'PluginAuthor' => array_key_exists( 'author', $l_arr_plugin ) ? html_entity_decode( $l_arr_plugin['author'] ) : 'unknown',
"PluginRatingText" => $l_int_Stars . " " . __("rating based on", MCI_Footnotes_Config::C_STR_PLUGIN_NAME) . " " . $l_int_NumRatings . " " . __("ratings", MCI_Footnotes_Config::C_STR_PLUGIN_NAME), 'PluginRatingText' => $l_int_stars . ' ' . __( 'rating based on', 'footnotes' ) . ' ' . $l_int_num_ratings . ' ' . __( 'ratings', 'footnotes' ),
"PluginRating1" => $l_int_Stars >= 0.5 ? "star-full" : "star-empty", 'PluginRating1' => $l_int_stars >= 0.5 ? 'star-full' : 'star-empty',
"PluginRating2" => $l_int_Stars >= 1.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", 'PluginRating3' => $l_int_stars >= 2.5 ? 'star-full' : 'star-empty',
"PluginRating4" => $l_int_Stars >= 3.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", 'PluginRating5' => $l_int_stars >= 4.5 ? 'star-full' : 'star-empty',
"PluginRating" => $l_int_NumRatings, 'PluginRating' => $l_int_num_ratings,
"PluginLastUpdated" => array_key_exists("last_updated", $l_arr_Plugin) ? $l_arr_Plugin["last_updated"] : "unknown", '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"] : "---" 'PluginDownloads' => array_key_exists( 'downloaded', $l_arr_plugin ) ? $l_arr_plugin['downloaded'] : '---',
) )
); );
exit; exit;

File diff suppressed because it is too large Load diff