comment.php
12 years ago
link.php
12 years ago
media.php
12 years ago
post.php
12 years ago
user.php
12 years ago
post.php
203 lines
| 1 | <?php |
| 2 | |
| 3 | class CPAC_Storage_Model_Post extends CPAC_Storage_Model { |
| 4 | |
| 5 | /** |
| 6 | * Constructor |
| 7 | * |
| 8 | * @since 2.0 |
| 9 | */ |
| 10 | function __construct( $post_type ) { |
| 11 | |
| 12 | $this->key = $post_type; |
| 13 | $this->label = $this->get_label(); |
| 14 | $this->type = 'post'; |
| 15 | $this->meta_type = 'post'; |
| 16 | $this->page = 'edit'; |
| 17 | $this->post_type = $post_type; |
| 18 | $this->menu_type = 'post'; |
| 19 | |
| 20 | // Headings |
| 21 | |
| 22 | // Since 3.1 |
| 23 | add_filter( "manage_{$post_type}_posts_columns", array( $this, 'add_headings' ), 100, 1 ); |
| 24 | |
| 25 | // Deprecated ( as of 3.1 ) Note: This one is still used by woocommerce. |
| 26 | // Priority set to 11 top make sure the WooCommerce headings are overwritten by CAC |
| 27 | // @todo_minor check compatibility issues for this deprecated filter |
| 28 | add_filter( "manage_{$this->page}-{$post_type}_columns", array( $this, 'add_headings' ), 100, 1 ); |
| 29 | |
| 30 | // values |
| 31 | add_action( "manage_{$this->post_type}_posts_custom_column", array( $this, 'manage_value' ), 100, 2 ); |
| 32 | |
| 33 | // @todo: description |
| 34 | add_action( 'load-edit.php', array( $this, 'set_columns_on_current_screen' ), 1000 ); |
| 35 | |
| 36 | parent::__construct(); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @since 2.2.1 |
| 41 | */ |
| 42 | public function get_original_column_value( $column, $id ) { |
| 43 | |
| 44 | global $post; |
| 45 | |
| 46 | // Setup post data for current post |
| 47 | $post_old = $post; |
| 48 | $post = get_post( $id ); |
| 49 | setup_postdata( $post ); |
| 50 | |
| 51 | // Remove Admin Columns action for this column's value |
| 52 | remove_action( "manage_{$this->post_type}_posts_custom_column", array( $this, 'manage_value' ), 100, 2 ); |
| 53 | |
| 54 | ob_start(); |
| 55 | |
| 56 | // Run WordPress native actions to display column content |
| 57 | if ( is_post_type_hierarchical( $this->post_type ) ) { |
| 58 | do_action( 'manage_pages_custom_column', $column, $id ); |
| 59 | } |
| 60 | else { |
| 61 | do_action( 'manage_posts_custom_column', $column, $id ); |
| 62 | } |
| 63 | |
| 64 | do_action( "manage_{$this->post_type}_posts_custom_column", $column, $id ); |
| 65 | |
| 66 | $contents = ob_get_clean(); |
| 67 | |
| 68 | // Add removed Admin Columns action for this column's value |
| 69 | add_action( "manage_{$this->post_type}_posts_custom_column", array( $this, 'manage_value' ), 100, 2 ); |
| 70 | |
| 71 | // Restore original post object |
| 72 | $post = $post_old; |
| 73 | |
| 74 | if ( $post ) { |
| 75 | setup_postdata( $post ); |
| 76 | } |
| 77 | |
| 78 | return $contents; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Get screen link |
| 83 | * |
| 84 | * @since 2.0 |
| 85 | * |
| 86 | * @return string Link |
| 87 | */ |
| 88 | protected function get_screen_link() { |
| 89 | |
| 90 | return add_query_arg( array( 'post_type' => $this->key ), admin_url( $this->page . '.php' ) ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @since 2.2 |
| 95 | * |
| 96 | * @return bool |
| 97 | */ |
| 98 | public function is_columns_screen() { |
| 99 | |
| 100 | $is_columns_screen = parent::is_columns_screen(); |
| 101 | |
| 102 | if ( ! $is_columns_screen ) { |
| 103 | if ( ! empty( $_REQUEST['_inline_edit'] ) && wp_verify_nonce( $_REQUEST['_inline_edit'], 'inlineeditnonce' ) ) { |
| 104 | $is_columns_screen = true; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return $is_columns_screen; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Get Label |
| 113 | * |
| 114 | * @since 2.0 |
| 115 | * |
| 116 | * @return string Singular posttype name |
| 117 | */ |
| 118 | private function get_label() { |
| 119 | $posttype_obj = get_post_type_object( $this->key ); |
| 120 | |
| 121 | return $posttype_obj->labels->name; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Get WP default supported admin columns per post type. |
| 126 | * |
| 127 | * @see CPAC_Type::get_default_columns() |
| 128 | * @since 1.0 |
| 129 | * |
| 130 | * @return array |
| 131 | */ |
| 132 | public function get_default_columns() { |
| 133 | |
| 134 | if ( ! function_exists('_get_list_table') ) { |
| 135 | return array(); |
| 136 | } |
| 137 | |
| 138 | // You can use this filter to add thirdparty columns by hooking into this. |
| 139 | // See classes/third_party.php for an example. |
| 140 | do_action( "cac/columns/default/posts" ); |
| 141 | do_action( "cac/columns/default/storage_key={$this->key}" ); |
| 142 | |
| 143 | // Initialize table so it can add actions to manage_{screenid}_columns |
| 144 | _get_list_table( 'WP_Posts_List_Table', array( 'screen' => 'edit-' . $this->key ) ); |
| 145 | |
| 146 | // get_column_headers() runs through both the manage_{screenid}_columns |
| 147 | // and manage_{$post_type}_posts_columns filters |
| 148 | $columns = apply_filters( 'manage_edit-' . $this->key . '_columns', array() ); |
| 149 | $columns = array_filter( $columns ); |
| 150 | |
| 151 | return $columns; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Get Meta |
| 156 | * |
| 157 | * @since 2.0 |
| 158 | * |
| 159 | * @return array |
| 160 | */ |
| 161 | public function get_meta() { |
| 162 | global $wpdb; |
| 163 | |
| 164 | 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 ); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Manage value |
| 169 | * |
| 170 | * @since 2.0 |
| 171 | * |
| 172 | * @param string $column_name |
| 173 | * @param int $post_id |
| 174 | */ |
| 175 | public function manage_value( $column_name, $post_id ) { |
| 176 | |
| 177 | global $post; |
| 178 | |
| 179 | // Setup post data for current post |
| 180 | $post_old = $post; |
| 181 | $post = get_post( $post_id ); |
| 182 | setup_postdata( $post ); |
| 183 | |
| 184 | // Column value |
| 185 | $value = ''; |
| 186 | if ( $column = $this->get_column_by_name( $column_name ) ) { |
| 187 | $value = $column->get_value( $post_id ); |
| 188 | } |
| 189 | |
| 190 | // Filters |
| 191 | $value = apply_filters( "cac/column/value", $value, $post_id, $column, $this->key ); |
| 192 | $value = apply_filters( "cac/column/value/{$this->type}", $value, $post_id, $column, $this->key ); |
| 193 | |
| 194 | // Reset query to old post |
| 195 | $post = $post_old; |
| 196 | |
| 197 | if ( $post ) { |
| 198 | setup_postdata( $post ); |
| 199 | } |
| 200 | |
| 201 | echo $value; |
| 202 | } |
| 203 | } |