add sidebar and custom directory widget update transient names, add spectrum tax

This commit is contained in:
Evan Mullins 2019-04-11 16:11:03 -06:00
parent 8120744ff7
commit d16c75d1ad
7 changed files with 248 additions and 14 deletions

View file

@ -291,6 +291,28 @@
"ajax": 0,
"return_format": "value",
"placeholder": ""
},
{
"key": "field_5cadf1eccb472",
"label": "Spectrum",
"name": "spectrum",
"type": "taxonomy",
"instructions": "Where are you on the spectrum? (add \"labels\" as needed)",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"taxonomy": "spectrum",
"field_type": "multi_select",
"allow_null": 0,
"add_term": 1,
"save_terms": 1,
"load_terms": 1,
"return_format": "id",
"multiple": 0
}
],
"location": [
@ -325,5 +347,5 @@
],
"active": 1,
"description": "",
"modified": 1553002733
"modified": 1554903603
}

View file

@ -1,5 +1,13 @@
<?php
require_once( get_stylesheet_directory() . '/includes/wasmo-directory-widget.php' );
// register Foo_Widget widget
function register_directory_widget() {
register_widget( 'wasmo\Directory_Widget' );
}
add_action( 'widgets_init', 'register_directory_widget' );
// Enqueue styles - get parent theme styles first.
function wasmo_enqueue() {
@ -76,12 +84,40 @@ function cptui_register_my_taxes() {
'assign_terms' => 'edit_posts'
)
);
);
register_taxonomy( "question", array( "post" ), $args );
/**
* Taxonomy: Spectrum.
*/
$labels = array(
"name" => __( "Spectrum", "wasmo" ),
"singular_name" => __( "Spectrum", "wasmo" ),
);
$args = array(
"label" => __( "Spectrum", "wasmo" ),
"labels" => $labels,
"public" => true,
"publicly_queryable" => true,
"hierarchical" => false,
"show_ui" => true,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"query_var" => true,
"rewrite" => array( 'slug' => 'spectrum', 'with_front' => true, ),
"show_admin_column" => false,
"show_in_rest" => true,
"rest_base" => "spectrum",
"rest_controller_class" => "WP_REST_Terms_Controller",
"show_in_quick_edit" => false,
);
register_taxonomy( "spectrum", array( "post", "user" ), $args );
}
add_action( 'init', 'cptui_register_my_taxes' );
function wasmo_widgets_init() {
register_sidebar(
@ -240,8 +276,10 @@ function wasmo_update_user( $post_id ) {
update_user_meta( $user_id, 'last_save', time() );
// clear directory transients
delete_transient( 'directory-private' );
delete_transient( 'directory-public' );
delete_transient( 'directory-private-full' );
delete_transient( 'directory-public-full' );
delete_transient( 'directory-private-widget' );
delete_transient( 'directory-public-widget' );
// update question counts if user includes any
if( have_rows( 'questions', 'user_' . $user_id ) ){

View file

@ -0,0 +1,72 @@
<?php
namespace wasmo;
/**
* Adds directory widget.
*/
class Directory_Widget extends \WP_Widget {
/**
* Register widget with WordPress.
*/
function __construct() {
parent::__construct(
'directory_widget', // Base ID
esc_html__( 'Directory Widget', 'text_domain' ), // Name
array( 'description' => esc_html__( 'wasmo Directory Widget', 'text_domain' ), ) // Args
);
}
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget( $args, $instance ) {
echo $args['before_widget'];
if ( ! empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
}
set_query_var( 'context', 'widget' );
get_template_part( 'template-parts/content/content', 'directory' );
echo $args['after_widget'];
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance ) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( 'Directory', 'text_domain' );
?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:', 'text_domain' ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
</p>
<?php
}
/**
* Sanitize widget form values as they are saved.
*
* @see WP_Widget::update()
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
*
* @return array Updated safe values to be saved.
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? sanitize_text_field( $new_instance['title'] ) : '';
return $instance;
}
} // class Directory_Widget

38
page.php Normal file
View file

@ -0,0 +1,38 @@
<?php
/**
* The template for displaying all single posts
*
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post
*
* @package WordPress
* @subpackage Twenty_Nineteen
* @since 1.0.0
*/
get_header();
?>
<section id="primary" class="content-area">
<main id="main" class="site-main">
<?php
/* Start the Loop */
while ( have_posts() ) :
the_post();
get_template_part( 'template-parts/content/content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
endwhile; // End of the loop.
?>
<?php //get_template_part( 'template-parts/sidebar/sidebar' ); ?>
</main><!-- #main -->
</section><!-- #primary -->
<?php
get_footer();

View file

@ -194,8 +194,28 @@ body.logged-in .menu-item.logged-in {
opacity: 0;
transition: all .2s ease;
}
/* directory widget */
.widget .the-directory {
max-width: 90%;
margin: 0 auto 2rem;
}
.widget .the-directory .directory .person {
width: 30%;
padding-top: 30%;
}
@media only screen and (min-width: 768px) { /* desktop - sidebar */
.widget .the-directory {
max-width: 100%;
margin: 0 auto 2rem;
}
.widget .the-directory .directory .person {
width: 30%;
padding-top: 30%;
}
.widget .directory .person:hover .directory-name {
opacity: 0;
}
}
/* profile */

View file

@ -1,14 +1,25 @@
<?php
// Directory
if ( 'widget' !== $context ) {
$context = 'full';
}
// define transient name - taxid + user state.
if ( is_user_logged_in() ) {
$transient_name = 'directory-private';
$state = 'private';
} else {
$transient_name = 'directory-public';
$state = 'public';
}
$transient_exp = 7 * 24 * HOUR_IN_SECONDS; // 1 week
$transient_name = 'directory-'.$state.'-'.$context;
$transient_exp = 7 * 24 * HOUR_IN_SECONDS; // one week
// debug
// delete_transient( 'directory-private-full' );
// delete_transient( 'directory-public-full' );
// delete_transient( 'directory-private-widget' );
// delete_transient( 'directory-public-widget' );
// if ( current_user_can('administrator') && WP_DEBUG ) {
// $transient_name = time();
// }
@ -22,16 +33,18 @@ if ( false === ( $the_directory = get_transient( $transient_name ) ) ) {
'meta_key' => 'last_save',
'order' => 'DESC',
'fields' => 'all',
);
);
// if ( 'widget' === $context ) {
// $args['number'] = 10;
// }
$users = get_users( $args );
$the_directory .= '<section class="entry-content the-directory">';
$the_directory .= '<div class="directory">';
$counter = 0;
// Array of WP_User objects.
foreach ( $users as $user ) {
$userid = $user->ID;
// only add to directory if user includes themself and has filled out the first two fields
// true = public
// private = only to a logged in user
@ -39,11 +52,12 @@ if ( false === ( $the_directory = get_transient( $transient_name ) ) ) {
get_field( 'tagline', 'user_' . $userid ) &&
'true' === get_field( 'in_directory', 'user_' . $userid ) ||
'private' === get_field( 'in_directory', 'user_' . $userid ) && is_user_logged_in() ) {
$counter++;
$userimg = get_field( 'photo', 'user_' . $userid );
$username = esc_html( $user->nickname );
$the_directory .= '<a class="person person-' . $userid . '" href="' . get_author_posts_url( $userid ) . '">';
$the_directory .= '<a class="person person-' . $counter . ' person-id-' . $userid . '" href="' . get_author_posts_url( $userid ) . '">';
$the_directory .= '<span class="directory-img">';
if ( $userimg ) {
$the_directory .= wp_get_attachment_image( $userimg, 'medium' );
@ -54,6 +68,12 @@ if ( false === ( $the_directory = get_transient( $transient_name ) ) ) {
$the_directory .= '<span class="directory-name">' . $username . '</span>';
$the_directory .= '</a>';
}
// only include 9 if a widget
if ( 'widget' === $context &&
$counter >= 9 ) {
break;
}
}
$the_directory .= '</div>';
$the_directory .= '</section>';

View file

@ -0,0 +1,24 @@
<?php
/**
* Displays the footer widget area
*
* @package WordPress
* @subpackage Twenty_Nineteen
* @since 1.0.0
*/
if ( is_active_sidebar( 'sidebar' ) ) : ?>
<aside class="widget-area sidebar" role="complementary" aria-label="<?php esc_attr_e( 'Sidebar', 'twentynineteen' ); ?>">
<?php
if ( is_active_sidebar( 'sidebar' ) ) {
?>
<div class="widget-column sidebar-widgets">
<?php dynamic_sidebar( 'sidebar' ); ?>
</div>
<?php
}
?>
</aside><!-- .widget-area -->
<?php endif; ?>