| 1 |
<?php |
| 2 |
declare(strict_types=1); |
| 3 |
namespace MaxButtons; |
| 4 |
|
| 5 |
// controller for our views |
| 6 |
abstract class MaxController |
| 7 |
{ |
| 8 |
|
| 9 |
protected static $instance; |
| 10 |
|
| 11 |
protected $view; // view data |
| 12 |
protected $page; |
| 13 |
|
| 14 |
protected $url; |
| 15 |
protected $messages = array(); // messages to display to user. |
| 16 |
protected $view_template; |
| 17 |
|
| 18 |
protected $actions = false; |
| 19 |
|
| 20 |
protected $modals_loaded = array(); |
| 21 |
|
| 22 |
public function __construct() |
| 23 |
{ |
| 24 |
$this->view = new \stdClass; |
| 25 |
} |
| 26 |
|
| 27 |
// Get the instance of this controller |
| 28 |
public static function getInstance() |
| 29 |
{ |
| 30 |
if (!isset(self::$instance)) { |
| 31 |
$c = get_called_class(); |
| 32 |
self::$instance = new $c; |
| 33 |
} |
| 34 |
|
| 35 |
return self::$instance; |
| 36 |
} |
| 37 |
|
| 38 |
// Handling any post related business |
| 39 |
abstract protected function handlePost(); |
| 40 |
|
| 41 |
public function load() |
| 42 |
{ |
| 43 |
if (isset($_POST) && count($_POST) > 0) |
| 44 |
$this->handlePost(); |
| 45 |
} |
| 46 |
|
| 47 |
public function view() |
| 48 |
{ |
| 49 |
$view = $this->view; |
| 50 |
|
| 51 |
|
| 52 |
if (! is_null($this->view_template)) |
| 53 |
{ |
| 54 |
$path = MB()->get_plugin_path() . 'includes/' . $this->view_template . '.php'; |
| 55 |
if (file_exists($path)) |
| 56 |
include_once($path); |
| 57 |
else { |
| 58 |
exit('Template Not Found'); |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public function getButtonLink($button_id = 0, $args = array()) |
| 66 |
{ |
| 67 |
$link = admin_url() . 'admin.php?page=maxbuttons-controller&action=edit'; |
| 68 |
if ($button_id > 0) |
| 69 |
{ |
| 70 |
$link = add_query_arg('id', $button_id, $link); |
| 71 |
} |
| 72 |
$link = add_query_arg($args,$link); |
| 73 |
return esc_url_raw($link); |
| 74 |
} |
| 75 |
|
| 76 |
public function getListLink($view = 'all', $args = array() ) |
| 77 |
{ |
| 78 |
$link = admin_url() . 'admin.php?page=maxbuttons-controller&view=' . $view; |
| 79 |
$link = add_query_arg($args,$link); |
| 80 |
return esc_url_raw($link); |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
// sets name of the requested page. can be used to load a specific template. |
| 85 |
public function setPage($page) |
| 86 |
{ |
| 87 |
$this->page = $page; |
| 88 |
} |
| 89 |
|
| 90 |
public function setUrl($url) |
| 91 |
{ |
| 92 |
$this->url = $url; |
| 93 |
} |
| 94 |
|
| 95 |
protected function loadFormActions() |
| 96 |
{ |
| 97 |
$actions = array(); |
| 98 |
|
| 99 |
$actions['add-new'] = array('href' => $this->getButtonLink(), 'text' => __('Add New', 'maxbuttons'), 'class' => ''); |
| 100 |
$actions['save'] = array('href' => 'javascript:void(0)', 'text' => __('Save', 'maxbuttons'), 'class' => 'button button-save disabled'); |
| 101 |
$actions['copy'] = array('href' => 'javascript:void(0)', 'text' => __('Copy', 'maxbuttons'), 'class' => 'maxmodal button', 'id' => 'button-copy', |
| 102 |
'modal' => 'copy-modal'); |
| 103 |
$actions['trash'] = array('href' => 'javascript:void(0)', 'text' => __('Move to Trash', 'maxbuttons'), 'class' => 'maxmodal button', 'id' => 'button-trash', 'modal' => 'trash-modal'); |
| 104 |
$actions['delete'] = array('href' => 'javascript:void(0)', 'text' => __("Delete","maxbuttons"), 'class' => 'maxmodal button', 'id' => 'button-delete', 'modal' => 'delete-modal'); |
| 105 |
|
| 106 |
return apply_filters('maxbuttons/editor/actions',$actions); |
| 107 |
} |
| 108 |
|
| 109 |
protected function getButton($name, $args = array() ) |
| 110 |
{ |
| 111 |
if (! $this->actions) |
| 112 |
$this->actions = $this->loadFormActions(); |
| 113 |
|
| 114 |
|
| 115 |
if (isset($this->actions[$name]) && $this->actions[$name] !== false) |
| 116 |
{ |
| 117 |
$action = $this->actions[$name]; |
| 118 |
|
| 119 |
$args = wp_parse_args($args, $action); |
| 120 |
|
| 121 |
$class = $args['class']; |
| 122 |
if (isset($args['add_class'])) |
| 123 |
$class .= ' ' . $args['add_class']; |
| 124 |
|
| 125 |
$output = '<a href="' . $args['href'] . '" class="' . $class . '"'; |
| 126 |
|
| 127 |
if (isset($args['id'])) |
| 128 |
$output .= ' id="' . $action['id'] . '"'; |
| 129 |
if (isset($args['modal'])) |
| 130 |
{ |
| 131 |
$output .= ' data-modal="' . $args['modal'] . '-' . $this->view->button_id . '"'; |
| 132 |
$this->loadModal($args['modal'], $this->view->button_id); |
| 133 |
} |
| 134 |
$output .= '>' . $args['text'] . '</a>'; |
| 135 |
|
| 136 |
return $output; |
| 137 |
} |
| 138 |
else |
| 139 |
return ''; |
| 140 |
} |
| 141 |
|
| 142 |
protected function loadModal($name, $id) |
| 143 |
{ |
| 144 |
if (isset($this->modals_loaded[$name])) |
| 145 |
return; |
| 146 |
|
| 147 |
MB()->load_modal_script(); |
| 148 |
$path = MB()->get_plugin_path(); |
| 149 |
|
| 150 |
include($path . 'views/modals/' . $name . '.php'); |
| 151 |
|
| 152 |
$this->modals_loaded[] = $name; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
} |
| 157 |
|