-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-api-json-import.php
227 lines (188 loc) · 5.63 KB
/
wp-api-json-import.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
<?php
/**
* Plugin Name: WP API JSON Import
* Plugin URI:
* Description:
* Author: valeriosza, leobaiano, nicholas_io
* Author URI:
* Version: 0.0.2
* License: GPLv2 or later
* Text Domain: wpapijson-import
* Domain Path: /languages/
*/
if ( ! defined( 'ABSPATH' ) )
exit; // Exit if accessed directly.
require_once( plugin_dir_path( __FILE__ ) . 'classes/class-settings.php' );
/**
* WP API JSON Import
*
* @author Leo Baiano <[email protected]>
*/
class WP_API_JSON_Import {
/**
* Plugin version.
*
* @var string
*/
const VERSION = '0.0.2';
/**
* Plugin Slug
* @var string
*/
public static $plugin_slug = 'wpapijson-import';
/**
* Instance of this class.
*
* @var object
*/
protected static $instance = null;
/**
* Holds a instance for the Settings Class
*/
protected $settings;
/**
* Holds the array of saved options
*
* @var string
*/
protected $options = array();
/**
* Holds options name under settings api
*
* @var string
*/
protected $option_name;
/**
* Initialize the plugin
*/
private function __construct() {
// Load plugin text domain
add_action( 'plugins_loaded', array( $this, 'load_plugin_textdomain' ) );
// Load scripts js and styles css
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
// Run Import
add_action( 'my_hourly_event', array( $this, 'import_posts') );
// Function AJAX impot posts
add_action( 'wp_ajax_import_posts', array( $this, 'import_posts' ) );
add_action( 'wp_ajax_nopriv_import_posts', array( $this, 'import_posts' ) );
$this->settings = new WP_API_JSON_Import_Settings( self::$plugin_slug );
$this->option_name = self::$plugin_slug . '-settings';
$this->options = get_option( $this->option_name );
// Load Importer API
//require_once ABSPATH . 'wp-admin/includes/import.php';
//$GLOBALS['wp_rest_import'] = new WP_API_JSON_Import();
//register_importer('wpapijsonimport', __('WP API JSON Import', 'wpapijson-import'), __('Import links in OPML format.', 'wpapijson-import'), array($GLOBALS['wp_rest_import'], 'dispatch'));
}
/**
* Return an instance of this class.
*
* @return object A single instance of this class.
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Load the plugin text domain for translation.
*
* @return void
*/
public function load_plugin_textdomain() {
load_plugin_textdomain( self::$plugin_slug, false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Load scripts js and styles css
*/
public function enqueue_scripts() {
// variables for main.js
$params = array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ) );
// Load main CSS file
wp_enqueue_style( self::$plugin_slug . '_css_main', plugins_url( 'assets/css/main.css', __FILE__ ), array(), null, 'all' );
// Load main JS file
wp_enqueue_script( self::$plugin_slug . '_js_main', plugins_url( 'assets/js/main.js', __FILE__ ), array( 'jquery' ), null, true );
// WP Localize Script pass variables for main.js
wp_localize_script( self::$plugin_slug . '_js_main', 'sale_post_variables', $params );
}
/**
* Fired when the plugin is activated.
*
* @param boolean $network_wide True if WPMU superadmin uses
* "Network Activate" action, false if
* WPMU is disabled or plugin is
* activated on an individual blog.
*
* @return void
*/
public static function activate( $network_wide ) {
if ( function_exists( 'is_multisite' ) && is_multisite() ) {
if ( $network_wide ) {
// Get all blog ids
$blog_ids = self::get_blog_ids();
foreach ( $blog_ids as $blog_id ) {
switch_to_blog( $blog_id );
self::single_activate();
}
restore_current_blog();
} else {
self::single_activate();
}
} else {
self::single_activate();
}
}
/**
* Fired for each blog when the plugin is activated.
*
* @return void
*/
private static function single_activate() {
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
if ( get_option('wpapijson-import_urls') !== false ) {
update_option( 'wpapijson-import_urls', array() );
}
update_option( 'wpapijson_import_version', self::VERSION );
}
/**
* Import Posts JSON
*/
public function import_posts() {
$return = '';
$message = '';
$response = array();
if ( !empty( $this->options['urls'] ) ) {
// Get and sanitize data input
$urls = sanitize_text_field( $this->options['urls'] );
// Get json and converte array
$posts = json_decode( file_get_contents( $urls ), true );
// iterator count posts
$i = 0;
// loop in posts return
if( count( $posts ) ){
foreach( $posts as $post ) {
if ( $i < 2 )
$return .= '<p>' . $post['title'] . '</p>';
$i++;
}
}
if ( empty( $return ) )
$message = 'No post found';
$response = array( 'status' => 1, 'message' => $return );
} else {
$response = array( 'status' => 0, 'message' => __( 'No url registered, go the the tab "settings" and sign at least one url to import posts.', self::$plugin_slug ) );
}
echo json_encode( $response );
die();
}
public static function schedule() {
wp_schedule_event(time(), 'hourly', 'my_hourly_event');
}
}
// Activate plugin when new blog is added.
register_activation_hook( __FILE__ , array( 'WP_API_JSON_Import', 'activate' ) );
register_activation_hook( __FILE__ , array( 'WP_API_JSON_Import', 'schedule' ));
add_action( 'plugins_loaded', array( 'WP_API_JSON_Import', 'get_instance' ), 0 );