Browse code

class化 プラグイン有効化時処理

nita authored on 2015/10/06 01:26:55
Showing 1 changed files
... ...
@@ -2,646 +2,55 @@
2 2
 /*
3 3
  * Plugin Name: Rucy
4 4
  * Plugin URI: https://github.com/gips/rucy
5
- * Description: Reservation Update (Published) Content.
5
+ * Description: Reservation Update "Published" Content(Post & Page).
6 6
  * Version: 0.3.0
7 7
  * Author: Nita
8 8
  * License: GPLv2 or later
9 9
  * Text Domain: rucy
10 10
  * Domain Path: /lang
11 11
  */
12
-define('RC_PLUGIN_URL',  plugin_dir_url(__FILE__));
13
-define('RC_SETTING_OPTION_KEY', 'rucy_post_type');
14
-define('RC_TXT_DOMAIN', 'rucy');
15
-define('RC_POSTTYPE_DEFAULT','post,page');
16
-define('RC_CRON_HOOK', 'rucy_update_reserved_content');
17
-load_plugin_textdomain( RC_TXT_DOMAIN, false, 'rucy/lang');
18
-
19
-add_action('admin_enqueue_scripts','load_rc_jscss');
20
-function load_rc_jscss()
21
-{
22
-    global $hook_suffix;
23
-    if(in_array($hook_suffix, array('post.php','post-new.php',)))
24
-    {
25
-        wp_register_style('rucy.css', RC_PLUGIN_URL . '/css/rucy.css',array(),'0.1.0');
26
-        wp_enqueue_style('rucy.css');
27
-        wp_register_script('rucy.js', RC_PLUGIN_URL . '/js/rucy.js', array('jquery'), '0.1.0');
28
-        wp_enqueue_script('rucy.js');
29
-    }
30
-}
31
-
32
-// load js and css for pointer
33
-add_action('admin_menu', 'load_rc_pointer_menu');
34
-function load_rc_pointer_menu() {
35
-    wp_enqueue_script('wp-pointer');
36
-    wp_enqueue_style('wp-pointer');
37
-}
38
-/**
39
- * get rucy post_metas or post_meta keys.
40
- * @param int $post_id
41
- * @return array 
42
- */
43
-function get_rc_metas($post_id = "")
44
-{
45
-    $base = array(
46
-        'accept' => 'rc_reserv_accept',
47
-        'content' => 'rc_reserv_content',
48
-        'date' => 'rc_reserv_date',
49
-        'feature_img' => 'rc_reserv_feature_image',
50
-        'accept_feature_img' => 'rc_reserv_accept_feature_image',
51
-        'accept_update' => 'rc_reserv_accept_post_update',
52
-            );
53
-    if($post_id > 0)
54
-    {
55
-        foreach ($base as $key => $value)
56
-        {
57
-            $res[$key] = get_post_meta($post_id, $value, true);
58
-        }
59
-    } else {
60
-        $res = $base;
61
-    }
62
-    return $res;
63
-}
64
-
65
-// add reserv_metabox
66
-add_action('admin_menu', 'add_rc_metabox_out');
67
-function add_rc_metabox_out()
68
-{
69
-    $accept_post_type = get_rc_setting();
70
-    foreach ($accept_post_type as $post_type)
71
-    {
72
-        add_meta_box('rucy_metabox','Rucy - Reservation Update Content -','add_rucy_metabox_inside',$post_type,'normal','high');
73
-    }
74
-}
75
-function add_rucy_metabox_inside()
76
-{
77
-    global $post;
78
-    $rc_keys = get_rc_metas();
79
-    $rc_content_name = $rc_keys['content'];
80
-    $rc_accept_name = $rc_keys['accept'];
81
-    $rc_metas = get_rc_metas($post->ID);
82
-    $reserv_accept = $rc_metas['accept'];
83
-    $reserv_date = $rc_metas['date'];
84
-    $reserv_feature_name = $rc_keys['feature_img'];
85
-    $reserv_feature = $rc_metas['feature_img'];
86
-    $reserv_accept_feature_name = $rc_keys['accept_feature_img'];
87
-    $reserv_accept_feature = $rc_metas['accept_feature_img'];
88
-    $thumb_id = get_post_thumbnail_id($post->ID);
89
-    $reserv_feature_width = "255";
90
-    $reserv_feature_height = "255";
91
-    if($thumb_id){
92
-        $image = wp_get_attachment_image_src($thumb_id, 'thumbnail');
93
-        $reserv_feature_width = $image[1];
94
-        $reserv_feature_height = $image[2];
95
-    }
96
-    $reserv_feature_image = ($reserv_feature != '') ? '<img width="'.$reserv_feature_width.'" height="'.$reserv_feature_height.'" class="rc_feature_image" src="'.$reserv_feature.'" />' : '';
97
-    if("" == $reserv_date)
98
-    {
99
-        $reserv_date = date_i18n('Y-m-d H:i:s');
100
-    }
101
-    $reserv_date_arr = getdate(strtotime($reserv_date));
102
-    $current_y = date_i18n('Y');
103
-    $reserv_content = $rc_metas['content'];
104
-    if("" == $reserv_content)
105
-    {
106
-        $reserv_content = $post->post_content;
107
-    }
108
-?>
109
-<div id="rc-post-wrap" class="curtime">
110
-    <input type="hidden" name="schroeder" id="schroeder" value="<?php echo wp_create_nonce(plugin_basename(__FILE__)); ?>"/>
111
-    <label class="rc_accept">
112
-        <input type="checkbox" name="<?php echo $rc_accept_name; ?>" value="1" <?php echo ($reserv_accept == "1") ? "checked" : ""; ?>> <?php _e('Accept reserve update content.',RC_TXT_DOMAIN) ?>
113
-    </label>
114
-    <div class="rc-datetime" id="timestamp">
115
-        <?php _e('UpdateTime',RC_TXT_DOMAIN) ?>:<b><?php echo date_i18n("Y/m/d @ H:i", strtotime($reserv_date)); ?></b>
116
-    </div>
117
-    <a href="#edit-reservdate" class="edit-timestamp rc-datetime-edit"><?php _e('Edit') ?></a>
118
-    <div class="rc-datetime-wrap">
119
-        <select name="rc_year">
120
-        <?php 
121
-        for($y = $current_y; $y <= ($current_y + 3); $y++){
122
-            $y_selected = ($y == date_i18n('Y',$reserv_date_arr[0])) ? "selected" : "";
123
-            echo '<option value="'.$y.'" '.$y_selected.'>'.$y.'</option>';
124
-        }
125
-        ?>
126
-        </select>
127
-        <?php echo '/' ?>
128
-        <select name="rc_month">
129
-        <?php
130
-        for($i = 1; $i <= 12; $i++)
131
-        {
132
-            $m = sprintf("%02d",$i);
133
-            $selected = ($m == date_i18n('m',$reserv_date_arr[0])) ? "selected" : "";
134
-            echo '<option value="'.$m.'" '.$selected.'>'.$m.'</option>';
135
-        }
136
-        ?>
137
-        </select><?php echo '/' ?>
138
-        <select name="rc_day">
139
-        <?php 
140
-        for($d = 1; $d<=31; $d++){
141
-            $d = sprintf("%02d",$d);
142
-            $d_selected = ($d == date_i18n('d',$reserv_date_arr[0])) ? "selected" : "";
143
-            echo '<option value="'.$d.'" '.$d_selected.'>'.$d.'</option>';
144
-        }
145
-        ?>
146
-        </select>    
147
-        @
148
-        <select name="rc_hour">
149
-        <?php 
150
-        for($h = 0; $h <= 23; $h++){
151
-            $h = sprintf("%02d",$h);
152
-            $h_selected = ($h == date_i18n('H',$reserv_date_arr[0])) ? "selected" : "";
153
-            echo '<option value="'.$h.'" '.$h_selected.'>'.$h.'</option>';
154
-        }
155
-        ?>
156
-        </select>
157
-        :
158
-        <select name="rc_minutes">
159
-        <?php 
160
-        for($min = 0; $min <= 59; $min++){
161
-            $min = sprintf("%02d",$min);
162
-            $min_selected = ($min == date_i18n('i',$reserv_date_arr[0])) ? "selected" : "";
163
-            echo '<option value="'.$min.'" '.$min_selected.'>'.$min.'</option>';
164
-        }
165
-        ?>
166
-        </select>
167
-        <a href="#edit-reservdate" class="rc-datetime-update button"><?php _e('OK',RC_TXT_DOMAIN) ?></a>
168
-        <a href="#edit-reservdate" class="rc-datetime-cancel"><?php _e('Cancel',RC_TXT_DOMAIN) ?></a>
169
-    </div>
170
-    <?php
171
-        $date_arr = array(
172
-            'rc_year' => date_i18n('Y',$reserv_date_arr[0]),
173
-            'rc_month' => date_i18n('m',$reserv_date_arr[0]),
174
-            'rc_day' => date_i18n('d',$reserv_date_arr[0]),
175
-            'rc_hour' => date_i18n('H',$reserv_date_arr[0]),
176
-            'rc_minutes' => date_i18n('i',$reserv_date_arr[0])
177
-        );
178
-        foreach ($date_arr as $k => $v)
179
-        {
180
-            echo '<input type="hidden" name="'.$k.'_cr" id="'.$k.'_cr" value="'.$v.'">';
181
-        }
182
-    ?>
183
-    <div id="rc-accept-update-update">
184
-        <label for="rc-accept-update-postdate">
185
-            <input id="rc-accept-update-postdate" type="checkbox" name="<?php echo $rc_keys['accept_update']; ?>" value="1" <?php echo ($rc_metas['accept_update'] == '1') ? "checked" : ""; ?> /> <?php _e('Accept reserve update post date.', RC_TXT_DOMAIN); ?>
186
-        </label>
187
-    </div>
188
-    <?php 
189
-    $dismissed = explode(',', get_user_meta(get_current_user_id(), 'dismissed_wp_pointers', true));
190
-    if(array_search('rc_update_postdate', $dismissed) === false):
191
-        $pointer_content = '<h3>' . __('Attention - reservation update UpdateTime', RC_TXT_DOMAIN) . '</h3>';
192
-        $pointer_content .= '<p>'.__("If update UpdateTime, this post\'s permalink is changed by permalink settings.", RC_TXT_DOMAIN).'</p>';
193
-    ?>
194
-    <script type="text/javascript">
195
-        jQuery(document).ready(function(){
196
-            // show notice pointer update postdate.
197
-            jQuery('#rc-accept-update-update').pointer({
198
-                content : '<?php echo $pointer_content ?>',
199
-                buttons : function(e, t){
200
-                    return jQuery('<a class="close" href="#"><?php _e('Do not show future', RC_TXT_DOMAIN) ?></a>').bind('click.pointer',function(e){
201
-                        e.preventDefault();
202
-                        t.element.pointer('close');
203
-                    });
204
-                },
205
-                position : { edge : "top", align : "left"},
206
-                close : function(){
207
-                    jQuery.post("<?php echo admin_url('admin-ajax.php'); ?>", {
208
-                        action : 'dismiss-wp-pointer',
209
-                        pointer : 'rc_update_postdate'
210
-                    });
211
-                }
212
-            }).pointer('open');
213
-        });
214
-    </script>
215
-    <?php 
216
-    endif;
217
-    ?>
218
-</div>
219
-<?php 
220
-    wp_editor($reserv_content, $rc_content_name);
221
-/*
222
- * support feature image reservation.
223
- */
224
-    if( current_theme_supports('post-thumbnails') ) {
225
-?>
226
-<fieldset>
227
-<h3><?php echo __('Featured Image for Reservation Update', RC_TXT_DOMAIN); ?></h3>
228
-<label class="rc_feature_accept">
229
-    <input type="checkbox" name="<?php echo $reserv_accept_feature_name; ?>" value="1" <?php echo ($reserv_accept_feature == "1") ? "checked" : ""; ?>> <?php _e('Accept reserve update feature image.', RC_TXT_DOMAIN); ?>
230
-</label>
231
-<div class="rc_feature_image_uploader">
232
-<p><button id="rc_feature_image_upload" class="button rc-feature-uploader-button <?php echo ($reserv_feature_image != '') ? ' has_image' : ''; ?>"><?php  _e('Set featured image for Reservation', RC_TXT_DOMAIN); ?></button></p>
233
-
234
-<div class="rc-feature-image-uploader__ctrl">
235
-    <div class="rc-feature-image-preview">
236
-        <?php
237
-        if ( ! empty( $reserv_feature_image ) ) {
238
-           echo $reserv_feature_image;
239
-        }
240
-        ?> 
241
-    </div>
242
-</div>
243
-
244
-<p><button class="button rc_remove_feature_image"><?php _e('Remove Featured image for Reservation', RC_TXT_DOMAIN) ?></button></p>
245
-<input type="hidden" id="rc_feature_image" name="<?php echo $reserv_feature_name ?>" value="<?php echo $reserv_feature ?>" />
246
-</div>
247
-</fieldset>
248
-<?php 
249
-    }
250
-}
251
-
252
-// save post meta
253
-add_action('save_post','save_rc_post_meta');
254
-function save_rc_post_meta($post_id)
255
-{
256
-    if(isset($_POST) && isset($_POST['post_type']))
257
-    {
258
-        if(isset($_POST['schroeder']) && !wp_verify_nonce($_POST['schroeder'], plugin_basename(__FILE__))){
259
-            return;
260
-        }
261
-        if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){
262
-            return;
263
-        }
264
-        $rc_keys = get_rc_metas();
265
-        $accept_post_type = get_rc_setting();
266
-        if(in_array($_POST['post_type'], $accept_post_type))
267
-        {
268
-            $date = mktime($_POST['rc_hour'], $_POST['rc_minutes'], 00, $_POST['rc_month'], $_POST['rc_day'], $_POST['rc_year']);
269
-            if($date)
270
-            {
271
-                $_POST[$rc_keys['date']] = date_i18n('Y-m-d H:i:s',$date);
272
-            } else {
273
-                $_POST[$rc_keys['date']] = "";
274
-            }
275
-            if(!isset($_POST[$rc_keys['accept']]) || $_POST[$rc_keys['accept']] != "1"){
276
-                $_POST[$rc_keys['accept']]  = "0";
277
-            }
278
-        }
279
-        foreach ($rc_keys as $key => $val)
280
-        {
281
-            save_rc_post_meta_base($post_id, $val);
282
-        }
283
-        if($_POST[$rc_keys['accept']] == "1")
284
-        {
285
-            $reserv_date = strtotime(get_gmt_from_date($_POST[$rc_keys['date']]) . " GMT");
286
-            if(in_array($_POST['post_type'], $accept_post_type) )
287
-            {
288
-                wp_schedule_single_event($reserv_date, RC_CRON_HOOK, array($post_id));
289
-            }
290
-        } else if($_POST[$rc_keys['accept']] == "0" || !isset ($_POST[$rc_keys['accept']])) {
291
-            // delete schedule
292
-            wp_clear_scheduled_hook(RC_CRON_HOOK, array($post_id));
293
-        }
294
-    }
295
-}
296
-
297
-/**
298
- * save, update, delete post_meta
299
- * 
300
- * @param int $post_id
301
- * @param string $post_metakey
302
- */
303
-function save_rc_post_meta_base($post_id, $post_metakey)
304
-{
305
-    if(isset($_POST))
306
-    {
307
-        $post_data = "";
308
-        if(isset($_POST[$post_metakey]))
309
-        {
310
-            $post_data = $_POST[$post_metakey];
311
-        }
312
-        $meta = get_post_meta($post_id, $post_metakey,true);
313
-       if ($post_data != $meta) {
314
-            update_post_meta($post_id, $post_metakey, $post_data, $meta);
315
-        } elseif("" == $post_data) {
316
-            delete_post_meta($post_id, $post_metakey);
317
-        }
318
-    }
319
-}
320
-
321
-// update post for wp-cron
322
-add_action('rucy_update_reserved_content', 'update_rc_reserved_content', 10, 1);
323
-function update_rc_reserved_content($post_id)
324
-{
325
-    $rc_metas = get_rc_metas((int)$post_id);
326
-    if("1" == $rc_metas['accept'])
327
-    {
328
-        $updates = array(
329
-            'ID' => (int)$post_id,
330
-            'post_content' => $rc_metas['content'],
331
-        );
332
-        // update post date
333
-        if(isset($rc_metas['accept_update']) && "1" == $rc_metas['accept_update']) {
334
-            $updates['post_date'] = $rc_metas['date'];
335
-            $updates['post_date_gmt'] = get_gmt_from_date($rc_metas['date']);
336
-        }
337
-        // feature_image
338
-        if(isset($rc_metas['accept_feature_img']) && "1" == $rc_metas['accept_feature_img'] &&
339
-           isset($rc_metas['feature_img']) && $rc_metas['feature_img'] != '') {
340
-            update_rc_post_thumbnail($post_id, $rc_metas['feature_img']);
341
-        }
342
-        remove_filter('content_save_pre', 'wp_filter_post_kses');
343
-        remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
344
-        add_filter('content_save_pre', 'rc_content_allow_iframe');
345
-        $upp = wp_update_post($updates,true);
346
-        remove_filter('content_save_pre', 'rc_content_allow_iframe');
347
-        add_filter('content_save_pre', 'wp_filter_post_kses');
348
-        add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
349
-    }
350
-    $dels = get_rc_metas();
351
-    foreach ($dels as $key => $del)
352
-    {
353
-        delete_post_meta($post_id, $del);
354
-    }
355
-    wp_clear_scheduled_hook(RC_CRON_HOOK, array($post_id));
356
-}
357
-
358
-function rc_content_allow_iframe($content)
359
-{
360
-    global $allowedposttags;
361
-    // iframe and attribute in iframe
362
-    $allowedposttags['iframe'] = array(
363
-        'class' => array(), 'src' => array(),
364
-        'width' => array(), 'height' => array(),
365
-        'frameborder' => array(), 'scrolling' => array(),
366
-        'marginheight' => array(), 'marginwidth' => array(),
367
-        'srcdoc' => array(), 'sandbox' => array(),
368
-        'seamless' => array(), 'name' => array(),
369
-    );
370
-    return $content;
371
-}
372
-
373
-function update_rc_post_thumbnail($post_id, $reserved_post_thumb_path)
374
-{
375
-    include_once(ABSPATH . 'wp-admin/includes/image.php');
376
-    $upload_dir = wp_upload_dir();
377
-    $image_data = file_get_contents($reserved_post_thumb_path);
378
-    $file_name = basename($reserved_post_thumb_path);
379
-    if(wp_mkdir_p($upload_dir['path'])) {
380
-        $file = $upload_dir['path'] . '/' . $file_name;
381
-    } else {
382
-        $file = $upload_dir['basedir'] . '/' . $file_name;
383
-    }
384
-    file_put_contents($file, $image_data);
385
-    $wp_file_type = wp_check_filetype($file_name, null);
386
-    $attachment = array(
387
-        'post_mime_type' => $wp_file_type['type'],
388
-        'post_title' => sanitize_file_name($file_name),
389
-        'post_content' => '',
390
-        'post_status' => 'inherit',
391
-    );
392
-    delete_post_thumbnail($post_id);
393
-    $attachment_id = wp_insert_attachment($attachment, $file, $post_id);
394
-    $attach_data = wp_generate_attachment_metadata($attachment_id, $file);
395
-    if(!empty($attach_data) && !is_wp_error($attach_data)){
396
-        $res = wp_update_attachment_metadata($attachment_id, $attach_data);
397
-        set_post_thumbnail($post_id, $attachment_id);
398
-        
399
-        return $res;
400
-    }
401
-}
402
-
403
-// add update message
404
-add_filter('post_updated_messages','add_rc_message');
405
-function add_rc_message($messages)
406
-{
407
-    global  $post, $post_ID;
408
-    $arr_post_types = get_rc_setting(true);
409
-    $post_type = get_post_type($post);
410
-    if(in_array($post_type, $arr_post_types))
411
-    {
412
-        $rc_metas = get_rc_metas($post_ID);
413
-        if("1" == $rc_metas['accept'])
414
-        {
415
-            $add_message_date = date_i18n('Y/m/d @ H:i',  strtotime($rc_metas['date']));
416
-            $str = __('registered reservation update content _RC_DATETIME_',RC_TXT_DOMAIN);
417
-            $add_message = '<br>' . strtr($str, array('_RC_DATETIME_' => $add_message_date));
418
-            // published
419
-            $messages[$post_type][1] .= $add_message;
420
-            $messages[$post_type][4] .= $add_message;
421
-            $messages[$post_type][6] .= $add_message;
422
-            // saved
423
-            $messages[$post_type][7] .= $add_message;
424
-            // submited
425
-            $messages[$post_type][8] .= $add_message;
426
-            // scheduled
427
-            $messages[$post_type][9] .= $add_message;
428
-        }
429
-    }
430
-    return $messages;
431
-}
432
-
433
-// add reservation info at postlist
434
-function manage_rucy_cols($columns)
435
-{
436
-    $columns['rucy_reservation_date'] = __("Reservation Update DateTime", RC_TXT_DOMAIN);
437
-    return $columns;
438
-}
439
-
440
-function add_rucy_col($column_name, $post_id)
441
-{
442
-    $rc_metas = get_rc_metas($post_id);
443
-    $s = "";
444
-    if($column_name == 'rucy_reservation_date')
445
-    {
446
-        $s = $rc_metas['accept'];
447
-        if($s == "1")
448
-        {
449
-            echo $rc_metas['date'];
450
-        } else {
451
-            echo __('None');
452
-        }
453
-    }
454
-}
455
-
456
-$accept_post_type = get_rc_setting();
457
-foreach ($accept_post_type as $p){
458
-    if(in_array($p, array('page', 'post'))) {
459
-        $p .= 's';
460
-    }
461
-    add_filter('manage_'.$p.'_columns', 'manage_rucy_cols');
462
-    add_action('manage_'.$p.'_custom_column', 'add_rucy_col', 10, 2);    
463
-}
464
-
465
-// setting page
466
-add_action('admin_menu','admin_menu_rucy');
467
-function admin_menu_rucy()
468
-{
469
-    add_options_page('Rucy', 'Rucy', 'manage_options',  'rucy', 'add_rc_setting');
470
-}
471
-function add_rc_setting()
472
-{
473
-    $post = $_POST;
474
-    $is_checked_post = "";
475
-    $is_checked_page = "";
476
-    $custom_post_types = "";
477
-    $error_class = "form-invalid";
478
-    $basic_post_types = array('page','post');
479
-    $invalid_post_types = array('attachment','revision');
480
-    $message = array();
481
-    $error = 0;
482
-    if(isset($post['page_options']))
483
-    {
484
-        $res = "";
485
-        if(isset($post['rc_post']) && $post['rc_post'] == 'post')
486
-        {
487
-            $res .= $post['rc_post'];
488
-            $is_checked_post = "checked";
489
-        } else {
490
-            $error += 1;
491
-        }
492
-        if(isset($post['rc_page']) && $post['rc_page'] == 'page') {
493
-            $res .= "," . $post['rc_page'];
494
-            $is_checked_page = "checked";
495
-        } else {
496
-            $error += 1;
497
-        }
498
-        if($error == 2){
499
-            $message['post_page'] = __("post or page is not allow.", RC_TXT_DOMAIN);
500
-        }
501
-        if(isset($post['rc_custom_post']) && $post['rc_custom_post'] != "") {
502
-            $custom_check = explode(',', $post['rc_custom_post']);
503
-            foreach ($custom_check as $check)
504
-            {
505
-                if(in_array($check, $basic_post_types))
506
-                {
507
-                    $message['custom_post'] = __('Do not input "post" or "page". ', RC_TXT_DOMAIN);
508
-                } else if(!preg_match('/[a-zA-Z0-9_-]/', $check)) {
509
-                    $message['custom_post'] = __("Please input alphabet or numeric. And do not input sequencial commas.", RC_TXT_DOMAIN);
510
-                } else if(in_array($check, $invalid_post_types)){
511
-                    $message['custom_post'] = __('Do not input "attachment" or "revision". ',RC_TXT_DOMAIN);
512
-                }
513
-            }
514
-            $res .= "," . $post['rc_custom_post'];
515
-            $custom_post_types = $post['rc_custom_post'];
516
-        }
517
-        if($res != "" && count($message) == 0)
518
-        {
12
+define( 'RC_PLUGIN_URL',  plugin_dir_url(__FILE__) );
13
+define( 'RC_PLUGIN_DIR',  untrailingslashit( dirname( __FILE__ ) ) );
14
+define( 'RC_SETTING_OPTION_KEY', 'rucy_post_type' );
15
+define( 'RC_TXT_DOMAIN', 'rucy' );
16
+define( 'RC_CRON_HOOK', 'rucy_update_reserved_content' );
17
+load_plugin_textdomain( RC_TXT_DOMAIN, false, 'rucy/lang' );
18
+
19
+class Rucy_Class {
20
+    public $support_post_type = array();
21
+
22
+    public function __construct() {
23
+        register_activation_hook( plugin_basename(__FILE__), array( $this, 'activate_plugin' ) );
24
+        add_action('admin_enqueue_scripts', array( $this, 'enqueue_style_script' ));
25
+    }
26
+    
27
+    public function activate_plugin() {
28
+        $this->support_post_type = $this->get_support_post_type();
29
+    }
30
+    
31
+    public function get_support_post_type() {
32
+        $res = get_option( RC_SETTING_OPTION_KEY );
33
+        if( !$res ) {
34
+            $res = "post,page";
519 35
             update_option(RC_SETTING_OPTION_KEY, $res);
520 36
         }
521
-    } else {
522
-        $message = array();
523
-        $arr_setting = get_rc_setting();
524
-        $is_checked_post = (in_array('post', $arr_setting) == TRUE) ? 'checked' : "";
525
-        $is_checked_page = (in_array('page', $arr_setting) == TRUE) ? 'checked' : "";
526
-        $arr_custom_post_types = array();
527
-        foreach ($arr_setting as $v)
528
-        {
529
-            if(!in_array($v, $basic_post_types))
530
-            {
531
-                array_push($arr_custom_post_types, $v);
532
-            }
533
-        }
534
-        $custom_post_types = implode(',', $arr_custom_post_types);
535
-    }
536
-?>
537
-<div class="wrap">
538
-    <h2><?php _e('Rucy Settings', RC_TXT_DOMAIN); ?></h2>
539
-    <p><?php _e('Configure content types reservation update.',RC_TXT_DOMAIN); ?></p>
540
-    <form method="post" action="#">
541
-        <?php wp_nonce_field('update-options'); ?>
542
-        <table class="form-table">
543
-            <tr class="<?php echo (isset($message['post_page']) == true) ? $error_class : ""; ?>">
544
-                <th><?php _e('post type',RC_TXT_DOMAIN) ?><br><small>*<?php _e('Require',RC_TXT_DOMAIN) ?></small></th>
545
-                <td>
546
-                    <ul>
547
-                        <li><label for="rc_post"><input type="checkbox" id="rc_post" name="rc_post" value="post" <?php echo $is_checked_post ?>><?php _e('post',RC_TXT_DOMAIN) ?></label></li>
548
-                        <li><label for="rc_page"><input type="checkbox" id="rc_page" name="rc_page" value="page" <?php echo $is_checked_page ?>><?php _e('page',RC_TXT_DOMAIN) ?></label></li>
549
-                    </ul>
550
-                    <?php 
551
-                        if(isset($message['post_page']))
552
-                        {
553
-                            echo '<p>'.$message['post_page'].'</p>';
554
-                        }
555
-                    ?>
556
-                </td>
557
-            </tr>
558
-            <tr class="<?php echo (isset($message['custom_post']) == true) ? $error_class : ""; ?>">
559
-                <th><?php _e('custom post type',RC_TXT_DOMAIN) ?></th>
560
-                <td>
561
-                    <input type="text" value="<?php echo $custom_post_types ?>" name="rc_custom_post" placeholder="<?php _e('Separated by commas',RC_TXT_DOMAIN) ?>">
562
-                    <?php 
563
-                        if(isset($message['custom_post']))
564
-                        {
565
-                            echo '<p>'.$message['custom_post'].'</p>';
566
-                        }
567
-                    ?>
568
-                </td>
569
-            </tr>
570
-        </table>
571
-        <input type="hidden" name="action" value="update" />
572
-        <input type="hidden" name="page_options" value="<?php echo RC_SETTING_OPTION_KEY ?>"/>
573
-        <p class="submit">
574
-            <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
575
-        </p>
576
-    </form>
577
-</div>
578
-<?php
579
-}
580
-/**
581
- * get post type allowed rucy
582
- * 
583
- * @param boolean $is_array
584
- * @return string
585
- */
586
-function get_rc_setting($is_array = true)
587
-{
588
-    $rc_setting = get_option(RC_SETTING_OPTION_KEY);
589
-    $res = "";
590
-    if(!$rc_setting)
591
-    {
592
-        $rc_setting = RC_POSTTYPE_DEFAULT;
593
-    }
594
-    if($is_array)
595
-    {
596
-        $res = explode(',', $rc_setting);
597
-    } else {
598
-        $res = $rc_setting;
37
+        $this->support_post_type = explode(",", $res);
38
+        return $res;
599 39
     }
600
-    return $res;
601
-}
602
-
603
-// uninstall
604
-if(function_exists('register_uninstall_hook'))
605
-{
606
-    register_uninstall_hook(__FILE__, 'uninstall_rucy');
607
-}
608
-// deactivation
609
-if(function_exists('register_deactivation_hook'))
610
-{
611
-    register_deactivation_hook(__FILE__, 'uninstall_rucy');
612
-}
613
-
614
-function uninstall_rucy()
615
-{
616
-    wp_clear_scheduled_hook(RC_CRON_HOOK);
617
-    delete_option(RC_SETTING_OPTION_KEY);
618
-    $allposts = get_posts('numberposts=-1&post_status=');
619
-    $meta_keys = get_rc_metas();
620
-    foreach ($allposts as $postinfo)
621
-    {
622
-        foreach ($meta_keys as $k => $val)
623
-        {
624
-            delete_post_meta($postinfo->ID, $val);
40
+    
41
+    /**
42
+     * load css and js for this plugin.
43
+     * 
44
+     * @global type $hook_suffix
45
+     */
46
+    public function enqueue_style_script() {
47
+        global $hook_suffix;
48
+        if ( in_array( $hook_suffix, array( 'post.php', 'post-new.php' ) ) ) {
49
+            wp_register_style('rucy.css', RC_PLUGIN_URL . 'css/rucy.css',array(),'0.1.0');
50
+            wp_register_script('rucy.js', RC_PLUGIN_URL . 'js/rucy.js', array('jquery'), '0.1.0');
51
+            wp_enqueue_style('rucy.css');
52
+            wp_enqueue_script('rucy.js');
625 53
         }
626 54
     }
627 55
 }
628
-
629
-// link to setting
630
-add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'add_rc_setting_link');
631
-function add_rc_setting_link($links)
632
-{
633
-    $links[] = '<a href="' . get_admin_url(null, 'options-general.php?page=rucy') . '">' . __('Settings') . '</a>';
634
-    return $links;
635
-}
636
-
637
-// activate plugin action
638
-register_activation_hook(plugin_basename(__FILE__), 'install_rucy');
639
-function install_rucy()
640
-{
641
-    $rc_setting = get_option(RC_SETTING_OPTION_KEY);
642
-    if(!$rc_setting)
643
-    {
644
-        $basic_post_types = RC_POSTTYPE_DEFAULT;
645
-        update_option(RC_SETTING_OPTION_KEY, $basic_post_types);
646
-    }
647
-}
56
+new Rucy_Class();
648 57
\ No newline at end of file