Skip to content

Commit

Permalink
Initial working version - Add route type filter in admin area post li…
Browse files Browse the repository at this point in the history
…sting #92
  • Loading branch information
alexboia committed Feb 17, 2024
1 parent ca4c003 commit cc5bb88
Show file tree
Hide file tree
Showing 15 changed files with 722 additions and 175 deletions.
123 changes: 62 additions & 61 deletions lib/display/postListing/ColumnCustomization.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,76 +34,77 @@
}

class Abp01_Display_PostListing_ColumnCustomization implements Abp01_Display_PostListing_Customization {
/**
* @var Abp01_Display_PostListing_Column[]
*/
private $_columns = array();
/**
* @var Abp01_Display_PostListing_Column[]
*/
private $_columns = array();

private $_forPostTypes = array();
private $_forPostTypes = array();

/**
* @param Abp01_Display_PostListing_Column[] $columns
* @param string[] $forPostTypes
* @return void
*/
public function __construct(array $columns, array $forPostTypes) {
$this->_columns = array();
foreach ($columns as $c) {
$this->_columns[$c->getKey()] = $c;
}
/**
* @param Abp01_Display_PostListing_Column[] $columns
* @param string[] $forPostTypes
* @return void
*/
public function __construct(array $columns, array $forPostTypes) {
$this->_columns = array();
foreach ($columns as $c) {
$this->_columns[$c->getKey()] = $c;
}

$this->_forPostTypes = $forPostTypes;
}
$this->_forPostTypes = $forPostTypes;
}

public function apply() {
add_filter('manage_posts_columns',
array($this, 'registerPostListingColumns'),
10, 2);
add_filter('manage_pages_columns',
array($this, 'registerPageListingColumns'),
10, 1);
public function apply() {
//TODO: obey post types returned by Abp01_AvailabilityHelper::getTripSummaryAvailableForPostTypes()
add_filter('manage_posts_columns',
array($this, 'registerPostListingColumns'),
10, 2);
add_filter('manage_pages_columns',
array($this, 'registerPageListingColumns'),
10, 1);

add_action('manage_posts_custom_column',
array($this, 'getPostListingCustomColumnValue'),
10, 2);
add_action('manage_pages_custom_column',
array($this, 'getPostListingCustomColumnValue'),
10, 2);
}
add_action('manage_posts_custom_column',
array($this, 'getPostListingCustomColumnValue'),
10, 2);
add_action('manage_pages_custom_column',
array($this, 'getPostListingCustomColumnValue'),
10, 2);
}

public function registerPostListingColumns($existingColumns, $postType) {
if ($this->_shouldAddColumnsForPostTypeListing($postType)) {
$existingColumns = $this->_addCustomColumns($existingColumns);
}
return $existingColumns;
}
public function registerPostListingColumns($existingColumns, $postType) {
if ($this->_shouldAddColumnsForPostTypeListing($postType)) {
$existingColumns = $this->_addCustomColumns($existingColumns);
}
return $existingColumns;
}

private function _shouldAddColumnsForPostTypeListing($postType) {
return in_array($postType, $this->_forPostTypes);
}
private function _shouldAddColumnsForPostTypeListing($postType) {
return in_array($postType, $this->_forPostTypes);
}

private function _addCustomColumns($existingColumns) {
foreach ($this->_columns as $key => $column) {
$existingColumns[$key] = $column->renderLabel();
}
return $existingColumns;
}
private function _addCustomColumns($existingColumns) {
foreach ($this->_columns as $key => $column) {
$existingColumns[$key] = $column->renderLabel();
}
return $existingColumns;
}

public function registerPageListingColumns($existingColumns) {
if ($this->_shouldAddColumnsForPageListing()) {
$existingColumns = $this->_addCustomColumns($existingColumns);
}
return $existingColumns;
}
public function registerPageListingColumns($existingColumns) {
if ($this->_shouldAddColumnsForPageListing()) {
$existingColumns = $this->_addCustomColumns($existingColumns);
}
return $existingColumns;
}

private function _shouldAddColumnsForPageListing() {
return $this->_shouldAddColumnsForPostTypeListing('page');
}
private function _shouldAddColumnsForPageListing() {
return $this->_shouldAddColumnsForPostTypeListing('page');
}

public function getPostListingCustomColumnValue($columnKey, $postId) {
if (isset($this->_columns[$columnKey])) {
$column = $this->_columns[$columnKey];
echo $column->renderValue($postId);
}
}
public function getPostListingCustomColumnValue($columnKey, $postId) {
if (isset($this->_columns[$columnKey])) {
$column = $this->_columns[$columnKey];
echo $column->renderValue($postId);
}
}
}
68 changes: 68 additions & 0 deletions lib/display/postListing/Filter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Copyright (c) 2014-2024 Alexandru Boia and Contributors
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/

if (!defined('ABP01_LOADED') || !ABP01_LOADED) {
exit;
}

abstract class Abp01_Display_PostListing_Filter {
protected $_key;

protected $_label;

protected $_processor;

public function __construct(string $key, string|null $label, Abp01_Display_PostListing_FilterProcessor $processor) {
$this->_key = $key;
$this->_label = $label;
$this->_processor = $processor;
}

abstract public function render();

public function process() {
$this->_processor->processFilter($this);
}

public function getCurrentValue(): mixed {
return isset($_GET[$this->_key])
? trim(sanitize_text_field($_GET[$this->_key]))
: '';
}

public function getKey(): string {
return $this->_key;
}

public function getLabel(): string|null {
return $this->_label;
}
}
75 changes: 75 additions & 0 deletions lib/display/postListing/FilterCustomization.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* Copyright (c) 2014-2024 Alexandru Boia and Contributors
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/

if (!defined('ABP01_LOADED') || !ABP01_LOADED) {
exit ;
}

class Abp01_Display_PostListing_FilterCustomization implements Abp01_Display_PostListing_Customization {

private $_filters = array();

private $_forPostTypes = array();

/**
* @param Abp01_Display_PostListing_Filter[] $filters
* @param string[] $postTypes
*/
public function __construct(array $filters, array $postTypes) {
$this->_filters = $filters;
$this->_forPostTypes = $postTypes;
}

public function apply() {
$this->_initProcessing();
$this->_initDisplay();
}

private function _initDisplay() {
add_action('restrict_manage_posts', array($this, 'displayFilters'), 10, 2);
}

public function displayFilters($postType, $which) {
if (!in_array($postType, $this->_forPostTypes)) {
return;
}

foreach ($this->_filters as $filter) {
echo $filter->render();
}
}

private function _initProcessing() {
foreach ($this->_filters as $filter) {
$filter->process();
}
}
}
38 changes: 38 additions & 0 deletions lib/display/postListing/FilterDataSource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Copyright (c) 2014-2024 Alexandru Boia and Contributors
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/

if (!defined('ABP01_LOADED') || !ABP01_LOADED) {
exit ;
}

interface Abp01_Display_PostListing_FilterDataSource {
function getOptions();
}
Loading

0 comments on commit cc5bb88

Please sign in to comment.