Validate form input

This commit is contained in:
Ben Goldsworthy 2021-01-23 13:26:25 +00:00
parent 3a97551bf1
commit abd8c65ee7
5 changed files with 168 additions and 150 deletions

View file

@ -215,8 +215,8 @@ if ( ! function_exists( 'omphaloskepsis_fonts_url' ) ) :
if ( $fonts ) { if ( $fonts ) {
$fonts_url = add_query_arg( $fonts_url = add_query_arg(
array( array(
'family' => urlencode( implode( '|', $fonts ) ), 'family' => rawurlencode( implode( '|', $fonts ) ),
'subset' => urlencode( $subsets ), 'subset' => rawurlencode( $subsets ),
), ),
'https://fonts.googleapis.com/css' 'https://fonts.googleapis.com/css'
); );
@ -452,6 +452,7 @@ function omphaloskepsis_the_content( $content ) {
global $post; global $post;
if ( 'program' === $post->post_type ) { if ( 'program' === $post->post_type ) {
// phpcs:disable Squiz.PHP.DisallowMultipleAssignments.FoundInControlStructure // phpcs:disable Squiz.PHP.DisallowMultipleAssignments.FoundInControlStructure
// phpcs:disable WordPress.CodeAnalysis.AssignmentInCondition.Found
if ( $meta = get_post_meta( $post->ID, 'Link', true ) ) { if ( $meta = get_post_meta( $post->ID, 'Link', true ) ) {
$links = $links . '<a class="hyperlink-button" target="_blank" href="' . $meta . '">Download</a>'; $links = $links . '<a class="hyperlink-button" target="_blank" href="' . $meta . '">Download</a>';
} }
@ -517,144 +518,152 @@ function display_companies() {
echo '<th>Associated Items</th>'; echo '<th>Associated Items</th>';
echo '</tr>'; echo '</tr>';
// phpcs:disable WordPress.Security.NonceVerification.Missing if ( isset( $_SERVER['REQUEST_METHOD'] ) && ( 'POST' === $_SERVER['REQUEST_METHOD'] ) ) {
// phpcs:disable WordPress.PHP.YodaConditions.NotYoda if ( ! isset( $_POST['settings_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( filter_input( INPUT_POST, 'settings_nonce' ) ), 'experience' ) ) {
// phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotValidated echo '<tr><td>Error</td></tr>';
if ( $_POST['toplevel'] == 'true' ) { wp_die( 'Invalid nonce' );
// Gets all of the top-level company terms. }
$terms = apply_filters(
'taxonomy-images-get-terms', // phpcs:disable WordPress.NamingConventions.ValidHookName.UseUnderscores
'', if ( isset( $_POST['toplevel'] ) && 'true' === $_POST['toplevel'] ) {
array( // Gets all of the top-level company terms.
'having_images' => false, $terms = apply_filters(
'taxonomy' => 'company', 'taxonomy-images-get-terms',
'term_args' => array( 'parent' => 0 ), '',
) array(
); 'having_images' => false,
$include = 1; 'taxonomy' => 'company',
} else { 'term_args' => array( 'parent' => 0 ),
// Gets all of the company terms. )
$terms = apply_filters( );
'taxonomy-images-get-terms', $include = 1;
'', } else {
array( // Gets all of the company terms.
'having_images' => false, $terms = apply_filters(
'taxonomy' => 'company', 'taxonomy-images-get-terms',
) '',
); array(
$include = 0; 'having_images' => false,
} 'taxonomy' => 'company',
)
if ( ! empty( $terms ) ) { );
foreach ( $terms as $term ) { $include = 0;
$term_children = get_term_children( $term->term_id, 'company' ); }
// phpcs:enable
// 0 = Jobs
// 1 = Blog Posts if ( ! empty( $terms ) ) {
// 2 = Websites foreach ( $terms as $term ) {
// 3 = Programs $term_children = get_term_children( $term->term_id, 'company' );
// 4 = Writings
// 5 = Videos // 0 = Jobs
// 6 = Others // 1 = Blog Posts
// 7 = Qualifications // 2 = Websites
// 8 = Awards // 3 = Programs
$post_types = array( 'job', 'post', 'website', 'program', 'writing', 'video', 'other', 'qualification', 'award' ); // 4 = Writings
$dashicons = array( 'hammer', 'admin-post', 'schedule', 'desktop', 'format-aside', 'video-alt', 'archive', 'id', 'awards' ); // 5 = Videos
$term_items = array(); // 6 = Others
$term_item_counts = array(); // 7 = Qualifications
// 8 = Awards
foreach ( $post_types as $post_type ) { $post_types = array( 'job', 'post', 'website', 'program', 'writing', 'video', 'other', 'qualification', 'award' );
$args = array( $dashicons = array( 'hammer', 'admin-post', 'schedule', 'desktop', 'format-aside', 'video-alt', 'archive', 'id', 'awards' );
'posts_per_page' => -1, $term_items = array();
'post_type' => $post_type, $term_item_counts = array();
'tax_query' => array(
array( // phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_tax_query
'taxonomy' => 'company', // phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_query
'field' => 'slug', foreach ( $post_types as $post_type ) {
'terms' => $term->slug, $args = array(
'include_children' => $include, 'posts_per_page' => -1,
), 'post_type' => $post_type,
), 'tax_query' => array(
'meta_query' => array(), array(
); 'taxonomy' => 'company',
'field' => 'slug',
if ( $_POST['currentjobs'] == 'true' && $post_type == 'job' ) { 'terms' => $term->slug,
$args['meta_query'] = array( 'include_children' => $include,
array( ),
'key' => 'end-date', ),
'compare' => 'NOT EXISTS', 'meta_query' => array(),
'value' => '1', );
),
); if ( isset( $_POST['currentjobs'] ) && 'true' === $_POST['currentjobs'] && 'job' === $post_type ) {
} $args['meta_query'] = array(
array(
if ( $_POST['showexpired'] != 'true' && $post_type == 'qualification' ) { 'key' => 'end-date',
$args['meta_query'] = array( 'compare' => 'NOT EXISTS',
array( 'value' => '1',
'key' => 'Expired', ),
'compare' => 'NOT EXISTS', );
'value' => '1', }
),
); if ( isset( $_POST['showexpired'] ) && 'true' !== $_POST['showexpired'] && 'qualification' === $post_type ) {
} $args['meta_query'] = array(
array(
$posts = get_posts( $args ); 'key' => 'Expired',
'compare' => 'NOT EXISTS',
array_push( $term_items, $posts ); 'value' => '1',
array_push( $term_item_counts, count( $posts ) ); ),
} );
}
if ( ( $_POST['job'] == 'true' && $term_item_counts[0] > 0 ) || // phpcs:enable
( $_POST['post'] == 'true' && $term_item_counts[1] > 0 ) ||
( $_POST['website'] == 'true' && $term_item_counts[2] > 0 ) || $posts = get_posts( $args );
( $_POST['program'] == 'true' && $term_item_counts[3] > 0 ) ||
( $_POST['writing'] == 'true' && $term_item_counts[4] > 0 ) || array_push( $term_items, $posts );
( $_POST['video'] == 'true' && $term_item_counts[5] > 0 ) || array_push( $term_item_counts, count( $posts ) );
( $_POST['other'] == 'true' && $term_item_counts[6] > 0 ) || }
( $_POST['qualification'] == 'true' && $term_item_counts[7] > 0 ) ||
( $_POST['award'] == 'true' && $term_item_counts[8] > 0 ) ) { if ( ( isset( $_POST['job'] ) && 'true' === $_POST['job'] && $term_item_counts[0] > 0 ) ||
$img_url = wp_get_attachment_image_src( $term->image_id, 'full' )[0]; ( isset( $_POST['post'] ) && 'true' === $_POST['post'] && $term_item_counts[1] > 0 ) ||
$bg_img = ( ! $img_url ) ? '' : ' background-image: url(' . strtok( $img_url, '?' ) . ');'; ( isset( $_POST['website'] ) && 'true' === $_POST['website'] && $term_item_counts[2] > 0 ) ||
$colour = get_term_meta( $term->term_id, 'color', true ); ( isset( $_POST['program'] ) && 'true' === $_POST['program'] && $term_item_counts[3] > 0 ) ||
$colour = ( $colour != '' ) ? $colour : 'transparent'; ( isset( $_POST['writing'] ) && 'true' === $_POST['writing'] && $term_item_counts[4] > 0 ) ||
( isset( $_POST['video'] ) && 'true' === $_POST['video'] && $term_item_counts[5] > 0 ) ||
echo '<tr class="organisation">'; ( isset( $_POST['other'] ) && 'true' === $_POST['other'] && $term_item_counts[6] > 0 ) ||
echo '<td class="organisation-logo">'; ( isset( $_POST['qualification'] ) && 'true' === $_POST['qualification'] && $term_item_counts[7] > 0 ) ||
echo '<a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '">'; ( isset( $_POST['award'] ) && 'true' === $_POST['award'] && $term_item_counts[8] > 0 ) ) {
echo wp_kses_post( '<img style="background-color: ' . $colour . ';" src="' . strtok( $img_url, '?' ) . '" alt="' . $term->name . ' logo">' ); $img_url = wp_get_attachment_image_src( $term->image_id, 'full' )[0];
echo '</a>'; $bg_img = ( ! $img_url ) ? '' : ' background-image: url(' . strtok( $img_url, '?' ) . ');';
echo '</td>'; $colour = get_term_meta( $term->term_id, 'color', true );
$colour = ( '' !== $colour ) ? $colour : 'transparent';
echo '<td class="organisation-name">';
echo '<a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '">'; echo '<tr class="organisation">';
echo wp_kses_post( '<p>' . $term->name . '</p>' ); echo '<td class="organisation-logo">';
echo '</a>'; echo '<a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '">';
echo '</td>'; echo wp_kses_post( '<img style="background-color: ' . $colour . ';" src="' . strtok( $img_url, '?' ) . '" alt="' . $term->name . ' logo">' );
echo '</a>';
echo '<td class="organisation-items organisation-children">'; echo '</td>';
$num = ( count( $term_children ) > 0 ) ? '' : 'none';
echo '<div class="organisation-item ' . esc_attr( $num ) . '">'; echo '<td class="organisation-name">';
echo wp_kses_post( '<span class="dashicons dashicons-groups"></span><br>' . count( $term_children ) ); echo '<a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '">';
echo '</div>'; echo wp_kses_post( '<p>' . $term->name . '</p>' );
echo '</td>'; echo '</a>';
echo '</td>';
echo '<td class="organisation-items">';
$i = 0; echo '<td class="organisation-items organisation-children">';
foreach ( $post_types as $post_type ) { $num = ( count( $term_children ) > 0 ) ? '' : 'none';
$num = ( $term_item_counts[ $i ] > 0 ) ? '' : 'none'; echo '<div class="organisation-item ' . esc_attr( $num ) . '">';
echo '<div class="organisation-item ' . esc_attr( $num ) . '">'; echo wp_kses_post( '<span class="dashicons dashicons-groups"></span><br>' . count( $term_children ) );
echo wp_kses_post( '<span class="dashicons dashicons-' . esc_attr( $dashicons[ $i ] ) . '"></span><br>' . $term_item_counts[ $i ] ); echo '</div>';
echo '</div>'; echo '</td>';
$i++;
}; echo '<td class="organisation-items">';
echo '</td>'; $i = 0;
echo '</tr>'; foreach ( $post_types as $post_type ) {
} $num = ( $term_item_counts[ $i ] > 0 ) ? '' : 'none';
echo '<div class="organisation-item ' . esc_attr( $num ) . '">';
echo wp_kses_post( '<span class="dashicons dashicons-' . esc_attr( $dashicons[ $i ] ) . '"></span><br>' . $term_item_counts[ $i ] );
echo '</div>';
$i++;
};
echo '</td>';
echo '</tr>';
}
}
} else {
echo '<p>No companies found</p>';
} }
} else {
echo '<p>No companies found</p>';
} }
// phpcs:enable
die(); die();
} }
add_action( 'wp_ajax_display_companies', 'display_companies' ); add_action( 'wp_ajax_display_companies', 'display_companies' );

View file

@ -26,8 +26,8 @@ get_header(); ?>
<div class="entry-content tile" id="settings"> <div class="entry-content tile" id="settings">
<h3 style="margin-bottom: 14px;" class="subheading">Settings</h3> <h3 style="margin-bottom: 14px;" class="subheading">Settings</h3>
<form action="/experience-by-companies" id="settings" method="GET"> <form action="/experience" id="settings" method="POST">
<?php wp_nonce_field( 'experience-by-companies' ); ?> <?php echo wp_kses_post(wp_nonce_field( 'experience', 'settings_nonce' )); ?>
<input class="checkbox" type="checkbox" id="toplevel" value="toplevel" checked="checked"> <input class="checkbox" type="checkbox" id="toplevel" value="toplevel" checked="checked">
<label for="toplevel">Display only top-level organisations</label> <label for="toplevel">Display only top-level organisations</label>
<br> <br>
@ -79,8 +79,14 @@ get_header(); ?>
$('.controlgroup').controlgroup(); $('.controlgroup').controlgroup();
$('.refresh').button(); $('.refresh').button();
$('#refresh').click(function(event) {
$(this).css('border-width', '1px');
event.preventDefault();
displayCompanies();
});
// Displays the initial organisations grid. // Displays the initial organisations grid.
displayCompanies(); $('#refresh').click();
$('.expired').hide(); $('.expired').hide();
@ -104,17 +110,13 @@ get_header(); ?>
} }
}); });
$('#refresh').click(function(event) {
$(this).css('border-width', '1px');
event.preventDefault();
displayCompanies();
});
function displayCompanies() { function displayCompanies() {
$('#organisations-grid').html('<img class="loading" src="/wp-content/uploads/2016/12/ajax-loader.gif">'); $('#organisations-grid').html('<img class="loading" src="/wp-content/uploads/2016/12/ajax-loader.gif">');
var settings = { var settings = {
'action': 'display_companies', 'action': 'display_companies',
'settings_nonce': $('#settings_nonce').val(),
'toplevel': $('#toplevel').is(':checked'), 'toplevel': $('#toplevel').is(':checked'),
'job': $('#jobs').is(':checked'), 'job': $('#jobs').is(':checked'),
'currentjobs': $('#currentjobs').is(':checked'), 'currentjobs': $('#currentjobs').is(':checked'),

View file

@ -607,6 +607,7 @@ html {
body > footer { body > footer {
grid-area: footer; grid-area: footer;
margin-top: 1em; margin-top: 1em;
margin-bottom: 1em;
} }
} }

View file

@ -94,7 +94,9 @@
<main id="split-page" role="main"> <main id="split-page" role="main">
<?php <?php
// phpcs:disable WordPress.NamingConventions.ValidHookName.UseUnderscores
$company_logo = apply_filters( 'taxonomy-images-queried-term-image-url', '', array( 'image_size' => 'full' ) ); $company_logo = apply_filters( 'taxonomy-images-queried-term-image-url', '', array( 'image_size' => 'full' ) );
// phpcs:enable
$company_name = get_queried_object()->name; $company_name = get_queried_object()->name;
?> ?>
<div id="wrapper"> <div id="wrapper">
@ -130,6 +132,7 @@
<div id="parents" class="col-6 col-m-12"> <div id="parents" class="col-6 col-m-12">
<?php <?php
if ( get_queried_object()->parent != 0 ) { if ( get_queried_object()->parent != 0 ) {
// phpcs:disable WordPress.NamingConventions.ValidHookName.UseUnderscores
$parents = apply_filters( $parents = apply_filters(
'taxonomy-images-get-terms', 'taxonomy-images-get-terms',
'', '',
@ -141,6 +144,7 @@
), ),
) )
); );
// phpcs:enable
} }
if ( count( $parents ) > 0 ) : if ( count( $parents ) > 0 ) :
@ -162,6 +166,7 @@
<div id="children" class="col-6 col-m-12"> <div id="children" class="col-6 col-m-12">
<?php <?php
// phpcs:disable WordPress.NamingConventions.ValidHookName.UseUnderscores
$children = apply_filters( $children = apply_filters(
'taxonomy-images-get-terms', 'taxonomy-images-get-terms',
'', '',
@ -171,6 +176,7 @@
'term_args' => array( 'parent' => get_queried_object()->term_id ), 'term_args' => array( 'parent' => get_queried_object()->term_id ),
) )
); );
// phpcs:enable
if ( count( $children ) > 0 ) : if ( count( $children ) > 0 ) :
?> ?>
<h2 class="subheading">Child<?php echo ( count( $children ) != 1 ) ? 'ren' : ''; ?></h2> <h2 class="subheading">Child<?php echo ( count( $children ) != 1 ) ? 'ren' : ''; ?></h2>

View file

@ -12,7 +12,7 @@
<header style="background-image: url('<?php the_post_thumbnail_url(); ?>');"> <header style="background-image: url('<?php the_post_thumbnail_url(); ?>');">
<div> <div>
<h1 id="post-title"> <h1 id="post-title">
<?php echo esc_html( get_the_title() ); ?> <?php echo wp_kses_post( get_the_title() ); ?>
</h1> </h1>
<?php <?php
@ -168,13 +168,13 @@
// phpcs:disable WordPress.CodeAnalysis.AssignmentInCondition // phpcs:disable WordPress.CodeAnalysis.AssignmentInCondition
$i = 1; $i = 1;
while ( $section_title = get_post_meta( get_the_ID(), 'ToC' . $i, true ) ) : while ( $section_title = get_post_meta( get_the_ID(), 'ToC' . $i, true ) ) :
?> ?>
<li> <li>
<a href="#section-<?php echo esc_attr( $i++ ); ?>"> <a href="#section-<?php echo esc_attr( $i++ ); ?>">
<?php echo wp_kses_post( $content ); ?> <?php echo wp_kses_post( $section_title ); ?>
</a> </a>
</li> </li>
<?php <?php
endwhile; endwhile;
// phpcs:enable // phpcs:enable
?> ?>