DetailsColumn.php
6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
namespace AIOSEO\Plugin\Common\Standalone;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use AIOSEO\Plugin\Common\Models;
/**
* Handles the AIOSEO Details post column.
*
* @since 4.2.0
*/
class DetailsColumn {
/**
* The slug for the script.
*
* @since 4.2.0
*
* @var string
*/
protected $scriptSlug = 'src/vue/standalone/posts-table/main.js';
/**
* Class constructor.
*
* @since 4.2.0
*/
public function __construct() {
if ( wp_doing_ajax() ) {
add_action( 'init', [ $this, 'addPostColumnsAjax' ], 1 );
}
if ( ! is_admin() || wp_doing_cron() ) {
return;
}
add_action( 'current_screen', [ $this, 'registerColumnHooks' ], 1 );
}
/**
* Adds the columns to the page/post types.
*
* @since 4.0.0
*
* @return void
*/
public function registerColumnHooks() {
$screen = get_current_screen();
if ( ! $this->shouldRegisterColumn( $screen->base, $screen->post_type ) ) {
return;
}
add_action( 'admin_enqueue_scripts', [ $this, 'enqueueScripts' ] );
if ( 'product' === $screen->post_type ) {
add_filter( 'manage_edit-product_columns', [ $this, 'addColumn' ] );
add_action( 'manage_posts_custom_column', [ $this, 'renderColumn' ], 10, 2 );
return;
}
if ( 'attachment' === $screen->post_type ) {
$enabled = apply_filters( 'aioseo_image_seo_media_columns', true );
if ( ! $enabled ) {
return;
}
add_filter( 'manage_media_columns', [ $this, 'addColumn' ] );
add_action( 'manage_media_custom_column', [ $this, 'renderColumn' ], 10, 2 );
return;
}
add_filter( "manage_edit-{$screen->post_type}_columns", [ $this, 'addColumn' ] );
add_action( "manage_{$screen->post_type}_posts_custom_column", [ $this, 'renderColumn' ], 10, 2 );
}
/**
* Registers our post columns after a post has been quick-edited.
*
* @since 4.2.3
*
* @return void
*/
public function addPostColumnsAjax() {
if (
! isset( $_POST['_inline_edit'], $_POST['post_ID'] ) ||
! wp_verify_nonce( $_POST['_inline_edit'], 'inlineeditnonce' )
) {
return;
}
$postId = (int) $_POST['post_ID'];
if ( ! $postId ) {
return;
}
$post = get_post( $postId );
$postType = $post->post_type;
add_filter( "manage_edit-{$postType}_columns", [ $this, 'addColumn' ] );
add_action( "manage_{$postType}_posts_custom_column", [ $this, 'renderColumn' ], 10, 2 );
}
/**
* Enqueues the JS/CSS for the page/posts table page.
*
* @since 4.0.0
*
* @return void
*/
public function enqueueScripts() {
$data = aioseo()->helpers->getVueData();
$data['posts'] = [];
$data['terms'] = [];
aioseo()->core->assets->load( $this->scriptSlug, [], $data );
}
/**
* Adds the AIOSEO Details column to the page/post tables in the admin.
*
* @since 4.0.0
*
* @param array $columns The columns we are adding ours onto.
* @return array The modified columns.
*/
public function addColumn( $columns ) {
$canManageSeo = apply_filters( 'aioseo_manage_seo', 'aioseo_manage_seo' );
if (
! current_user_can( $canManageSeo ) &&
(
! current_user_can( 'aioseo_page_general_settings' ) &&
! current_user_can( 'aioseo_page_analysis' )
)
) {
return $columns;
}
// Translators: 1 - The short plugin name ("AIOSEO").
$columns['aioseo-details'] = sprintf( esc_html__( '%1$s Details', 'all-in-one-seo-pack' ), AIOSEO_PLUGIN_SHORT_NAME );
return $columns;
}
/**
* Renders the column in the page/post table.
*
* @since 4.0.0
*
* @param string $columnName The column name.
* @param int $postId The current rows, post id.
* @return void
*/
public function renderColumn( $columnName, $postId ) {
if ( ! current_user_can( 'edit_post', $postId ) && ! current_user_can( 'aioseo_manage_seo' ) ) {
return;
}
if ( 'aioseo-details' !== $columnName ) {
return;
}
// Add this column/post to the localized array.
global $wp_scripts;
$data = $wp_scripts->get_data( 'aioseo/js/' . $this->scriptSlug, 'data' );
if ( ! is_array( $data ) ) {
$data = json_decode( str_replace( 'var aioseo = ', '', substr( $data, 0, -1 ) ), true );
}
$nonce = wp_create_nonce( "aioseo_meta_{$columnName}_{$postId}" );
$posts = ! empty( $data['posts'] ) ? $data['posts'] : [];
$thePost = Models\Post::getPost( $postId );
$postType = get_post_type( $postId );
$postData = [
'id' => $postId,
'columnName' => $columnName,
'nonce' => $nonce,
'title' => $thePost->title,
'titleParsed' => aioseo()->meta->title->getPostTitle( $postId ),
'defaultTitle' => aioseo()->meta->title->getPostTypeTitle( $postType ),
'description' => $thePost->description,
'descriptionParsed' => aioseo()->meta->description->getPostDescription( $postId ),
'defaultDescription' => aioseo()->meta->description->getPostTypeDescription( $postType ),
'value' => (int) $thePost->seo_score,
'showMedia' => false,
'isSpecialPage' => aioseo()->helpers->isSpecialPage( $postId ),
'postType' => $postType
];
foreach ( aioseo()->addons->getLoadedAddons() as $loadedAddon ) {
if ( isset( $loadedAddon->admin ) && method_exists( $loadedAddon->admin, 'renderColumnData' ) ) {
$postData = array_merge( $postData, $loadedAddon->admin->renderColumnData( $columnName, $postId, $postData ) );
}
}
$posts[] = $postData;
$data['posts'] = $posts;
$wp_scripts->add_data( 'aioseo/js/' . $this->scriptSlug, 'data', '' );
wp_localize_script( 'aioseo/js/' . $this->scriptSlug, 'aioseo', $data );
require AIOSEO_DIR . '/app/Common/Views/admin/posts/columns.php';
}
/**
* Checks whether the AIOSEO Details column should be registered.
*
* @since 4.0.0
*
* @return bool Whether the column should be registered.
*/
public function shouldRegisterColumn( $screen, $postType ) {
if ( 'type' === $postType ) {
$postType = '_aioseo_type';
}
if ( 'edit' === $screen || 'upload' === $screen ) {
if (
aioseo()->options->advanced->postTypes->all &&
in_array( $postType, aioseo()->helpers->getPublicPostTypes( true ), true )
) {
return true;
}
$postTypes = aioseo()->options->advanced->postTypes->included;
if ( in_array( $postType, $postTypes, true ) ) {
return true;
}
}
return false;
}
}