| 1 |
<?php |
| 2 |
/** |
| 3 |
* AdminSettingsController.php |
| 4 |
* |
| 5 |
* The AdminSettingsController class file. |
| 6 |
* |
| 7 |
* PHP versions 5 |
| 8 |
* |
| 9 |
* @author Alexander Schneider <alexanderschneider85@gmail.com> |
| 10 |
* @copyright 2008-2017 Alexander Schneider |
| 11 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 |
| 12 |
* @version SVN: $id$ |
| 13 |
* @link http://wordpress.org/extend/plugins/user-access-manager/ |
| 14 |
*/ |
| 15 |
namespace UserAccessManager\Controller; |
| 16 |
|
| 17 |
use UserAccessManager\Config\Config; |
| 18 |
use UserAccessManager\Config\ConfigParameter; |
| 19 |
use UserAccessManager\FileHandler\FileHandler; |
| 20 |
use UserAccessManager\ObjectHandler\ObjectHandler; |
| 21 |
use UserAccessManager\Wrapper\Php; |
| 22 |
use UserAccessManager\Wrapper\Wordpress; |
| 23 |
|
| 24 |
class AdminSettingsController extends Controller |
| 25 |
{ |
| 26 |
/** |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
protected $template = 'AdminSettings.php'; |
| 30 |
|
| 31 |
/** |
| 32 |
* @var ObjectHandler |
| 33 |
*/ |
| 34 |
private $objectHandler; |
| 35 |
|
| 36 |
/** |
| 37 |
* @var FileHandler |
| 38 |
*/ |
| 39 |
private $fileHandler; |
| 40 |
|
| 41 |
/** |
| 42 |
* AdminSettingsController constructor. |
| 43 |
* |
| 44 |
* @param Php $php |
| 45 |
* @param Wordpress $wordpress |
| 46 |
* @param Config $config |
| 47 |
* @param ObjectHandler $objectHandler |
| 48 |
* @param FileHandler $fileHandler |
| 49 |
*/ |
| 50 |
public function __construct( |
| 51 |
Php $php, |
| 52 |
Wordpress $wordpress, |
| 53 |
Config $config, |
| 54 |
ObjectHandler $objectHandler, |
| 55 |
FileHandler $fileHandler |
| 56 |
) { |
| 57 |
parent::__construct($php, $wordpress, $config); |
| 58 |
$this->objectHandler = $objectHandler; |
| 59 |
$this->fileHandler = $fileHandler; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Returns true if the server is a nginx server. |
| 64 |
* |
| 65 |
* @return bool |
| 66 |
*/ |
| 67 |
public function isNginx() |
| 68 |
{ |
| 69 |
return $this->wordpress->isNginx(); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Returns the pages. |
| 74 |
* |
| 75 |
* @return array |
| 76 |
*/ |
| 77 |
public function getPages() |
| 78 |
{ |
| 79 |
$pages = $this->wordpress->getPages('sort_column=menu_order'); |
| 80 |
return is_array($pages) !== false ? $pages : []; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Returns the config parameters. |
| 85 |
* |
| 86 |
* @return \UserAccessManager\Config\ConfigParameter[] |
| 87 |
*/ |
| 88 |
public function getConfigParameters() |
| 89 |
{ |
| 90 |
return $this->config->getConfigParameters(); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Returns the post types as object. |
| 95 |
* |
| 96 |
* @return \WP_Post_Type[] |
| 97 |
*/ |
| 98 |
private function getPostTypes() |
| 99 |
{ |
| 100 |
return $this->wordpress->getPostTypes(['public' => true], 'objects'); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Returns the taxonomies as objects. |
| 105 |
* |
| 106 |
* @return \WP_Taxonomy[] |
| 107 |
*/ |
| 108 |
private function getTaxonomies() |
| 109 |
{ |
| 110 |
return $this->wordpress->getTaxonomies(['public' => true], 'objects'); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Returns the grouped config parameters. |
| 115 |
* |
| 116 |
* @return array |
| 117 |
*/ |
| 118 |
public function getGroupedConfigParameters() |
| 119 |
{ |
| 120 |
$configParameters = $this->config->getConfigParameters(); |
| 121 |
|
| 122 |
$groupedConfigParameters = []; |
| 123 |
$postTypes = $this->getPostTypes(); |
| 124 |
|
| 125 |
foreach ($postTypes as $postType => $postTypeObject) { |
| 126 |
if ($postType === ObjectHandler::ATTACHMENT_OBJECT_TYPE) { |
| 127 |
continue; |
| 128 |
} |
| 129 |
|
| 130 |
$groupedConfigParameters[$postType] = [ |
| 131 |
$configParameters["hide_{$postType}"], |
| 132 |
$configParameters["hide_{$postType}_title"], |
| 133 |
$configParameters["{$postType}_title"], |
| 134 |
$configParameters["{$postType}_content"], |
| 135 |
$configParameters["hide_{$postType}_comment"], |
| 136 |
$configParameters["{$postType}_comment_content"], |
| 137 |
$configParameters["{$postType}_comments_locked"] |
| 138 |
]; |
| 139 |
|
| 140 |
if ($postType === 'post') { |
| 141 |
$groupedConfigParameters[$postType][] = $configParameters["show_{$postType}_content_before_more"]; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
$taxonomies = $this->getTaxonomies(); |
| 146 |
|
| 147 |
foreach ($taxonomies as $taxonomy => $taxonomyObject) { |
| 148 |
$groupedConfigParameters[$taxonomy][] = $configParameters["hide_empty_{$taxonomy}"]; |
| 149 |
} |
| 150 |
|
| 151 |
$groupedConfigParameters['file'] = [ |
| 152 |
$configParameters['lock_file'], |
| 153 |
$configParameters['download_type'] |
| 154 |
]; |
| 155 |
|
| 156 |
$groupedConfigParameters['author'] = [ |
| 157 |
$configParameters['authors_has_access_to_own'], |
| 158 |
$configParameters['authors_can_add_posts_to_groups'], |
| 159 |
$configParameters['full_access_role'], |
| 160 |
]; |
| 161 |
|
| 162 |
$groupedConfigParameters['other'] = [ |
| 163 |
$configParameters['lock_recursive'], |
| 164 |
$configParameters['protect_feed'], |
| 165 |
$configParameters['redirect'], |
| 166 |
$configParameters['blog_admin_hint'], |
| 167 |
$configParameters['blog_admin_hint_text'], |
| 168 |
]; |
| 169 |
|
| 170 |
if ($this->config->isPermalinksActive() === true) { |
| 171 |
$groupedConfigParameters['file'][] = $configParameters['lock_file_types']; |
| 172 |
$groupedConfigParameters['file'][] = $configParameters['file_pass_type']; |
| 173 |
} |
| 174 |
|
| 175 |
return $groupedConfigParameters; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Update settings action. |
| 180 |
*/ |
| 181 |
public function updateSettingsAction() |
| 182 |
{ |
| 183 |
$this->verifyNonce('uamUpdateSettings'); |
| 184 |
|
| 185 |
$newConfigParameters = $this->getRequestParameter('config_parameters'); |
| 186 |
$newConfigParameters = array_map( |
| 187 |
function ($entry) { |
| 188 |
return htmlentities(str_replace('\\', '', $entry)); |
| 189 |
}, |
| 190 |
$newConfigParameters |
| 191 |
); |
| 192 |
$this->config->setConfigParameters($newConfigParameters); |
| 193 |
|
| 194 |
if ($this->config->lockFile() === false) { |
| 195 |
$this->fileHandler->deleteFileProtection(); |
| 196 |
} else { |
| 197 |
$this->fileHandler->createFileProtection(); |
| 198 |
} |
| 199 |
|
| 200 |
$this->wordpress->doAction('uam_update_options', $this->config); |
| 201 |
$this->setUpdateMessage(TXT_UAM_UPDATE_SETTINGS); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Checks if the group is a post type. |
| 206 |
* |
| 207 |
* @param string $groupKey |
| 208 |
* |
| 209 |
* @return bool |
| 210 |
*/ |
| 211 |
public function isPostTypeGroup($groupKey) |
| 212 |
{ |
| 213 |
$postTypes = $this->getPostTypes(); |
| 214 |
|
| 215 |
return isset($postTypes[$groupKey]); |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Returns the right translation string. |
| 220 |
* |
| 221 |
* @param string $groupKey |
| 222 |
* @param string $ident |
| 223 |
* @param bool $description |
| 224 |
* |
| 225 |
* @return mixed|string |
| 226 |
*/ |
| 227 |
private function getObjectText($groupKey, $ident, $description = false) |
| 228 |
{ |
| 229 |
$objects = $this->getPostTypes() + $this->getTaxonomies(); |
| 230 |
$ident .= ($description === true) ? '_DESC' : ''; |
| 231 |
|
| 232 |
if (isset($objects[$groupKey]) === true) { |
| 233 |
$ident = str_replace(strtoupper($groupKey), 'OBJECT', $ident); |
| 234 |
$text = constant($ident); |
| 235 |
$count = substr_count($text, '%s'); |
| 236 |
$arguments = $this->php->arrayFill(0, $count, $objects[$groupKey]->labels->name); |
| 237 |
return vsprintf($text, $arguments); |
| 238 |
} |
| 239 |
|
| 240 |
return constant($ident); |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* @param string $groupKey |
| 245 |
* @param bool $description |
| 246 |
* |
| 247 |
* @return string |
| 248 |
*/ |
| 249 |
public function getSectionText($groupKey, $description = false) |
| 250 |
{ |
| 251 |
return $this->getObjectText( |
| 252 |
$groupKey, |
| 253 |
'TXT_UAM_'.strtoupper($groupKey).'_SETTING', |
| 254 |
$description |
| 255 |
); |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Returns the label for the parameter. |
| 260 |
* |
| 261 |
* @param string $groupKey |
| 262 |
* @param ConfigParameter $configParameter |
| 263 |
* @param bool $description |
| 264 |
* |
| 265 |
* @return string |
| 266 |
*/ |
| 267 |
public function getParameterText($groupKey, ConfigParameter $configParameter, $description = false) |
| 268 |
{ |
| 269 |
$ident = 'TXT_UAM_'.strtoupper($configParameter->getId()); |
| 270 |
|
| 271 |
return $this->getObjectText( |
| 272 |
$groupKey, |
| 273 |
$ident, |
| 274 |
$description |
| 275 |
); |
| 276 |
} |
| 277 |
} |
| 278 |
|