Update company to pledge, some code cleanup

This commit is contained in:
Corey McKrill 2019-09-30 17:04:49 -07:00
parent 697a4b2329
commit 9bf2f9dd1a
No known key found for this signature in database
GPG key ID: C2C0746F7BF17E38
6 changed files with 157 additions and 212 deletions

View file

@ -1,136 +0,0 @@
<?php
/**
* This file handles the operations related to setting up a custom post type. We change change the filename, namespace,
* etc. once we have a better idea of what the CPT will be called.
*/
// todo normalize text domain across all files
namespace WordPressDotOrg\FiveForTheFuture\Company;
defined( 'WPINC' ) || die();
const CPT_SLUG = '5ftf_company';
/**
*
*/
function register() {
register_custom_post_type();
register_custom_taxonomy();
}
add_action( 'init', __NAMESPACE__ . '\register', 0 );
/**
*
*/
function register_custom_post_type() {
// TODO update the labels
$labels = array(
'name' => _x( 'Companies', 'Companies General Name', 'wporg' ),
'singular_name' => _x( 'Company', 'Company Type Singular Name', 'wporg' ),
'menu_name' => __( 'Five for the Future', 'wporg' ),
'name_admin_bar' => __( 'Company', 'wporg' ),
'archives' => __( 'Item Archives', 'wporg' ),
'attributes' => __( 'Item Attributes', 'wporg' ),
'parent_item_colon' => __( 'Parent Item:', 'wporg' ),
'all_items' => __( 'All Items', 'wporg' ),
'add_new_item' => __( 'Add New Company', 'wporg' ),
'add_new' => __( 'Add New', 'wporg' ),
'new_item' => __( 'New Item', 'wporg' ),
'edit_item' => __( 'Edit Item', 'wporg' ),
'update_item' => __( 'Update Item', 'wporg' ),
'view_item' => __( 'View Item', 'wporg' ),
'view_items' => __( 'View Items', 'wporg' ),
'search_items' => __( 'Search Item', 'wporg' ),
'not_found' => __( 'Not found', 'wporg' ),
'not_found_in_trash' => __( 'Not found in Trash', 'wporg' ),
'featured_image' => __( 'Featured Image', 'wporg' ),
'set_featured_image' => __( 'Set featured image', 'wporg' ),
'remove_featured_image' => __( 'Remove featured image', 'wporg' ),
'use_featured_image' => __( 'Use as featured image', 'wporg' ),
'insert_into_item' => __( 'Insert into item', 'wporg' ),
'uploaded_to_this_item' => __( 'Uploaded to this item', 'wporg' ),
'items_list' => __( 'Items list', 'wporg' ),
'items_list_navigation' => __( 'Items list navigation', 'wporg' ),
'filter_items_list' => __( 'Filter items list', 'wporg' ),
);
$args = array(
'label' => __( 'Company Type', 'wporg' ),
'description' => __( 'Company Type Description', 'wporg' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'revisions', 'author' ),
//'taxonomies' => array( '5ftf_tax' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 25,
'show_in_admin_bar' => false,
'show_in_nav_menus' => false,
'can_export' => false,
'has_archive' => false,
'exclude_from_search' => true,
'publicly_queryable' => true, //false?
'capability_type' => 'page',
'show_in_rest' => true,
// rewerite url to be pretty
);
register_post_type( CPT_SLUG, $args );
}
/**
*
*/
function register_custom_taxonomy() {
// TODO update the labels
// add taxonomy for teams
/*
$labels = array(
'name' => _x( 'Companies', 'Company General Name', 'wporg' ),
'singular_name' => _x( 'Company', 'Company Singular Name', 'wporg' ),
'menu_name' => __( 'Companies', 'wporg' ),
'all_items' => __( 'All Companies', 'wporg' ),
'parent_item' => __( 'Parent Item', 'wporg' ),
'parent_item_colon' => __( 'Parent Item:', 'wporg' ),
'new_item_name' => __( 'New Item Name', 'wporg' ),
'add_new_item' => __( 'Add New Item', 'wporg' ),
'edit_item' => __( 'Edit Item', 'wporg' ),
'update_item' => __( 'Update Item', 'wporg' ),
'view_item' => __( 'View Item', 'wporg' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'wporg' ),
'add_or_remove_items' => __( 'Add or remove items', 'wporg' ),
'choose_from_most_used' => __( 'Choose from the most used', 'wporg' ),
'popular_items' => __( 'Popular Items', 'wporg' ),
'search_items' => __( 'Search Items', 'wporg' ),
'not_found' => __( 'Not Found', 'wporg' ),
'no_terms' => __( 'No items', 'wporg' ),
'items_list' => __( 'Items list', 'wporg' ),
'items_list_navigation' => __( 'Items list navigation', 'wporg' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => false,
'show_tagcloud' => false,
'show_in_rest' => true,
);
register_taxonomy( CPT_SLUG, array( CPT_SLUG ), $args );
*/
// also add one for skills - design,dev, project management, etc
// and another for using as a case study / featuring on the front end
// - this way we can use this as a database of contributors but not all of them have to be on the front end
}

View file

@ -1,13 +1,27 @@
<?php
/**
*
*/
namespace WordPressDotOrg\FiveForTheFuture\PledgeForm;
use WordPressDotOrg\FiveForTheFuture;
use WordPressDotOrg\FiveForTheFuture\Company;
use WordPressDotOrg\FiveForTheFuture\CompanyMeta;
use WordPressDotOrg\FiveForTheFuture\Pledge;
use WordPressDotOrg\FiveForTheFuture\PledgeMeta;
use WP_Error;
defined( 'WPINC' ) || die();
add_shortcode( 'five_for_the_future_pledge_form', __NAMESPACE__ . '\render_shortcode' );
/**
*
*
* @param $attributes
* @param $content
*
* @return false|string
*/
function render_shortcode( $attributes, $content ) {
$action = filter_input( INPUT_POST, 'action' );
$messages = [];
@ -35,8 +49,6 @@ function render_shortcode( $attributes, $content ) {
return $html;
}
add_shortcode( 'five_for_the_future_pledge_form', __NAMESPACE__ . '\render_shortcode' );
/**
*
*
@ -45,26 +57,26 @@ add_shortcode( 'five_for_the_future_pledge_form', __NAMESPACE__ . '\render_short
* @return string|WP_Error String "success" if the form processed correctly. Otherwise WP_Error.
*/
function process_form( array $form_values ) {
$required_fields = CompanyMeta\has_required_company_meta( $form_values );
$required_fields = PledgeMeta\has_required_pledge_meta( $form_values );
if ( is_wp_error( $required_fields ) ) {
return $required_fields;
}
$name = sanitize_meta(
CompanyMeta\META_PREFIX . 'company-name',
PledgeMeta\META_PREFIX . 'company-name',
$form_values['company-name'],
'post',
Company\CPT_SLUG
Pledge\CPT_ID
);
$created = create_new_company( $name );
$created = create_new_pledge( $name );
if ( is_wp_error( $created ) ) {
return $created;
}
CompanyMeta\save_company_meta( $created, $form_values );
PledgeMeta\save_pledge_meta( $created, $form_values );
// save teams contirbuted to as terms
return 'success';
@ -77,9 +89,9 @@ function process_form( array $form_values ) {
*
* @return int|WP_Error Post ID on success. Otherwise WP_Error.
*/
function create_new_company( $name ) {
function create_new_pledge( $name ) {
$args = [
'post_type' => Company\CPT_SLUG,
'post_type' => Pledge\CPT_ID,
'post_title' => $name,
'post_status' => 'pending',
'post_author' => get_current_user_id(), // TODO is this how we want to do this?

View file

@ -1,49 +1,52 @@
<?php
/**
* This file handles the operations related to registering and handling company meta values for the CPT.
* This file handles the operations related to registering and handling pledge meta values for the CPT.
*/
namespace WordPressDotOrg\FiveForTheFuture\CompanyMeta;
use WordPressDotOrg\FiveForTheFuture\Company;
namespace WordPressDotOrg\FiveForTheFuture\PledgeMeta;
use WordPressDotOrg\FiveForTheFuture;
use WordPressDotOrg\FiveForTheFuture\Pledge;
use WP_Post, WP_Error;
defined( 'WPINC' ) || die();
const META_PREFIX = '5ftf-';
const META_PREFIX = FiveForTheFuture\PREFIX . '-';
add_action( 'init', __NAMESPACE__ . '\register' );
add_action( 'admin_init', __NAMESPACE__ . '\add_meta_boxes' );
add_action( 'save_post', __NAMESPACE__ . '\save_pledge', 10, 2 );
/**
*
*/
function register() {
register_company_meta();
register_pledge_meta();
}
add_action( 'init', __NAMESPACE__ . '\register' );
/**
* Define company meta fields and their properties.
* Define pledge meta fields and their properties.
*
* @return array
*/
function get_company_meta_config() {
function get_pledge_meta_config() {
return [
'company-name' => [
'company-name' => [
'show_in_rest' => true,
'sanitize_callback' => 'sanitize_text_field',
'required' => true,
],
'company-url' => [
'company-url' => [
'show_in_rest' => true,
'sanitize_callback' => 'esc_url_raw',
'required' => true,
],
'company-email' => [
'company-email' => [
'show_in_rest' => false,
'sanitize_callback' => 'sanitize_email',
'required' => true,
],
'company-phone' => [
'company-phone' => [
'show_in_rest' => false,
'sanitize_callback' => 'sanitize_text_field',
'required' => false,
@ -54,22 +57,22 @@ function get_company_meta_config() {
'required' => true,
],
// todo add # sponsored employees here and also to form, etc
'contact-name' => [
'contact-name' => [
'show_in_rest' => false,
'sanitize_callback' => 'sanitize_text_field',
'required' => true,
],
'contact-wporg-username' => [
'contact-wporg-username' => [
'show_in_rest' => false,
'sanitize_callback' => 'sanitize_user',
'required' => true,
],
'pledge-hours' => [
'pledge-hours' => [
'show_in_rest' => true,
'sanitize_callback' => 'absint',
'required' => true,
],
'pledge-agreement' => [
'pledge-agreement' => [
'show_in_rest' => false,
'sanitize_callback' => 'wp_validate_boolean',
'required' => true,
@ -78,15 +81,15 @@ function get_company_meta_config() {
}
/**
* Register post meta keys for the Company post type.
* Register post meta keys for the custom post type.
*/
function register_company_meta() {
$meta = get_company_meta_config();
function register_pledge_meta() {
$meta = get_pledge_meta_config();
foreach ( $meta as $key => $args ) {
$meta_key = META_PREFIX . $key;
register_post_meta( Company\CPT_SLUG, $meta_key, $args );
register_post_meta( Pledge\CPT_ID, $meta_key, $args );
}
}
@ -97,32 +100,25 @@ function add_meta_boxes() {
add_meta_box(
'company-information',
__( 'Company Information', 'wordpressorg' ),
__NAMESPACE__ . '\markup_meta_boxes',
Company\CPT_SLUG,
__NAMESPACE__ . '\render_meta_boxes',
Pledge\CPT_ID,
'normal',
'default'
);
}
add_action( 'admin_init', __NAMESPACE__ . '\add_meta_boxes' );
/**
* Builds the markup for all meta boxes
*
* @param WP_Post $company
* @param WP_Post $pledge
* @param array $box
*/
function markup_meta_boxes( $company, $box ) {
/** @var $view string */
function render_meta_boxes( $pledge, $box ) {
switch ( $box['id'] ) {
case 'company-information':
$wporg_user = get_user_by( 'login', $company->_5ftf_wporg_username );
$avatar_url = $wporg_user ? get_avatar_url( $wporg_user->ID ) : false;
require dirname( __DIR__ ) . '/views/metabox-' . sanitize_file_name( $box['id'] ) . '.php';
break;
}
require_once( dirname( __DIR__ ) . '/views/metabox-' . sanitize_file_name( $box['id'] ) . '.php' );
}
/**
@ -130,12 +126,12 @@ function markup_meta_boxes( $company, $box ) {
*
* @return bool|WP_Error True if all required values are present.
*/
function has_required_company_meta( array $values ) {
$config = get_company_meta_config();
$plucked = wp_list_pluck( get_company_meta_config(), 'required' );
function has_required_pledge_meta( array $values ) {
$config = get_pledge_meta_config();
$plucked = wp_list_pluck( get_pledge_meta_config(), 'required' );
$required = array_combine( array_keys( $config ), $plucked );
$required_keys = array_keys( $required, true );
$required_keys = array_keys( $required, true, true );
$error = new WP_Error();
foreach ( $required_keys as $key ) {
@ -157,44 +153,41 @@ function has_required_company_meta( array $values ) {
}
/**
* Save the company data
* Save the pledge data
*
* @param int $company_id
* @param WP_Post $company
* @param int $pledge_id
* @param WP_Post $pledge
*/
function save_company( $company_id, $company ) {
function save_pledge( $pledge_id, $pledge ) {
$ignored_actions = array( 'trash', 'untrash', 'restore' );
if ( isset( $_GET['action'] ) && in_array( $_GET['action'], $ignored_actions ) ) {
if ( isset( $_GET['action'] ) && in_array( $_GET['action'], $ignored_actions, true ) ) {
return;
}
if ( ! $company || $company->post_type != Company\CPT_SLUG || ! current_user_can( 'edit_company', $company_id ) ) {
if ( ! $pledge || $pledge->post_type !== Pledge\CPT_ID || ! current_user_can( 'edit_pledge', $pledge_id ) ) {
return;
}
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || $company->post_status == 'auto-draft' ) {
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || $pledge->post_status === 'auto-draft' ) {
return;
}
if ( is_wp_error( has_required_company_meta( $_POST ) ) ) {
if ( is_wp_error( has_required_pledge_meta( $_POST ) ) ) {
return;
}
save_company_meta( $company_id, $_POST );
save_pledge_meta( $pledge_id, $_POST );
}
add_action( 'save_post', __NAMESPACE__ . '\save_company', 10, 2 );
/**
* Save the company's meta fields
* Save the pledge's meta fields
*
* @param int $company_id
* @param int $pledge_id
* @param array $new_values
*/
function save_company_meta( $company_id, $new_values ) {
$keys = array_keys( get_company_meta_config() );
function save_pledge_meta( $pledge_id, $new_values ) {
$keys = array_keys( get_pledge_meta_config() );
foreach ( $keys as $key ) {
$meta_key = META_PREFIX . $key;
@ -202,9 +195,9 @@ function save_company_meta( $company_id, $new_values ) {
if ( isset( $new_values[ $key ] ) ) {
// Since the sanitize callback is called during this function, it could still end up
// saving an empty value to the database.
update_post_meta( $company_id, $meta_key, $new_values[ $key ] );
update_post_meta( $pledge_id, $meta_key, $new_values[ $key ] );
} else {
delete_post_meta( $company_id, $meta_key );
delete_post_meta( $pledge_id, $meta_key );
}
}

View file

@ -0,0 +1,73 @@
<?php
/**
* This file handles the operations related to setting up a custom post type. We change change the filename, namespace,
* etc. once we have a better idea of what the CPT will be called.
*/
// todo normalize text domain across all files
namespace WordPressDotOrg\FiveForTheFuture\Pledge;
use WordPressDotOrg\FiveForTheFuture;
defined( 'WPINC' ) || die();
const SLUG = 'pledge';
const SLUG_PL = 'pledges';
const CPT_ID = FiveForTheFuture\PREFIX . '_' . SLUG;
add_action( 'init', __NAMESPACE__ . '\register_custom_post_type', 0 );
/**
* Register the post type.
*/
function register_custom_post_type() {
$labels = array(
'name' => _x( 'Pledges', 'Pledges General Name', 'wporg' ),
'singular_name' => _x( 'Pledge', 'Pledge Singular Name', 'wporg' ),
'menu_name' => __( 'Five for the Future', 'wporg' ),
'archives' => __( 'Pledge Archives', 'wporg' ),
'attributes' => __( 'Pledge Attributes', 'wporg' ),
'parent_item_colon' => __( 'Parent Pledge:', 'wporg' ),
'all_items' => __( 'All Pledges', 'wporg' ),
'add_new_item' => __( 'Add New Pledge', 'wporg' ),
'add_new' => __( 'Add New', 'wporg' ),
'new_item' => __( 'New Pledge', 'wporg' ),
'edit_item' => __( 'Edit Pledge', 'wporg' ),
'update_item' => __( 'Update Pledge', 'wporg' ),
'view_item' => __( 'View Pledge', 'wporg' ),
'view_items' => __( 'View Pledges', 'wporg' ),
'search_items' => __( 'Search Pledges', 'wporg' ),
'not_found' => __( 'Not found', 'wporg' ),
'not_found_in_trash' => __( 'Not found in Trash', 'wporg' ),
'insert_into_item' => __( 'Insert into pledge', 'wporg' ),
'uploaded_to_this_item' => __( 'Uploaded to this pledge', 'wporg' ),
'items_list' => __( 'Pledges list', 'wporg' ),
'items_list_navigation' => __( 'Pledges list navigation', 'wporg' ),
'filter_items_list' => __( 'Filter pledges list', 'wporg' ),
);
$args = array(
'labels' => $labels,
'supports' => array( 'title', 'thumbnail', 'author', 'revisions' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 25,
'show_in_admin_bar' => false,
'show_in_nav_menus' => false,
'can_export' => false,
'taxonomies' => array(),
'has_archive' => SLUG_PL,
'rewrite' => array(
'slug' => SLUG,
),
'exclude_from_search' => true,
'publicly_queryable' => true,
'capability_type' => 'page',
'show_in_rest' => false, // todo Maybe turn this on later.
);
register_post_type( CPT_ID, $args );
}

View file

@ -7,7 +7,7 @@
namespace WordPressDotOrg\FiveForTheFuture\Blocks;
use WordPressDotOrg\FiveForTheFuture;
use WordPressDotOrg\FiveForTheFuture\Company;
use WordPressDotOrg\FiveForTheFuture\Pledge;
defined( 'WPINC' ) || die();
@ -36,7 +36,7 @@ function enqueue_scripts() {
$params = array(
// explain 100 is just sanity limit to keep page size performant. might need to lazy-load more in the future
// maybe order by donated_employees, or rand, to ensure the top companies are always displayed first, or to make sure treta everyone equal
'post_type' => Company\CPT_SLUG,
'post_type' => Pledge\CPT_ID,
'post_status' => 'publish',
'posts_per_page' => 100,
'orderby' => 'title',

View file

@ -1,7 +1,7 @@
<?php
/**
* Plugin Name: Five For The Future
* Plugin URI: https://wordpress.org
* Plugin URI: https://wordpress.org/five-for-the-future/
* Description:
* Author: WordPress.org
* Author URI: https://wordpress.org
@ -9,19 +9,22 @@
*/
namespace WordPressDotOrg\FiveForTheFuture;
defined( 'WPINC' ) || die();
define( __NAMESPACE__ . '\PATH', plugin_dir_path( __FILE__ ) );
define( __NAMESPACE__ . '\URL', plugin_dir_url( __FILE__ ) );
const PREFIX = '5ftf';
add_action( 'plugins_loaded', __NAMESPACE__ . '\load' );
/**
*
*/
function load() {
require_once PATH . 'includes/company.php';
require_once PATH . 'includes/company-meta.php';
require_once PATH . 'includes/shortcodes.php';
require_once PATH . 'includes/pledge.php';
require_once PATH . 'includes/pledge-meta.php';
require_once PATH . 'includes/pledge-form.php';
require_once PATH . 'includes/shortcodes.php';
}
add_action( 'plugins_loaded', __NAMESPACE__ . '\load' );