From bdadceaa43ee712ae37890437e7e4a8aa53e0564 Mon Sep 17 00:00:00 2001 From: jorge-vitrubio Date: Fri, 23 Feb 2024 11:03:02 +0100 Subject: [PATCH] added espais associats as custom post type --- ...eld-associats-filter-function-frontend.php | 180 +++++++++++++ includes/custom-field-associats-filter.php | 84 ++++++ includes/custom-post-type-associats.php | 241 ++++++++++++++++++ 3 files changed, 505 insertions(+) create mode 100644 includes/custom-field-associats-filter-function-frontend.php create mode 100644 includes/custom-field-associats-filter.php create mode 100644 includes/custom-post-type-associats.php diff --git a/includes/custom-field-associats-filter-function-frontend.php b/includes/custom-field-associats-filter-function-frontend.php new file mode 100644 index 0000000..4d297a7 --- /dev/null +++ b/includes/custom-field-associats-filter-function-frontend.php @@ -0,0 +1,180 @@ + + +
+ + +
+ + $fieldname ) { + + // check for values in url and get value if available + if( isset($_GET[ $fieldname ]) ) { + $filteredvalues['value'] = explode(',', $_GET[ $fieldname ]); + } else { + $filteredvalues['value'] = []; + }; + //end check for values in url + + // construct the checkboxes + if( $fieldname == $fields['name']) { +?> + +
+
+ $choicelabel ) { ?> + +
+ value="" /> + +
+ + + +
+ + + + + + + + + + + +

';?> +

enviar

+
+ + + + +
+ + + + + diff --git a/includes/custom-field-associats-filter.php b/includes/custom-field-associats-filter.php new file mode 100644 index 0000000..ebc7520 --- /dev/null +++ b/includes/custom-field-associats-filter.php @@ -0,0 +1,84 @@ + field name) +$GLOBALS['my_query_filters_associat'] = array( + 'associatfield' => 'xxp_associat_field', //disciplines + 'associatterritory' => 'xxp_associat_territory', //territori + 'associatservice' => 'xxp_associat_service', //serveis + 'associatcenter' => 'xxp_associat_center' //centre + +); +$GLOBALS['my_query_filters_associat_dates'] = array( + 'associatapplybegin' => 'xxp_associat_apply_begin', + 'associatapplyend' => 'xxp_associat_apply_end' + //'associatcall' => 'xxp_associat_call', +); + +// action +add_action('pre_get_posts', 'my_pre_get_posts_associats',10,1); +if ( ! function_exists( 'my_pre_get_posts_associats' ) ){ + function my_pre_get_posts_associats( $query ) { + + // bail early if is in admin + if( is_admin() ) { return; } + + // bail early if not main query or plugin $the_query_associat + // - allows custom code / plugins to continue working + if( !$query->is_main_query() ) return; + + + + // loop over filters + foreach( $GLOBALS['my_query_filters_associat'] as $key => $fieldname) + { + // continue if not found in url + if( empty($_GET[ $fieldname ]) ) { continue; } + + // get original meta query + $meta_query = []; + $meta_query[] = $query->get('meta_query'); + $meta_query[] = array('relation' => 'OR'); + + // get the values for this filter + // eg: http://domain.tdl/associat/?xxp_associat_target=autonoma,entitat-publica + $filtervalues= explode(',', $_GET[ $fieldname ]); + + // loop retreived values from checkboxes and filter them with REGEXP + // http://domain.tdl/associat/?xxp_associat_target=autonoma&xxp_associat_field=circi + // http://domain.tdl/associat/?xxp_associat_target=autonoma,entitat-publica&xxp_associat_field=dansa + foreach( $filtervalues as $filteredvalue ) + { + // they are array based fields + // https://barn2.com/blog/querying-posts-by-custom-field-acf/#array-based-fields + $strippedvalue = sprintf( '^%1$s$|s:%2$u:"%1$s";', $filteredvalue, strlen( $filteredvalue ) ); + + // add our meta query to the original meta queries + $meta_query[] = array( + array( + 'key' => $fieldname, + 'value' => $strippedvalue, + 'compare' => 'REGEXP', + ), + ); + } + // update the meta query arguments + $query->set('meta_query', $meta_query); + } + + //always return + return; + } +} diff --git a/includes/custom-post-type-associats.php b/includes/custom-post-type-associats.php new file mode 100644 index 0000000..07ea253 --- /dev/null +++ b/includes/custom-post-type-associats.php @@ -0,0 +1,241 @@ + + * + * @internal never define functions inside callbacks. + * these functions could be run multiple times; this would result in a fatal error. + */ + +/** + * Start custom post types + * ---------------------------------------------------------------------------- + * https://developer.wordpress.org/plugins/post-types/registering-custom-post-types/ + * https://developer.wordpress.org/reference/functions/register_post_type/ + */ + + + // Register Custom Post Type +if ( ! function_exists('xarxaprod_wpplugin_custom_post_associat_init') ){ + function xarxaprod_wpplugin_custom_post_associat_init() { + + $labels = array( + 'name' => _x( 'Associats', 'Post Tipu General Name', 'text_domain' ), + 'singular_name' => _x( 'Associat', 'Post Tipu Singular Name', 'text_domain' ), + 'menu_name' => __( 'Associats', 'text_domain' ), + 'parent_item_colon' => __( 'Associats mare/pare:', 'text_domain' ), + 'all_items' => __( 'Tots els associats', 'text_domain' ), + 'view_item' => __( 'Mostra els associats', 'text_domain' ), + 'add_new_item' => __( 'Affegeix un associat', 'text_domain' ), + 'add_new' => __( 'Affegeix associat', 'text_domain' ), + 'edit_item' => __( 'Edita associat', 'text_domain' ), + 'update_item' => __( 'Desa associat', 'text_domain' ), + 'search_items' => __( 'Cerca associat', 'text_domain' ), + 'not_associat' => __( 'No hem pogut trovar ningĂșna associat', 'text_domain' ), + 'not_associat_in_trash' => __( 'No hem trovat cap associat a la brosa', 'text_domain' ) + ); + $rewrite = array( + 'slug' => 'associat' + ); + // https://wordpress.stackexchange.com/questions/108338/capabilities-and-custom-post-types#108375 + // https://developer.wordpress.org/reference/functions/register_post_type/#capability_type + //$capabilities = array( + // 'read' => 'read_associat', + // 'publish_posts' => 'publish_associat', + // 'edit_posts' => 'edit_associat', + // 'edit_published_posts' => 'edit_published_associat', + // 'edit_others_posts' => 'edit_others_associat', + // 'delete_posts' => 'delete_associat', + // 'delete_published_posts' => 'delete_published_associat', + // 'delete_others_posts' => 'delete_others_associat', + // 'delete_private_posts' => 'delete_private_associat' + // ); + $args = array( + 'label' => __( 'xarxaprod-associat', 'text_domain' ), + 'description' => __( 'Associats', 'text_domain' ), + 'labels' => $labels, + //'supports' => array( 'title', 'editor', 'thumbnail', 'trackbacks', 'revisions', 'custom-fields', 'page-attributes' ), + 'supports' => array( 'title', 'editor', 'trackbacks', 'revisions', 'custom-fields', 'page-attributes' ), + // 'taxonomies' => array( + // 'xarxaprod-associat-category', + // 'xarxaprod-associat-tag' + // ), + 'hierarchical' => true, + 'public' => true, + 'show_ui' => true, + 'show_in_menu' => true, + 'show_in_nav_menus' => true, + 'show_in_admin_bar' => true, + 'show_in_rest' => true, + 'menu_position' => 5, + 'menu_icon' => 'dashicons-calendar', + 'can_export' => true, + 'has_archive' => true, + 'exclude_from_search' => false, + 'publicly_queryable' => true, + 'capability_type' => 'post', + 'rewrite' => $rewrite, + //'capabilities' => $capabilities, + //'map_meta_cap' => true //neded to apply the new capabilities. + ); + register_post_type( 'xarxaprod-associat', $args ); + } +}// end function_exists xarxaprod_wpplugin_custom_post_associat_init + // Hook into the 'init' action + add_action( 'init', 'xarxaprod_wpplugin_custom_post_associat_init', 0 ); + + + // flush rewrite and when changing theme or plugin +// https://developer.wordpress.org/reference/functions/register_post_type/#flushing-rewrite-on-activation +if ( ! function_exists( 'xarxaprod_associat_rewrite_flush') ){ + function xarxaprod_associat_rewrite_flush() { + xarxaprod_wpplugin_custom_post_associat_init(); + flush_rewrite_rules(); + } +}// end function_exists xarxaprod_associat_rewrite_flush + add_action( 'after_switch_theme', 'xarxaprod_associat_rewrite_flush' ); + + // give capabilities once the custom post id activated +// function xarxaprod_wpplugin_add_caps() { + // gets the XX role + // $admins = get_role( '' ); + // $admins->add_cap( ); + // gets the administrator role + + +//} +//add_action( 'admin_init', 'xarxaprod_wpplugin_add_caps'); + + + +/** + * Start custom taxonomies + * ---------------------------------------------------------------------------- + * https://developer.wordpress.org/plugins/taxonomies/working-with-custom-taxonomies/ + * https://developer.wordpress.org/reference/functions/register_taxonomy/ + */ + + // Register Custom Taxonomy Category for Associats + //function xarxaprod_wpplugin_associat_category_register() { + + // $labels = array( + // 'name' => _x( 'Associats Tipus', 'Taxonomy General Name', 'text_domain' ), + // 'singular_name' => _x( 'Tipus d`associat', 'Taxonomy Singular Name', 'text_domain' ), + // 'menu_name' => __( 'Associats Tipus', 'text_domain' ), + // 'all_items' => __( 'All type associat', 'text_domain' ), + // 'parent_item' => __( 'Parent type associat', 'text_domain' ), + // 'parent_item_colon' => __( 'Parent type associat:', 'text_domain' ), + // 'new_item_name' => __( 'New type associat', 'text_domain' ), + // 'add_new_item' => __( 'Add New type associat', 'text_domain' ), + // 'edit_item' => __( 'Edit type associat', 'text_domain' ), + // 'update_item' => __( 'Update type of associat', 'text_domain' ), + // 'separate_items_with_commas' => __( 'Separate type associat with commas', 'text_domain' ), + // 'search_items' => __( 'Search type of associat', 'text_domain' ), + // 'add_or_remove_items' => __( 'Add or remove type of associat', 'text_domain' ), + // 'choose_from_most_used' => __( 'Choose from the most used type of associat', 'text_domain' ), + // 'not_associat' => __( 'Tipus d`associat Not Associat', 'text_domain' ) + // ); + // $rewrite = array( + // 'slug' => 'associat-tipus' + // ); + // //$capabilities = array( + // // 'manage_terms' => 'Xarxaprod Manage associat Tipus', + // // 'edit_terms' => 'Xarxaprod Edit associat Tipus', + // // 'delete_terms' => 'Xarxaprod Delete associat Tipus', + // // 'assign_terms' => 'Xarxaprod Assign associat Tipus', + // // ); + // $args = array( + // 'labels' => $labels, + // 'hierarchical' => true, + // 'public' => true, + // 'show_ui' => true, + // 'show_admin_column' => true, + // 'show_in_nav_menus' => true, + // 'show_tagcloud' => false, + // 'rewrite' => $rewrite, + // //'capabilities' => $capabilities, + // 'map_meta_cap' => true //neded to apply the new capabilities. + + // ); + // register_taxonomy( 'xarxaprod-associat-category', array( 'xarxaprod-associat' ) , $args ); + //} + + // Hook into the 'init' action + //add_action( 'init', 'xarxaprod_wpplugin_associat_category_register', 0 ); + + // Register Custom Taxonomy Tag for Associats +// function xarxaprod_wpplugin_associat_tag_register() { +// +// $labels = array( +// 'name' => _x( 'Associats Tags', 'Taxonomy General Name', 'text_domain' ), +// 'singular_name' => _x( 'Associat Tag', 'Taxonomy Singular Name', 'text_domain' ), +// 'menu_name' => __( 'Associats Tags', 'text_domain' ), +// 'all_items' => __( 'All associat tags', 'text_domain' ), +// 'parent_item' => __( 'Parent associat tag', 'text_domain' ), +// 'parent_item_colon' => __( 'Parent associat tag:', 'text_domain' ), +// 'new_item_name' => __( 'New associat tag', 'text_domain' ), +// 'add_new_item' => __( 'Add New associat tag', 'text_domain' ), +// 'edit_item' => __( 'Edit associat tag', 'text_domain' ), +// 'update_item' => __( 'Update associat tag', 'text_domain' ), +// 'separate_items_with_commas' => __( 'Separate associat tag with commas', 'text_domain' ), +// 'search_items' => __( 'Search associat tags', 'text_domain' ), +// 'add_or_remove_items' => __( 'Add or remove associat tag', 'text_domain' ), +// 'choose_from_most_used' => __( 'Choose from the most used associat tags', 'text_domain' ), +// 'not_associat' => __( 'Associat tag Not Associat', 'text_domain' ), +// ); +// $rewrite = array( +// 'slug' => 'associat-tag', +// // 'with_front' => false, +// ); +// //$capabilities = array( +// // 'manage_terms' => 'Xarxaprod Manage associat Tag', +// // 'edit_terms' => 'Xarxaprod Edit associat Tag', +// // 'delete_terms' => 'Xarxaprod Delete associat Tag', +// // 'assign_terms' => 'Xarxaprod Assign associat Tag', +// // ); +// +// $args = array( +// 'labels' => $labels, +// 'hierarchical' => false, +// 'public' => true, +// 'show_ui' => true, +// 'show_admin_column' => true, +// 'show_in_nav_menus' => true, +// 'show_tagcloud' => true, +// 'rewrite' => $rewrite, +// //'capabilities' => $capabilities, +// 'map_meta_cap' => true //neded to apply the new capabilities. +// ); +// register_taxonomy( 'xarxaprod-associat-tag', array( 'xarxaprod-associat' ) , $args ); +// } + + // Hook into the 'init' action + //add_action( 'init', 'xarxaprod_wpplugin_associat_tag_register', 0 ); + + // changing the permalink + // https://wordpress.stackexchange.com/questions/108642/permalinks-custom-post-type-custom-taxonomy-post + // change in register_post_type the slug 'rewrite' to this + // $rewrite = array( + // 'slug' => 'associat/%xarxaprod-associat-category%', + // 'with_front' => true, + // 'hierarchical' => true, + // ); + // uncomment below + // + // function xarxaprod_wpplugin_associat_and_category_permalink( $post_link, $id = 0 ){ + // $post = get_post($id); + // if ( is_object( $post ) && $post->post_type == 'xarxaprod-associat' ){ + // $terms = wp_get_object_terms( $post->ID, 'xarxaprod-associat-category' ); + // if( $terms ){ + // return str_replace( '%xarxaprod-associat-category%' , $terms[0]->slug , $post_link ); + // } + // } + // return $post_link; + // } + // add_filter( 'post_type_link', 'xarxaprod_wpplugin_associat_and_category_permalink', 1, 2 );