| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* A group of classes and methods to create and manage post viewtables. |
| 5 |
* |
| 6 |
* @author Alex Kovalev <alex.kovalevv@gmail.com> |
| 7 |
* @copyright (c) 2018, Webcraftic Ltd |
| 8 |
* |
| 9 |
* @package factory-viewtables |
| 10 |
* @since 1.0.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
// Exit if accessed directly |
| 14 |
if( !defined('ABSPATH') ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
if( !class_exists('Wbcr_FactoryViewtables410_Viewtable') ) { |
| 19 |
|
| 20 |
abstract class Wbcr_FactoryViewtables410_Viewtable { |
| 21 |
|
| 22 |
/** |
| 23 |
* A type used to display the table. |
| 24 |
* @var Wbcr_FactoryTypes410_Type |
| 25 |
*/ |
| 26 |
public $type; |
| 27 |
|
| 28 |
/** |
| 29 |
* Table's columns |
| 30 |
* @var FactoryViewtables410_Columns |
| 31 |
*/ |
| 32 |
public $columns; |
| 33 |
|
| 34 |
/** |
| 35 |
* Scripts that must be included on edit page. |
| 36 |
* @var Wbcr_Factory422_ScriptList |
| 37 |
*/ |
| 38 |
public $scripts; |
| 39 |
|
| 40 |
/** |
| 41 |
* Styles that must be included on edit page. |
| 42 |
* @var Wbcr_Factory422_StyleList |
| 43 |
*/ |
| 44 |
public $styles; |
| 45 |
|
| 46 |
/** |
| 47 |
* Creates a new instance of a viewtabl. |
| 48 |
* |
| 49 |
* @since 1.0.0 |
| 50 |
* @param Wbcr_Factory422_Plugin $plugin |
| 51 |
*/ |
| 52 |
public function __construct(Wbcr_Factory422_Plugin $plugin) |
| 53 |
{ |
| 54 |
$this->plugin = $plugin; |
| 55 |
} |
| 56 |
|
| 57 |
public function connect($type) |
| 58 |
{ |
| 59 |
|
| 60 |
$this->type = $type; |
| 61 |
$this->columns = new FactoryViewtables410_Columns(); |
| 62 |
|
| 63 |
$this->scripts = $this->plugin->newScriptList(); |
| 64 |
$this->styles = $this->plugin->newStyleList(); |
| 65 |
|
| 66 |
$this->configure(); |
| 67 |
|
| 68 |
add_filter('manage_edit-' . $type->name . '_columns', array($this, 'actionColumns')); |
| 69 |
add_action('manage_' . $type->name . '_posts_custom_column', array($this, 'actionColumnValues'), 2); |
| 70 |
|
| 71 |
// includes styles and scripts |
| 72 |
if( !$this->scripts->isEmpty() || !$this->styles->isEmpty() ) { |
| 73 |
add_action('admin_enqueue_scripts', array($this, 'actionAdminScripts')); |
| 74 |
} |
| 75 |
|
| 76 |
// remove quiik edit for non-public types |
| 77 |
if( $type->template !== 'public' ) { |
| 78 |
add_filter('post_row_actions', array($this, 'actionPostRowActions'), 10, 2); |
| 79 |
} |
| 80 |
|
| 81 |
// remove buld edit action |
| 82 |
if( $type->template !== 'public' ) { |
| 83 |
add_filter('bulk_actions-edit-' . $this->type->name, array($this, 'actionBulk')); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
public function configure() |
| 88 |
{ |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* @param $columns |
| 93 |
* @return array |
| 94 |
*/ |
| 95 |
public function actionColumns($columns) |
| 96 |
{ |
| 97 |
|
| 98 |
if( $this->columns->isClearn ) { |
| 99 |
$columns = array(); |
| 100 |
$columns["cb"] = "<input type=\"checkbox\" />"; |
| 101 |
} |
| 102 |
|
| 103 |
foreach($this->columns->getAll() as $column) { |
| 104 |
$columns[$column['id']] = $column['title']; |
| 105 |
} |
| 106 |
|
| 107 |
return $columns; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* @param $column |
| 112 |
* @return bool |
| 113 |
*/ |
| 114 |
public function actionColumnValues($column) |
| 115 |
{ |
| 116 |
global $post; |
| 117 |
|
| 118 |
$postfix = strtoupper(substr($column, 0, 1)) . substr($column, 1, strlen($column)); |
| 119 |
$function_name = 'column' . $postfix; |
| 120 |
$full_mode = (isset($_GET['mode']) && $_GET['mode'] == 'excerpt'); |
| 121 |
|
| 122 |
if( !method_exists($this, $function_name) ) { |
| 123 |
return false; |
| 124 |
} |
| 125 |
call_user_func(array($this, $function_name), $post, $full_mode); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Actions that includes registered fot this type scritps and styles. |
| 130 |
* @global Wp_Post $post |
| 131 |
* @param string $hook |
| 132 |
*/ |
| 133 |
public function actionAdminScripts($hook) |
| 134 |
{ |
| 135 |
global $post; |
| 136 |
|
| 137 |
if( !$post ) { |
| 138 |
return; |
| 139 |
} |
| 140 |
if( $hook !== 'edit.php' ) { |
| 141 |
return; |
| 142 |
} |
| 143 |
if( $post->post_type != $this->type->name ) { |
| 144 |
return; |
| 145 |
} |
| 146 |
if( $this->scripts->isEmpty() && $this->styles->isEmpty() ) { |
| 147 |
return; |
| 148 |
} |
| 149 |
|
| 150 |
$this->scripts->connect(); |
| 151 |
$this->styles->connect(); |
| 152 |
} |
| 153 |
|
| 154 |
public function actionPostRowActions($actions) |
| 155 |
{ |
| 156 |
global $post; |
| 157 |
|
| 158 |
if( $post->post_type !== $this->type->name ) { |
| 159 |
return $actions; |
| 160 |
} |
| 161 |
unset($actions['inline hide-if-no-js']); |
| 162 |
|
| 163 |
return $actions; |
| 164 |
} |
| 165 |
|
| 166 |
public function actionBulk($actions) |
| 167 |
{ |
| 168 |
global $post; |
| 169 |
|
| 170 |
if( !$post ) { |
| 171 |
return $actions; |
| 172 |
} |
| 173 |
if( $post->post_type !== $this->type->name ) { |
| 174 |
return $actions; |
| 175 |
} |
| 176 |
unset($actions['edit']); |
| 177 |
|
| 178 |
return $actions; |
| 179 |
} |
| 180 |
} |
| 181 |
} |