| 1 |
<?php |
| 2 |
namespace MaxButtons; |
| 3 |
/** Buttons class - handles paging issues and sanity check for individual buttons |
| 4 |
*/ |
| 5 |
class maxButtons |
| 6 |
{ |
| 7 |
protected static $loadedButtons = array(); // loaded button in current scope |
| 8 |
/* |
| 9 |
|
| 10 |
array [ index ] [ button_id ] [data - document_id, done (bool ) |
| 11 |
*/ |
| 12 |
protected static $documentArray = array(); // given out document ID's |
| 13 |
|
| 14 |
// override to give out next document id. This is useful when buttons are set but not displayed directly. |
| 15 |
protected static $doNext = false; |
| 16 |
protected static $current_doc_id = null; |
| 17 |
protected static $current_button_id = null; |
| 18 |
|
| 19 |
protected static $instance = null; |
| 20 |
|
| 21 |
|
| 22 |
public static function getInstance() |
| 23 |
{ |
| 24 |
if (is_null(self::$instance)) |
| 25 |
self::$instance = MB()->getClass('button'); |
| 26 |
|
| 27 |
return self::$instance; |
| 28 |
} |
| 29 |
|
| 30 |
static function buttonLoad($args) |
| 31 |
{ |
| 32 |
|
| 33 |
$button_id = $args["button_id"]; |
| 34 |
self::$loadedButtons[] = $button_id; |
| 35 |
$document_id = self::getDocumentID(array("button_id" => $button_id)); |
| 36 |
self::$documentArray[] = array($button_id => array('document_id' => $document_id , 'done' => false)); |
| 37 |
|
| 38 |
} |
| 39 |
|
| 40 |
static function forceNextID() |
| 41 |
{ |
| 42 |
self::$doNext = true; |
| 43 |
self::$current_doc_id = null; |
| 44 |
self::$current_button_id = null; |
| 45 |
} |
| 46 |
|
| 47 |
static function getDocumentID($args) |
| 48 |
{ |
| 49 |
$button_id = $args["button_id"]; |
| 50 |
|
| 51 |
if (! is_null(self::$current_doc_id) && self::$current_button_id == $button_id ) |
| 52 |
return self::$current_doc_id; |
| 53 |
|
| 54 |
|
| 55 |
if (self::$doNext == false) |
| 56 |
{ |
| 57 |
foreach(self::$documentArray as $index => $ids) |
| 58 |
{ |
| 59 |
foreach($ids as $doc_button_id => $doc_vars) |
| 60 |
{ |
| 61 |
if ($doc_button_id == $button_id) |
| 62 |
{ |
| 63 |
if (! $doc_vars["done"]) |
| 64 |
return $doc_vars["document_id"]; |
| 65 |
} |
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
} |
| 70 |
} |
| 71 |
// if not found in documentarray make a new one |
| 72 |
$loaded = self::$loadedButtons; |
| 73 |
end($loaded); |
| 74 |
$i = 0; |
| 75 |
|
| 76 |
foreach($loaded as $btn_id) |
| 77 |
{ |
| 78 |
if ($btn_id == $button_id) |
| 79 |
$i++; |
| 80 |
} |
| 81 |
$i--; // minus the current added button.. |
| 82 |
|
| 83 |
//$index = key($loaded); // find last index |
| 84 |
if ($i == 0) |
| 85 |
$document_id = $button_id; |
| 86 |
else |
| 87 |
$document_id = $button_id . "_" . $i; |
| 88 |
|
| 89 |
self::$doNext = false; |
| 90 |
self::$current_doc_id = $document_id; |
| 91 |
self::$current_button_id = $button_id; |
| 92 |
return $document_id; |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
static function buttonDone($args) |
| 97 |
{ |
| 98 |
$button_id = $args["button_id"]; |
| 99 |
$document_id = $args["document_id"]; |
| 100 |
|
| 101 |
foreach(self::$documentArray as $index => $data) |
| 102 |
{ |
| 103 |
foreach($data as $doc_button_id => $doc_data) |
| 104 |
{ |
| 105 |
if ($doc_button_id == $button_id && $doc_data["document_id"] == $document_id) |
| 106 |
{ |
| 107 |
self::$documentArray[$index][$button_id]["done"] = true; |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
public static function ajax_action() |
| 114 |
{ |
| 115 |
$action = sanitize_text_field($_POST['button_action']); |
| 116 |
$check = wp_verify_nonce($_POST['nonce'], 'button-' . $action); |
| 117 |
$button_id = isset($_POST['button_id']) ? intval($_POST["button_id"]) : -1; |
| 118 |
$button = MB()->getClass('button'); |
| 119 |
$paged = isset($_POST['paged']) ? intval($_POST['paged']) : ''; |
| 120 |
|
| 121 |
$redirect = admin_url() . 'admin.php'; |
| 122 |
$page = 'maxbuttons-controller'; |
| 123 |
|
| 124 |
if ($paged) |
| 125 |
$redirect = add_query_arg('paged', $paged, $redirect); |
| 126 |
|
| 127 |
if (! $check) |
| 128 |
exit('Invalid Nonce'); |
| 129 |
|
| 130 |
switch($action) |
| 131 |
{ |
| 132 |
case "delete": |
| 133 |
$button->delete($button_id); |
| 134 |
$redirect = add_query_arg(array('action' => 'list', 'message' => '1delete'), $redirect); |
| 135 |
//$redirect_url = admin_url() . 'admin.php?page=maxbuttons-controller&action=list&message=1delete'; |
| 136 |
break; |
| 137 |
|
| 138 |
case "trash": |
| 139 |
$result = $button->set($button_id); |
| 140 |
if ($result) |
| 141 |
$button->setStatus('trash'); |
| 142 |
$redirect = add_query_arg(array('action' => 'list', 'message' => '1'), $redirect); |
| 143 |
//$redirect_url = admin_url() . 'admin.php?page=maxbuttons-controller&action=list&message=1'; |
| 144 |
break; |
| 145 |
case "restore": |
| 146 |
$set = $button->set($button_id,'','trash'); |
| 147 |
$button->setStatus("publish"); |
| 148 |
$redirect = add_query_arg(array('action' => 'list', 'message' => '1restore', 'status' => 'trash'), $redirect); |
| 149 |
//$redirect_url = admin_url() . 'admin.php?page=maxbuttons-controller&action=list&status=trash&message=1restore'; |
| 150 |
break; |
| 151 |
case "copy": |
| 152 |
$button->set($button_id); |
| 153 |
$new_id = $button->copy(); |
| 154 |
$redirect = add_query_arg(array('action' => 'button', 'id' => $new_id), $redirect); |
| 155 |
//$redirect_url = admin_url() . 'admin.php?page=maxbuttons-controller&action=button&id=' . $new_id; |
| 156 |
break; |
| 157 |
case 'empty-trash': |
| 158 |
$mbadmin = MB()->getClass("admin"); |
| 159 |
$args = array('status' => 'trash', 'limit' => -1 ); |
| 160 |
$buttons = $mbadmin->getButtons($args); |
| 161 |
|
| 162 |
foreach($buttons as $btnar) |
| 163 |
{ |
| 164 |
$button_id = $btnar['id']; |
| 165 |
$button->delete($button_id); |
| 166 |
} |
| 167 |
$redirect = add_query_arg(array('action' => 'list', 'message' => 'empty-trash'), $redirect); |
| 168 |
// $redirect_url = admin_url() . 'admin.php?page=maxbuttons-controller&action=list&message=empty-trash'; |
| 169 |
break; |
| 170 |
|
| 171 |
} |
| 172 |
|
| 173 |
$redirect = add_query_arg('page', $page, $redirect); |
| 174 |
|
| 175 |
if (isset($redirect)) { |
| 176 |
$response = array('redirection' => $redirect); |
| 177 |
echo json_encode($response); |
| 178 |
exit(); |
| 179 |
} |
| 180 |
|
| 181 |
exit(true); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
public static function generate_css() |
| 186 |
{ |
| 187 |
global $wpdb; |
| 188 |
$id = isset($_REQUEST['id']) ? sanitize_text_field($_REQUEST['id']) : false; |
| 189 |
|
| 190 |
if (! $id) |
| 191 |
return; |
| 192 |
|
| 193 |
$idar = explode(',', $id); |
| 194 |
$idar = array_filter($idar, 'is_numeric'); |
| 195 |
$id = implode(',',$idar); |
| 196 |
|
| 197 |
$row = $wpdb->get_col($wpdb->prepare("SELECT cache FROM " . maxUtils::get_table_name() . " WHERE id in($id) and status ='%s'", 'publish')); |
| 198 |
|
| 199 |
|
| 200 |
header( "Content-type: text/css; charset: UTF-8" ); |
| 201 |
foreach($row as $content) |
| 202 |
{ |
| 203 |
echo $content; |
| 204 |
} |
| 205 |
exit(); |
| 206 |
} |
| 207 |
|
| 208 |
} // class |
| 209 |
|