Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
That's righchyall
when_the_user_indicates_which_classes_they_wanna_see_homework_for(
send_user_choice_for_which_classes_they_wanna_see_homework
);
when_the_user_indicates_which_calendars_they_want_to_see(
send_user_choice_of_which_calendars_they_want_to_see
);
//-----------------Calendar Details-------------------//
function when_the_user_indicates_which_calendars_they_want_to_see( then_do_this ) {
when_the_user_clicks( '.save-calendars', then_do_this );
}
function send_user_choice_of_which_calendars_they_want_to_see() {
var what_theyre_choosing = '',
what_theyve_chosen;
send_user_choice(
what_theyre_choosing = 'calendars',
what_theyve_chosen = calendars_the_user_wants_to_see()
).done( function( html ) {
plug_in_the_HTML_of_the_list_of_events_for_the_calendars_user_wants_to_see( html );
} ).fail( function() {
ask_the_user_to_refresh_page_and_try_again( '#homepage-calendar-events' );
} );
}
function calendars_the_user_wants_to_see() {
return values_of_selected_checkboxes_inside( '.calendarsAsCheckboxes' );
}
function plug_in_the_HTML_of_the_list_of_events_for_the_calendars_user_wants_to_see( html ) {
jQuery( '#homepage-calendar-events' ).html( html );
}
//-----------------Homework Details-------------------//
function when_the_user_indicates_which_classes_they_wanna_see_homework_for( then_do_this ) {
when_the_user_clicks( '.save-classes', then_do_this );
}
function send_user_choice_for_which_classes_they_wanna_see_homework() {
var what_theyre_choosing = '',
what_theyve_chosen;
send_user_choice(
what_theyre_choosing = 'classes-for-homework',
what_theyve_chosen = classes_for_which_the_user_wants_to_see_homework()
).done( function( html ) {
plug_in_the_HTML_of_the_list_of_homework_for_the_selected_classes( html );
} ).fail( function() {
ask_the_user_to_refresh_page_and_try_again( '.homework' );
} );
}
function classes_for_which_the_user_wants_to_see_homework() {
return values_of_selected_checkboxes_inside( '.classesAsCheckboxes' );
}
function plug_in_the_HTML_of_the_list_of_homework_for_the_selected_classes( html ) {
jQuery( '.homework' ).html( html );
}
//---------------Shared--------------------//
function when_the_user_clicks( selector_of_clicked_element, then_do_this ) {
jQuery( function() {
jQuery( selector_of_clicked_element ).click( function() {
then_do_this();
return false;
} );
});
}
function send_user_choice( what_theyre_choosing, what_theyve_chosen ) {
return jQuery.ajax( {
url: WGMC_[ 'site_url' ] + '/remember-user-choice-of/' + what_theyre_choosing + '/',
data: {
user_choice: what_theyve_chosen
}
} );
}
function ask_the_user_to_refresh_page_and_try_again( element_to_put_message_in ) {
jQuery( element_to_put_message_in ).html( '<li>Oops! Sarry. Please refresh the page and try again!</li>' );
}
function values_of_selected_checkboxes_inside( checkbox_container_element ) {
var values_of_selected_checkboxes = [];
jQuery.each(
jQuery( checkbox_container_element + ' input:checked' ),
function( index, selected_checkbox ) {
var $selected_checkbox = jQuery( selected_checkbox );
values_of_selected_checkboxes.push( $selected_checkbox.val() );
}
);
return values_of_selected_checkboxes;
}
<?php
send_instructions_to_browser_for_how_to_send_user_choices__wgmc();
enable_receiving_user_choices_from_the_browser__wgmc();
function send_instructions_to_browser_for_how_to_send_user_choices__wgmc() {
add_action( 'wp_enqueue_scripts', '_send_instructions_to_browser_for_how_to_send_user_choices__wgmc' );
}
function _send_instructions_to_browser_for_how_to_send_user_choices__wgmc() {
$js_file_path_relative_to_theme_dir = '/js/westgate.js';
$script_name = 'westgate-js';
wp_enqueue_script(
$script_name,
$location = get_stylesheet_directory_uri() . $js_file_path_relative_to_theme_dir,
$load_after = array( 'jquery' ),
$version = $time_of_last_edit_of_js_file = filemtime( get_stylesheet_directory() ) . $js_file_path_relative_to_theme_dir, // This will ensure that the version number is updated every time the file changes. That, in turn, will ensure that the browsers always use the newest code
$load_in_footer = true
);
wp_localize_script(
$script_name = 'westgate-js',
$localization_object_name = 'WGMC_',
$data = array(
'site_url' => home_url(),
)
);
}
function enable_receiving_user_choices_from_the_browser__wgmc() {
set_up_sessions__wgmc();
create_user_choice_remembering_url__wgmc();
handle_user_choice_remembering_url_requests__wgmc();
}
function set_up_sessions__wgmc() {
session_set_cookie_params( $session_lifetime = YEAR_IN_SECONDS );
session_start();
}
function create_user_choice_remembering_url__wgmc() {
add_action( 'init', '_create_user_choice_remembering_url__wgmc' );
}
define( 'USER_CHOICE_REMEMBERING_QUERY_PARAM', 'the-user-is-submitting-choice-for' );
function _create_user_choice_remembering_url__wgmc() {
add_rewrite_rule(
$rule = 'remember-user-choice-of/([^/]*)/?',
$rewrite = 'index.php?' . USER_CHOICE_REMEMBERING_QUERY_PARAM . '=$matches[1]',
$position = 'top'
);
add_rewrite_tag( '%' . USER_CHOICE_REMEMBERING_QUERY_PARAM . '%', '([^&]+)' );
}
function handle_user_choice_remembering_url_requests__wgmc() {
add_action( 'wp', '_handle_user_choice_remembering_url_requests__wgmc' );
}
function _handle_user_choice_remembering_url_requests__wgmc() {
global $wp_query;
if ( $a_user_choice_was_submitted = isset( $wp_query->query_vars[ USER_CHOICE_REMEMBERING_QUERY_PARAM ] ) ) {
$what_the_user_is_submitting_choice_for = $wp_query->query_vars[ USER_CHOICE_REMEMBERING_QUERY_PARAM ];
remember_user_choice__wgmc(
$what_the_user_is_submitting_choice_for,
$what_their_choice_was = $_GET[ 'user_choice' ]
);
switch( $what_the_user_is_submitting_choice_for ) {
case 'calendars':
show_calendar_events( $_GET[ 'user_choice' ] );
break;
case 'classes-for-homework':
default:
show_homework_posts( $_GET[ 'user_choice' ] );
break;
}
die;
}
}
function remember_user_choice__wgmc( $what_the_user_is_submitting_choice_for, $what_their_choice_was ) {
$_SESSION[ $what_the_user_is_submitting_choice_for ] = $what_their_choice_was;
}
function get_user_choice__wgmc( $which_choice ) {
return isset( $_SESSION[ $which_choice ] ) ? $_SESSION[ $which_choice ] : array();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.