-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin-page.php
More file actions
324 lines (270 loc) · 13.1 KB
/
Copy pathadmin-page.php
File metadata and controls
324 lines (270 loc) · 13.1 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
class Gust_Post
{
public function index()
{
$this->init_hooks();
}
public function init_hooks()
{
// self::$initiated = true;
add_action('admin_menu', array($this, 'guest_post_admin_menu'), 5, 1);
add_action('init', array($this, 'resgiter_post_type'), 1, 1);
add_action('wp_enqueue_scripts', array($this, 'gust_post_enqueue_files'), 10, 3);
add_action('admin_enqueue_scripts', array($this, 'gust_post_enqueue_files'), 10, 5);
add_action('init', array($this, 'create_guestPostType_taxonomies'), 0);
add_shortcode('add_guest_post_form', array($this, 'create_Guest_Post_Form_shortcode'));
add_shortcode('list_of_posts', array($this, 'show_posts_list'));
}
public function gust_post_enqueue_files()
{
wp_enqueue_script('ajax-script', CUSTOM_POST_TYPE_PLUGIN_DIR . 'assets/js/jquery.min.js', array('jquery'));
wp_register_script('custom-functions', CUSTOM_POST_TYPE_PLUGIN_DIR . 'assets/js/custom-functions.js?' . time());
wp_enqueue_script('custom-functions');
wp_enqueue_style('guest-post', CUSTOM_POST_TYPE_PLUGIN_DIR . 'assets/css/gues-posts.css?' . time());
wp_localize_script('ajax-script', 'my_ajax_object', array('ajax_url' => admin_url('admin-ajax.php')));
}
// Guest Post Setting page function
public function guest_post_contents()
{
if (isset($_POST['save'])) {
array_pop($_POST);
update_option('custom_post_options', $_POST);
}
$postOptions = get_option('custom_post_options');
$postTItle = $postOptions ? $postOptions['post_name'] : 'Guest Posts';
$postSlug = $postOptions ? $postOptions['post_slug'] : 'guest_posts';
$categoryName = $postOptions ? $postOptions['category_name'] : 'guest_posts';
$categorySlug = $postOptions ? $postOptions['category_slug'] : 'guest_posts';
$html = "<form method='post'><table cellpadding='10' bgcolor='#f2f2f2'><tbody>";
$html .= '<tr><td><h2>Shortcode to display posts listing on the page: </h2></td><td>[list_of_posts]</td></tr>';
$html .= '<tr><td colspan="2"><h2>Custom Post Type Settings</h2></tr>';
if ($postOptions) {
$html .= '<tr><td>Post Name: <td><td><input type="text" name="post_name" value="' . $postTItle . '"></td></tr>';
$html .= '<tr><td>Post Slug: <td><td><input type="text" name="post_slug" value="' . $postSlug . '"></td></tr>';
$html .= '<tr><td>Category Name: <td><td><input type="text" name="category_name" value="' . $categoryName . '"></td></tr>';
$html .= '<tr><td>Category Slug: <td><td><input type="text" name="category_slug" value="' . $categorySlug . '"></td></tr>';
} else {
$html .= '<tr><td>Post Name: <td><td><input type="text" name="post_name" required></td></tr>';
$html .= '<tr><td>Post Slug: <td><td><input type="text" name="post_slug" required></td></tr>';
$html .= '<tr><td>Category Name: <td><td><input type="text" name="category_name" required></td></tr>';
$html .= '<tr><td>Category Slug: <td><td><input type="text" name="category_slug" required></td></tr>';
}
$html .= '<tr><td colspan="2"><input type="submit" name="save" class="components-button editor-post-trash is-secondary is-destructive" value="Save"></td></tr>';
$html .= '</tbody></html</form>';
echo $html;
}
// Function to register custom post type Guest Posts
public function resgiter_post_type()
{
$supports = array(
'title',
// post title
'editor',
// post content
'author',
// post author
'thumbnail',
// featured images
'excerpt',
// post excerpt
// 'custom-fields', // custom fields
// 'comments', // post comments
'revisions', // post revisions
// 'post-formats', // post formatsss
);
$postOptions = get_option('custom_post_options');
$postTItle = $postOptions ? $postOptions['post_name'] : 'Guest Posts';
$postSlug = $postOptions ? $postOptions['post_slug'] : 'Guest Posts';
$labels = array(
'name' => _x($postTItle, 'plural'),
'singular_name' => _x($postSlug, 'singular'),
'menu_name' => _x($postTItle, 'admin menu'),
'name_admin_bar' => _x($postTItle, 'admin bar'),
'add_new' => _x('Add New', 'add new'),
'add_new_item' => __('Add New'),
'new_item' => __('New'),
'edit_item' => __('Edit '),
'view_item' => __('View'),
'all_items' => __('All'),
'search_items' => __("Search $postTItle"),
'not_found' => __("No $postTItle found"),
);
$args = array(
'supports' => $supports,
'labels' => $labels,
'public' => true,
'query_var' => true,
'rewrite' => array('slug' => $postSlug),
'has_archive' => true,
'hierarchical' => false,
'menu_icon' => _("dashicons-admin-page"),
);
register_post_type($postSlug, $args);
}
public function create_guestPostType_taxonomies()
{
$postOptions = get_option('custom_post_options');
$postSlug = $postOptions ? $postOptions['post_slug'] : 'guest_posts';
$categoryName = $postOptions ? $postOptions['category_name'] : 'Guest Category';
$categorySlug = $postOptions ? $postOptions['category_slug'] : 'guest_category';
$labels = array(
'name' => _x('Guest Categories', 'taxonomy general name', 'textdomain'),
'singular_name' => _x($categoryName, 'taxonomy singular name', 'textdomain'),
'search_items' => __("Search " . $categoryName, 'textdomain'),
'all_items' => __('All ' . $categoryName . 's', 'textdomain'),
'parent_item' => __('Parent ' . $categoryName, 'textdomain'),
'parent_item_colon' => __('Parent ' . $categoryName, 'textdomain'),
'edit_item' => __('Edit ' . $categoryName, 'textdomain'),
'update_item' => __('Update ' . $categoryName, 'textdomain'),
'add_new_item' => __('Add New Category', 'textdomain'),
'new_item_name' => __('New Category Name', 'textdomain'),
'menu_name' => __($categoryName, 'textdomain'),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array('slug' => $categorySlug),
);
register_taxonomy($categorySlug, array($postSlug), $args);
}
public function guest_post_admin_menu()
{
$postOptions = get_option('custom_post_options');
$postTItle = $postOptions ? $postOptions['post_name'] : 'Guest Posts';
$postSlug = $postOptions ? $postOptions['post_slug'] : 'guest_posts';
add_menu_page(
__($postTItle . ' Setting', 'my-textdomain'),
__($postTItle . ' Posts Setting', 'my-textdomain'),
'manage_options',
'guest_post_setting',
array($this, 'guest_post_contents'),
//callback function setting page
'dashicons-admin-settings',
30
);
}
public function add_featured_img($data, $postId)
{
$image_url = $data['tmp_name']; // Define the image URL here
$image_name = $data['name'];
$upload_dir = wp_upload_dir(); // Set upload folder
$image_data = file_get_contents($image_url); // Get image data
$unique_file_name = wp_unique_filename($upload_dir['path'], $image_name); // Generate unique name
$filename = basename($unique_file_name); // Create image file name
// Check folder permission and define file location
if (wp_mkdir_p($upload_dir['path'])) {
$file = $upload_dir['path'] . '/' . $filename;
} else {
$file = $upload_dir['basedir'] . '/' . $filename;
}
// Create the image file on the server
file_put_contents($file, $image_data);
// Check image file type
$wp_filetype = wp_check_filetype($filename, null);
// Set attachment data
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'pending',
);
// Create the attachment
$attach_id = wp_insert_attachment($attachment, $file, $postId);
// Include image.php
require_once ABSPATH . 'wp-admin/includes/image.php';
$attach_data = wp_generate_attachment_metadata($attach_id, $file);
wp_update_attachment_metadata($attach_id, $attach_data);
set_post_thumbnail($postId, $attach_id);
}
public function show_posts_list()
{
$postOptions = get_option('custom_post_options');
$postSlug = $postOptions['post_slug'] ? $postOptions['post_slug'] : 'guest_posts';
ob_start();
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('post_type' => array($postSlug), 'post_status' => 'publish', 'posts_per_page' => 10, 'paged' => $paged);
$loop = new WP_Query($args);
if ($loop->have_posts()):
while ($loop->have_posts()):
$loop->the_post();?>
<div class="pindex">
<div class="ptitle">
<h2><a href="<?php the_permalink(get_the_ID());?>"><?php echo get_the_title(); ?></a></h2>
<div class="thumnailofPost">
<?php if (has_post_thumbnail(get_the_ID())) {
echo get_the_post_thumbnail(get_the_ID(), 'medium_large', ['class' => 'img-responsive responsive--full', 'title' => get_the_title()]);
} else {?>
<img src="<?php echo plugin_dir_url(__FILE__); ?>/assets/imgs/default.jpg" alt="thumbnail">
<?php }?>
</div>
<p>
<?php echo get_the_excerpt(); ?>
</p>
</div>
</div>
<?php endwhile;
endif;
?>
<nav class="pagination">
<?php $this->pagination_bar($loop);?>
</nav>
<?php
wp_reset_postdata();
$output = ob_get_clean();
return $output;
}
// Pagination function
public function pagination_bar($custom_query)
{
$total_pages = $custom_query->max_num_pages;
$big = 999999999; // need an unlikely integer
if ($total_pages > 1) {
$current_page = max(1, get_query_var('paged'));
echo paginate_links(
array(
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
'format' => '?paged=%#%',
'current' => $current_page,
'total' => $total_pages,
)
);
}
}
//Create Guest Post Function
public function create_Guest_Post_Form_shortcode()
{
if (is_user_logged_in()) {
// your code for logged in user
$html = '<div class="outer-section"><img src="' . CUSTOM_POST_TYPE_PLUGIN_DIR . 'assets/imgs/loader.gif" class="loader"><div id="alert-notification"></div>';
$html .= '<form id="createPostFrom" method="POST" enctype="multipart/form-data">';
$html .= '<div class="input-group"><label>Title <span class="required">*</span></label><input id="title" type="text" class="form-controle input" name="title"></div>';
$html .= '<div class="input-group"><label>Description</label><textarea id="desc" class="form-constol" name="description" rows="4" cols="50"></textarea></div>';
$html .= '<div class="input-group"><label>Excerpt</label><textarea id="excerpt" class="form-constol" name="excerpt" rows="4" cols="50"></textarea></div>';
$html .= '<div class="input-group"><label>Select Post Type <span class="required">*</span></label><select class="form-controle" id="post_type" name="post_type"> <option value="">Select Post Type</option>';
// Get custom post types
$args = array(
'public' => true,
'_builtin' => false,
);
$output = 'objects';
$operator = 'and';
$post_types = get_post_types($args, $output, $operator);
foreach ($post_types as $key => $value) {
$html .= '<option value="' . $value->name . '">' . $value->label . '</option>';
}
$html .= '</select></div>';
$html .= '<div class="input-group"><label>Featured image</label><input type="file" id="featured_img" name="file" class="form-control"></div>';
$html .= '<input type="hidden" name ="action" value="get_data">';
$html .= '<div class="input-group"><button href="javascript:void(0)" id="createform_submit_btn" class="btn btn_primary">Submit</button></div>';
$html .= '</form></div>';
} else {
$html = "Your should logged in to access this form!";
}
return $html;
}
}
?>