-
Notifications
You must be signed in to change notification settings - Fork 0
/
JonTag.php
206 lines (187 loc) · 6.25 KB
/
JonTag.php
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
<?php
/*
* Plugin Name: JonTag
* Plugin URI: http://www.github.com/jonkarlen78/jon-tag
* Description: Plugin to allow designation of a primary tag per post
* Version 0.1
* Author: Jon Karlen
*/
defined( 'ABSPATH' ) or die( 'Plugin file cannot be accessed directly.' );
if( ! class_exists('JonTag') )
{
class JonTag
{
/**
* @var string
*/
protected $tag = 'jon-tag';
/**
* @var string
*/
protected $name = 'JonTag';
/**
* @var string
*/
protected $version = '0.1';
/**
* Settings array adding settings to the admin panel
* @var array
*/
protected $settings = [
'css_class' => [
'description' => 'CSS class to display the primary tag in the theme with',
'default' => 'primary',
'title' => 'CSS Class',
]
];
/**
* Constructor
* Registers admin actions , sets up dependencies, adds actions
*/
public function __construct()
{
add_shortcode( $this->tag, [ &$this, 'shortcode' ] );
if ( is_admin() ) {
add_action( 'admin_init', [ &$this, 'settings' ] );
wp_enqueue_script('', '/wp-content/plugins/jon-tag/jon-tag.js');
}
if ( $options = get_option( $this->tag ) ) {
$this->options = $options;
}
add_action('add_meta_boxes', [ &$this, 'meta_box_init'] );
add_action('save_post', [ &$this, 'save_featured_tag_box'] );
add_filter('the_title', [ &$this, 'tag_the_title'], 1, 2 );
}
/**
* Registered action for add_meta_boxes action
*/
public function meta_box_init()
{
add_meta_box(
'featured-tag-meta-box',
'Select Featured Tag',
[ &$this, 'draw_featured_tag_box'],
'post',
'side',
'high'
);
}
/**
* Callback to add the meta box
*/
public function draw_featured_tag_box()
{
global $post_id;
$existing_tags = wp_get_post_tags( $post_id );
$selected_tag = get_post_meta($post_id, '_featured_tag', true);
?>
<select name="featured_tag">
<option value=''>-- NONE --</option>
<?php foreach( $existing_tags as $tag ) :
$selected = $selected_tag == $tag->term_id ? ' selected' : '';
?>
<option value="<?= $tag->term_id ?>"<?= $selected ?>><?= $tag->name ?></option>
<?php endforeach; ?>
</select>
<?php
}
/**
* Callback for save_post
*/
public function save_featured_tag_box()
{
global $post_id;
update_post_meta( $post_id, '_featured_tag', $_POST['featured_tag'] );
}
/**
* Override for the_title() to show the featured tag
* @param $title
* @param null $id
* @return string
*/
public function tag_the_title($title, $id = null)
{
if( ! in_the_loop() || ! $id )
{
return $title;
}
if( $term_id = get_post_meta($id, '_featured_tag', true) )
{
$primary_tag = ' <span class="tags-links">(' . get_term($term_id, 'post_tag')->name . ')</span>';
} else {
$primary_tag = '';
}
return $title . $primary_tag;
}
/**
* Add the setting fields to the Reading settings page.
*
* @access public
*/
public function settings()
{
$section = 'reading';
add_settings_section(
$this->tag . '_settings_section',
$this->name . ' Settings',
function () {
echo '<p>Configuration options for the ' . esc_html( $this->name ) . ' plugin.</p>';
},
$section
);
foreach ( $this->settings as $id => $options ) {
$options['id'] = $id;
add_settings_field(
$this->tag . '_' . $id . '_settings',
$options['title'],
[ &$this, 'settings_field' ],
$section,
$this->tag . '_settings_section',
$options
);
}
register_setting(
$section,
$this->tag
);
}
/**
* Defines settings to be added to the reading config
* @param array $options
*/
public function settings_field( array $options = [] )
{
$atts = array(
'id' => $this->tag . '_' . $options['id'],
'name' => $this->tag . '[' . $options['id'] . ']',
'type' => ( isset( $options['type'] ) ? $options['type'] : 'text' ),
'class' => 'medium-text',
'value' => ( array_key_exists( 'default', $options ) ? $options['default'] : null ),
);
if ( isset( $this->options[$options['id']] ) ) {
$atts['value'] = $this->options[$options['id']];
}
if ( isset( $options['placeholder'] ) ) {
$atts['placeholder'] = $options['placeholder'];
}
if ( isset( $options['type'] ) && $options['type'] == 'checkbox' ) {
if ( $atts['value'] ) {
$atts['checked'] = 'checked';
}
$atts['value'] = true;
}
array_walk( $atts, function( &$item, $key ) {
$item = esc_attr( $key ) . '="' . esc_attr( $item ) . '"';
} );
?>
<label>
<input <?php echo implode( ' ', $atts ); ?> />
<?php if ( array_key_exists( 'description', $options ) ) : ?>
<?php esc_html_e( $options['description'] ); ?>
<?php endif; ?>
</label>
<?php
}
}
new JonTag();
}