Guest User

Untitled

a guest
Sep 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. <?php
  2. require( 'wp-load.php' );
  3. if ( !function_exists( 'post_exists' ) ) {
  4. require_once( ABSPATH . 'wp-admin/includes/post.php' );
  5. }
  6. require( 'simple_html_dom.php' );
  7.  
  8. update_bac();
  9.  
  10. function update_bac()
  11. {
  12. // log each cron execution for easier troubleshooting in the event of issues
  13. $logfile = 'bac_cron.log';
  14. file_put_contents( $logfile, date( "Y-m-d H:i:s" ) . " n", FILE_APPEND );
  15. file_put_contents( $logfile, date( "Y-m-d H:i:s" ) . " Cron executedn", FILE_APPEND );
  16.  
  17. // Set a long time limit to avoid cron timeouts
  18. $time_limit_success = (set_time_limit( 900 )) ? 'TRUE' : 'FALSE';
  19. file_put_contents( $logfile, date( "Y-m-d H:i:s" ) . " set_time_limit successful? {$time_limit_success} n", FILE_APPEND );
  20.  
  21. // create a WordPress post for each entry in the array that doesn't already exist in the database
  22. foreach( $entries_to_post as $postdata )
  23. {
  24. file_put_contents( $logfile, date( "Y-m-d H:i:s" ) . " Looping entries_to_post, found {$postdata['name']}, {$postdata['date']} n", FILE_APPEND );
  25.  
  26. $matching_post_id = post_exists( $postdata['name'], '', $postdata['date'] );
  27.  
  28. if( $matching_post_id === 0 )
  29. {
  30. file_put_contents( $logfile, date( "Y-m-d H:i:s" ) . " Match not found in database, adding new post... n", FILE_APPEND );
  31.  
  32. $postarr = [
  33. 'post_author' => 0,
  34. 'post_date' => $postdata['date'],
  35. 'post_title' => $postdata['name'],
  36. 'post_status' => 'publish',
  37. 'post_type' => '[[custom-post-type]]'
  38. ];
  39. $post_id = wp_insert_post( $postarr, true );
  40.  
  41. if( is_int( $post_id ) )
  42. {
  43. update_field( 'field_5741032e15daf', $postdata['href'], $post_id );
  44. update_field( 'field_5b93201831b5d', $postdata['img'], $post_id );
  45.  
  46. file_put_contents( $logfile, date( "Y-m-d H:i:s" ) . " Post created: " . $post_id . ", " . $postdata['name'] . ", " . $postdata['href'] . "n", FILE_APPEND );
  47. }
  48. // wp_insert_post returns a WP_Error instead of a post ID if it fails
  49. else if( is_wp_error( $post_id ) )
  50. {
  51. file_put_contents( $logfile, date( "Y-m-d H:i:s" ) . " Something went wrong!n", FILE_APPEND );
  52. file_put_contents( $logfile, date( "Y-m-d H:i:s" ) . " " . $post_id->get_error_message(), FILE_APPEND );
  53. }
  54. }
  55. else
  56. {
  57. file_put_contents( $logfile, date( "Y-m-d H:i:s" ) . " Matching post found with ID {$matching_post_id} n", FILE_APPEND );
  58. }
  59. }
  60.  
  61. file_put_contents( $logfile, date( "Y-m-d H:i:s" ) . " Reached end of arrayn", FILE_APPEND );
  62. }
  63.  
  64. 2018-09-21 17:36:03
  65. 2018-09-21 17:36:03 Cron executed
  66. 2018-09-21 17:36:03 set_time_limit successful? TRUE
  67. 2018-09-21 17:36:24 Looping entries_to_post, found [[post-title-1]], 2018-06-08 00:00:00
  68. 2018-09-21 17:36:24 Matching post found with ID 516
  69. 2018-09-21 17:36:24 Looping entries_to_post, found [[post-title-2]], 2018-07-12 00:00:00
  70. 2018-09-21 17:36:24 Matching post found with ID 475
  71. 2018-09-21 17:36:24 Looping entries_to_post, found [[post-title-3]], 2018-08-31 00:00:00
  72. 2018-09-21 17:36:24 Matching post found with ID 476
  73. 2018-09-21 17:36:24 Looping entries_to_post, found [[post-title-4]], 2018-08-17 00:00:00
  74. 2018-09-21 17:36:24 Matching post found with ID 477
  75. 2018-09-21 17:36:24 Looping entries_to_post, found [[post-title-5]], 2018-09-07 00:00:00
  76. 2018-09-21 17:36:24 Matching post found with ID 478
  77. ...
  78. 2018-09-21 17:36:24 Reached end of array
  79.  
  80. 2018-09-21 16:53:27
  81. 2018-09-21 16:53:27 Cron executed
  82. 2018-09-21 16:53:27 set_time_limit successful? TRUE
  83. 2018-09-21 16:54:27
  84. 2018-09-21 16:54:27 Cron executed
  85. 2018-09-21 16:54:27 set_time_limit successful? TRUE
  86. 2018-09-21 16:55:01 Looping entries_to_post, found [[post-title-1]], 2018-06-08 00:00:00
  87. 2018-09-21 16:55:01 Matching post found with ID 516
  88. 2018-09-21 16:55:01 Looping entries_to_post, found [[post-title-2]], 2018-07-12 00:00:00
  89. 2018-09-21 16:55:01 Matching post found with ID 475
  90. 2018-09-21 16:55:01 Looping entries_to_post, found [[post-title-3]], 2018-08-31 00:00:00
  91. 2018-09-21 16:55:01 Matching post found with ID 476
  92. 2018-09-21 16:55:01 Looping entries_to_post, found [[post-title-4]], 2018-08-17 00:00:00
  93. 2018-09-21 16:55:01 Matching post found with ID 477
  94. 2018-09-21 16:55:01 Looping entries_to_post, found [[post-title-5]], 2018-09-07 00:00:00
  95. 2018-09-21 16:55:01 Matching post found with ID 478
  96. ...
  97. 2018-09-21 16:55:01 Reached end of array
  98. 2018-09-21 16:55:02 Looping entries_to_post, found [[post-title-1]], 2018-06-08 00:00:00
  99. 2018-09-21 16:55:02 Match not found in database, adding new post...
  100. 2018-09-21 16:55:02 Post created: 526, [[post-title-1]], [[href]]
  101. 2018-09-21 16:55:02 Looping entries_to_post, found [[post-title-2]], 2018-07-12 00:00:00
  102. 2018-09-21 16:55:02 Matching post found with ID 475
  103. 2018-09-21 16:55:02 Looping entries_to_post, found [[post-title-3]], 2018-08-31 00:00:00
  104. 2018-09-21 16:55:02 Matching post found with ID 476
  105. 2018-09-21 16:55:02 Looping entries_to_post, found [[post-title-4]], 2018-08-17 00:00:00
  106. 2018-09-21 16:55:02 Matching post found with ID 477
  107. 2018-09-21 16:55:02 Looping entries_to_post, found [[post-title-5]], 2018-09-07 00:00:00
  108. 2018-09-21 16:55:02 Matching post found with ID 478
  109. ...
  110. 2018-09-21 16:55:02 Reached end of array
Add Comment
Please, Sign In to add comment