-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathclass-article-settings.php
More file actions
242 lines (211 loc) · 7.49 KB
/
class-article-settings.php
File metadata and controls
242 lines (211 loc) · 7.49 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
240
241
242
<?php
/**
* Republication Tracker Tool Article Settings.
*
* @since 1.0
* @package Republication_Tracker_Tool
*/
/**
* Republication Tracker Tool Article Settings class.
*
* @since 1.0
*/
class Republication_Tracker_Tool_Article_Settings {
/**
* Parent plugin class.
*
* @var Republication_Tracker_Tool
* @since 1.0
*/
protected $plugin = null;
/**
* Constructor.
*
* @since 1.0
*
* @param Republication_Tracker_Tool $plugin Main plugin object.
*/
public function __construct( $plugin ) {
$this->plugin = $plugin;
$this->hooks();
}
/**
* Initiate our hooks.
*
* @since 1.0
*/
public function hooks() {
add_action( 'add_meta_boxes', array( $this, 'register_meta_boxes' ) );
add_action( 'manage_edit-post_columns', array( $this, 'add_custom_columns' ) );
add_action( 'manage_edit-post_sortable_columns', array( $this, 'add_sortable_columns' ) );
add_action( 'manage_posts_custom_column', array( $this, 'custom_column_content' ), 10, 2 );
add_action( 'save_post', array( $this, 'save_hide_widget_metabox' ), 10 );
add_action( 'wp_insert_post', array( $this, 'apply_default_post_distribution' ), 10, 3 );
}
/**
* Add custom metaboxes.
*
* @since 1.0
*/
public function register_meta_boxes() {
add_meta_box(
'republication-tracker-tool',
esc_html__( 'Republication Tracker Tool', 'republication-tracker-tool' ),
array( $this, 'render_metabox' ),
array( 'post', 'page' ),
'advanced',
'default'
);
add_meta_box(
'republication-tracker-tool-hide-widget',
esc_html__( 'Hide Republication Widget', 'republication-tracker-tool' ),
array( $this, 'render_hide_widget_metabox' ),
array( 'post', 'page' ),
'side',
'default',
array(
'__block_editor_compatible_meta_box' => true,
)
);
}
/**
* Save the value of the hide widget metabox checkbox
*
* @since 1.0.2
* @param int $post_id The post ID.
*/
public function save_hide_widget_metabox( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( ! isset( $_POST['republication-tracker-tool-hide-widget-submit'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
return;
}
if ( isset( $_POST['republication-tracker-tool-hide-widget'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
update_post_meta( $post_id, 'republication-tracker-tool-hide-widget', true );
} else {
update_post_meta( $post_id, 'republication-tracker-tool-hide-widget', false );
}
}
/**
* Render a custom metabox.
*
* @since 1.0
* @param WP_Post $post Post object.
* @param array $args Arguments object.
*/
public function render_metabox( $post, $args ) {
$shares = get_post_meta( $post->ID, 'republication_tracker_tool_sharing', true );
$total_count = 0;
if ( is_array( $shares ) ) {
foreach ( $shares as $url => $count ) {
$total_count = $total_count + $count;
}
}
echo wp_kses_post( wpautop( 'Total number of views: ' . $total_count ) );
if ( is_array( $shares ) && ! empty( $shares ) ) {
echo '<table class="wp-list-table widefat striped posts">';
echo '<thead>';
printf( '<th scope="col" id="url" class="manage-column column-primary"><span>%s</span><span class="sorting-indicator"></span></th>', esc_html__( 'Republished URL', 'republication-tracker-tool' ) );
printf( '<th scope="col" id="views" class="manage-column ">%s</th>', esc_html__( 'Views', 'republication-tracker-tool' ) );
echo '</thead>';
echo '<tbody id="the-list">';
foreach ( $shares as $url => $count ) {
printf(
'<tr><td class="column-primary" data-colname="URL"><a href="%1$s" target="_blank">%1$s</a></td><td class="views" data-colname="Views">%2$s</td></tr>',
wp_kses_post( $url ),
wp_kses_post( $count )
);
}
echo '</tbody>';
echo '<tfoot>';
printf( '<th scope="col" id="url" class="manage-column column-primary"><span>%s</span><span class="sorting-indicator"></span></th>', esc_html__( 'Republished URL', 'republication-tracker-tool' ) );
printf( '<th scope="col" id="views" class="manage-column">%s</th>', esc_html__( 'Views', 'republication-tracker-tool' ) );
echo '</tfoot>';
echo '</table>';
} else {
echo esc_html_e( 'There are no shares to display.', 'republication-tracker-tool' );
}
}
/**
* Render a custom metabox to check/uncheck whether or not the sharing widget should be hidden
*
* @since 1.0.2
* @param obj $post Post object.
* @param obj $args Arguments object.
*/
public function render_hide_widget_metabox( $post, $args ) {
$hide_republication_widget = get_post_meta( $post->ID, 'republication-tracker-tool-hide-widget', true );
$checked = '';
if ( true == $hide_republication_widget ) {
$checked = 'checked';
}
$hide_republication_widget_by_filter = false;
$hide_republication_widget_by_filter = apply_filters( 'hide_republication_widget', $hide_republication_widget_by_filter, $post );
if ( true == $hide_republication_widget_by_filter ) {
echo '<p>The Republication sharing widget on this post is programatically disabled through the <code>hide_republication_widget</code> filter. <a href="https://github.com/Automattic/republication-tracker-tool/blob/trunk/docs/removing-republish-button-from-categories.md" target="_blank">Read more about this filter</a>.</p>';
} else {
echo '<label>';
echo '<input type="hidden" name="republication-tracker-tool-hide-widget-submit" value="yes">';
echo '<input type="checkbox" name="republication-tracker-tool-hide-widget" id="republication-tracker-tool-hide-widget" ' . esc_attr( $checked ) . '>';
echo esc_html__( 'Hide the Republication sharing widget on this post?', 'republication-tracker-tool' );
echo '</label>';
}
}
/**
* Add custom columns to the post list table.
*
* @param array $columns The columns.
* @return array The columns.
*/
public function add_custom_columns( $columns ) {
$columns['republication_tracker_tool'] = esc_html__( 'Total Views', 'republication-tracker-tool' );
return $columns;
}
/**
* Add sortable columns to the post list table.
*
* @param array $columns The columns.
* @return array The columns.
*/
public function add_sortable_columns( $columns ) {
$columns['republication_tracker_tool'] = esc_html__( 'Total Views', 'republication-tracker-tool' );
return $columns;
}
/**
* Display the content of the custom columns in the post list table.
*
* @param string $column The column name.
* @param int $post_id The post ID.
*/
public function custom_column_content( $column, $post_id ) {
switch ( $column ) {
case 'republication_tracker_tool':
$shares = get_post_meta( $post_id, 'republication_tracker_tool_sharing', true );
$total_count = 0;
if ( $shares ) {
foreach ( $shares as $url => $count ) {
$total_count = $total_count + $count;
}
}
printf( '%s', number_format( $total_count ) );
break;
}
}
/**
* Apply default post distribution settings to new posts.
*
* @param int $post_id Post ID.
* @param WP_Post $post Post object.
* @param bool $update Whether this is an existing post being updated.
*/
public function apply_default_post_distribution( $post_id, $post, $update ) {
// Only apply to new posts.
if ( ! $update && 'post' === $post->post_type ) {
$default_post_distribution = get_option( 'republication_tracker_tool_default_post_distribution', 'off' );
if ( 'on' === $default_post_distribution ) {
update_post_meta( $post_id, 'republication-tracker-tool-hide-widget', true );
}
}
}
}