This repository was archived by the owner on Jun 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathposts.php
More file actions
147 lines (126 loc) · 5.17 KB
/
Copy pathposts.php
File metadata and controls
147 lines (126 loc) · 5.17 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
<?php
class CacheStatus {
private $_post;
private $_cached_by_category;
private $_post_status;
private $_labels;
function __construct(WP_Post $post) {
$this->_post = $post;
$this->_labels= array(
__( 'Explicitely: no' ),
__( 'Explicitely: yes' ),
__( 'From category: no' ),
__( 'From category: yes' )
);
}
function status() {
$post_status = $this->post_status();
if ( $post_status === NULL ) {
if ( $this->is_cached_by_category() ) {
return 3;
} else {
return 2;
}
}
return $post_status;
}
function short_status() {
$status = $this->status();
if ( $status > 2) {
return 2;
}
return $status;
}
function post_status() {
if ( ! isset( $this->_post_status ) ) {
$meta = get_post_meta( $this->_post->ID, '_wpc_cached', true );
$this->_post_status = NULL;
if ( $meta === 0 || $meta === '0' ) {
$this->_post_status = 0;
} elseif ( $meta === 1 || $meta === '1' ) {
$this->_post_status = 1;
}
}
return $this->_post_status;
}
function is_cached_by_category() {
if ( ! isset( $this->_cached_by_category ) ) {
$post_categories = wp_get_post_categories( $this->_post->ID );
$cached_categories = get_option( 'wpc_cached_categories', Array() );
if ( ! $cached_categories ) {
$cached_categories = Array();
}
$common_categories = array_intersect( $post_categories, $cached_categories );
$this->_cached_by_category = count( $common_categories ) > 0;
}
return $this->_cached_by_category;
}
function display( $status = NULL ) {
if ( $status == NULL ) {
$status = $this->status();
}
return $this->_labels[$status];
}
function choice_display( $status ) {
if ( $status == 2) {
if ( $this->is_cached_by_category() ) {
return $this->_labels[3];
}
}
return $this->_labels[$status];
}
}
function wpc_post_submitbox_misc_actions( $post ){
$cache_status = new CacheStatus( $post );
?>
<div class="misc-pub-section misc-pub-post-cache" id="cacheability">
<span class="dashicons dashicons-portfolio wp-media-buttons-icon"></span>
<?php
echo __( 'Cache' );
?> : <b id="cacheability-label">
<?php
echo $cache_status->display();
?></b>
<a href="#cacheability" class="edit-cacheability hide-if-no-js"><span aria-hidden="true">
Modifier
</span> <span class="screen-reader-text">
Modifier la visibilit
</span></a>
<div id="post-cacheability-select" class="hide-if-js">
<input type="hidden" name="initial-cacheability" id="hidden-post-cacheabililty" value="<?php echo $cache_status->short_status(); ?>">
<input type="radio" name="cacheability" id="cacheability-radio-0" value="0" <?php checked( 0, $cache_status->status() ); ?>> <label for="cacheability-radio-0" class="selectit"><?php echo $cache_status->choice_display(0) ?></label><br>
<input type="radio" name="cacheability" id="cacheability-radio-1" value="1" <?php checked( 1, $cache_status->status() ); ?>> <label for="cacheability-radio-1" class="selectit"><?php echo $cache_status->choice_display(1) ?></label><br>
<input type="radio" name="cacheability" id="cacheability-radio-2" value="2" <?php checked( 2, $cache_status->short_status() ); ?>> <label for="cacheability-radio-2" class="selectit"><?php echo $cache_status->choice_display(2) ?></label><br>
<p>
<a href="#cacheability" class="save-post-cacheability hide-if-no-js button"><?php echo __( 'OK' ); ?></a>
<a href="#cacheability" class="cancel-post-cacheability hide-if-no-js button-cancel"><?php echo __( 'Cancel' ); ?></a>
</p>
</div>
</div>
<?php
}
function wpc_save_post($post_ID){
if(isset($_POST['cacheability'])){
switch($_POST['cacheability']) {
case 0:
if ( ! add_post_meta( $post_ID, '_wpc_cached', $_POST['cacheability'], true ) ) {
update_post_meta( $post_ID, '_wpc_cached', $_POST['cacheability'] );
}
break;
case 1:
if ( ! add_post_meta( $post_ID, '_wpc_cached', $_POST['cacheability'], true ) ) {
update_post_meta( $post_ID, '_wpc_cached', $_POST['cacheability'] );
}
break;
case 2:
delete_post_meta( $post_ID, '_wpc_cached' );
break;
}
}
}
function wpc_posts_run( $version = '1.0.0') {
wp_enqueue_script( 'ẃpc-posts-js', plugin_dir_url( __FILE__ ) . 'wpc_posts.js', array( 'jquery' ), $version, false );
add_action( 'post_submitbox_misc_actions', 'wpc_post_submitbox_misc_actions' );
add_action('save_post','wpc_save_post');
}
?>