Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require( 'wp-load.php' );
- if ( !function_exists( 'post_exists' ) ) {
- require_once( ABSPATH . 'wp-admin/includes/post.php' );
- }
- require( 'simple_html_dom.php' );
- update_bac();
- function update_bac()
- {
- // log each cron execution for easier troubleshooting in the event of issues
- $logfile = 'bac_cron.log';
- file_put_contents( $logfile, date( "Y-m-d H:i:s" ) . " n", FILE_APPEND );
- file_put_contents( $logfile, date( "Y-m-d H:i:s" ) . " Cron executedn", FILE_APPEND );
- // Set a long time limit to avoid cron timeouts
- $time_limit_success = (set_time_limit( 900 )) ? 'TRUE' : 'FALSE';
- file_put_contents( $logfile, date( "Y-m-d H:i:s" ) . " set_time_limit successful? {$time_limit_success} n", FILE_APPEND );
- // create a WordPress post for each entry in the array that doesn't already exist in the database
- foreach( $entries_to_post as $postdata )
- {
- file_put_contents( $logfile, date( "Y-m-d H:i:s" ) . " Looping entries_to_post, found {$postdata['name']}, {$postdata['date']} n", FILE_APPEND );
- $matching_post_id = post_exists( $postdata['name'], '', $postdata['date'] );
- if( $matching_post_id === 0 )
- {
- file_put_contents( $logfile, date( "Y-m-d H:i:s" ) . " Match not found in database, adding new post... n", FILE_APPEND );
- $postarr = [
- 'post_author' => 0,
- 'post_date' => $postdata['date'],
- 'post_title' => $postdata['name'],
- 'post_status' => 'publish',
- 'post_type' => '[[custom-post-type]]'
- ];
- $post_id = wp_insert_post( $postarr, true );
- if( is_int( $post_id ) )
- {
- update_field( 'field_5741032e15daf', $postdata['href'], $post_id );
- update_field( 'field_5b93201831b5d', $postdata['img'], $post_id );
- file_put_contents( $logfile, date( "Y-m-d H:i:s" ) . " Post created: " . $post_id . ", " . $postdata['name'] . ", " . $postdata['href'] . "n", FILE_APPEND );
- }
- // wp_insert_post returns a WP_Error instead of a post ID if it fails
- else if( is_wp_error( $post_id ) )
- {
- file_put_contents( $logfile, date( "Y-m-d H:i:s" ) . " Something went wrong!n", FILE_APPEND );
- file_put_contents( $logfile, date( "Y-m-d H:i:s" ) . " " . $post_id->get_error_message(), FILE_APPEND );
- }
- }
- else
- {
- file_put_contents( $logfile, date( "Y-m-d H:i:s" ) . " Matching post found with ID {$matching_post_id} n", FILE_APPEND );
- }
- }
- file_put_contents( $logfile, date( "Y-m-d H:i:s" ) . " Reached end of arrayn", FILE_APPEND );
- }
- 2018-09-21 17:36:03
- 2018-09-21 17:36:03 Cron executed
- 2018-09-21 17:36:03 set_time_limit successful? TRUE
- 2018-09-21 17:36:24 Looping entries_to_post, found [[post-title-1]], 2018-06-08 00:00:00
- 2018-09-21 17:36:24 Matching post found with ID 516
- 2018-09-21 17:36:24 Looping entries_to_post, found [[post-title-2]], 2018-07-12 00:00:00
- 2018-09-21 17:36:24 Matching post found with ID 475
- 2018-09-21 17:36:24 Looping entries_to_post, found [[post-title-3]], 2018-08-31 00:00:00
- 2018-09-21 17:36:24 Matching post found with ID 476
- 2018-09-21 17:36:24 Looping entries_to_post, found [[post-title-4]], 2018-08-17 00:00:00
- 2018-09-21 17:36:24 Matching post found with ID 477
- 2018-09-21 17:36:24 Looping entries_to_post, found [[post-title-5]], 2018-09-07 00:00:00
- 2018-09-21 17:36:24 Matching post found with ID 478
- ...
- 2018-09-21 17:36:24 Reached end of array
- 2018-09-21 16:53:27
- 2018-09-21 16:53:27 Cron executed
- 2018-09-21 16:53:27 set_time_limit successful? TRUE
- 2018-09-21 16:54:27
- 2018-09-21 16:54:27 Cron executed
- 2018-09-21 16:54:27 set_time_limit successful? TRUE
- 2018-09-21 16:55:01 Looping entries_to_post, found [[post-title-1]], 2018-06-08 00:00:00
- 2018-09-21 16:55:01 Matching post found with ID 516
- 2018-09-21 16:55:01 Looping entries_to_post, found [[post-title-2]], 2018-07-12 00:00:00
- 2018-09-21 16:55:01 Matching post found with ID 475
- 2018-09-21 16:55:01 Looping entries_to_post, found [[post-title-3]], 2018-08-31 00:00:00
- 2018-09-21 16:55:01 Matching post found with ID 476
- 2018-09-21 16:55:01 Looping entries_to_post, found [[post-title-4]], 2018-08-17 00:00:00
- 2018-09-21 16:55:01 Matching post found with ID 477
- 2018-09-21 16:55:01 Looping entries_to_post, found [[post-title-5]], 2018-09-07 00:00:00
- 2018-09-21 16:55:01 Matching post found with ID 478
- ...
- 2018-09-21 16:55:01 Reached end of array
- 2018-09-21 16:55:02 Looping entries_to_post, found [[post-title-1]], 2018-06-08 00:00:00
- 2018-09-21 16:55:02 Match not found in database, adding new post...
- 2018-09-21 16:55:02 Post created: 526, [[post-title-1]], [[href]]
- 2018-09-21 16:55:02 Looping entries_to_post, found [[post-title-2]], 2018-07-12 00:00:00
- 2018-09-21 16:55:02 Matching post found with ID 475
- 2018-09-21 16:55:02 Looping entries_to_post, found [[post-title-3]], 2018-08-31 00:00:00
- 2018-09-21 16:55:02 Matching post found with ID 476
- 2018-09-21 16:55:02 Looping entries_to_post, found [[post-title-4]], 2018-08-17 00:00:00
- 2018-09-21 16:55:02 Matching post found with ID 477
- 2018-09-21 16:55:02 Looping entries_to_post, found [[post-title-5]], 2018-09-07 00:00:00
- 2018-09-21 16:55:02 Matching post found with ID 478
- ...
- 2018-09-21 16:55:02 Reached end of array
Add Comment
Please, Sign In to add comment