Controller
6 days ago
Helper
3 weeks ago
Model
6 days ago
external
6 days ago
view
6 days ago
.gitignore
5 years ago
BuildAutoLoader.php
3 weeks ago
Controller.php
3 weeks ago
Model.php
3 weeks ago
ViewController.php
3 weeks ago
plugin.json
1 month ago
Controller.php
62 lines
| 1 | <?php |
| 2 | namespace ShortPixel; |
| 3 | |
| 4 | if ( ! defined( 'ABSPATH' ) ) { |
| 5 | exit; // Exit if accessed directly. |
| 6 | } |
| 7 | |
| 8 | use ShortPixel\Helper\UiHelper as UiHelper; |
| 9 | |
| 10 | /** |
| 11 | * Proto parent class for all controllers. |
| 12 | * |
| 13 | * So far none of the controllers need or implement similar enough functions for a parent |
| 14 | * to make sense. Perhaps this will change over time, so most controllers extend this parent. |
| 15 | * |
| 16 | * @package ShortPixel |
| 17 | */ |
| 18 | |
| 19 | class Controller |
| 20 | { |
| 21 | |
| 22 | /** @var mixed|null Connected model instance. */ |
| 23 | protected $model; |
| 24 | |
| 25 | /** @var bool Whether the current user has sufficient privileges to use the plugin. */ |
| 26 | protected $userIsAllowed = false; |
| 27 | |
| 28 | public function __construct() |
| 29 | { |
| 30 | $this->userIsAllowed = $this->checkUserPrivileges(); |
| 31 | } |
| 32 | |
| 33 | |
| 34 | /** |
| 35 | * Determines whether the current WordPress user has the required capabilities |
| 36 | * to interact with the plugin (manage_options, upload_files, or edit_posts). |
| 37 | * |
| 38 | * @return bool True if the user is allowed, false otherwise. |
| 39 | */ |
| 40 | protected function checkUserPrivileges() |
| 41 | { |
| 42 | if ((current_user_can( 'manage_options' ) || current_user_can( 'upload_files' ) || current_user_can( 'edit_posts' ))) |
| 43 | return true; |
| 44 | |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Formats a number for display, delegating to UiHelper::formatNumber. |
| 50 | * |
| 51 | * @param int|float $number The number to format. |
| 52 | * @param int $precision Number of decimal places. Default 2. |
| 53 | * @return string Formatted number string. |
| 54 | */ |
| 55 | // helper for a helper. |
| 56 | protected function formatNumber($number, $precision = 2) |
| 57 | { |
| 58 | return UIHelper::formatNumber($number, $precision); |
| 59 | } |
| 60 | |
| 61 | } // class |
| 62 |