GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

WP_SYND_Action::set_enclosure()   B
last analyzed

Complexity

Conditions 7
Paths 5

Size

Total Lines 20
Code Lines 14

Duplication

Lines 5
Ratio 25 %

Importance

Changes 0
Metric Value
cc 7
eloc 14
nc 5
nop 1
dl 5
loc 20
rs 8.2222
c 0
b 0
f 0
1
<?php
2
class WP_SYND_Action {
3
	private $args = array(
4
					'post_type' => 'wp-syndicate',
5
					'posts_per_page' => -1,
6
					'post_status' => 'publish'
7
				);
8
	private $post;
9
	private $host;
10
	private $match_count = 0;
11
	private $media_id;
12
	private $is_enclosure = false;
13
	private $enclosure_url;
14
15
16
	public function __construct() {
17
		add_filter( 'cron_schedules', array( $this, 'cron_schedules' ) );
18
		add_action( 'publish_to_trash', array( $this, 'publish_to_trash' ));
19
		add_action( 'save_post', array( $this, 'set_event' ) );
20
		add_action( 'admin_head', array( $this, 'ping' ) );
21
		add_action( 'template_redirect', array( $this, 'ping' ) );
22
		add_action( 'init', array( $this, 'init' ) );
23
	}
24
25
	public function init() {
26
		$posts = get_posts($this->args);
27
28
		if ( empty($posts) )
29
			return;
30
31
		foreach ( $posts as $post ) {
32
			add_action( 'wp_syndicate_' . $post->post_name . '_import', array( $this, 'import' ) );
33
		}
34
	}
35
36
	public function publish_to_trash($post) {
37
		if ( 'wp-syndicate' !== $post->post_type )
38
			return;
39
40
		$hook = 'wp_syndicate_' .  str_replace( '__trashed', '', $post->post_name ) . '_import';
41
		if ( wp_next_scheduled( $hook, array( $post->ID )) )
42
			var_dump(wp_clear_scheduled_hook( $hook, array( $post->ID ) ));
0 ignored issues
show
Security Debugging Code introduced by
var_dump(wp_clear_schedu...ok, array($post->ID))); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
43
	}
44
45
	public function ping() {
46
		$posts = get_posts($this->args);
47
		if ( empty($posts) )
48
			return;
49
50
		foreach ( $posts as $post ) {
51
			$key = $post->post_name;
52
			$hook = 'wp_syndicate_' . $key . '_import';
53
54
			if ( !wp_next_scheduled( $hook, array( $post->ID ) ) ) {
55
				$this->set_event($post->ID);
56
				$subject = '[' . get_bloginfo( 'name' ) . ']' . __( 'WP Cron Error', WPSYND_DOMAIN );
57
				$msg  = sprintf( __( '%s of WP Cron restart, because it stopped.', WPSYND_DOMAIN ), $hook ) . "\n". __( 'action time', WPSYND_DOMAIN ). ':' . date_i18n('Y/m/d:H:i:s') . "\n\n\n";
58
				$msg .= admin_url();
59
				WP_SYND_logger::get_instance()->error( $subject, $msg );
60
				$options = get_option( 'wp_syndicate_options' );
61
				wp_mail( $options['error_mail'], $subject, $msg );
62
			}
63
		}
64
	}
65
66
	public function cron_schedules($schedules) {
67
		$posts = get_posts($this->args);
68
69
		if ( empty($posts) ) {
70
			return $schedules;
71
		}
72
73
		foreach ( $posts as $post ) {
74
			$key = 'wp_syndicate_' . $post->post_name;
75
			$interval_min = get_post_meta( $post->ID, 'wp_syndicate-feed-retrieve-term', true );
76
			$interval = intval($interval_min)*60;
77
			$display = get_the_title( $post->ID );
78
			$schedules[$key] = array( 'interval' => $interval, 'display' => $display );
79
		}
80
81
		return $schedules;
82
	}
83
84
	public function set_event($post_id) {
85
86
		if ( wp_is_post_revision($post_id) )
87
			return;
88
89
		if ( 'wp-syndicate' != get_post_type($post_id) )
90
			return;
91
92
		$post = get_post( $post_id );
93
		if ( !is_object($post) )
94
			return;
95
96
		$key = $post->post_name;
97
		$interval_min = get_post_meta( $post_id, 'wp_syndicate-feed-retrieve-term', true );
98
		$interval = intval($interval_min)*60;
99
		$action_time = time() + $interval;
100
101
		$hook = 'wp_syndicate_' . $key . '_import';
102
		$event = 'wp_syndicate_' . $key;
103
104
		if ( wp_next_scheduled( $hook, array( $post_id )) )
105
			wp_clear_scheduled_hook( $hook, array( $post_id ) );
106
107
		wp_schedule_event( $action_time, $event, $hook, array( $post_id )  );
108
		spawn_cron( $action_time );
109
	}
110
111
	public function import($post_id) {
112
		global $allowedposttags;
113
114
		$allowedposttags = apply_filters( 'wp_syndicate_allowedposttags', $allowedposttags );
115
116
		$options = get_option( 'wp_syndicate_options' );
117
		$post = get_post($post_id);
118
119
		if ( !is_object($post) )
120
			return;
121
122
		$this->media_id = $post_id;
123
		$feed_url = html_entity_decode(get_post_meta( $post_id, 'wp_syndicate-feed-url', true ), ENT_QUOTES, 'UTF-8');
124
125
		add_action('wp_feed_options', function(&$feed){
126
			$feed->set_timeout(30);
127
			$feed->force_feed(true);
128
			$feed->enable_cache(false);
129
		}, 10);
130
131
		add_filter( 'wp_feed_cache_transient_lifetime' , array( $this, 'return_0' ) );
132
		$rss = fetch_feed( $feed_url );
133
		remove_filter( 'wp_feed_cache_transient_lifetime' , array( $this, 'return_0' ) );
134 View Code Duplication
		if ( is_wp_error( $rss ) ) {
135
			$subject = '[' . get_bloginfo( 'name' ) . ']' . __( 'feed import failed', WPSYND_DOMAIN );
136
			$msg  = sprintf( __( 'An error occurred at a feed retrieval of %s', WPSYND_DOMAIN ), $post->post_name ) . "\n". __( 'action time', WPSYND_DOMAIN ). ':' . date_i18n('Y/m/d:H:i:s') . "\n\n\n";
137
			$msg .= __( 'below error message', WPSYND_DOMAIN ) . "\n";
138
			$msg .= $rss->get_error_message()."\n\n";
139
			$msg .= __( 'feed URL', WPSYND_DOMAIN ) . ':' . $feed_url;
140
			$error_post_id = WP_SYND_logger::get_instance()->error( $subject, $msg );
141
			$msg .= admin_url('/post.php?post=' . $error_post_id . '&action=edit');
142
			wp_mail( $options['error_mail'], $subject, $msg );
143
			return;
144
		}
145
146
		$url = parse_url($rss->get_base());
147
		$this->host = $url['host'];
148
149
		$rss_items = $rss->get_items(0, 50);
150
		$post_ids = array();
151
		$flg = true;
152
		$registration_method = get_post_meta( $post_id, 'wp_syndicate-registration-method', true );
153
		$post_type = get_post_meta( $post_id, 'wp_syndicate-default-post-type', true );
154
		foreach ( $rss_items as $item ) {
155
			$this->is_enclosure = false;
156
157
			//投稿ID取得
158
			$slug = $post->post_name . '_' . $item->get_id();
159
			$set_post = get_page_by_path( sanitize_title($slug), OBJECT, $post_type );
160
			$set_post_id = $set_post == null ? '' : $set_post->ID;
161
162
			if ( empty($set_post_id) ) {
163
				global $wpdb;
164
				$db_ret = $wpdb->get_row( $wpdb->prepare( "SELECT count(1) as cnt FROM $wpdb->postmeta WHERE meta_key='%s'", $slug) );
165
				if ( $db_ret === null || $db_ret->cnt !== '0' )
166
					continue;
167
			}
168
169
			if ( $registration_method == 'insert' && is_object($set_post) ) {
170
				continue;
171
			}
172
173
			$updated = empty($set_post_id) ? false : true;
174
			$is_skip = apply_filters( 'wp_syndicate_is_skip', false, $item, $updated, $set_post_id, $post_id );
175
			if ( $is_skip ) {
176
				continue;
177
			}
178
179
			$post_args = array(
180
								'ID' => $set_post_id,
181
								'post_name' => $slug,
182
								'post_date' => apply_filters( 'wp_syndicate_get_date', $item->get_date('Y/m/d H:i:s'), $post_id ),
183
 								'post_title' => apply_filters( 'wp_syndicate_get_title', $item->get_title(), $post_id ),
184
								'post_content' => '',
185
							);
186
			if ( !$updated ) {
187
				$post_args['post_author'] = get_post_meta( $post_id, 'wp_syndicate-author-id', true );
188
				$post_args['post_status'] = get_post_meta( $post_id, 'wp_syndicate-default-post-status', true );
189
				$post_args['post_type'] = get_post_meta( $post_id, 'wp_syndicate-default-post-type', true );
190
			}
191
			$this->post = new wp_post_helper($post_args);
192
193
			//画像の登録
194
			if ( $updated ) {
195
				$images = get_attached_media( 'image', $set_post_id );
196
				if ( is_array( $images ) ) {
197
					foreach ( $images as $image ) {
198
						wp_delete_attachment( $image->ID );
199
					}
200
				}
201
			}
202
203
			$content = apply_filters( 'the_content', $item->get_content() );
204
			if ( $item->get_enclosure() && !empty($item->get_enclosure()->link) ) {
205
				$this->is_enclosure = true;
206
				$this->set_enclosure( $item->get_enclosure()->link );
207
			}
208
209
			$this->match_count = 0;
210
			$content = preg_replace_callback( '#<img([^>]*)src=["\']([^"\']+)["\']([^>]*)>#i',  array($this, 'update_link'), $content, -1 );
211
			$this->match_count = 0;
212
			$this->post->set(array(
213
								'post_content' => apply_filters( 'wp_syndicate_get_content', $content, $post_id )
214
							));
215
			$this->post->add_meta( 'wp_syndicate-origin-of-syndication-slug', $post->post_name, true );
216
			$this->post->add_meta( 'wp_syndicate-origin-of-syndication-siteurl', $this->host, true );
217
218
			$update_post_id = $this->post->update();
219
			if ( !$update_post_id ) {
220
				$flg = false;
221
			} else {
222
				update_post_meta( $update_post_id, $slug, 1 );
223
				do_action( 'wp_syndicate_save_post', $update_post_id, $item, $updated, $post_id );
224
				$post_ids[] = $update_post_id;
225
			}
226
		}
227
228
		if ( $flg ) {
229
			$subject = '[' . get_bloginfo( 'name' ) . ']' . __( 'feed import success', WPSYND_DOMAIN );
230
			$msg = __( 'feed URL:', WPSYND_DOMAIN ) . $feed_url . "\n";
231
			$msg .= sprintf( __( 'Feed acquisition completion of %s', WPSYND_DOMAIN ), $post->post_name ) . "\n" . __( 'action time', WPSYND_DOMAIN ). ':' . date_i18n('Y/m/d:H:i:s') . "\n\n\n";
232
			$msg .= __( 'below ID data updates', WPSYND_DOMAIN ) . "\n";
233
			$msg .= implode( "\n", $post_ids );
234
			WP_SYND_logger::get_instance()->success( $subject, $msg );
235 View Code Duplication
		} else {
236
			$subject = '[' . get_bloginfo( 'name' ) . ']' . __( 'feed import failed', WPSYND_DOMAIN );
237
			$msg = __( 'feed URL:', WPSYND_DOMAIN ) . $feed_url . "\n";
238
			$msg .= sprintf( __( 'Failed to some data registration of %s', WPSYND_DOMAIN ), $post->post_name ) . "\n". __( 'action time', WPSYND_DOMAIN ). ':' . date_i18n('Y/m/d:H:i:s') . "\n\n\n";
239
			$msg .= __( 'below ID data updates', WPSYND_DOMAIN ) . "\n";
240
			$msg .= implode( "\n", $post_ids );
241
			$error_post_id = WP_SYND_logger::get_instance()->error( $subject, $msg );
242
			$msg .= admin_url('/post.php?post=' . $error_post_id . '&action=edit');
243
			wp_mail( $options['error_mail'], $subject, $msg );
244
		}
245
246
	}
247
248
	public function return_0( $seconds ) {
0 ignored issues
show
Unused Code introduced by
The parameter $seconds is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
249
		return 0;
250
	}
251
252
	public function update_link( $matches ) {
253
254
		if ( is_array($matches) && array_key_exists(2, $matches) && isset($this->post) && is_object($this->post) && is_a($this->post, 'wp_post_helper') ) {
255
			$args    = array();
256
			$user    = get_post_meta( $this->media_id, 'wp_syndicate-basic-auth-user', true );
257
			$pass    = get_post_meta( $this->media_id, 'wp_syndicate-basic-auth-pass', true );
258 View Code Duplication
			if ( !empty($user) && !empty($pass) ) {
259
				$args = array(
260
					'headers' => array( 'Authorization' => 'Basic ' . base64_encode( $user . ':' . $pass ) )
261
				);
262
			}
263
264
			$args['timeout'] = 30;
265
			if ( $media = remote_get_file($matches[2], '', $args) ) {
266
267
				if ( $this->is_enclosure === true ) {
268
					$thumnail_flg = false;
269
				} else {
270
					$thumnail_flg = $this->match_count > 0 ? false : true;
271
				}
272
273
				$url = preg_split( '/wp-content/', $media );
274
				$url = home_url( 'wp-content' . $url[1] );
275
276
				if ( $url == $this->enclosure_url ) {
277
					$thumnail_flg = true;
278
				}
279
280
				$this->post->add_media($media, '', '', '', $thumnail_flg);
281
				$this->match_count++;
282
283
				return apply_filters( 'wp_syndicate_return_img', '<img' . $matches[1] . 'src="' . $url . '"' . $matches[3] . '>', $thumnail_flg, $url, $this->enclosure_url, $this->match_count );
284
			} else {
285
				return $matches[0];
286
			}
287
		}
288
		return $matches[0];
289
	}
290
291
	public function set_enclosure($link) {
292
		if ( !empty($link) && is_object($this->post) && is_a($this->post, 'wp_post_helper') ) {
293
			$args    = array();
294
			$user    = get_post_meta( $this->media_id, 'wp_syndicate-basic-auth-user', true );
295
			$pass    = get_post_meta( $this->media_id, 'wp_syndicate-basic-auth-pass', true );
296 View Code Duplication
			if ( !empty($user) && !empty($pass) ) {
297
				$args = array(
298
					'headers' => array( 'Authorization' => 'Basic ' . base64_encode( $user . ':' . $pass ) )
299
				);
300
			}
301
302
			$args['timeout'] = 30;
303
			if ( $media = remote_get_file($link, '', $args) ) {
304
				$this->post->add_media($media, '', '', '', true);
305
				$url = preg_split( '/wp-content/', $media );
306
				$url = home_url( 'wp-content' . $url[1] );
307
				$this->enclosure_url = $url;
308
			}
309
		}
310
	}
311
}
312
new WP_SYND_Action();
313