Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kofimokome committed Nov 29, 2021
0 parents commit 3262b44
Show file tree
Hide file tree
Showing 8 changed files with 555 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
101 changes: 101 additions & 0 deletions MenuPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php
/**
* Created by PhpStorm.
* User: kofi
* Date: 6/5/19
* Time: 11:59 AM
* @version 1.0.0
* @author kofi mokome
*/


class MenuPage {
private $page_title;
private $menu_title;
private $capability;
private $menu_slug;
private $icon_url;
private $position;
private $function;
private $sub_menu_pages;

/**
* @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
* @param string $menu_title The text to be used for the menu.
* @param string $capability The capability required for this menu to be displayed to the user.
* @param string $menu_slug The slug name to refer to this menu by. Should be unique for this menu page and only
* include lowercase alphanumeric, dashes, and underscores characters to be compatible
* with sanitize_key().
* @param callable $function The function to be called to output the content for this page.
* @param string $icon_url The URL to the icon to be used for this menu.
* * Pass a base64-encoded SVG using a data URI, which will be colored to match
* the color scheme. This should begin with 'data:image/svg+xml;base64,'.
* * Pass the name of a Dashicons helper class to use a font icon,
* e.g. 'dashicons-chart-pie'.
* * Pass 'none' to leave div.wp-menu-image empty so an icon can be added via CSS.
* @param int $position The position in the menu order this item should appear.
*
* @since 1.0.0
*/
public function __construct( $page_title, $menu_title, $capability, $menu_slug, $icon_url = '', $position = null, $function = '' ) {
$this->page_title = $page_title;
$this->menu_title = $menu_title;
$this->capability = $capability;
$this->menu_slug = $menu_slug;
$this->icon_url = $icon_url;
$this->position = $position;
$this->function = $function == '' ? array( $this, 'default_function' ) : $function;

$this->sub_menu_pages = array();
}

/**
* @since 1.0.0
*/
public function run() {
add_action( 'admin_menu', array( $this, 'create_menu_page' ) );
}

/**
* @since 1.0.0
*/
public function create_menu_page() {
add_menu_page(
$this->page_title,
$this->menu_title,
$this->capability,
$this->menu_slug,
$this->function,
$this->icon_url,
$this->position

);

foreach ( $this->sub_menu_pages as $sub_menu_page ) {
$sub_menu_page->run();
}
}

/**
* @since 1.0.0
*/
public function default_function() {
echo "";
}

/**
* @since 1.0.0
*/
public function get_menu_slug() {
return $this->menu_slug;
}

/**
* @param SubMenuPage $sub_menu_page
* @since 1.0.0
*/
public function add_sub_menu_page( $sub_menu_page ) {
array_push( $this->sub_menu_pages, $sub_menu_page );
}

}
157 changes: 157 additions & 0 deletions Setting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php
/**
* Created by PhpStorm.
* User: kofi
* Date: 6/5/19
* Time: 12:41 PM
* @version 1.0.0
* @author kofi mokome
*/

class Setting {
private $menu_slug;
private $fields;
private $section_id;
private $sections;

/**
* @since 1.0.0
*/
public function __construct( $menu_slug ) {
$this->menu_slug = $menu_slug;
$this->fields = array();
$this->sections = array();
}

/**
* @since 1.0.0
*/
public function show_form() {
settings_errors(); ?>
<form method="post" action="options.php">
<?php
foreach ( $this->sections as $section ):
settings_fields( $section[0] );
do_settings_sections( $this->menu_slug );
endforeach;
submit_button();
?>
</form>

<?php
//echo $this->default_content;
}

/**
* @since 1.0.0
*/
public function save() {
add_action( 'admin_init', array( $this, 'add_settings' ) );
}

/**
* @since 1.0.0
*/
public function add_settings() {

foreach ( $this->sections as $section ) {
add_settings_section(
$section[0],
$section[1],
array( $this, 'default_section_callback' ),
$this->menu_slug );
}

foreach ( $this->fields as $field ) {
add_settings_field(
$field['id'],
$field['label'],
array( $this, 'default_field_callback' ),
$this->menu_slug,
$field['section_id'],
$field
);
register_setting( $field['section_id'], $field['id'] );
}
}

/**
* @since 1.0.0
*/
public function default_field_callback( $data ) {
switch ( $data['type'] ) {
case 'text':
echo "<p><input type='text' name='{$data['id']}' value='" . get_option( $data['id'] ) . "' class='{$data['input_class']}' placeholder='{$data['placeholder']}'></p>";
echo "<strong>{$data['tip']} </strong>";
break;
case 'number':
echo "<p><input type='number' name='{$data['id']}' value='" . get_option( $data['id'] ) . "' min='" . $data['min'] . "' max='" . $data['max'] . "' class='{$data['input_class']}' placeholder='{$data['placeholder']}'></p>";
echo "<strong>{$data['tip']} </strong>";
break;
case 'textarea':
echo "<p><textarea name='{$data['id']}' id='{$data['id']}' cols='80'
rows='8'
placeholder='{$data['placeholder']}' class='{$data['input_class']}' autocomplete='{$data['autocomplete']}'>" . get_option( $data['id'] ) . "</textarea></p>";
echo "<strong>{$data['tip']} </strong>";
break;
case 'checkbox':
$state = get_option( $data['id'] ) == 'on' ? 'checked' : '';
echo "<p><input type='checkbox' name='{$data['id']}' id='{$data['id']}' " . $state . " class='{$data['input_class']}'></p>";
echo "<strong>{$data['tip']} </strong>";
break;
case 'select':
$selected_value = get_option( $data['id'] );
echo "<p><select type='text' name='{$data['id']}' id='{$data['id']}' class='{$data['input_class']}'>";
foreach ( $data['options'] as $key => $value ):?>
<option value='<?php echo $value ?>' <?php echo ( $value === $selected_value ) ? 'selected' : '' ?> ><?php echo $key ?></option>
<?php
endforeach;
echo "</select></p>";
echo "<strong>{$data['tip']} </strong>";
break;
default:
echo "<< <span style='color: red;'>Please enter a valid field type</span> >>";
break;
}
}

/**
* @since 1.0.0
*/
public function add_field( $data ) {
$default_data = array(
'type' => '',
'id' => '',
'label' => '',
'tip' => '',
'min' => '',
'max' => '',
'input_class' => '', // class for input element
'class' => '', // class for parent element
'options' => array( 'Select a value' => '' ),
'default_option' => '',
'autocomplete' => 'on',
'placeholder' => ''
);
$data = array_merge( $default_data, $data );
// todo: compare two arrays
$data['section_id'] = $this->section_id;
array_push( $this->fields, $data );

}

/**
* @since 1.0.0
*/
public function add_section( $id, $title = '' ) {
array_push( $this->sections, array( $id, $title ) );
$this->section_id = $id;
}

/**
* @since 1.0.0
*/
public function default_section_callback() {

}
}
109 changes: 109 additions & 0 deletions SubMenuPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
/**
* Created by PhpStorm.
* User: kofi
* Date: 6/5/19
* Time: 12:41 PM
* @author kofi mokome
* @version 1.0.0
*/

class SubMenuPage {
private $page_title;
private $menu_title;
private $capability;
private $menu_slug;
private $parent_slug;
private $function;
private $tabs;

/**
* @since 1.0.0
*/
public function __construct( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function = null, $use_tabs = false ) {
$this->page_title = $page_title;
$this->menu_title = $menu_title;
$this->capability = $capability;
$this->menu_slug = $menu_slug;
$this->parent_slug = $parent_slug;
$this->function = $function;
if ( $use_tabs ) {
$this->function = array( &$this, 'show_tabs' );
}
$this->tabs = array();
}

/**
* @since 1.0.0
*/
public function show_tabs() {
$current_tab = isset( $_GET['tab'] ) ? $_GET['tab'] : null;
?>
<div class="wrap">
<div id="icon-options-general" class="icon32"></div>
<h1><?php echo $this->page_title ?></h1>
<?php if ( sizeof( $this->tabs ) > 0 ): ?>
<nav class="nav-tab-wrapper">
<?php foreach ( $this->tabs as $id => $tab ): ?>
<a href="?page=<?php echo $this->menu_slug ?>&tab=<?php echo $id ?>"
class="nav-tab <?php if ( $id === $current_tab ): ?>nav-tab-active<?php endif; ?>"><?php echo $tab['title'] ?></a>
<?php endforeach; ?>

</nav>
<?php
$to_display = $current_tab == null ? array_shift( $this->tabs ) : $this->tabs[ $current_tab ];
?>
<?php echo is_callable( $to_display['contents'] ) ? $to_display['contents']( $to_display['args'] ) : $to_display; ?>
<?php else: ?>
<div class="notice notice-error">
<p><strong>Please add a tab first, or set <code>use_tab</code> to false</strong></p>
</div>
<?php endif; ?>


</div>
<?php
//echo $this->default_content;
}

/**
* Adds a new tab to the page
*
* @param string $id ID of the tab
* @param string $title Title of the tab
* @param callable|string $contents Content to display in the tab
* @param array $args Arguments to pass to callback function
*
* @since 1.0.0
*/
public function add_tab( $id, $title, $contents, array $args = [] ) {
$id = trim( $id );
$this->tabs[ $id ] = array( 'title' => $title, 'contents' => $contents, 'args' => $args );
// array_push($this->tabs, array($id, $title));
}

/**
* @since 1.0.0
*/
public function run() {
$this->create_sub_menu_page();
}


/**
* @since 1.0.0
*/
public function create_sub_menu_page() {
add_submenu_page(
$this->parent_slug,
$this->page_title,
$this->menu_title,
$this->capability,
$this->menu_slug,
$this->function
);
}
}



Binary file added images/img1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/img2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/img3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 3262b44

Please sign in to comment.