From 9bf2f9dd1a39ad890abe643c39abba2610d87d31 Mon Sep 17 00:00:00 2001 From: Corey McKrill <916023+coreymckrill@users.noreply.github.com> Date: Mon, 30 Sep 2019 17:04:49 -0700 Subject: [PATCH] Update company to pledge, some code cleanup --- plugins/wporg-5ftf/includes/company.php | 136 ------------------ plugins/wporg-5ftf/includes/pledge-form.php | 36 +++-- .../{company-meta.php => pledge-meta.php} | 105 +++++++------- plugins/wporg-5ftf/includes/pledge.php | 73 ++++++++++ plugins/wporg-5ftf/includes/shortcodes.php | 4 +- plugins/wporg-5ftf/index.php | 15 +- 6 files changed, 157 insertions(+), 212 deletions(-) delete mode 100755 plugins/wporg-5ftf/includes/company.php rename plugins/wporg-5ftf/includes/{company-meta.php => pledge-meta.php} (61%) create mode 100755 plugins/wporg-5ftf/includes/pledge.php diff --git a/plugins/wporg-5ftf/includes/company.php b/plugins/wporg-5ftf/includes/company.php deleted file mode 100755 index 923c162..0000000 --- a/plugins/wporg-5ftf/includes/company.php +++ /dev/null @@ -1,136 +0,0 @@ - _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 -} diff --git a/plugins/wporg-5ftf/includes/pledge-form.php b/plugins/wporg-5ftf/includes/pledge-form.php index 3584c1d..5d2f13b 100755 --- a/plugins/wporg-5ftf/includes/pledge-form.php +++ b/plugins/wporg-5ftf/includes/pledge-form.php @@ -1,13 +1,27 @@ 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? ]; return wp_insert_post( $args, true ); -} \ No newline at end of file +} diff --git a/plugins/wporg-5ftf/includes/company-meta.php b/plugins/wporg-5ftf/includes/pledge-meta.php similarity index 61% rename from plugins/wporg-5ftf/includes/company-meta.php rename to plugins/wporg-5ftf/includes/pledge-meta.php index 6543c85..f1533d0 100755 --- a/plugins/wporg-5ftf/includes/company-meta.php +++ b/plugins/wporg-5ftf/includes/pledge-meta.php @@ -1,49 +1,52 @@ [ + '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 ); } } diff --git a/plugins/wporg-5ftf/includes/pledge.php b/plugins/wporg-5ftf/includes/pledge.php new file mode 100755 index 0000000..fd4d8d2 --- /dev/null +++ b/plugins/wporg-5ftf/includes/pledge.php @@ -0,0 +1,73 @@ + _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 ); +} diff --git a/plugins/wporg-5ftf/includes/shortcodes.php b/plugins/wporg-5ftf/includes/shortcodes.php index eb6680d..ada8a10 100755 --- a/plugins/wporg-5ftf/includes/shortcodes.php +++ b/plugins/wporg-5ftf/includes/shortcodes.php @@ -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', diff --git a/plugins/wporg-5ftf/index.php b/plugins/wporg-5ftf/index.php index 6ef2b64..2c083a8 100755 --- a/plugins/wporg-5ftf/index.php +++ b/plugins/wporg-5ftf/index.php @@ -1,7 +1,7 @@