Need help diagnosing an issue.

[ad_1]

I have a function in a plugin Ive made that will go to a gov website and look for files that have been added to their document database over the last 7 days. It posts to the url, and recieves a csv of the data. Then I loop through the data and depending on the information, check for data at another website and combine that info into the body of a post, then check to see if that post exists in my wordpress site, and if it doesn’t then it adds the post.

It works well when I call it from the admin panel page I made, ( it has a single button called “update media titles and ALT text” ) but it stops running at the first check of “post exists” when I run it by wp cron. So it will fetch all the data in the csv and check if the post exists, but only once, and then the function never completes.

I dont understand what could be the problem.

my wp memory limit is 256M and the wp max memory limit is 768M. I have timed the process when I run it manually and the whole thing runs in less than 60 seconds. So I am at a loss. I will share the code. can anyone help me understand why this works in one context and not another? and how to solve it ?

​

function prpfwn_fetch_fieldworknotices() {
wp_mail(“[email protected]”, “fetch function started”, “The fectch function started at ” . current_time(“mysql”));
$prpfwn_date = date_create();
$prpfwn_todaysdate = date_timestamp_get($prpfwn_date);
$prpfwn_todate = mktime(0, 0, 0, date(“m”), date(“d”) – 1, date(“Y”));
$prpfwn_fromdate = mktime(0, 0, 0, date(“m”), date(“d”) – 8, date(“Y”));
$prpfwn_url = ‘https://prodenv.dep.state.fl.us/DepNexus/public/electronic-documents’;
$prpfwn_body = array(
‘__checkbox_electronicDocument.airDivision’ => ‘true’,
‘__checkbox_electronicDocument.waterDivision’ => ‘true’,
‘__checkbox_electronicDocument.wasteDivision’ => ‘true’,
‘electronicDocument.documentType’ => ‘FIELD WORK NOTIFICATION’,
‘electronicDocument.dateFrom’ => date(“m/d/Y”, $prpfwn_fromdate),
‘electronicDocument.dateTo’ => date(“m/d/Y”, $prpfwn_todate),
‘electronicDocument.dateReceivedFrom’ => ”,
‘electronicDocument.dateReceivedTo’ => ”,
‘electronicDocument.subject’ => ”,
‘electronicDocument.facilityId’ => ”,
‘electronicDocument.permitId’ => ”,
‘electronicDocument.facilityDistrict’ => ”,
‘electronicDocument.facilityCounty’ => ”,
‘newSearch’ => ‘Yes’,
‘electronicDocument.sortCriteria’ => ”
);

$prpfwn_args = array(
‘body’ => $prpfwn_body,
‘timeout’ => ’60’,
‘redirection’ => ‘5’,
‘httpversion’ => ‘1.0’,
‘blocking’ => true,
‘headers’ => array(),
‘cookies’ => array()
);

$prpfwn_fieldworktable = wp_remote_retrieve_body(wp_remote_get(‘https://prodenv.dep.state.fl.us/DepNexus/public/export!exportElectronicDocuments?wildCardMatch=true’, array(
‘timeout’ => ’60’,
‘cookies’ => wp_remote_retrieve_cookies(wp_remote_post($prpfwn_url, $prpfwn_args))
)));
wp_mail(“[email protected]”, “vardump”, $prpfwn_fieldworktable);
if (str_contains($prpfwn_fieldworktable, “We encountered an error while trying to process your request.”)) {
wp_mail(“[email protected]”,”function stopped line 314″, “function stopped”);
return;
}
$prpfwn_loop_count = 0;
foreach (array_slice(parse_csv($prpfwn_fieldworktable), 1, -1) as $prpfwn_row) {
// Create post object
$prpfwn_loop_count++;
//echo “loop $prpfwn_loop_count\n”;
//if this is a PRP submission confirmation pdf, skip loop
if ($prpfwn_row[9] == “PRP SUBMISSION CONFIRMATION”){
continue;
}
if (str_starts_with($prpfwn_row[0],’ERIC’)){
do_action( ‘inspect’, [ ‘prpfwn_row[0]’, $prpfwn_row[0] ]);
$prpfwn_publicdataeric = wp_remote_get(“https://ca.dep.state.fl.us/arcgis/rest/services/OpenData/CLEANUP_SP/MapServer/8/query?where=ERIC_ID%20%3D%20′”.$prpfwn_row[0] .”‘&outFields=*&outSR=4326&f=json”);
$prpfwn_jsoneric = json_decode(wp_remote_retrieve_body($prpfwn_publicdataeric), true);
$prpfwn_xcoord = $prpfwn_jsoneric[‘features’][0][‘geometry’][‘x’];
$prpfwn_ycoord = $prpfwn_jsoneric[‘features’][0][‘geometry’][‘y’];
$prpfwn_documents = $prpfwn_jsoneric[‘features’][0][‘attributes’][‘DOCUMENTS’];
$prpfwn_fac_status = ”;
$prpfwn_fac_type = ”;
$prpfwn_fac_cleanup_status = ”;
}

if (is_numeric($prpfwn_row[0])) {
//echo “is numeric ran\n”;
//echo “checking public data from FDEP Arcgis\n”;
$prpfwn_publicdata = wp_remote_get(“https://ca.dep.state.fl.us/arcgis/rest/services/OpenData/DWM_STCM/MapServer/1/query?where=%20FACILITY_ID%20%3D%20” . $prpfwn_row[0] . “%20&outFields=*&outSR=4326&f=json”);
$prpfwn_json = json_decode(wp_remote_retrieve_body($prpfwn_publicdata), true);
$prpfwn_xcoord = $prpfwn_json[‘features’][0][‘geometry’][‘x’];
$prpfwn_ycoord = $prpfwn_json[‘features’][0][‘geometry’][‘y’];
$prpfwn_documents = $prpfwn_json[‘features’][0][‘attributes’][‘DOCUMENTS’];
$prpfwn_fac_status = $prpfwn_json[‘features’][0][‘attributes’][‘FACILITY_STATUS’];
$prpfwn_fac_type = $prpfwn_json[‘features’][0][‘attributes’][‘FACILITY_TYPE’];
$prpfwn_fac_cleanup_status = $prpfwn_json[‘features’][0][‘attributes’][‘FACILITY_CLEANUP_STATUS’];

}

$prpfwn_post_received = prpfwn_date_convert($prpfwn_row[7], ‘m-d-Y’, ‘Y-m-d’);

$prpfwn_title = $prpfwn_row[0] . ‘ ‘ . $prpfwn_row[1] . ‘ ‘ . $prpfwn_post_received;
$prpfwn_address = $prpfwn_row[2] . ‘ ‘ . $prpfwn_row[3] . ‘ ‘ . $prpfwn_row[4];
$prpfwn_content = “[leaflet-map lat=$prpfwn_ycoord lng=$prpfwn_xcoord] [leaflet-marker] <table>
<tbody>
<tr>
<td><strong>Date Submitted</strong></td>
<td>[prpfwn_received]</td>
</tr>
<tr>
<td><strong>Name</strong></td>
<td>[prpfwn_name]</td>
</tr>
<tr>
<td><strong>Facility ID</strong></td>
<td>[prpfwn_facid]</td>
</tr>
<tr>
<td><strong>Address</strong></td>
<td>[prpfwn_address] [prpfwn_city] [prpfwn_county]</td>
</tr>
<tr>
<td><strong>X,Y Coordinates</strong></td>
<td>[prpfwn_xcoord]” . “, ” . “[prpfwn_ycoord]</td>
</tr>
<tr>
<td><strong>Status</strong></td>
<td>[prpfwn_fac_status]</td>
</tr>
<tr>
<td><strong>Type</strong></td>
<td>[prpfwn_cleanup_fac_type]</td>
</tr>
<tr>
<td><strong>Cleanup Status</strong></td>
<td>[prpfwn_cleanup_status]</td>
</tr>
<tr>
<td><strong>Summary</strong></td>
<td>[prpfwn_summary]</td>
</tr>
<tr>
<td><strong>Documents</strong></td>
<td><a href=[prpfwn_documents]>FDEP Files<a></td>
</tr>
<tr>
<td><strong>Email Link</strong></td>
<td><a href=[prpfwn_path]>Email Notification<a></td>
</tr>
</tbody>
</table>
“;

$prpfwn_meta = array(
‘FACID’ => $prpfwn_row[0],
‘NAME’ => $prpfwn_row[1],
‘ADDRESS’ => $prpfwn_row[2],
‘CITY’ => $prpfwn_row[3],
‘COUNTY’ => $prpfwn_row[4],
‘RECEIVED’ => $prpfwn_post_received,
‘SUBJECT’ => $prpfwn_row[9],
‘FILETYPE’ => $prpfwn_row[11],
‘PATH’ => $prpfwn_row[13],
‘XCOORD’ => $prpfwn_xcoord,
‘YCOORD’ => $prpfwn_ycoord,
‘EMAIL’ => $prpfwn_row[13],
‘DOCUMENTS’ => $prpfwn_documents,
‘SUMMARY’ => $prpfwn_summary,
‘CLEANUPSTATUS’ => $prpfwn_fac_cleanup_status,
‘FACILITYTYPE’ => $prpfwn_fac_type,
‘STATUS’ => $prpfwn_fac_status

);
$prpfwn_post = array(
‘post_title’ => wp_strip_all_tags($prpfwn_title),
‘post_content’ => $prpfwn_content,
‘post_status’ => ‘publish’,
‘post_type’ => ‘prpfwn_event’,
‘meta_input’ => $prpfwn_meta,
‘post_date’ => $prpfwn_post_received
);

// Insert the post into the database
wp_mail(“[email protected]”, “checking if post exists”, “checking if post $prpfwn_title , $prpfwn_content, $prpfwn_post_received , exists at ” . current_time(“mysql”));
if (!post_exists(wp_strip_all_tags($prpfwn_title), $prpfwn_content, $prpfwn_post_received, ‘prpfwn_event’)) {
wp_insert_post($prpfwn_post);
}
}
wp_mail(“[email protected]”, “fetch function ran”, “The fectch function ran at ” . current_time(“mysql”));
}

&#x200B;

[ad_2]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer