comment.php
10 years ago
link.php
10 years ago
media.php
10 years ago
post.php
10 years ago
user.php
10 years ago
post.php
246 lines
| 1 | <?php |
| 2 | |
| 3 | class CPAC_Storage_Model_Post extends CPAC_Storage_Model { |
| 4 | |
| 5 | public $post_type; |
| 6 | |
| 7 | private $post_type_object; |
| 8 | |
| 9 | /** |
| 10 | * @since 2.0 |
| 11 | */ |
| 12 | public function __construct( $post_type ) { |
| 13 | |
| 14 | $this->set_post_type( $post_type ); |
| 15 | |
| 16 | $this->key = $this->post_type; |
| 17 | $this->label = $this->post_type_object->labels->name; |
| 18 | $this->singular_label = $this->post_type_object->labels->singular_name; |
| 19 | $this->type = 'post'; |
| 20 | $this->meta_type = 'post'; |
| 21 | $this->page = 'edit'; |
| 22 | $this->menu_type = 'post'; |
| 23 | |
| 24 | // Headings |
| 25 | |
| 26 | // Since 3.1 |
| 27 | add_filter( "manage_{$post_type}_posts_columns", array( $this, 'add_headings' ), 100 ); |
| 28 | |
| 29 | // Deprecated ( as of 3.1 ) Note: This one is still used by woocommerce. |
| 30 | // Priority set to 100 top make sure the WooCommerce headings are overwritten by CAC |
| 31 | // Filter is located in get_column_headers(). |
| 32 | // @todo_minor check compatibility issues for this deprecated filter |
| 33 | add_filter( "manage_{$this->page}-{$post_type}_columns", array( $this, 'add_headings' ), 100 ); |
| 34 | |
| 35 | // values |
| 36 | add_action( "manage_{$this->post_type}_posts_custom_column", array( $this, 'manage_value_callback' ), 100, 2 ); |
| 37 | |
| 38 | add_action( 'load-edit.php', array( $this, 'set_columns_on_current_screen' ), 1000 ); |
| 39 | |
| 40 | parent::__construct(); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * @since 2.3.5 |
| 45 | */ |
| 46 | public function get_post_type() { |
| 47 | return $this->post_type; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @since 2.4.7 |
| 52 | */ |
| 53 | public function get_posts( $args = array() ) { |
| 54 | $defaults = array( |
| 55 | 'numberposts' => - 1, |
| 56 | 'post_status' => array( 'any', 'trash' ), |
| 57 | 'post_type' => $this->post_type, |
| 58 | 'fields' => 'ids', |
| 59 | 'no_found_rows' => 1, // lowers our carbon footprint |
| 60 | ); |
| 61 | return (array) get_posts( array_merge( $defaults, $args ) ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @since 2.3.5 |
| 66 | */ |
| 67 | private function set_post_type( $post_type ) { |
| 68 | $this->post_type = $post_type; |
| 69 | $this->post_type_object = get_post_type_object( $post_type ); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @since 2.2.1 |
| 74 | */ |
| 75 | public function get_original_column_value( $column, $id ) { |
| 76 | |
| 77 | global $post; |
| 78 | |
| 79 | // Setup post data for current post |
| 80 | $post_old = $post; |
| 81 | $post = get_post( $id ); |
| 82 | setup_postdata( $post ); |
| 83 | |
| 84 | // Remove Admin Columns action for this column's value |
| 85 | remove_action( "manage_{$this->post_type}_posts_custom_column", array( $this, 'manage_value_callback' ), 100, 2 ); |
| 86 | |
| 87 | ob_start(); |
| 88 | // Run WordPress native actions to display column content |
| 89 | if ( is_post_type_hierarchical( $this->post_type ) ) { |
| 90 | do_action( 'manage_pages_custom_column', $column, $id ); |
| 91 | } else { |
| 92 | do_action( 'manage_posts_custom_column', $column, $id ); |
| 93 | } |
| 94 | |
| 95 | do_action( "manage_{$this->post_type}_posts_custom_column", $column, $id ); |
| 96 | |
| 97 | $contents = ob_get_clean(); |
| 98 | |
| 99 | // Add removed Admin Columns action for this column's value |
| 100 | add_action( "manage_{$this->post_type}_posts_custom_column", array( $this, 'manage_value_callback' ), 100, 2 ); |
| 101 | |
| 102 | // Restore original post object |
| 103 | $post = $post_old; |
| 104 | |
| 105 | if ( $post ) { |
| 106 | setup_postdata( $post ); |
| 107 | } |
| 108 | |
| 109 | return $contents; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @since 2.4.4 |
| 114 | */ |
| 115 | public function get_default_column_names() { |
| 116 | |
| 117 | $defaults = array( 'date' ); |
| 118 | |
| 119 | if ( post_type_supports( $this->post_type, 'title' ) ) { |
| 120 | $defaults[] = 'title'; |
| 121 | } |
| 122 | if ( post_type_supports( $this->post_type, 'comments' ) ) { |
| 123 | $defaults[] = 'comments'; |
| 124 | } |
| 125 | |
| 126 | if ( in_array( $this->post_type, array( 'post', 'page' ) ) ) { |
| 127 | $defaults[] = 'cb'; |
| 128 | $defaults[] = 'author'; |
| 129 | $defaults[] = 'categories'; |
| 130 | $defaults[] = 'comments'; |
| 131 | $defaults[] = 'parent'; |
| 132 | $defaults[] = 'tags'; |
| 133 | } |
| 134 | |
| 135 | return $defaults; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * @since 2.0 |
| 140 | */ |
| 141 | protected function get_screen_link() { |
| 142 | return add_query_arg( array( 'post_type' => $this->key ), admin_url( $this->page . '.php' ) ); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * @since 2.2 |
| 147 | */ |
| 148 | public function is_columns_screen() { |
| 149 | |
| 150 | $is_columns_screen = parent::is_columns_screen(); |
| 151 | |
| 152 | if ( ! $is_columns_screen ) { |
| 153 | if ( ! empty( $_REQUEST['_inline_edit'] ) && wp_verify_nonce( $_REQUEST['_inline_edit'], 'inlineeditnonce' ) ) { |
| 154 | $is_columns_screen = true; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | return $is_columns_screen; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Get WP default supported admin columns per post type. |
| 163 | * |
| 164 | * @see CPAC_Type::get_default_columns() |
| 165 | * @since 1.0 |
| 166 | * |
| 167 | * @return array |
| 168 | */ |
| 169 | public function get_default_columns() { |
| 170 | |
| 171 | if ( ! function_exists( '_get_list_table' ) ) { |
| 172 | return array(); |
| 173 | } |
| 174 | |
| 175 | // You can use this filter to add thirdparty columns by hooking into this. |
| 176 | // See classes/third_party.php for an example. |
| 177 | do_action( "cac/columns/default/posts" ); |
| 178 | do_action( "cac/columns/default/storage_key={$this->key}" ); |
| 179 | do_action( "cac/columns/default/post_type={$this->post_type}" ); |
| 180 | |
| 181 | // Initialize table so it can add actions to manage_{screenid}_columns |
| 182 | _get_list_table( 'WP_Posts_List_Table', array( 'screen' => 'edit-' . $this->key ) ); |
| 183 | |
| 184 | // get_column_headers() runs through both the manage_{screenid}_columns |
| 185 | // and manage_{$post_type}_posts_columns filters |
| 186 | $columns = (array) apply_filters( 'manage_edit-' . $this->key . '_columns', array() ); |
| 187 | $columns = array_filter( $columns ); |
| 188 | |
| 189 | return $columns; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * @since 2.0 |
| 194 | */ |
| 195 | public function get_meta() { |
| 196 | global $wpdb; |
| 197 | |
| 198 | return $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT meta_key FROM {$wpdb->postmeta} pm JOIN {$wpdb->posts} p ON pm.post_id = p.ID WHERE p.post_type = %s ORDER BY 1", $this->key ), ARRAY_N ); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * @since 2.0 |
| 203 | */ |
| 204 | public function manage_value( $column_name, $post_id ) { |
| 205 | |
| 206 | if ( ! ( $column = $this->get_column_by_name( $column_name ) ) ) { |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | global $post; |
| 211 | |
| 212 | // Setup post data for current post |
| 213 | $post_old = $post; |
| 214 | $post = get_post( $post_id ); |
| 215 | setup_postdata( $post ); |
| 216 | |
| 217 | $value = $column->get_value( $post_id ); |
| 218 | |
| 219 | $value = apply_filters( "cac/column/value", $value, $post_id, $column, $this->key ); |
| 220 | $value = apply_filters( "cac/column/value/{$this->type}", $value, $post_id, $column, $this->key ); |
| 221 | |
| 222 | // Reset query to old post |
| 223 | $post = $post_old; |
| 224 | |
| 225 | if ( $post ) { |
| 226 | setup_postdata( $post ); |
| 227 | } |
| 228 | |
| 229 | echo $value; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * @since 2.4.7 |
| 234 | */ |
| 235 | public function manage_value_callback( $column_name, $post_id ) { |
| 236 | |
| 237 | $column = $this->get_column_by_name( $column_name ); |
| 238 | if ( $column && ! empty( $column->properties->handle ) ) { |
| 239 | ob_start(); |
| 240 | $this->manage_value( $column_name, $post_id ); |
| 241 | ob_end_clean(); |
| 242 | } else { |
| 243 | $this->manage_value( $column_name, $post_id ); |
| 244 | } |
| 245 | } |
| 246 | } |