Conditions | 20 |
Paths | 416 |
Total Lines | 136 |
Code Lines | 100 |
Lines | 21 |
Ratio | 15.44 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
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 | |||
313 |