Browse code

予約更新処理追加

nita authored on 2015/10/17 19:14:43
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,92 @@
0
+<?php
1
+/**
2
+ * Description of class-rucy-cron
3
+ *
4
+ * @author Nita
5
+ */
6
+require_once RC_PLUGIN_DIR . '/inc/class-rucy-component.php';
7
+
8
+class Class_Rucy_Cron {
9
+    
10
+    public function update_rc_reserved_content( $post_id ) {
11
+        $component = new Class_Rucy_Component();
12
+        $post_metas = $component->get_post_rc_meta( (int)$post_id );
13
+        if ( $post_metas->accept != "1" ) {
14
+            return;
15
+        }
16
+        // set update content
17
+        $updates = array( 
18
+            'ID' => (int)$post_id,    
19
+            'post_content' => apply_filters( 'the_content', $post_metas->content ),
20
+        );
21
+        wp_update_post( $updates, true );
22
+        // set update post date
23
+        if ( $post_metas->accept_update == "1" ) {
24
+            $updates['post_date'] = $post_metas->date;
25
+            $updates['post_date_gmt'] = get_gmt_from_date( $post_metas->date );
26
+        }
27
+        // update post
28
+        remove_filter( 'content_save_pre', 'wp_filter_post_kses' );
29
+        remove_filter( 'content_filtered_save_pre', 'wp_filter_post_kses' );
30
+        add_filter( 'content_save_pre', array( $this, 'rc_content_allow_iframe' ) );
31
+        wp_update_post( $updates, true );
32
+        remove_filter( 'content_save_pre', array( $this, 'rc_content_allow_iframe' ) );
33
+        add_filter( 'content_save_pre', 'wp_filter_post_kses' );
34
+        add_filter( 'content_filtered_save_pre', 'wp_filter_post_kses' );
35
+        // update feature image
36
+        if ( $post_metas->accept_feature_img == "1" && $post_metas->feature_img != "" ) {
37
+            $this->update_post_thumbnail( $post_id, $post_metas->feature_img );
38
+        }
39
+        // delete post metas
40
+        $post_meta_keys = $component->get_post_meta_keys();
41
+        foreach ( $post_meta_keys as $key => $value ) {
42
+            delete_post_meta( $post_id, $value );
43
+        }
44
+        // clear schedule on wp_cron
45
+        wp_clear_scheduled_hook( RC_CRON_HOOK, array( $post_id ) );
46
+    }
47
+    
48
+    public function rc_content_allow_iframe( $content ) {
49
+        global $allowedposttags;
50
+        // iframe and attribute in iframe
51
+        $allowedposttags['iframe'] = array(
52
+            'class' => array(), 'src' => array(),
53
+            'width' => array(), 'height' => array(),
54
+            'frameborder' => array(), 'scrolling' => array(),
55
+            'marginheight' => array(), 'marginwidth' => array(),
56
+            'srcdoc' => array(), 'sandbox' => array(),
57
+            'seamless' => array(), 'name' => array(),
58
+        );
59
+        return $content;
60
+    }
61
+    
62
+    private function update_post_thumbnail( $post_id, $reserved_post_thumb_path ) {
63
+        include_once(ABSPATH . 'wp-admin/includes/image.php');
64
+        $upload_dir = wp_upload_dir();
65
+        $image_data = file_get_contents( $reserved_post_thumb_path );
66
+        $file_name = basename( $reserved_post_thumb_path );
67
+        
68
+        if( wp_mkdir_p( $upload_dir['path'] ) ) {
69
+           $file = $upload_dir['path'] . '/' . $file_name; 
70
+        } else {
71
+           $file = $upload_dir['basedir'] . '/' . $file_name;
72
+        }
73
+        file_put_contents( $file, $image_data );
74
+        $wp_file_type = wp_check_filetype( $file_name, null );
75
+        $attachment = array(
76
+        'post_mime_type' => $wp_file_type['type'],
77
+        'post_title'     => sanitize_file_name( $file_name ),
78
+        'post_content'   => '',
79
+        'post_status'    => 'inherit',
80
+        );
81
+        delete_post_thumbnail( $post_id );
82
+        $attachment_id = wp_insert_attachment( $attachment, $file, $post_id );
83
+        $attach_data = wp_generate_attachment_metadata( $attachment_id, $file );
84
+        if( !empty( $attach_data ) && !is_wp_error( $attach_data ) ){
85
+            $res = wp_update_attachment_metadata( $attachment_id, $attach_data );
86
+            set_post_thumbnail( $post_id, $attachment_id );
87
+
88
+            return $res;
89
+        }
90
+    }
91
+}
... ...
@@ -21,6 +21,7 @@ load_plugin_textdomain( RC_TXT_DOMAIN, false, 'rucy/lang' );
21 21
 require_once RC_PLUGIN_DIR . '/inc/class-rucy-component.php';
22 22
 require_once RC_PLUGIN_DIR . '/inc/class-rucy-setting.php';
23 23
 require_once RC_PLUGIN_DIR . '/inc/class-rucy-editor.php';
24
+require_once RC_PLUGIN_DIR . '/inc/class-rucy-cron.php';
24 25
 
25 26
 class Rucy_Class {
26 27
     public $support_post_type = array();
... ...
@@ -46,6 +47,9 @@ class Rucy_Class {
46 46
             add_filter('manage_'.$p.'_columns', array( $this, 'manage_rucy_cols' ) );
47 47
             add_action('manage_'.$p.'_custom_column', array( $this, 'add_rucy_col' ), 10, 2); 
48 48
         }
49
+        // update content to reserved content
50
+        $cron = new Class_Rucy_Cron();
51
+        add_action( RC_CRON_HOOK, array( $cron, 'update_rc_reserved_content' ) );
49 52
         // deactivation this plugin
50 53
         register_deactivation_hook( __FILE__ , array( $this, 'uninstall_rucy' ) );
51 54
         // uninstall this plugin