Below is a differential between the unpatched vulnerable code and the patched update, for reference.
--- a/my-calendar/includes/conditionals.php
+++ b/my-calendar/includes/conditionals.php
@@ -129,7 +129,7 @@
function mc_is_preview() {
$mc_id = mc_get_mc_id();
if ( $mc_id && isset( $_GET['preview'] ) && 'true' === $_GET['preview'] && current_user_can( 'mc_manage_events' ) ) {
- $nonce = sanitize_text_field( wp_unslash( $_GET['mcpreviewnonce'] ) );
+ $nonce = isset( $_GET['mcpreviewnonce'] ) ? sanitize_text_field( wp_unslash( $_GET['mcpreviewnonce'] ) ) : '';
if ( ! wp_verify_nonce( $nonce, 'mcpreviewnonce' ) ) {
return false;
}
--- a/my-calendar/includes/general-utilities.php
+++ b/my-calendar/includes/general-utilities.php
@@ -72,7 +72,7 @@
return $new_array;
}
-add_action( 'admin_menu', 'mc_add_outer_box' );
+add_action( 'add_meta_boxes', 'mc_add_outer_box' );
/**
* Add meta boxes
*
--- a/my-calendar/includes/kses.php
+++ b/my-calendar/includes/kses.php
@@ -51,6 +51,39 @@
}
}
+/**
+ * Decode HTML entities (including multiply-encoded ones) so that encoded markup is exposed before filtering.
+ *
+ * @param string $input Any string.
+ *
+ * @return string Decoded string.
+ */
+function mc_decode_entities( $input ) {
+ if ( ! is_string( $input ) ) {
+ return $input;
+ }
+ for ( $i = 0; $i < 3; $i++ ) {
+ $decoded = html_entity_decode( $input, ENT_QUOTES | ENT_HTML5, 'UTF-8' );
+ if ( $decoded === $input ) {
+ break;
+ }
+ $input = $decoded;
+ }
+
+ return $input;
+}
+
+/**
+ * Sanitize a title: decode entities, then run KSES so encoded markup is stripped.
+ *
+ * @param string $input Title string.
+ *
+ * @return string Cleaned string.
+ */
+function mc_kses_title( $input ) {
+ return mc_kses_post( mc_decode_entities( $input ) );
+}
+
add_filter( 'wp_kses_allowed_html', 'mc_allowed_tags', 10, 2 );
/**
* My Calendar needs to allow input and select in posts and a variety of other key elements; also provide support for schema.org data.
--- a/my-calendar/includes/post-types.php
+++ b/my-calendar/includes/post-types.php
@@ -617,6 +617,9 @@
if ( ! $event || ! $post_id ) {
return;
}
+ if ( 'hidden' !== mc_event_states_type( (int) $event->event_approved ) ) {
+ mc_publish_event_location( $event_id );
+ }
$target_status = mc_get_event_post_status( $event );
$post_status = get_post_status( $post_id );
@@ -675,7 +678,7 @@
if ( ! $event_id ) {
return;
}
- $previous_status = (int) mc_get_data( $event_id, 'event_approved' );
+ $previous_status = (int) mc_get_data( 'event_approved', $event_id );
if ( 1 === $previous_status ) {
return;
}
--- a/my-calendar/my-calendar-access-terms.php
+++ b/my-calendar/my-calendar-access-terms.php
@@ -27,16 +27,12 @@
$default_terms = (array) mc_get_option( $option, array() );
if ( ! in_array( $term_id, $default_terms, true ) && 'add' === $action ) {
$default_terms[] = $term_id;
- mc_update_option( $option, $default_terms );
-
- return true;
+ return mc_update_category_settings( array( $option => $default_terms ) );
}
if ( in_array( $term_id, $default_terms, true ) && 'remove' === $action ) {
$key = array_search( $term_id, $default_terms, true );
unset( $default_terms[ $key ] );
- mc_update_option( $option, $default_terms );
-
- return true;
+ return mc_update_category_settings( array( $option => $default_terms ) );
}
return false;
@@ -61,9 +57,12 @@
if ( isset( $post['mode'] ) && 'add' === $post['mode'] ) {
$term_id = mc_create_access_term( $post, $taxonomy );
- if ( isset( $post['mc_default_access_term'] ) ) {
- mc_update_default_access_terms( $term_id );
- $append[] = __( 'Term Added to Default Event Access Terms', 'my-calendar' );
+ if ( $term_id && isset( $post['mc_default_access_term'] ) ) {
+ if ( ! mc_update_default_access_terms( $term_id ) ) {
+ $append[] = esc_html__( 'The default access terms could not be updated.', 'my-calendar' );
+ } else {
+ $append[] = esc_html__( 'Term Added to Default Event Access Terms', 'my-calendar' );
+ }
}
if ( $term_id ) {
@@ -75,13 +74,18 @@
}
// Display the edit form if we're editing a term.
if ( isset( $post['mode'] ) && isset( $post['access_term_id'] ) && isset( $post['access_term_name'] ) && 'edit' === $post['mode'] ) {
+ $append = '';
// This term is in the set, but not checked.
if ( in_array( (int) $post['access_term_id'], $default_access, true ) && ! isset( $post['mc_default_access_term'] ) ) {
- mc_update_default_access_terms( (int) $post['access_term_id'], 'remove' );
+ if ( mc_update_default_access_terms( (int) $post['access_term_id'], 'remove' ) ) {
+ $append = esc_html__( 'The default access terms could not be updated.', 'my-calendar' );
+ }
}
// This term is checked, but not in the set.
if ( ! in_array( (int) $post['access_term_id'], $default_access, true ) && isset( $post['mc_default_access_term'] ) ) {
- mc_update_default_access_terms( $post['access_term_id'] );
+ if ( mc_update_default_access_terms( $post['access_term_id'] ) ) {
+ $append = esc_html__( 'The default access terms could not be updated.', 'my-calendar' );
+ }
}
$results = wp_update_term(
@@ -92,9 +96,9 @@
)
);
if ( $results ) {
- mc_show_notice( __( 'Access term edited successfully.', 'my-calendar' ), true, false, 'success' );
+ mc_show_notice( __( 'Access term edited successfully.', 'my-calendar' ) . " $append", true, false, 'success' );
} else {
- mc_show_error( __( 'Access term was not changed.', 'my-calendar' ) );
+ mc_show_error( __( 'Access term was not changed.', 'my-calendar' ) . " $append" );
}
$cur_cat = (int) $post['access_term_id'];
}
@@ -103,8 +107,11 @@
if ( isset( $_GET['default'] ) && is_numeric( $_GET['default'] ) ) {
if ( isset( $_REQUEST['_mcnonce'] ) && wp_verify_nonce( $_REQUEST['_mcnonce'], 'mcnonce' ) ) {
$add_default_access = (int) $_GET['default'];
- mc_update_default_access_terms( $add_default_access );
- mc_show_notice( __( 'Term Added to Default Event Access Terms', 'my-calendar' ), true, false, 'success' );
+ if ( mc_update_default_access_terms( $add_default_access ) ) {
+ mc_show_notice( __( 'Term Added to Default Event Access Terms', 'my-calendar' ), true, false, 'success' );
+ } else {
+ mc_show_error( __( 'The default access terms were not changed.', 'my-calendar' ) );
+ }
} else {
mc_show_error( 'Invalid security key; please try again!', 'my-calendar' );
}
@@ -115,8 +122,10 @@
$term_id = (int) $_GET['access_term_id'];
$results = wp_delete_term( $term_id, $taxonomy );
- mc_update_default_access_terms( $term_id, 'remove' );
if ( $results ) {
+ if ( in_array( $term_id, (array) $default_access, true ) && ! mc_update_default_access_terms( $term_id, 'remove' ) ) {
+ mc_show_error( __( 'The default access terms could not be updated.', 'my-calendar' ) );
+ }
mc_show_notice( __( 'Accessibility term deleted successfully.', 'my-calendar' ), true, false, 'success' );
}
} else {
--- a/my-calendar/my-calendar-behaviors.php
+++ b/my-calendar/my-calendar-behaviors.php
@@ -26,7 +26,9 @@
$options['ajax_javascript'] = ( empty( $_POST['ajax_js'] ) ) ? 0 : 1;
$options['show_js'] = ( '' === $_POST['mc_show_js'] ) ? '' : sanitize_text_field( $_POST['mc_show_js'] );
- mc_update_options( $options );
+ if ( ! mc_update_design_settings( $options ) ) {
+ wp_die( esc_html__( 'Script settings could not be saved.', 'my-calendar' ) );
+ }
set_transient( 'mc_behaviors_updated', true, 5 );
wp_safe_redirect( esc_url_raw( admin_url( 'admin.php?page=my-calendar-design&scriptaction=saved#my-calendar-scripts' ) ) );
} else {
--- a/my-calendar/my-calendar-categories.php
+++ b/my-calendar/my-calendar-categories.php
@@ -161,14 +161,20 @@
if ( isset( $post['mode'] ) && 'add' === $post['mode'] ) {
$cat_id = mc_create_category( $post );
- if ( isset( $post['mc_default_category'] ) ) {
- mc_update_option( 'default_category', $cat_id );
- $append[] = __( 'Default category changed.', 'my-calendar' );
+ if ( $cat_id && isset( $post['mc_default_category'] ) ) {
+ if ( ! mc_update_category_settings( array( 'default_category' => $cat_id ) ) ) {
+ $append[] = __( 'Default category could not be changed.', 'my-calendar' );
+ } else {
+ $append[] = __( 'Default category changed.', 'my-calendar' );
+ }
}
- if ( isset( $post['mc_skip_holidays_category'] ) ) {
- mc_update_option( 'skip_holidays_category', $cat_id );
- $append[] = __( 'Holiday category changed.', 'my-calendar' );
+ if ( $cat_id && isset( $post['mc_skip_holidays_category'] ) ) {
+ if ( ! mc_update_category_settings( array( 'skip_holidays_category' => $cat_id ) ) ) {
+ $append[] = __( 'Holiday category could not be changed.', 'my-calendar' );
+ } else {
+ $append[] = __( 'Holiday category changed.', 'my-calendar' );
+ }
}
if ( $cat_id ) {
@@ -183,18 +189,30 @@
$append = array();
$mode = 'edit';
if ( isset( $post['mc_default_category'] ) && $default_category !== $post['category_id'] ) {
- mc_update_option( 'default_category', (int) $post['category_id'] );
- $append[] = __( 'Default category changed.', 'my-calendar' );
+ if ( ! mc_update_category_settings( array( 'default_category' => (int) $post['category_id'] ) ) ) {
+ $append[] = __( 'Default category could not be changed.', 'my-calendar' );
+ } else {
+ $append[] = __( 'Default category changed.', 'my-calendar' );
+ }
} elseif ( $default_category === $post['category_id'] ) {
- mc_update_option( 'default_category', '' );
+ if ( ! mc_update_category_settings( array( 'default_category' => '' ) ) ) {
+ $append[] = __( 'Default category could not be cleared.', 'my-calendar' );
+ } else {
$append[] = __( 'Default category removed.', 'my-calendar' );
+ }
}
if ( isset( $post['mc_skip_holidays_category'] ) && $holiday_category !== $post['category_id'] ) {
- mc_update_option( 'skip_holidays_category', (int) $post['category_id'] );
- $append[] = __( 'Holiday category changed.', 'my-calendar' );
+ if ( ! mc_update_category_settings( array( 'skip_holidays_category' => (int) $post['category_id'] ) ) ) {
+ $append[] = __( 'Holiday category could not be changed.', 'my-calendar' );
+ } else {
+ $append[] = __( 'Holiday category changed.', 'my-calendar' );
+ }
} elseif ( $holiday_category === (string) $post['category_id'] ) {
- mc_update_option( 'skip_holidays_category', '' );
+ if ( ! mc_update_category_settings( array( 'skip_holidays_category' => '' ) ) ) {
+ $append[] = __( 'Holiday category could not be cleared.', 'my-calendar' );
+ } else {
$append[] = __( 'Holiday category removed.', 'my-calendar' );
+ }
}
$update = array(
@@ -221,9 +239,13 @@
if ( isset( $_REQUEST['_mcnonce'] ) && wp_verify_nonce( $_REQUEST['_mcnonce'], 'mcnonce' ) ) {
// Set default.
if ( isset( $_GET['default'] ) && is_numeric( $_GET['default'] ) ) {
- mc_update_option( 'default_category', (int) $_GET['default'] );
+ if ( ! mc_update_category_settings( array( 'default_category' => (int) $_GET['default'] ) ) ) {
+ $message = esc_html__( 'The default category could not be updated.', 'my-calendar' );
+ } else {
+ $message = esc_html__( 'Default Category Changed', 'my-calendar' );
+ }
$default_category = (int) $_GET['default'];
- mc_show_notice( __( 'Default Category Changed', 'my-calendar' ), true, false, 'success' );
+ mc_show_notice( $message, true, false, 'success' );
}
// Trigger delete.
if ( isset( $_GET['mode'] ) && isset( $_GET['category_id'] ) && 'delete' === $_GET['mode'] ) {
@@ -248,7 +270,9 @@
$cal_results = false;
}
if ( $default_category === (string) $cat_id ) {
- mc_update_option( 'default_category', '' );
+ if ( ! mc_update_category_settings( array( 'default_category' => '' ) ) ) {
+ mc_show_error( __( 'The default category could not be cleared.', 'my-calendar' ) );
+ }
}
if ( $results && ( $cal_results || $rel_results ) ) {
mc_show_notice( __( 'Category deleted successfully. Categories in calendar updated.', 'my-calendar' ), true, false, 'success' );
@@ -326,9 +350,12 @@
* @param array $category Array of params to update.
* @param int $category_id ID of the category to update.
*
- * @return mixed boolean/int query result
+ * @return boolean|int query result
*/
function mc_update_category( $category, $category_id ) {
+ if ( ! current_user_can( 'mc_edit_cats' ) ) {
+ return false;
+ }
global $wpdb;
$category_id = (int) ( $category_id );
$formats = array( '%s', '%s', '%s', '%d', '%d', '%d' );
@@ -911,7 +938,7 @@
<th scope="row" aria-label="<?php echo esc_attr( $cat_name ); ?>"><?php echo absint( $cat->category_id ); ?></th>
<td>
<?php
- $category_event_url = add_query_arg( 'filter', $cat->category_id, admin_url( 'admin.php?page=my-calendar-manage&restrict=category&view=list&limit=all' ) );
+ $category_event_url = wp_nonce_url( add_query_arg( 'filter', $cat->category_id, admin_url( 'admin.php?page=my-calendar-manage&restrict=category&view=list&limit=all' ) ), 'mc_admin_view', '_mc_view_nonce' );
$count = mc_get_category_count( $cat->category_id );
$count = ( $count ) ? '<a href="' . esc_url( $category_event_url ) . '">' . $count . '</a>' : '0';
echo esc_html( $cat_name );
--- a/my-calendar/my-calendar-core.php
+++ b/my-calendar/my-calendar-core.php
@@ -580,10 +580,12 @@
if ( mc_is_single_event() ) {
$mc_id = mc_get_mc_id();
if ( $mc_id ) {
- $event = mc_get_event( $mc_id );
- $schema = mc_event_schema( $event );
+ $event = mc_get_event( $mc_id );
+ if ( is_object( $event ) ) {
+ $schema = mc_event_schema( $event );
- echo PHP_EOL . '<script type="application/ld+json">' . PHP_EOL . '[' . wp_json_encode( map_deep( $schema, 'esc_html' ), JSON_UNESCAPED_SLASHES ) . ']' . PHP_EOL . '</script>' . PHP_EOL;
+ echo PHP_EOL . '<script type="application/ld+json">' . PHP_EOL . '[' . wp_json_encode( map_deep( $schema, 'esc_html' ), JSON_UNESCAPED_SLASHES ) . ']' . PHP_EOL . '</script>' . PHP_EOL;
+ }
}
}
@@ -615,16 +617,18 @@
if ( mc_is_single_event() ) {
$mc_id = mc_get_mc_id();
if ( $mc_id ) {
- $event = mc_get_event( $mc_id );
- $schema = mc_event_schema( $event );
+ $event = mc_get_event( $mc_id );
+ if ( is_object( $event ) ) {
+ $schema = mc_event_schema( $event );
- unset( $schema['@context'] );
- $schema['mainEntityOfPage'] = array( '@id' => $context->canonical );
+ unset( $schema['@context'] );
+ $schema['mainEntityOfPage'] = array( '@id' => $context->canonical );
- if ( isset( $schema['location'] ) ) {
- unset( $schema['location']['@context'] );
+ if ( isset( $schema['location'] ) ) {
+ unset( $schema['location']['@context'] );
+ }
+ $graph[] = $schema;
}
- $graph[] = $schema;
}
}
@@ -817,8 +821,7 @@
if ( version_compare( $version, '2.1', '<' ) ) {
wp_register_style( 'mcs-back-compat', plugins_url( 'css/backcompat.css', __FILE__ ), array(), $version );
}
- wp_register_script( 'mc-stickyscroll', plugins_url( 'js/jquery.stick.js', __FILE__ ), array( 'jquery' ), $version, true );
- wp_register_script( 'mc-color-picker', plugins_url( 'js/color-picker.js', __FILE__ ), array( 'wp-color-picker', 'mc-stickyscroll' ), $version, true );
+ wp_register_script( 'mc-color-picker', plugins_url( 'js/color-picker.js', __FILE__ ), array( 'wp-color-picker' ), $version, true );
$api_key = mc_get_option( 'gmap_api_key' );
if ( $api_key ) {
$gmaps = ( SCRIPT_DEBUG ) ? plugins_url( 'js/gmaps.js', __FILE__ ) : plugins_url( 'js/gmaps.min.js', __FILE__ );
@@ -859,14 +862,17 @@
$ajax = 'false';
if ( false !== strpos( $id, 'my-calendar' ) || $is_mc_page ) {
// Toggle CSS & Scripts based on current mode.
- $mode = mc_get_option( 'default_admin_view' );
- if ( isset( $_GET['view'] ) && 'grid' === $_GET['view'] && 'grid' !== $mode ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
- mc_update_option( 'default_admin_view', 'grid' );
- $mode = 'grid';
- }
- if ( isset( $_GET['view'] ) && 'list' === $_GET['view'] && 'list' !== $mode ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
- mc_update_option( 'default_admin_view', 'list' );
- $mode = 'list';
+ $mode = mc_get_option( 'default_admin_view' );
+ $can_save_view = isset( $_GET['_mc_view_nonce'] ) && wp_verify_nonce( $_GET['_mc_view_nonce'], 'mc_admin_view' );
+ if ( $can_save_view && isset( $_GET['view'] ) && 'grid' === $_GET['view'] && 'grid' !== $mode ) {
+ if ( mc_update_event_view_settings( array( 'default_admin_view' => 'grid' ) ) ) {
+ $mode = 'grid';
+ }
+ }
+ if ( $can_save_view && isset( $_GET['view'] ) && 'list' === $_GET['view'] && 'list' !== $mode ) {
+ if ( mc_update_event_view_settings( array( 'default_admin_view' => 'list' ) ) ) {
+ $mode = 'list';
+ }
}
$grid = ( 'grid' === $mode );
if ( $grid ) {
@@ -2189,7 +2195,7 @@
DB Version: $mc_db_version
URI: $mc_uri
CSS: $mc_css$license
-Requester Email: wp_get_current_user()->user_email
+Requester Email: " . wp_get_current_user()->user_email . "
Admin Email: $admin_email
==WordPress:==
@@ -2323,9 +2329,12 @@
$post_id = get_the_ID();
$parent_id = get_post_meta( $post_id, '_mc_event_id', true );
$event = mc_get_nearest_event( $parent_id, true );
- $mc_id = $event->occur_id;
+ if ( $event ) {
+ $mc_id = $event->occur_id;
+ }
}
}
+
return $mc_id;
}
@@ -2371,7 +2380,12 @@
$post_id = get_the_ID();
$parent_id = get_post_meta( $post_id, '_mc_event_id', true );
$event = mc_get_nearest_event( $parent_id, true );
- $mc_id = $event->occur_id;
+ if ( $event ) {
+ $mc_id = $event->occur_id;
+ } else {
+ // return early if no valid event.
+ return '';
+ }
}
$event = mc_adjacent_event( $mc_id, 'next' );
if ( empty( $event ) ) {
--- a/my-calendar/my-calendar-design.php
+++ b/my-calendar/my-calendar-design.php
@@ -88,7 +88,9 @@
// Custom sanitizing.
$post = map_deep( $_POST, 'sanitize_textarea_field' );
if ( isset( $post['mc_list_template'] ) ) {
- mc_update_option( 'list_template', $post['mc_list_template'] );
+ if ( ! mc_update_design_settings( array( 'list_template' => $post['mc_list_template'] ) ) ) {
+ wp_die( esc_html__( 'The list template could not be saved.', 'my-calendar' ) );
+ }
mc_show_notice( __( 'My Calendar List Template Updated', 'my-calendar' ), true, false, 'success' );
}
} else {
@@ -179,4 +181,3 @@
return $template_options;
}
-
--- a/my-calendar/my-calendar-event-editor.php
+++ b/my-calendar/my-calendar-event-editor.php
@@ -48,17 +48,11 @@
if ( 'add' === $action || 'copy' === $action ) {
$post_id = mc_create_event_post( $data, $event_id );
} elseif ( 'edit' === $action ) {
- if ( isset( $data['event_post'] ) && 'group' === $data['event_post'] ) {
- $post_id = mc_get_event_post( $event_id );
- } elseif ( isset( $post['event_post'] ) && ( 0 === (int) $post['event_post'] || '' === $post['event_post'] ) ) {
- $post_id = mc_create_event_post( $data, $event_id );
- } else {
- $post_id = ( isset( $post['event_post'] ) ) ? absint( $post['event_post'] ) : false;
- $post_id = ( isset( $data['event_post'] ) ) ? absint( $data['event_post'] ) : $post_id;
- }
+ // The stored event_post is authoritative; submitted values are ignored.
+ $post_id = mc_get_event_post( $event_id );
// If, after all that, the post doesn't exist, create it.
- if ( ! get_post_status( $post_id ) ) {
- mc_create_event_post( $data, $event_id );
+ if ( ! $post_id || ! get_post_status( $post_id ) ) {
+ $post_id = mc_create_event_post( $data, $event_id );
}
$categories = mc_get_categories( $event_id );
$terms = array();
@@ -459,6 +453,20 @@
}
/**
+ * Check whether an event's post ID can be written. The event_post value is permanent once set.
+ *
+ * @param int $event_id Event ID.
+ *
+ * @return bool True if the event has no post assigned yet.
+ */
+function mc_event_post_assignable( $event_id ) {
+ global $wpdb;
+ $current = $wpdb->get_var( $wpdb->prepare( 'SELECT event_post FROM ' . my_calendar_table() . ' WHERE event_id = %d', $event_id ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
+
+ return ! (int) $current;
+}
+
+/**
* Update a single field in an event.
*
* @param string $field database column.
@@ -471,6 +479,9 @@
function mc_update_event( $field, $data, $event, $type = '%d' ) {
global $wpdb;
$field = sanitize_key( $field );
+ if ( 'event_post' === $field && ! mc_event_post_assignable( $event ) ) {
+ return 0;
+ }
if ( '%d' === $type ) {
$sql = 'UPDATE ' . my_calendar_table() . " SET $field = %d WHERE event_id=%d";
} elseif ( '%s' === $type ) {
@@ -826,18 +837,20 @@
$update['event_recur'] = 'S1';
if ( 0 === (int) $update['event_group_id'] ) {
$update['event_group_id'] = $event_id;
- mc_update_data( $event_id, 'event_group_id', $event_id );
+ $result = mc_update_event_data( $event_id, 'event_group_id', $event_id );
}
- // retain saved location unless actively changed.
- if ( isset( $post['preset_location'] ) && 'none' === $post['location_preset'] ) {
- $location = absint( $post['preset_location'] );
- $update['event_location'] = $location;
+ if ( false !== $result ) {
+ // retain saved location unless actively changed.
+ if ( isset( $post['preset_location'] ) && 'none' === $post['location_preset'] ) {
+ $location = absint( $post['preset_location'] );
+ $update['event_location'] = $location;
+ }
+ $result = $wpdb->insert( my_calendar_table(), $update, $formats );
+ $new_event = $wpdb->insert_id;
+ mc_update_category_relationships( $cats, $new_event );
+ mc_update_instance( $event_instance, $new_event, $update );
+ $event_post = mc_do_event_save_actions( 'add', $update, $new_event, $result );
}
- $result = $wpdb->insert( my_calendar_table(), $update, $formats );
- $new_event = $wpdb->insert_id;
- mc_update_category_relationships( $cats, $new_event );
- mc_update_instance( $event_instance, $new_event, $update );
- $event_post = mc_do_event_save_actions( 'add', $update, $new_event, $result );
} elseif ( ! ( $update['event_begin'][0] === $post['prev_event_begin'] && $update['event_end'][0] === $post['prev_event_end'] ) ) {
// Only dates were changed.
mc_update_instance( $event_instance, $event_id, $update );
@@ -845,6 +858,8 @@
$type = 'update';
}
} else {
+ // The event's post is permanently bonded; never overwrite it.
+ unset( $update['event_post'] );
$result = $wpdb->update(
my_calendar_table(),
$update,
@@ -2371,7 +2386,7 @@
}
if ( 'add' === $action || 'edit' === $action || 'copy' === $action || 'check' === $action ) {
- $title = ! empty( $post['event_title'] ) ? trim( $post['event_title'] ) : '';
+ $title = ! empty( $post['event_title'] ) ? trim( mc_kses_title( $post['event_title'] ) ) : '';
$desc = ! empty( $post['content'] ) ? trim( $post['content'] ) : '';
$short = ! empty( $post['event_short'] ) ? trim( $post['event_short'] ) : '';
$recurs = ( isset( $post['prev_event_recur'] ) ) ? $post['prev_event_recur'] : '';
@@ -2544,7 +2559,7 @@
if ( 'check' !== $action ) {
if ( $has_location_data && 0 === $i ) {
// Only add this with the first event, if adding multiples.
- $add_loc = array(
+ $add_loc = array(
'location_label' => $event_label,
'location_street' => $event_street,
'location_street2' => $event_street2,
@@ -2561,8 +2576,9 @@
'location_phone2' => $event_phone2,
'location_access' => '',
);
- $result = mc_insert_location( $add_loc );
- $saved_location = $result['location_id'];
+ $publish_location = ( 'hidden' === mc_event_states_type( $approved ) ) ? false : true;
+ $result = mc_insert_location( $add_loc, $publish_location );
+ $saved_location = $result['location_id'];
// Set static multi_location_id to track when creating multiple events.
$multi_location_id = ( is_numeric( $saved_location ) ) ? (int) $saved_location : 0;
} elseif ( $has_location_data && $multi_location_id > 0 ) {
@@ -2916,7 +2932,10 @@
}
/**
- * Update a single arbitrary field in event table
+ * Update a single field in the event table from a trusted internal context.
+ *
+ * This helper does not authorize the caller. User-initiated writes must check
+ * permissions and request authenticity, or use mc_update_event_data().
*
* @param int $event_id Event to modify.
* @param string $field column name for field.
@@ -2927,6 +2946,9 @@
*/
function mc_update_data( $event_id, $field, $value, $format = '%d' ) {
global $wpdb;
+ if ( 'event_post' === $field && ! mc_event_post_assignable( $event_id ) ) {
+ return false;
+ }
$data = array( $field => $value );
$result = $wpdb->update( my_calendar_table(), $data, array( 'event_id' => $event_id ), $format, '%d' );
@@ -2934,6 +2956,27 @@
}
/**
+ * Update a field when the current user is allowed to edit the event.
+ *
+ * Callers must validate the field and value, enforce any field-specific
+ * permissions, and verify the appropriate nonce when handling requests.
+ *
+ * @param int $event_id Event to modify.
+ * @param string $field Column name for field.
+ * @param mixed $value Value to save.
+ * @param string $format Data format.
+ *
+ * @return int|false Number of rows updated, or false on denial or failure.
+ */
+function mc_update_event_data( $event_id, $field, $value, $format = '%d' ) {
+ if ( ! mc_can_edit_event( $event_id ) ) {
+ return false;
+ }
+
+ return mc_update_data( $event_id, $field, $value, $format );
+}
+
+/**
* Generate normal date time input fields
*
* @param string $form Previous defined values.
--- a/my-calendar/my-calendar-event-manager.php
+++ b/my-calendar/my-calendar-event-manager.php
@@ -377,8 +377,8 @@
<h2 class="mc-heading-inline"><?php esc_html_e( 'My Events', 'my-calendar' ); ?></h2>
<?php
$grid = ( 'grid' === mc_get_option( 'default_admin_view' ) ) ? true : false;
- $grid_url = admin_url( 'admin.php?page=my-calendar-manage&view=grid' );
- $list_url = admin_url( 'admin.php?page=my-calendar-manage&view=list' );
+ $grid_url = wp_nonce_url( admin_url( 'admin.php?page=my-calendar-manage&view=grid' ), 'mc_admin_view', '_mc_view_nonce' );
+ $list_url = wp_nonce_url( admin_url( 'admin.php?page=my-calendar-manage&view=list' ), 'mc_admin_view', '_mc_view_nonce' );
?>
<ul class="mc-admin-mode">
<li><span class="dashicons dashicons-calendar" aria-hidden="true"></span><a <?php echo ( $grid ) ? 'aria-current="true"' : ''; ?> href="<?php echo esc_url( $grid_url ); ?>"><?php esc_html_e( 'Grid View', 'my-calendar' ); ?></a></li>
@@ -505,10 +505,12 @@
if ( $verify ) {
$action = ! empty( $_POST['event_action'] ) ? sanitize_text_field( wp_unslash( $_POST['event_action'] ) ) : '';
if ( 'delete' === $action ) {
- $event_id = ! empty( $_POST['event_id'] ) ? (int) $_POST['event_id'] : '';
- $event_instance = ! empty( $_POST['event_instance'] ) ? (int) $_POST['event_instance'] : 0;
- $message = mc_delete_event( $event_id, $event_instance );
- echo wp_kses_post( $message );
+ $event_id = ! empty( $_POST['event_id'] ) ? (int) $_POST['event_id'] : '';
+ if ( mc_can_edit_event( $event_id ) ) {
+ $event_instance = ! empty( $_POST['event_instance'] ) ? (int) $_POST['event_instance'] : 0;
+ $message = mc_delete_event( $event_id, $event_instance );
+ echo wp_kses_post( $message );
+ }
}
} else {
return;
--- a/my-calendar/my-calendar-events.php
+++ b/my-calendar/my-calendar-events.php
@@ -25,7 +25,7 @@
if ( ! property_exists( $event, 'categories' ) ) {
$event->categories = mc_get_categories( $event, 'objects' );
}
- if ( ! property_exists( $event, 'location' ) && is_numeric( $event->event_location ) && 0 !== (int) $event->event_location ) {
+ if ( ! property_exists( $event, 'location' ) && is_numeric( $event->event_location ) && 0 !== (int) $event->event_location && ( is_admin() || ! mc_location_is_draft( $event->event_location ) ) ) {
$event->location = mc_get_location( $event->event_location );
}
if ( ! property_exists( $event, 'uid' ) ) {
@@ -743,7 +743,7 @@
* @param int $id Event core ID.
* @param bool $next If true, return closest event that's in the future. If false, return the event object for $id.
*
- * @return object Event
+ * @return object|false Event object or false if not found.
*/
function mc_get_nearest_event( $id, $next = false ) {
$next_event = false;
@@ -753,6 +753,9 @@
if ( true === $next ) {
$next_event = $mcdb->get_row( $mcdb->prepare( 'SELECT *, ' . $ts_string . ' FROM ' . my_calendar_event_table() . ' JOIN ' . my_calendar_table() . ' AS e ON (event_id=occur_event_id) JOIN ' . my_calendar_categories_table() . ' AS c ON (e.event_category=c.category_id) WHERE occur_event_id=%d AND occur_begin > NOW() ORDER BY ABS( DATEDIFF( occur_begin, NOW() ) )', $id ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
}
+ if ( ! is_object( $event ) && ! is_object( $next_event ) ) {
+ return false;
+ }
$event = ( $next_event ) ? mc_event_object( $next_event ) : mc_event_object( $event );
@@ -931,13 +934,14 @@
$template = mc_get_custom_template( $template );
}
- $args = array(
+ $template_class = esc_attr( wp_strip_all_tags( $template ) );
+ $args = array(
'event' => $arr_events[0],
'tags' => $event,
'template' => $template,
- 'class' => ( str_contains( $template, 'list_preset_' ) ) ? "list-preset $template" : '',
+ 'class' => ( str_contains( $template, 'list_preset_' ) ) ? "list-preset $template_class" : '',
);
- $details = mc_load_template( 'event/now', $args );
+ $details = mc_load_template( 'event/now', $args );
if ( $details ) {
$return = $details;
} else {
@@ -1023,14 +1027,14 @@
if ( mc_key_exists( $template ) ) {
$template = mc_get_custom_template( $template );
}
-
- $args = array(
+ $template_class = esc_attr( wp_strip_all_tags( $template ) );
+ $args = array(
'event' => $arr_events[0],
'tags' => $event,
'template' => $template,
- 'class' => ( str_contains( $template, 'list_preset_' ) ) ? "list-preset $template" : '',
+ 'class' => ( str_contains( $template, 'list_preset_' ) ) ? "list-preset $template_class" : '',
);
- $details = mc_load_template( 'event/next', $args );
+ $details = mc_load_template( 'event/next', $args );
if ( $details ) {
$return = $details;
} else {
--- a/my-calendar/my-calendar-generator.php
+++ b/my-calendar/my-calendar-generator.php
@@ -51,7 +51,8 @@
$templatekey = "Today's Events Shortcode";
break;
default:
- $shortcode = 'my_calendar';
+ // For unrecognized or custom shortcode types, exit early.
+ return;
}
$keys = array( 'navigation', 'category', 'ltype', 'lvalue', 'search', 'format', 'time', 'year', 'month', 'day', 'months', 'above', 'below', 'author', 'host', 'order', 'from', 'to', 'type', 'skip', 'after', 'before', 'preset_template', 'template', 'fallback', 'show_recurring', 'weekends' );
$post = map_deep( $_POST, 'sanitize_text_field' );
@@ -92,7 +93,7 @@
}
}
$output = esc_html( $shortcode . $string );
- mc_update_option( 'last_shortcode_' . $type, $output );
+ mc_update_generator_settings( array( 'last_shortcode_' . $type => $output ) );
}
if ( 'shortcode' === $format && ! is_array( $output ) ) {
$return = "<div class='notice notice-success'><p><textarea readonly='readonly' class='large-text readonly'>[$output]</textarea>$append</p></div>";
--- a/my-calendar/my-calendar-group-manager.php
+++ b/my-calendar/my-calendar-group-manager.php
@@ -38,29 +38,33 @@
switch ( $_POST['event_action'] ) {
case 'edit':
if ( isset( $_POST['apply'] ) && is_array( $_POST['apply'] ) ) {
- $post = map_deep( $_POST, 'wp_kses_post' );
- $mc_output = mc_check_group_data( $action, $post );
- foreach ( $post['apply'] as $event_id ) {
+ foreach ( $_POST['apply'] as $event_id ) {
$event_id = absint( $event_id );
- $response = my_calendar_save_group( $action, $mc_output, $event_id, $post );
- echo wp_kses_post( $response );
+ if ( mc_can_edit_event( $event_id ) ) {
+ $post = map_deep( $_POST, 'wp_kses_post' );
+ $mc_output = mc_check_group_data( $action, $post );
+ $response = my_calendar_save_group( $action, $mc_output, $event_id, $post );
+ echo wp_kses_post( $response );
+ }
}
}
break;
case 'break':
$events = map_deep( $_POST['break'], 'absint' );
foreach ( $events as $event_id ) {
- $update = array( 'event_group_id' => 0 );
- $formats = array( '%d' );
- $result = $wpdb->update( my_calendar_table(), $update, array( 'event_id' => $event_id ), $formats, '%d' );
- // Translators: Calendar URL.
- $url = sprintf( __( 'View <a href="%s">your calendar</a>.', 'my-calendar' ), mc_get_uri() );
- if ( false === $result ) {
- $message = mc_show_error( __( 'Event not updated.', 'my-calendar' ) . " $url", false );
- } elseif ( 0 === $result ) {
- $message = mc_show_notice( "#$event_id: " . __( 'Nothing was changed in that update.', 'my-calendar' ) . " $url", false, false, 'info' );
- } else {
- $message = mc_show_notice( "#$event_id: " . __( 'Event updated successfully', 'my-calendar' ) . ". $url", false, false, 'success' );
+ if ( mc_can_edit_event( $event_id ) ) {
+ $update = array( 'event_group_id' => 0 );
+ $formats = array( '%d' );
+ $result = $wpdb->update( my_calendar_table(), $update, array( 'event_id' => $event_id ), $formats, '%d' );
+ // Translators: Calendar URL.
+ $url = sprintf( __( 'View <a href="%s">your calendar</a>.', 'my-calendar' ), mc_get_uri() );
+ if ( false === $result ) {
+ $message = mc_show_error( __( 'Event not updated.', 'my-calendar' ) . " $url", false );
+ } elseif ( 0 === $result ) {
+ $message = mc_show_notice( "#$event_id: " . __( 'Nothing was changed in that update.', 'my-calendar' ) . " $url", false, false, 'info' );
+ } else {
+ $message = mc_show_notice( "#$event_id: " . __( 'Event updated successfully', 'my-calendar' ) . ". $url", false, false, 'success' );
+ }
}
}
break;
@@ -69,18 +73,20 @@
$events = map_deep( $_POST['group'], 'absint' );
sort( $events );
foreach ( $events as $event_id ) {
- $group_id = $events[0];
- $update = array( 'event_group_id' => $group_id );
- $formats = array( '%d' );
- $result = $wpdb->update( my_calendar_table(), $update, array( 'event_id' => $event_id ), $formats, '%d' );
+ if ( mc_can_edit_event( $event_id ) ) {
+ $group_id = $events[0];
+ $update = array( 'event_group_id' => $group_id );
+ $formats = array( '%d' );
+ $result = $wpdb->update( my_calendar_table(), $update, array( 'event_id' => $event_id ), $formats, '%d' );
- if ( false === $result ) {
- $message = mc_show_error( __( 'Event not grouped.', 'my-calendar' ), false );
- } elseif ( 0 === $result ) {
- $message = mc_show_notice( "#$event_id: " . __( 'Nothing was changed in that update.', 'my-calendar' ), false, false, 'warning' );
- } else {
- // Translators: Event group ID.
- $message = mc_show_notice( sprintf( __( 'Group %s: Events grouped successfully', 'my-calendar' ), "#$event_id" ), false, false, 'success' );
+ if ( false === $result ) {
+ $message = mc_show_error( __( 'Event not grouped.', 'my-calendar' ), false );
+ } elseif ( 0 === $result ) {
+ $message = mc_show_notice( "#$event_id: " . __( 'Nothing was changed in that update.', 'my-calendar' ), false, false, 'warning' );
+ } else {
+ // Translators: Event group ID.
+ $message = mc_show_notice( sprintf( __( 'Group %s: Events grouped successfully', 'my-calendar' ), "#$event_id" ), false, false, 'success' );
+ }
}
}
}
--- a/my-calendar/my-calendar-location-manager.php
+++ b/my-calendar/my-calendar-location-manager.php
@@ -44,8 +44,12 @@
}
}
if ( isset( $_GET['default'] ) && is_numeric( $_GET['default'] ) ) {
- mc_update_option( 'default_location', (int) $_GET['default'] );
- mc_show_notice( __( 'Default Location Changed', 'my-calendar' ), true, false, 'success' );
+ if ( ! mc_update_location_settings( array( 'default_location' => (int) $_GET['default'] ) ) ) {
+ $message = esc_html__( 'The default location could not be updated.', 'my-calendar' );
+ } else {
+ $message = esc_html__( 'Default Location Changed', 'my-calendar' );
+ }
+ mc_show_notice( $message, true, false, 'success' );
}
}
@@ -461,9 +465,12 @@
$default = '<a href="' . esc_url( $url ) . '">' . __( 'Set as Default', 'my-calendar' ) . '</a>';
}
$delete_url = add_query_arg( '_mcnonce', $mcnonce, admin_url( "admin.php?page=my-calendar-location-manager&mode=delete&location_id=$location->location_id" ) );
- $view_url = get_the_permalink( mc_get_location_post( $location->location_id, false ) );
- $edit_url = admin_url( "admin.php?page=my-calendar-locations&mode=edit&location_id=$location->location_id" );
- $view_link = '';
+ if ( mc_location_is_draft( $location->location_id ) ) {
+ $card = str_replace( '</strong>', ' — ' . esc_html__( 'Draft', 'my-calendar' ) . '</strong>', $card );
+ }
+ $view_url = get_the_permalink( mc_get_location_post( $location->location_id, false ) );
+ $edit_url = admin_url( "admin.php?page=my-calendar-locations&mode=edit&location_id=$location->location_id" );
+ $view_link = '';
if ( $view_url && esc_url( $view_url ) ) {
$view_link = "<a href='" . esc_url( $view_url ) . "' class='view' aria-describedby='location" . absint( $location->location_id ) . "'>" . esc_html__( 'View', 'my-calendar' ) . '</a> | ';
}
--- a/my-calendar/my-calendar-locations.php
+++ b/my-calendar/my-calendar-locations.php
@@ -14,6 +14,76 @@
}
/**
+ * Get the requested location post status from submitted data.
+ *
+ * @param array $post POST data.
+ * @param string $default_status Status to use if no valid status was submitted.
+ *
+ * @return string 'publish' or 'draft'.
+ */
+function mc_location_requested_status( $post, $default_status = 'publish' ) {
+ if ( is_array( $post ) && isset( $post['location_status'] ) && in_array( $post['location_status'], array( 'publish', 'draft' ), true ) ) {
+ return $post['location_status'];
+ }
+
+ return ( 'draft' === $default_status ) ? 'draft' : 'publish';
+}
+
+/**
+ * Publish a draft location created with an event when that event becomes publicly visible.
+ *
+ * @param int $prev_status Previous event status.
+ * @param int $new_status New event status.
+ * @param string $action Action being performed.
+ * @param array $data Submitted event data.
+ * @param int $event_id Event ID.
+ */
+function mc_publish_location_with_event( $prev_status, $new_status, $action, $data, $event_id ) {
+ if ( 'hidden' === mc_event_states_type( $new_status ) ) {
+ return;
+ }
+ mc_publish_event_location( $event_id );
+}
+add_action( 'mc_transition_event', 'mc_publish_location_with_event', 10, 5 );
+
+/**
+ * Publish an event's location if it was created as a draft along with the event.
+ *
+ * @param int $event_id Event ID.
+ */
+function mc_publish_event_location( $event_id ) {
+ $location_id = (int) mc_get_data( 'event_location', $event_id );
+ if ( ! $location_id ) {
+ return;
+ }
+ $post_id = mc_get_location_post( $location_id, false );
+ if ( $post_id && 'draft' === get_post_status( $post_id ) && get_post_meta( $post_id, '_mc_draft_with_event', true ) ) {
+ wp_publish_post( $post_id );
+ delete_post_meta( $post_id, '_mc_draft_with_event' );
+ delete_transient( 'mc_location_' . $location_id );
+ }
+}
+
+/**
+ * Check whether a location is a draft, based on its location post status.
+ *
+ * @param int $location_id Location ID.
+ *
+ * @return bool
+ */
+function mc_location_is_draft( $location_id ) {
+ if ( ! $location_id || ! is_numeric( $location_id ) ) {
+ return false;
+ }
+ $post_id = mc_get_location_post( (int) $location_id, false );
+ if ( ! $post_id ) {
+ return false;
+ }
+
+ return 'draft' === get_post_status( $post_id );
+}
+
+/**
* Handle updating location posts
*
* @param array $where Array with where query.
@@ -32,7 +102,7 @@
$post_id = mc_create_location_post( $location_id, $data, $post );
}
$title = $data['location_label'];
- $post_status = 'publish';
+ $post_status = mc_location_requested_status( $post, get_post_status( $post_id ) );
$auth = get_current_user_id();
$type = 'mc-locations';
$my_post = array(
@@ -47,7 +117,10 @@
switch_to_blog( BLOG_ID_CURRENT_SITE );
}
$post_id = wp_update_post( $my_post );
-
+ if ( isset( $post['location_status'] ) ) {
+ // An explicit choice in the location editor overrides any automatic publishing.
+ delete_post_meta( $post_id, '_mc_draft_with_event' );
+ }
/**
* Executed an action when a location post is updated.
*
@@ -73,10 +146,11 @@
* @param bool|int $location_id Result of save action; location ID or false.
* @param array $data Saved location data.
* @param array $post POST data.
+ * @param bool $status Whether the post should be published or drafted.
*
* @return int|false newly created post ID or false if error.
*/
-function mc_create_location_post( $location_id, $data, $post = array() ) {
+function mc_create_location_post( $location_id, $data, $post = array(), $status = true ) {
if ( ! $location_id ) {
return false;
}
@@ -84,7 +158,7 @@
// If not post ID or the post ID has no status.
if ( ! $post_id || ! get_post_status( $post_id ) ) {
$title = $data['location_label'];
- $post_status = 'publish';
+ $post_status = mc_location_requested_status( $post, $status ? 'publish' : 'draft' );
$auth = get_current_user_id();
$type = 'mc-locations';
$my_post = array(
@@ -96,6 +170,10 @@
'post_type' => $type,
);
$post_id = wp_insert_post( $my_post );
+ if ( ! $status && 'draft' === $post_status ) {
+ // Mark as created from a draft event, so it is published with that event.
+ update_post_meta( $post_id, '_mc_draft_with_event', '1' );
+ }
if ( isset( $post['update'] ) ) {
mc_update_location_post_relationship( $location_id, $post_id );
} else {
@@ -113,12 +191,14 @@
* @param int $location_id Location ID.
*/
do_action( 'mc_create_location_post', $post_id, $post, $data, $location_id );
- wp_publish_post( $post_id );
+ if ( 'publish' === $post_status ) {
+ wp_publish_post( $post_id );
+ }
}
return $post_id;
}
-add_filter( 'mc_save_location', 'mc_create_location_post', 10, 3 );
+add_filter( 'mc_save_location', 'mc_create_location_post', 10, 4 );
/**
* Update custom fields for a location.
@@ -306,10 +386,11 @@
* Insert a new location.
*
* @param array $post Array of location details to insert.
+ * @param bool $status Whether the location should be published. Default true.
*
* @return array Array with location_id and location_post.
*/
-function mc_insert_location( $post ) {
+function mc_insert_location( $post, $status = true ) {
$add = array(
'location_label' => $post['location_label'],
'location_street' => $post['location_street'],
@@ -329,7 +410,37 @@
);
global $wpdb;
- $add = array_map( 'mc_kses_post', $add );
+ $add = array_map( 'mc_kses_post', $add );
+ // Check if any location fields have data before inserting into the database, excluding location_zoom.
+ $location_fields = array(
+ 'location_label',
+ 'location_street',
+ 'location_street2',
+ 'location_city',
+ 'location_state',
+ 'location_postcode',
+ 'location_region',
+ 'location_country',
+ 'location_url',
+ 'location_longitude',
+ 'location_latitude',
+ 'location_phone',
+ 'location_phone2',
+ );
+ $has_location_data = false;
+ foreach ( $location_fields as $field ) {
+ if ( isset( $add[ $field ] ) && is_scalar( $add[ $field ] ) && '' !== trim( (string) $add[ $field ] ) ) {
+ $has_location_data = true;
+ break;
+ }
+ }
+ if ( ! $has_location_data ) {
+ return array(
+ 'location_id' => false,
+ 'location_post' => false,
+ );
+ }
+
$formats = array( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%f', '%f', '%d', '%s', '%s', '%s' );
$results = $wpdb->insert( my_calendar_locations_table(), $add, $formats );
if ( $results ) {
@@ -346,10 +457,11 @@
* @param int|false $results Result of database insertion. Row ID or false.
* @param array $add Array of location parameters to add.
* @param array $post POST array.
+ * @param bool $status Whether the location should be published. Default true.
*
* @return int Before priority 10, returns the location ID; after priority 10 returns the location post ID. Sorry.
*/
- $results = apply_filters( 'mc_save_location', $insert_id, $add, $post );
+ $results = apply_filters( 'mc_save_location', $insert_id, $add, $post, $status );
return array(
'location_id' => $insert_id,
@@ -390,12 +502,16 @@
/**
* Update a location.
+ * Only users with the 'mc_edit_locations' capability can update locations.
*
* @param array $post Array of data posted to update.
*
* @return mixed boolean/int query result.
*/
function mc_update_location( $post ) {
+ if ( ! current_user_can( 'mc_edit_locations' ) ) {
+ return false;
+ }
global $wpdb;
$update = array(
'location_label' => $post['location_label'],
@@ -462,7 +578,9 @@
$return = mc_show_notice( __( 'Location deleted successfully', 'my-calendar' ), false, false, 'success' );
$default_location = mc_get_option( 'default_location', false );
if ( (int) $default_location === $location ) {
- mc_update_option( 'default_location', '' );
+ if ( ! mc_update_location_settings( array( 'default_location' => '' ) ) ) {
+ mc_show_error( __( 'The default location could not be cleared.', 'my-calendar' ) );
+ }
}
} else {
$value = false;
@@ -496,9 +614,11 @@
$results = mc_insert_location( $post );
$location_id = $results['location_id'];
// If this is being generated from an event/location merge, update the event to the new location ID.
- if ( isset( $_GET['event_source'] ) ) {
+ if ( $location_id && isset( $_GET['event_source'] ) ) {
$source = absint( $_GET['event_source'] );
- mc_update_data( $source, 'event_location', $location_id );
+ if ( false === mc_update_event_data( $source, 'event_location', $location_id ) ) {
+ mc_show_error( __( 'The location was added, but the event could not be updated.', 'my-calendar' ) );
+ }
}
if ( $location_id ) {
@@ -512,8 +632,10 @@
} else {
mc_show_error( __( 'Location could not be added to database', 'my-calendar' ) );
}
- if ( isset( $post['mc_default_location'] ) ) {
- mc_update_option( 'default_location', (int) $location_id );
+ if ( $location_id && isset( $post['mc_default_location'] ) ) {
+ if ( ! mc_update_location_settings( array( 'default_location' => (int) $location_id ) ) ) {
+ mc_show_error( __( 'The default location could not be updated.', 'my-calendar' ) );
+ }
}
}
// Edit location mode.
@@ -522,11 +644,15 @@
$mode = 'edit';
if ( isset( $post['mc_default_location'] ) ) {
- mc_update_option( 'default_location', (int) $post['location_id'] );
+ if ( ! mc_update_location_settings( array( 'default_location' => (int) $post['location_id'] ) ) ) {
+ mc_show_error( __( 'The default location could not be updated.', 'my-calendar' ) );
+ }
}
$default_location = mc_get_option( 'default_location' );
if ( (int) $post['location_id'] === (int) $default_location && ! isset( $post['mc_default_location'] ) ) {
- mc_update_option( 'default_location', '' );
+ if ( ! mc_update_location_settings( array( 'default_location' => '' ) ) ) {
+ mc_show_error( __( 'The default location could not be cleared.', 'my-calendar' ) );
+ }
}
if ( false === $location_id ) {
mc_show_error( __( 'Location could not be edited.', 'my-calendar' ) );
@@ -621,6 +747,7 @@
if ( isset( $_GET['event_source'] ) ) {
$params['event_source'] = absint( $_GET['event_source'] );
}
+ $current_status = ( $loc_id ) ? get_post_status( mc_get_location_post( $loc_id, false ) ) : 'publish';
?>
<form id="my-calendar" method="post" action="<?php echo esc_url( add_query_arg( $params, admin_url( 'admin.php?page=my-calendar-locations' ) ) ); ?>">
<div class="mc-controls">
@@ -633,8 +760,9 @@
<li><span class="dashicons dashicons-no" aria-hidden="true"></span><a class="delete" href="<?php echo esc_url( $delete_url ); ?>"><?php esc_html_e( 'Delete', 'my-calendar' ); ?></a></li>
<?php
if ( $view_url && esc_url( $view_url ) ) {
+ $text = ( 'publish' === $current_status ) ? __( 'View', 'my-calendar' ) : __( 'Preview', 'my-calendar' );
?>
- <li><span class="dashicons dashicons-laptop" aria-hidden="true"></span><a class="view" href="<?php echo esc_url( $view_url ); ?>"><?php esc_html_e( 'View', 'my-calendar' ); ?></a></li>
+ <li><span class="dashicons dashicons-laptop" aria-hidden="true"></span><a class="view" href="<?php echo esc_url( $view_url ); ?>"><?php echo esc_html( $text ); ?></a></li>
<li><span class="dashicons dashicons-edit" aria-hidden="true"></span><a class="edit" href="<?php echo esc_url( get_edit_post_link( mc_get_location_post( $loc_id, false ) ) ); ?>"><?php esc_html_e( 'Edit Post', 'my-calendar' ); ?></a></li>
<?php
@@ -642,6 +770,13 @@
}
?>
<li><input type="submit" name="save" class="button button-primary" value="<?php echo esc_attr( ( 'edit' === $view ) ? __( 'Save Changes', 'my-calendar' ) : __( 'Add Location', 'my-calendar' ) ); ?> "/></li>
+ <?php
+ $current_status = ( 'draft' === $current_status ) ? 'draft' : 'publish';
+ echo '<li><label for="mc_location_status">' . esc_html__( 'Status', 'my-calendar' ) . '</label> <select name="location_status" id="mc_location_status">' .
+ '<option value="publish"' . selected( $current_status, 'publish', false ) . '>' . esc_html__( 'Published', 'my-calendar' ) . '</option>' .
+ '<option value="draft"' . selected( $current_status, 'draft', false ) . '>' . esc_html__( 'Draft', 'my-calendar' ) . '</option>' .
+ '</select></li>';
+ ?>
</ul>
</div>
<div><input type="hidden" name="_wpnonce" value="<?php echo esc_attr( wp_create_nonce( 'my-calendar-nonce' ) ); ?>"/></div>
@@ -1419,7 +1554,9 @@
$orderby = 'location_label';
}
$results = $wpdb->get_results( $wpdb->prepare( 'SELECT location_id,location_label,location_street FROM ' . my_calendar_locations_table() . ' WHERE ' . esc_sql( $where ) . ' = %s ORDER BY ' . esc_sql( $orderby ) . ' ' . esc_sql( $order ), $is ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
-
+ if ( 'select-locations' === $args ) {
+ $results = mc_remove_draft_locations( $results );
+ }
/**
* Filter returned results when searching locations.
*
@@ -1471,7 +1608,28 @@
$locations = $wpdb->get_results( 'SELECT location_id, location_label FROM ' . my_calendar_locations_table() . " $search ORDER BY location_label ASC" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQL.NotPrepared
- return $locations;
+ return mc_remove_draft_locations( $locations );
+}
+
+/**
+ * Remove locations with a draft location post from a list of location objects.
+ *
+ * @param array $locations Array of location objects with a location_id property.
+ *
+ * @return array
+ */
+function mc_remove_draft_locations( $locations ) {
+ if ( ! is_array( $locations ) ) {
+ return $locations;
+ }
+ return array_values(
+ array_filter(
+ $locations,
+ function ( $location ) {
+ return ! mc_location_is_draft( $location->location_id );
+ }
+ )
+ );
}
--- a/my-calendar/my-calendar-navigation.php
+++ b/my-calendar/my-calendar-navigation.php
@@ -450,7 +450,7 @@
// If category colors are ignored, don't render HTML for them.
$cat_key .= $cat_name;
}
- $key .= '<li class="cat_' . $class . '"><button type="button" class="mc-navigation-button" id="mc_cat_' . $category_id . '-' . $id . '" data-href="' . esc_url( $url ) . '" ' . $aria_current . '>' . $cat_key . '</button></li>';
+ $key .= '<li class="cat_' . esc_attr( $class ) . '"><button type="button" class="mc-navigation-button" id="mc_cat_' . $category_id . '-' . $id . '" data-href="' . esc_url( $url ) . '" ' . $aria_current . '>' . $cat_key . '</button></li>';
}
/**
* Filter text label for 'All Categories'.
--- a/my-calendar/my-calendar-output.php
+++ b/my-calendar/my-calendar-output.php
@@ -473,7 +473,7 @@
$single_template = mc_get_template( 'title_solo' ) === '' ? '{title}' : mc_get_template( 'title_solo' );
$event_title_single = mc_draw_template( $tags, $single_template );
if ( ( 'modal' === $gridtype && 'calendar' === $type ) || ( 'modal' === $listtype && 'list' === $type ) ) {
- $params = "id='modal-button-$container_id' data-modal-content-id='$container_id' data-modal-prefix-class='my-calendar' data-modal-close-text='" . esc_attr( __( 'Close', 'my-calendar' ) ) . "' data-modal-title='" . esc_attr( $event_title_single ) . "'";
+ $params = "id='modal-button-$container_id' data-modal-content-id='$container_id' data-modal-prefix-class='my-calendar' data-modal-close-text='" . esc_attr( __( 'Close', 'my-calendar' ) ) . "' data-modal-title='" . esc_attr( wp_strip_all_tags( mc_decode_entities( $event_title_single ) ) ) . "'";
$classes = 'mc-modal mc-button mc-text-button button-link';
} else {
$params = " aria-expanded='false'";
@@ -2153,7 +2153,7 @@
$body_classes = apply_filters( 'mc_body_classes', $body_classes, $params );
$classes = implode( ' ', map_deep( $body_classes, 'sanitize_html_class' ) );
$mc_wrapper = "
-<div id='" . esc_attr( $id ) . "' class='$classes' $lang>";
+<div id='" . esc_attr( $id ) . "' class='" . esc_attr( $classes ) . "' $lang>";
$mc_closer = '
</div><!-- Close Main My Calendar Wrapper -->';
@@ -2371,7 +2371,7 @@
}
}
$label = date_i18n( mc_date_format(), $start );
- $modal_attrs = "id='{format}-modal-button-$date_is' data-modal-content-id='{target_id}' data-modal-prefix-class='my-calendar' data-modal-close-text='" . esc_attr( __( 'Close', 'my-calendar' ) ) . "' data-modal-title='" . esc_attr( $label ) . "'";
+ $modal_attrs = "id='{format}-modal-button-$date_is' data-modal-content-id='{target_id}' data-modal-prefix-class='my-calendar' data-modal-close-text='" . esc_attr( __( 'Close', 'my-calendar' ) ) . "' data-modal-title='" . esc_attr( wp_strip_all_tags( mc_decode_entities( $label ) ) ) . "'";
if ( ! empty( $events ) ) {
/**
--- a/my-calendar/my-calendar-search.php
+++ b/my-calendar/my-calendar-search.php
@@ -194,8 +194,11 @@
* @return string New title
*/
function mc_search_results_title( $title, $id = null ) {
- if ( ( isset( $_GET['mcs'] ) || isset( $_POST['mcs'] ) ) && ( is_page( $id ) || is_single( $id ) ) && in_the_loop() ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
- $query = ( isset( $_GET['mcs'] ) ) ? sanitize_text_field( wp_unslash( $_GET['mcs'] ) ) : sanitize_text_field( wp_unslash( $_POST['mcs'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
+ if ( ( isset( $_GET['mcs'] ) ) && ( is_page( $id ) || is_single( $id ) ) && in_the_loop() ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
+ $query = ( isset( $_GET['mcs'] ) ) ? sanitize_text_field( wp_unslash( $_GET['mcs'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
+ if ( '' === $query ) {
+ return $title;
+ }
// Translators: entered search query.
$title = sprintf( __( 'Events Search for “%s”', 'my-calendar' ), esc_html( $query ) );
}
@@ -213,7 +216,8 @@
*/
function mc_show_search_results( $content ) {
global $post;
- if ( is_object( $post ) && in_the_loop() && ! is_page( mc_get_option( 'uri_id' ) ) && ! has_shortcode( $post->post_content, 'my_calendar' ) ) {
+ $calendar_page = mc_get_option( 'uri_id' );
+ if ( is_object( $post ) && in_the_loop() && ( ! $calendar_page || ! is_page( $calendar_page ) ) && ! has_shortcode( $post->post_content, 'my_calendar' ) ) {
// if this is the result of a search, show search output.
$ret = false;
$query = false;
@@ -403,15 +407,16 @@
$html = '';
foreach ( $output as $out ) {
- $data = array(
+ $template_class = esc_attr( wp_strip_all_tags( $template ) );
+ $data = array(
'event' => $out['event'],
'tags' => $out['tags'],
'template' => $template,
'type' => 'list',
- 'class' => ( str_contains( $template, 'list_preset_' ) ) ? "list-preset $template" : '',
+ 'class' => ( str_contains( $template, 'list_preset_' ) ) ? "list-preset $template_class" : '',
);
- $details = mc_load_template( 'event/search', $data );
- $html .= $details;
+ $details = mc_load_template( 'event/search', $data );
+ $html .= $details;
if ( ! $details ) {
$html .= mc_format_upcoming_event( $out, $template, 'list' );
}
--- a/my-calendar/my-calendar-settings.php
+++ b/my-calendar/my-calendar-settings.php
@@ -66,6 +66,7 @@
/**
* Save a My Calendar setting.
+ * Only users with the 'mc_edit_settings' capability can update options.
*
* @param string $key Setting key.
* @param mixed $value Setting value.
@@ -73,6 +74,9 @@
* @return bool
*/
function mc_update_option( $key, $value = '' ) {
+ if ( ! current_user_can( 'mc_edit_settings' ) ) {
+ return false;
+ }
$options = get_option( 'my_calendar_options', mc_default_options() );
if ( ! is_array( $options ) ) {
$options = mc_default_options();
@@ -85,12 +89,16 @@
/**
* Save a group of My Calendar settings.
+ * Only users with the 'mc_edit_settings' capability can update options.
*
* @param array $options An array of settings.
*
* @return bool
*/
function mc_update_options( $options ) {
+ if ( ! current_user_can( 'mc_edit_settings' ) ) {
+ return false;
+ }
if ( empty( $options ) ) {
return false;
}
@@ -101,6 +109,140 @@
}
/**
+ * Update an explicitly authorized set of context-specific settings.
+ *
+ * Request handlers must independently verify their nonces and sanitize values.
+ *
+ * @param array $options Settings to update.
+ * @param string $context Settings group.
+ *
+ * @return bool True if saved or unchanged; false on denial or failure.
+ */
+function mc_update_context_settings( $options, $context ) {
+ $groups = array(
+ 'category'