-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSID177_photo-gallery.php
300 lines (252 loc) · 10.1 KB
/
SID177_photo-gallery.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
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
<?php
/*
* Plugin Name: SID177 Photo-Gallery Plugin
* Author: SID177
* Description: A simple Photogallery plugin. Add photos, reorder them. Paste the
* shortcode given to any post to show slideshow to your post.
* Version: 1.0.0
*/
/*
* MAIN CLASS, INITIALIZED AT THE END OF THE FILE
*/
class SID177_photogallery{
// CONSTRUCTOR
public function __construct(){
/*
* ADD FILTERS HERE
*/
add_filter('manage_'.$this->post_type.'s_columns', array($this,'SID177_imagegallery_shortcode_posttable_head'));
/*
* ADD ACTIONS HERE
*/
add_action('init',array($this,'SID177_create_photogallery_posttype'));
add_action('add_meta_boxes',array($this,'SID177_add_photogallery_metabox'));
add_action('save_post',array($this,'SID177_photogallery_savegallery'),5,3);
add_action('manage_'.$this->post_type.'s_custom_column', array($this,'SID177_imagegallery_shortcode_posttable'), 10, 2 );
// add_action('the_posts',array($this,'SID177_photogallery_modifyquery'),10);
/*
* ADD GLOBALS HERE
*/
global $wpdb;
$this->wpdb=$wpdb;
/*
* PATH TO CSS AND JS FOLDER
*/
$this->css=plugins_url()."/".plugin_basename( __DIR__ )."/assets/css/";
$this->js=plugins_url()."/".plugin_basename( __DIR__ )."/assets/js/";
/*
* PATH TO FRONT-END STYLE AND JS
*/
$this->frontend_style=$this->css."frontend-style.css";
$this->frontend_script=$this->js."frontend-app.js";
/*
* PATH TO BACK-END STYLE AND JS
*/
$this->admin_style=$this->css."admin-style.css";
$this->admin_script=$this->js."admin-app.js";
}
private $post_type="post";
private $wpdb;
private $css,$js;
private $frontend_style,$frontend_script;
private $admin_style,$admin_script;
private $posttype_title="SID177 PhotoGallery";
private $posttype_public=true;
/*
* META BOX SUPPORT ARRAY
*/
private $posttype_supports=array(
'title'
);
//NAME OF THE POST_TYPE, USED IN post_type FIELD IN POSTS TABLE
private $posttype_name="SID177photogallery";
//SHORTCODE NAME FOR THE POST_TYPE
private $posttype_shortcode="SID177_photogallery_shortcode";
//IMAGE UPLOAD AND REORDERING METABOX DETAILS
private $posttype_metabox_id="SID177_photogallery_metabox";
private $posttype_metabox_title="Upload Images";
private $posttype_metabox_context="normal";
private $posttype_metabox_priority="high";
//SHOWING SHORTCODE OF THE POST IN THIS METABOX WITH POST ID
private $posttype_shortcode_metabox_id="SID177_photogallery_shortcode_metabox";
private $posttype_shortcode_metabox_title="Shortcode";
private $posttype_shortcode_metabox_context="side";
private $posttype_shortcode_metabox_priority="high";
/*
* WHAT SHOULD BE DISPLAYED ON THE FRONT-END WHILE APPLYING THIS
* SHORTCODE IN THE POST (HTML)
*/
public function SID177_photogallery_shortcode($attr=[],$content=null){
//CONVERTING ATTRIBUTES VALUES TO LOWERCASE
//eg. iD="1" TO id="1"
$attr = array_change_key_case((array)$attr, CASE_LOWER);
//PARSING THE PASSED VALUES TO $values VARIABLE
$values = shortcode_atts([
'id'=>'0',
'limit'=>'-1'
], $attr,'');
$values['id']=sanitize_text_field($values['id']);
$values['limit']=sanitize_text_field($values['limit']);
//GET POSTS HAVING ID TAKEN FROM SHORTCODE AND HAVING POSTTYPE OF THIS POST
// $result=$this->wpdb->get_results("select * from ".$this->wpdb->posts." where ID=".$values['id']." and post_type='".$this->posttype_name."'");
$result=$this->wpdb->get_results($this->wpdb->prepare("select * from ".$this->wpdb->posts." where ID=%d and post_type='%s'",$values['id'],$this->posttype_name));
//START BUFFER (EXPLAINED BETTER BELOW)
ob_start();
if(!empty($result)){
//IF WE FOUND ANY POST!
/*
* APPLY FRONT-END STYLES AND CSS HERE
*/
wp_enqueue_style('style.css',$this->frontend_style);
wp_enqueue_script('app.js',$this->frontend_script);
//TITLE OF THE POST
$title=$result[0]->post_title;
global $post;
$id=$result[0]->ID.",".$post->ID;
//SPLITTING CONTENT WITH LI IN ORDER TO SHOW IMGS ONLY
$result=explode('<li',$result[0]->post_content);
$i=1;
$total=0;
foreach ($result as $img) {
if(!strpos($img,"img"))
continue;
$total++;
}
if($total==0)
return "";
if($total>$values['limit'] && (int)$values['limit']>0)
$total=$values['limit'];
?>
<div class="slideshow-container">
<?php
foreach ($result as $img) {
if(!strpos($img,"img"))
continue;
if($i>$values['limit'] && (int)$values['limit']>0)
break;
?>
<div class="mySlides fade mySlides_<?php echo $id; ?>" style="background-color: black;">
<center>
<div class="numbertext"><?php echo esc_html(($i++)." / ".$total); ?></div>
<?php
$img=explode('<img ',$img)[1];
echo "<img ".$img;
?>
<div class="text"><?php echo esc_html($title); ?></div>
</center>
</div>
<?php
}
?>
<a class="prev" onclick="plusSlides(-1,'<?php echo $id; ?>')">❮</a>
<a class="next" onclick="plusSlides(1,'<?php echo $id; ?>')">❯</a>
</div>
<br>
<div style="text-align:center">
<?php
$i=1;
foreach ($result as $img) {
if(!strpos($img,"img"))
continue;
if($i>$values['limit'] && (int)$values['limit']>0)
break;
?>
<span class="dot dot_<?php echo $id; ?>" onclick="currentSlide(<?php echo esc_html($i++); ?>,'<?php echo $id; ?>')"></span>
<?php
}
?>
</div>
<script type="text/javascript">
jQuery(document).ready(function(){
showSlides(slideIndex,'<?php echo $id; ?>');
});
</script>
<?php
}
$o=ob_get_contents();
ob_end_clean();
return $o;
}
public function SID177_create_photogallery_posttype(){
$args=array(
'public'=>$this->posttype_public,
'label'=>$this->posttype_title,
'supports'=>$this->posttype_supports
);
register_post_type($this->posttype_name,$args);
$args=array(
'public'=>false,
'label'=>'SID177_status',
'internal'=>true,
'private'=>true,
'show_in_admin_status_list'=>true,
'show_in_admin_all_list'=>true
);
register_post_status('SID177_status',$args);
add_shortcode($this->posttype_shortcode,array($this,'SID177_photogallery_shortcode'));
}
public function SID177_photogallery_shortcode_metabox_html(){
?>
<div>
<div class="meta-row">
<span>[<?php echo esc_html($this->posttype_shortcode); ?> id="<?php echo esc_html($_REQUEST['post']); ?>"]</span>
</div>
</div>
<?php
}
public function SID177_add_photogallery_metabox(){
if($_REQUEST['action']=='edit'){
$post=sanitize_text_field($_REQUEST['post']);
$result=$this->SID177_getImageGalleryById($post);
if(!empty($result))
add_meta_box($this->posttype_shortcode_metabox_id,$this->posttype_shortcode_metabox_title,array($this,'SID177_photogallery_shortcode_metabox_html'),$this->posttype_name,$this->posttype_shortcode_metabox_context,$this->posttype_shortcode_metabox_priority);
}
add_meta_box($this->posttype_metabox_id,$this->posttype_metabox_title,array($this,'SID177_photogallery_metabox_html'),$this->posttype_name,$this->posttype_metabox_context,$this->posttype_metabox_priority);
}
public function SID177_photogallery_metabox_html(){
wp_enqueue_media();
wp_enqueue_script('jquery-ui-sortable');
wp_enqueue_script('admin-js',$this->admin_script);
wp_enqueue_style('admin-css',$this->admin_style);
?>
<div>
<button type="button" id="insert-media-button" onclick="openMedia()" class="button add_media" data-editor="content"><span class="wp-media-buttons-icon"></span> Add Media</button>
<div class="meta-row">
<ul id="sortable">
<?php
if(isset($_REQUEST['post'])){
$result=$this->SID177_getImageGalleryById($_REQUEST['post']);
if(!empty($result)){
echo $result[0]->post_content;
}
}
?>
</ul>
</div>
<input type="hidden" name="SID177_content" id="SID177_content" form="post"/>
</div>
<?php
}
public function SID177_photogallery_savegallery($post_id,$post,$update){
if(isset($_REQUEST['SID177_content'])){
$content=$_REQUEST['SID177_content'];
$content=str_replace('\"','',$content);
$this->wpdb->update($this->wpdb->posts,array('post_status'=>'SID177_status'),array('ID'=>$post_id));
$this->wpdb->update($this->wpdb->posts,array('post_content'=>$content),array('ID'=>$post_id));
}
}
public function SID177_imagegallery_shortcode_posttable_head( $defaults ) {
if(isset($_REQUEST['post_type']) && $_REQUEST['post_type']==strtolower($this->posttype_name))
$defaults['shortcode'] = 'Shortcode';
return $defaults;
}
public function SID177_imagegallery_shortcode_posttable( $column_name, $post_id ) {
if($column_name=="shortcode" && isset($_REQUEST['post_type']) && $_REQUEST['post_type']==strtolower($this->posttype_name))
echo esc_html("[".$this->posttype_shortcode." id=\"$post_id\" limit=\"10\"]");
}
public function SID177_getImageGalleryById($id){
$result=$this->wpdb->get_results($this->wpdb->prepare("select * from ".$this->wpdb->posts." where ID=%d",$id));
return $result;
}
}
new SID177_photogallery();