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