Skip to content

Commit

Permalink
Add a way to trigger Sched sync from Settings
Browse files Browse the repository at this point in the history
Signed-off-by: James Hunt <[email protected]>
  • Loading branch information
thetwopct committed Aug 20, 2024
1 parent f9f5319 commit 2817809
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
die;
}
$args = array(
'post_type' => 'page',
'post_parent' => 0,
'meta_query' => array(
'post_type' => 'page',
'post_parent' => 0,
'meta_query' => array(
array(
'key' => 'lfes_event_has_passed',
'compare' => '!=',
Expand All @@ -39,10 +39,10 @@

while ( $the_query->have_posts() ) {
$the_query->the_post();
$sched_event_id = get_post_meta( get_the_ID(), 'lfes_sched_event_id', true );
$sched_event_api_key = get_post_meta( get_the_ID(), 'lfes_sched_event_api_key', true );
$sched_event_id = get_post_meta( get_the_ID(), 'lfes_sched_event_id', true );
$sched_event_api_key = get_post_meta( get_the_ID(), 'lfes_sched_event_api_key', true );

$url = 'https://' . $sched_event_id . '.sched.com/api/session/export?api_key=' . $sched_event_api_key . '&format=json&strip_html=Y&custom_data=Y';
$url = 'https://' . $sched_event_id . '.sched.com/api/session/export?api_key=' . $sched_event_api_key . '&format=json&strip_html=Y&custom_data=Y';
$data = wp_remote_get( $url );
if ( is_wp_error( $data ) || ( wp_remote_retrieve_response_code( $data ) != 200 ) ) {
return false;
Expand All @@ -67,12 +67,12 @@
foreach ( $speakers as $username => $speaker ) {
// Step throught the speaker array and jsonify each element.
// Write that string to the speaker's postmeta using the event_id as the meta key.
$args = array(
'post_type' => 'lfe_speaker',
'meta_query' => array(
$args = array(
'post_type' => 'lfe_speaker',
'meta_query' => array(
array(
'key' => 'lfes_speaker_sched_username',
'value' => $username,
'key' => 'lfes_speaker_sched_username',
'value' => $username,
),
),
);
Expand All @@ -82,6 +82,7 @@
$speaker_query->the_post();
$speaker_id = get_the_ID();
update_post_meta( $speaker_id, $sched_event_id, json_encode( $speaker ) );
update_option( 'lfevents_sync_sched_last_run', current_time( 'timestamp' ) );
}
wp_reset_postdata(); // Restore original Post Data.
}
Expand Down
75 changes: 70 additions & 5 deletions web/wp-content/themes/lfevents/library/lfe-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function lfe_settings_page() {
function lfe_faster_np_checkbox_display() {
?>
<!-- Here we are comparing stored value with 1. Stored value is 1 if user checks the checkbox otherwise empty string. -->
<input type='checkbox' name='lfe-faster-np-checkbox' value='1'
<input type="checkbox" name="lfe-faster-np-checkbox" value="1"
<?php checked( 1, get_option( 'lfe-faster-np-checkbox' ), true ); ?> />
<p class='description'>
Check this box to speed up the Nested Pages tool. When checked, the hidden Events will not be accessible.
Expand Down Expand Up @@ -76,7 +76,7 @@ function lfe_generic_staff_image_display() {
<div class="image-preview-wrapper">
<img
src="<?php echo esc_url( wp_get_attachment_url( $generic_staff_image_id ) ); ?>"
class="image-preview"
class="image-preview" height="200" width="200"
data-id="lfe-generic-staff-image-id">
</div>

Expand All @@ -93,15 +93,14 @@ class="clear_upload_image_button button" value="Remove image" />
<?php
}


/**
* Options page callback.
*/
function lfe_options_page() {
?>
<div class='wrap'>
<div class="wrap">
<h1>LF Events Options</h1>
<form method='post' action='options.php'>
<form method="post" action="options.php">
<?php
settings_fields( 'lfe_options_section' );

Expand All @@ -110,6 +109,72 @@ function lfe_options_page() {
submit_button();
?>
</form>
<hr>
<h2>Sched Sync</h2>
<?php lfe_sync_sched_button_display(); ?>
</div>
<?php
}

/**
* Sched Sync Button Display
*/
function lfe_sync_sched_button_display() {
$last_run_time = get_option( 'lfevents_sync_sched_last_run' );
$timezone = get_option( 'timezone_string' ) ?? null;
?>
<form method="post" action="">
<?php wp_nonce_field( 'lfe_sync_sched_nonce_action', 'lfe_sync_sched_nonce' ); ?>
<input type="hidden" name="lfe_sync_sched_action" value="1">
<input type="submit" class="button-primary" value="Run Sched Sync Now">
</form>
<p>
The Sched schedule syncs twice per day. This button triggers the sync to run now.
</p>
<?php if ( $last_run_time ) : ?>
<p class="description"><strong>Last sync:</strong>
<?php
echo esc_html( gmdate( 'F j, Y, g:i a', $last_run_time ) );
if ( $timezone ) {
echo ' (' . esc_html( $timezone ) . ')';
}
?>
</p>
<?php else : ?>
<p class="description"><strong>Last sync:</strong> Never run</p>
<?php
endif;
}

/**
* Sched Sync request processing
*/
function lfe_handle_sync_sched() {
if ( isset( $_POST['lfe_sync_sched_action'] ) && current_user_can( 'manage_options' ) ) {
if ( isset( $_POST['lfe_sync_sched_nonce'] ) ) {
$nonce = sanitize_text_field( wp_unslash( $_POST['lfe_sync_sched_nonce'] ) );

if ( ! wp_verify_nonce( $nonce, 'lfe_sync_sched_nonce_action' ) ) {
wp_die( 'Nonce verification failed.' );
}
} else {
wp_die( 'Nonce is missing.' );
}

$admin = new LFEvents_Admin( 'lfevents', LFEVENTS_VERSION );
$admin->sync_sched();
add_action( 'admin_notices', 'lfe_sync_sched_success_notice' );
}
}
add_action( 'admin_init', 'lfe_handle_sync_sched' );

/**
* Sched Sync request success handler
*/
function lfe_sync_sched_success_notice() {
?>
<div class="notice notice-success is-dismissible">
<p>Sched sync successfully triggered. If Last Sync time updates, the sync request has been successful.</p>
</div>
<?php
}

0 comments on commit 2817809

Please sign in to comment.