[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

/includes/Views/ -> AdminParams.php (source)

   1  <?php
   2  /**
   3   * Parameters used to display links the admin view (eg admin/index.php)
   4   *
   5   * @since 1.8.2
   6   */
   7  
   8  namespace YOURLS\Views;
   9  
  10  /**
  11   * Class AdminParams to get admin view parameters (number of links to display, search, ...)
  12   *
  13   * @since   1.8.2
  14   * @package YOURLS\Views
  15   */
  16  class AdminParams
  17  {
  18  
  19      /**
  20       * All possible search parameters. Populated in the constructor.
  21       *
  22       * @var array
  23       */
  24      private $possible_search_params;
  25  
  26      /**
  27       * All possible sort parameters. Populated in the constructor.
  28       *
  29       * @var array
  30       */
  31      private $possible_sort_params;
  32  
  33      /**
  34       * All possible date sorting parameters. Populated in the constructor.
  35       *
  36       * @var array
  37       */
  38      private $possible_date_sorting;
  39  
  40      /**
  41       * Parameter translations. Populated in the constructor.
  42       *
  43       * @var array
  44       */
  45      private $params_translations;
  46  
  47  
  48      /**
  49       * Admin constructor : populate all default parameters
  50       *
  51       */
  52      public function __construct()
  53      {
  54          // Cast return values of yourls_apply_filter() to array in case a hook would incorrectly return something else
  55          $this->possible_search_params = (array)yourls_apply_filter('admin_params_possible_search',
  56              ['all', 'keyword', 'url', 'title', 'ip']);
  57          $this->possible_sort_params   = (array)yourls_apply_filter('admin_params_possible_sort',
  58              ['keyword', 'url', 'title', 'ip', 'timestamp', 'clicks']);
  59          $this->params_translations    = (array)yourls_apply_filter('admin_params_possible_translations',[
  60              'all'       => yourls__('All fields'),
  61              'keyword'   => yourls__('Short URL'),
  62              'url'       => yourls__('URL'),
  63              'title'     => yourls__('Title'),
  64              'ip'        => yourls__('IP Address'),
  65              'timestamp' => yourls__('Date'),
  66              'clicks'    => yourls__('Clicks'),
  67          ]);
  68          $this->possible_date_sorting  = (array)yourls_apply_filter('admin_params_possible_date_sort',
  69              ['before', 'after', 'between']);
  70      }
  71  
  72      /**
  73       * Get the number of links to display per page
  74       *
  75       * @since 1.8.2
  76       *
  77       * @param int $default default number of links to display
  78       * @return int
  79       */
  80      public function get_per_page(int $default): int
  81      {
  82          // return if we have a value and it's not 0
  83          if (isset($_GET['perpage']) && intval($_GET['perpage'])) {
  84              $per_page = intval($_GET['perpage']);
  85              // otherwise return filtered default value
  86          } else {
  87              // @hook Default number of links to display (value provided by caller eg /admin/index.php)
  88              $per_page = yourls_apply_filter('admin_view_per_page', $default);
  89          }
  90  
  91          return $per_page;
  92      }
  93  
  94      /**
  95       * Get the current page number to be displayed
  96       *
  97       * @since 1.8.2
  98       *
  99       * @return int
 100       */
 101      public function get_page(): int
 102      {
 103          return isset($_GET['page']) ? intval($_GET['page']) : 1;
 104      }
 105  
 106      /**
 107       * Get search text (the 'Search for') from query string variables search_protocol, search_slashes and search
 108       *
 109       * Some servers don't like query strings containing "(ht|f)tp(s)://". A javascript bit
 110       * explodes the search text into protocol, slashes and the rest (see JS function
 111       * split_search_text_before_search()) and this function glues pieces back together
 112       * See issue https://github.com/YOURLS/YOURLS/issues/1576
 113       *
 114       * @since 1.8.2
 115       *
 116       * @return string
 117       */
 118      public function get_search(): string
 119      {
 120          $search = '';
 121          if (isset($_GET['search_protocol'])) {
 122              $search .= $_GET['search_protocol'];
 123          }
 124          if (isset($_GET['search_slashes'])) {
 125              $search .= $_GET['search_slashes'];
 126          }
 127          if (isset($_GET['search'])) {
 128              $search .= $_GET['search'];
 129          }
 130  
 131          // @hook Default search text in links displayed
 132          return yourls_apply_filter('admin_view_get_search_text', htmlspecialchars(trim($search)));
 133      }
 134  
 135      /**
 136       * Get the 'Search In' parameter (one of 'all', 'keyword', 'url', 'title', 'ip')
 137       *
 138       * @since 1.8.2
 139       *
 140       * @return string
 141       */
 142      public function get_search_in(): string
 143      {
 144          if (isset($_GET['search_in']) && in_array($_GET['search_in'], $this->possible_search_params)) {
 145              $search_in = $_GET['search_in'];
 146          } else {
 147              // @hook Default searching in the admin view (in all fields)
 148              $search_in = yourls_apply_filter('admin_view_search_in', 'all');
 149          }
 150  
 151          return $search_in;
 152      }
 153  
 154      /**
 155       * Get the 'Sort by' parameter
 156       *
 157       * @since 1.8.2
 158       *
 159       * @return string
 160       */
 161      public function get_sort_by(): string
 162      {
 163          if (isset($_GET['sort_by']) && in_array($_GET['sort_by'], $this->possible_sort_params)) {
 164              $sort_by = $_GET['sort_by'];
 165          } else {
 166              // @hook Default sorting in the admin view (by Timestamp)
 167              $sort_by = yourls_apply_filter('admin_view_sort_by', 'timestamp');
 168          }
 169  
 170          return $sort_by;
 171      }
 172  
 173      /**
 174       * Get the correct phrasing associated to a search or sort parameter (ie 'all' -> 'All fields' for instance)
 175       *
 176       * No checks : you need to supply an existing parameter, see $params_translations
 177       *
 178       * @since 1.8.2
 179       *
 180       * @param string $param
 181       * @return string
 182       */
 183      public function get_param_long_name(string $param): string
 184      {
 185          return $this->params_translations[$param];
 186      }
 187  
 188      /**
 189       * Get the sort order (asc or desc)
 190       *
 191       * @since 1.8.2
 192       *
 193       * @return mixed
 194       */
 195      public function get_sort_order()
 196      {
 197          // @hook Default sorting order in the admin view (descending)
 198          return isset($_GET['sort_order']) && $_GET['sort_order'] == 'asc' ? 'asc' : yourls_apply_filter('admin_view_sort_order', 'desc');
 199  
 200      }
 201  
 202      /**
 203       * Get the click "more or less than"
 204       *
 205       * @since 1.8.2
 206       *
 207       * @return mixed
 208       */
 209      public function get_click_filter()
 210      {
 211          // @hook Default 'Show links with more/less than' ('more')
 212          return isset($_GET['click_filter']) && $_GET['click_filter'] == 'less' ? 'less' : yourls_apply_filter('admin_view_click_filter', 'more');
 213      }
 214  
 215      /**
 216       * Get the click threshold
 217       *
 218       * @since 1.8.2
 219       *
 220       * @return int|string
 221       */
 222      public function get_click_limit()
 223      {
 224          // @hook Default link click threshold (unset)
 225          return (isset($_GET['click_limit']) && intval($_GET['click_limit'])) ?
 226              intval($_GET['click_limit']) : yourls_apply_filter('admin_view_click_limit', '');
 227      }
 228  
 229  
 230      /**
 231       * Get the date parameters : the date "filter" and the two dates
 232       *
 233       * @since 1.8.2
 234       *
 235       * @return array
 236       */
 237      public function get_date_params(): array
 238      {
 239          if (isset($_GET['date_filter']) && in_array($_GET['date_filter'], $this->possible_date_sorting)) {
 240              $date_filter = $_GET['date_filter'];
 241          } else {
 242              // @hook Default date filtering (unset)
 243              $date_filter = yourls_apply_filter('admin_view_date_filter', '');
 244          }
 245  
 246          switch ($date_filter) {
 247              case 'after':
 248              case 'before':
 249                  if (isset($_GET['date_first']) && yourls_sanitize_date($_GET['date_first'])) {
 250                      $date_first = yourls_sanitize_date($_GET['date_first']);
 251                  } else {
 252                      // @hook Default date when date filter is either 'after' or 'before' (unset)
 253                      // In such case, the filter is either 'admin_view_date_first_after' or 'admin_view_date_first_before'
 254                      $date_first = yourls_apply_filter('admin_view_date_first_' . $date_filter, '');
 255                  }
 256                  $date_second = '';
 257                  break;
 258  
 259              case 'between':
 260                  if (isset($_GET['date_first']) && isset($_GET['date_second']) && yourls_sanitize_date($_GET['date_first']) && yourls_sanitize_date($_GET['date_second'])) {
 261                      $date_first  = yourls_sanitize_date($_GET['date_first']);
 262                      $date_second = yourls_sanitize_date($_GET['date_second']);
 263                  } else {
 264                      // @hook Default dates when date filter is 'between' (unset)
 265                      $date_first  = yourls_apply_filter('admin_view_date_first_between', '');
 266                      $date_second = yourls_apply_filter('admin_view_date_second_between', '');
 267                  }
 268                  break;
 269  
 270              default:
 271                  // @hook Default date when date filter is unset (unset)
 272                  $date_first  = yourls_apply_filter('admin_view_date_first_unset', '');
 273                  $date_second = yourls_apply_filter('admin_view_date_second_unset', '');
 274  
 275          }
 276  
 277          return ['date_filter' => $date_filter, 'date_first' => $date_first, 'date_second' => $date_second];
 278      }
 279  
 280  }


Generated: Wed Sep 28 05:10:02 2022 Cross-referenced by PHPXref 0.7.1