-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwp-social-posts.php
More file actions
102 lines (60 loc) · 2.32 KB
/
wp-social-posts.php
File metadata and controls
102 lines (60 loc) · 2.32 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
<?php
/*
Plugin Name: Social Posts
Version: 1.0
Author: Laure Guicherd
Author Email: laure.guicherd@gmail.com
*/
namespace SocialPosts;
use SocialPosts\Importer as Importer;
use WP_Query;
require_once( plugin_dir_path( __FILE__ ) . 'autoload.php' );
class Plugin {
static protected $custom_field = 'social_url';
protected $post_metas = [];
public function __construct() {
register_activation_hook( __FILE__, array( $this, 'activate' ) );
register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) );
add_action( 'admin_init', [ $this, 'admin_init' ], 9, 2 );
add_action( 'social_posts/import', [ $this, 'import' ], 10, 2 );
}
public function activate() {
// Nothing to do here yet.
}
public function deactivate() {
// Nothing to do here yet.
}
public function admin_init() {
if ( array_key_exists('action', $_REQUEST) && $_REQUEST['action']=='editpost' ) {
if ( array_key_exists('post_ID', $_REQUEST) ) {
$post_id = intval( $_REQUEST['post_ID'] );
$meta_value = get_post_meta( $post_id, self::$custom_field, true );
$this->post_metas[ self::$custom_field ] = $meta_value;
add_action( 'save_post', [ $this, 'after_update_post' ], 100, 3 );
}
}
}
public function after_update_post( $post_id, $post, $update ) {
$meta_value_before = $this->post_metas[ self::$custom_field ];
$meta_value = get_post_meta( $post_id, self::$custom_field, true );
if ( ! $meta_value && array_key_exists('meta', $_REQUEST) ) {
$metas = wp_list_filter( $_REQUEST['meta'], ['key' => self::$custom_field] );
$meta = reset( $metas );
$meta_value = $meta['value'];
}
if ( $meta_value != $meta_value_before ) {
remove_action( 'save_post', [ $this, 'after_update_post' ], 100, 3 );
$this->import( $meta_value, $post_id );
add_action( 'save_post', [ $this, 'after_update_post' ], 100, 3 );
}
}
public function import( $social_url, $post_id ) {
$importer = new Importer( $social_url, $post_id );
$importer->import();
}
}
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
new Plugin();