forked from setola/Wordpress-Theme-Utils-Classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinksManager.class.php
212 lines (191 loc) · 6.23 KB
/
LinksManager.class.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
207
208
209
210
211
212
<?php
/*
* stores the class for LinksManager feature
*/
/**
* Manages the links subsection
*
* Adds support for link image and translations
* @author etessore
* @version 1.0.1
*
*/
class LinksManager extends Singleton {
/**
* Stores the name of custom post type
*/
const POST_TYPE = 'wtu-custom-links';
/**
* Stores the name of the post meta where link details are saved
*/
const META_NAME = '_link_details';
/**
* @var SimpleMetabox stores the metabox used for Links Details
*/
public $metabox;
/**
* @var string the label in WordPress admin menu
*/
private $label;
/**
* @var string the description of a single Link
*/
private $description;
/**
* @var array list of labels for register_post_type()
*/
private $labels;
/**
* Initializes default settings
* Singleton private constructor
*/
protected function __construct() {
$this->set_labels();
$this->metabox = new SimpleMetabox(
self::META_NAME,
array(
//'id' => $id,
'title' => __( 'Link Details', 'wtu_framework' ),
'post_type' => self::POST_TYPE
),
array(
array(
'id' => 'url',
'label' => __( 'URL', 'wtu_framework' ),
'type' => 'text',
'parms' => array( 'placeholder' => __( '<a> href attribute', 'wtu_framework' ) ),
'description' => __( 'Enter URL with http://', 'wtu_framework' )
),
array(
'id' => 'title',
'label' => __( 'Title', 'wtu_framework' ),
'type' => 'text',
'parms' => array( 'placeholder' => __( '<a> title attribute', 'wtu_framework' ) ),
'description' => __( 'Enter link title, SEO friendly', 'wtu_framework' )
),
array(
'id' => 'label',
'label' => __( 'Label', 'wtu_framework' ),
'type' => 'text',
'parms' => array( 'placeholder' => __( '<a> text content', 'wtu_framework' ) ),
'description' => __( 'Enter the text inside the link', 'wtu_framework' )
),
array(
'id' => 'open_new_tab',
'label' => __( 'Open in new tab', 'wtu_framework' ),
'type' => 'checkbox',
'description' => __( 'Check this if you want the link to be opened in a new window', 'wtu_framework' )
),
array(
'id' => 'noindex',
'label' => __( 'Noindex', 'wtu_framework' ),
'type' => 'checkbox',
'description' => __( 'Check this if you want the link to be marked as no-index', 'wtu_framework' )
),
)
);
add_action( 'init', array( &$this, 'init' ) );
//add_filter( 'gettext', array( &$this, 'custom_title' ) );
add_action( 'add_meta_boxes', array( &$this->metabox, 'register_metaboxes' ) );
add_action( 'add_meta_boxes', array( &$this, 'remove_useless_metaboxes' ), 99 );
add_action( 'save_post', array( &$this->metabox, 'save_metabox_data' ) );
}
/**
* Customize the labels for this LinkManager
*
* @param string $label the label on the WordPress admin menu
* @param string $description description of a single link
* @param array $labels list of labels for register_post_type()
*/
public function set_labels( $label = '', $description = '', $labels = array() ) {
$this->label = ( empty( $label ) ) ? __( 'Links', 'wtu_framework' ) : $label;
$this->description = ( empty( $description ) ) ? __( 'A translable link with image', 'wtu_framework' ) : $description;
$this->labels = array_merge(
array(
'name' => __( 'Links', 'wtu_framework' ),
'singular_name' => __( 'Link', 'wtu_framework' ),
'menu_name' => __( 'Links', 'wtu_framework' ),
'add_new' => __( 'Add Link', 'wtu_framework' ),
'add_new_item' => __( 'Add New Link', 'wtu_framework' ),
'edit' => __( 'Edit', 'wtu_framework' ),
'edit_item' => __( 'Edit Link', 'wtu_framework' ),
'new_item' => __( 'New Link', 'wtu_framework' ),
'view' => __( 'View Link', 'wtu_framework' ),
'view_item' => __( 'View Link', 'wtu_framework' ),
'search_items' => __( 'Search Links', 'wtu_framework' ),
'not_found' => __( 'No Links Found', 'wtu_framework' ),
'not_found_in_trash' => __( 'No Links Found in Trash', 'wtu_framework' ),
'parent' => __( 'Parent Link', 'wtu_framework' ),
),
$labels
);
}
/**
* Called by WordPress on init
*/
public function init() {
$this->register_custom_types();
add_post_type_support( self::POST_TYPE, 'thumbnail' );
if ( is_admin() ) {
} else {
}
}
/**
* Callback to modify the title field in the admin panel
*
* @param unknown $input
*
* @return string|unknown
*/
public function custom_title( $input ) {
global $post_type;
if ( is_admin() && 'Enter title here' == $input && self::POST_TYPE == $post_type ) {
return __( 'Enter URL here', 'wtu_framework' );
}
return $input;
}
/**
* Register some custom post types and categories
*/
public function register_custom_types() {
register_post_type(
self::POST_TYPE,
array(
'label' => $this->label,
'description' => $this->description,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => array( 'slug' => '' ),
'query_var' => true,
'exclude_from_search' => true,
'supports' => array( 'title', 'thumbnail', 'author' ),
'taxonomies' => array( 'category', ),
'labels' => $this->labels,
)
);
}
/**
* Register the link details metabox and removes some useless one
*/
public static function remove_useless_metaboxes() {
remove_meta_box( 'fbseo', self::POST_TYPE, 'normal' );
remove_meta_box( 'wpseo_meta', self::POST_TYPE, 'normal' );
}
/**
* Retrieves a list of links
*
* @param array $args
*
* @return array links list
*/
public static function get_links( $args = array() ) {
$defaults = array(
'post_type' => self::POST_TYPE
);
$args = wp_parse_args( $args, $defaults );
return get_posts( $args );
}
}