From 4cd884163b6dfbc7fe8bcd9a518d71ac67394b6a Mon Sep 17 00:00:00 2001 From: jorge-vitrubio Date: Thu, 18 Jan 2024 12:15:33 +0100 Subject: [PATCH] added post type convo --- ...-field-convos-filter-function-frontend.php | 180 +++++++++++++ includes/custom-field-convos-filter.php | 81 ++++++ includes/custom-post-type-convos.php | 237 ++++++++++++++++++ xarxaprod-wp-plugin.php | 8 + 4 files changed, 506 insertions(+) create mode 100644 includes/custom-field-convos-filter-function-frontend.php create mode 100644 includes/custom-field-convos-filter.php create mode 100644 includes/custom-post-type-convos.php diff --git a/includes/custom-field-convos-filter-function-frontend.php b/includes/custom-field-convos-filter-function-frontend.php new file mode 100644 index 0000000..6a06105 --- /dev/null +++ b/includes/custom-field-convos-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-convos-filter.php b/includes/custom-field-convos-filter.php new file mode 100644 index 0000000..ae75fce --- /dev/null +++ b/includes/custom-field-convos-filter.php @@ -0,0 +1,81 @@ + field name) +$GLOBALS['my_query_filters'] = array( + 'calltarget' => 'os_call_target', + 'callsource' => 'os_call_source', + 'callfield' => 'os_call_field' +); +$GLOBALS['my_query_filters_dates'] = array( + 'callapplybegin' => 'os_call_apply_begin', + 'callapplyend' => 'os_call_apply_end' + //'callcall' => 'os_call_call', +); + +// action +add_action('pre_get_posts', 'my_pre_get_posts',10,1); + +function my_pre_get_posts( $query ) { + + // bail early if is in admin + if( is_admin() ) { return; } + + // bail early if not main query or plugin $the_query_convo + // - allows custom code / plugins to continue working + if( !$query->is_main_query() ) return; + + + + // loop over filters + foreach( $GLOBALS['my_query_filters'] 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/convo/?os_call_target=autonoma,entitat-publica + $filtervalues= explode(',', $_GET[ $fieldname ]); + + // loop retreived values from checkboxes and filter them with REGEXP + // http://domain.tdl/convo/?os_call_target=autonoma&os_call_field=circi + // http://domain.tdl/convo/?os_call_target=autonoma,entitat-publica&os_call_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-convos.php b/includes/custom-post-type-convos.php new file mode 100644 index 0000000..6428ead --- /dev/null +++ b/includes/custom-post-type-convos.php @@ -0,0 +1,237 @@ + + * + * @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 + function xarxaprod_wpplugin_custom_post_convocatoria_init() { + + $labels = array( + 'name' => _x( 'Convocatories', 'Post Tipu General Name', 'text_domain' ), + 'singular_name' => _x( 'Convocatoria', 'Post Tipu Singular Name', 'text_domain' ), + 'menu_name' => __( 'Convocatories', 'text_domain' ), + 'parent_item_colon' => __( 'Convocatories mare/pare:', 'text_domain' ), + 'all_items' => __( 'Totas las convocatories', 'text_domain' ), + 'view_item' => __( 'Mostra la convocatoria', 'text_domain' ), + 'add_new_item' => __( 'Affegeix una convocatoria', 'text_domain' ), + 'add_new' => __( 'Affegeix convocatoria', 'text_domain' ), + 'edit_item' => __( 'Edita convocatoria', 'text_domain' ), + 'update_item' => __( 'Desa convocatoria', 'text_domain' ), + 'search_items' => __( 'Cerca convocatoria', 'text_domain' ), + 'not_convocatoria' => __( 'No hem pogut trovar ningĂșn convocatoria', 'text_domain' ), + 'not_convocatoria_in_trash' => __( 'No hem trovat cap convocatoria a la brosa', 'text_domain' ) + ); + $rewrite = array( + 'slug' => 'convocatories' + ); + // 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_convocatoria', + // 'publish_posts' => 'publish_convocatoria', + // 'edit_posts' => 'edit_convocatoria', + // 'edit_published_posts' => 'edit_published_convocatoria', + // 'edit_others_posts' => 'edit_others_convocatoria', + // 'delete_posts' => 'delete_convocatoria', + // 'delete_published_posts' => 'delete_published_convocatoria', + // 'delete_others_posts' => 'delete_others_convocatories', + // 'delete_private_posts' => 'delete_private_convocatories' + // ); + $args = array( + 'label' => __( 'xarxaprod-convo', 'text_domain' ), + 'description' => __( 'Convocatories', '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-convo-category', + // 'xarxaprod-convo-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' => 6, + 'menu_icon' => 'dashicons-calendar', //https://developer.wordpress.org/resource/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-convo', $args ); + } + // Hook into the 'init' action + add_action( 'init', 'xarxaprod_wpplugin_custom_post_convocatoria_init', 0 ); + + + // flush rewrite and when changing theme or plugin + // https://developer.wordpress.org/reference/functions/register_post_type/#flushing-rewrite-on-activation + function xarxaprod_convocatoria_rewrite_flush() { + xarxaprod_wpplugin_custom_post_convocatoria_init(); + flush_rewrite_rules(); + } + add_action( 'after_switch_theme', 'xarxaprod_convocatoria_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 Convocatories + //function xarxaprod_wpplugin_convocatoria_category_register() { + + // $labels = array( + // 'name' => _x( 'Convocatories Tipus', 'Taxonomy General Name', 'text_domain' ), + // 'singular_name' => _x( 'Tipus d`convocatoria', 'Taxonomy Singular Name', 'text_domain' ), + // 'menu_name' => __( 'Convocatories Tipus', 'text_domain' ), + // 'all_items' => __( 'All type convocatories', 'text_domain' ), + // 'parent_item' => __( 'Parent type convocatoria', 'text_domain' ), + // 'parent_item_colon' => __( 'Parent type convocatoria:', 'text_domain' ), + // 'new_item_name' => __( 'New type convocatoria', 'text_domain' ), + // 'add_new_item' => __( 'Add New type convocatoria', 'text_domain' ), + // 'edit_item' => __( 'Edit type convocatoria', 'text_domain' ), + // 'update_item' => __( 'Update type of convocatoria', 'text_domain' ), + // 'separate_items_with_commas' => __( 'Separate type convocatoria with commas', 'text_domain' ), + // 'search_items' => __( 'Search type of convocatoria', 'text_domain' ), + // 'add_or_remove_items' => __( 'Add or remove type of convocatoria', 'text_domain' ), + // 'choose_from_most_used' => __( 'Choose from the most used type of convocatoria', 'text_domain' ), + // 'not_convocatoria' => __( 'Tipus d`convocatoria Not Convocatoria', 'text_domain' ) + // ); + // $rewrite = array( + // 'slug' => 'convocatoria-tipus' + // ); + // //$capabilities = array( + // // 'manage_terms' => 'Xarxaprod Manage convocatories Tipus', + // // 'edit_terms' => 'Xarxaprod Edit convocatoria Tipus', + // // 'delete_terms' => 'Xarxaprod Delete convocatories Tipus', + // // 'assign_terms' => 'Xarxaprod Assign convocatories 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-convo-category', array( 'xarxaprod-convo' ) , $args ); + //} + + // Hook into the 'init' action + //add_action( 'init', 'xarxaprod_wpplugin_convocatoria_category_register', 0 ); + + // Register Custom Taxonomy Tag for Convocatories +// function xarxaprod_wpplugin_convocatoria_tag_register() { +// +// $labels = array( +// 'name' => _x( 'Convocatories Tags', 'Taxonomy General Name', 'text_domain' ), +// 'singular_name' => _x( 'Convocatoria Tag', 'Taxonomy Singular Name', 'text_domain' ), +// 'menu_name' => __( 'Convocatories Tags', 'text_domain' ), +// 'all_items' => __( 'All convocatories tags', 'text_domain' ), +// 'parent_item' => __( 'Parent convocatoria tag', 'text_domain' ), +// 'parent_item_colon' => __( 'Parent convocatoria tag:', 'text_domain' ), +// 'new_item_name' => __( 'New convocatoria tag', 'text_domain' ), +// 'add_new_item' => __( 'Add New convocatoria tag', 'text_domain' ), +// 'edit_item' => __( 'Edit convocatoria tag', 'text_domain' ), +// 'update_item' => __( 'Update convocatoria tag', 'text_domain' ), +// 'separate_items_with_commas' => __( 'Separate convocatoria tag with commas', 'text_domain' ), +// 'search_items' => __( 'Search convocatoria tags', 'text_domain' ), +// 'add_or_remove_items' => __( 'Add or remove convocatoria tag', 'text_domain' ), +// 'choose_from_most_used' => __( 'Choose from the most used convocatoria tags', 'text_domain' ), +// 'not_convocatoria' => __( 'Convocatoria tag Not Convocatoria', 'text_domain' ), +// ); +// $rewrite = array( +// 'slug' => 'convocatoria-tag', +// // 'with_front' => false, +// ); +// //$capabilities = array( +// // 'manage_terms' => 'Xarxaprod Manage convocatories Tag', +// // 'edit_terms' => 'Xarxaprod Edit convocatoria Tag', +// // 'delete_terms' => 'Xarxaprod Delete convocatories Tag', +// // 'assign_terms' => 'Xarxaprod Assign convocatories 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-convo-tag', array( 'xarxaprod-convo' ) , $args ); +// } + + // Hook into the 'init' action + //add_action( 'init', 'xarxaprod_wpplugin_convocatoria_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' => 'convocatories/%xarxaprod-convo-category%', + // 'with_front' => true, + // 'hierarchical' => true, + // ); + // uncomment below + // + // function xarxaprod_wpplugin_convocatoria_and_category_permalink( $post_link, $id = 0 ){ + // $post = get_post($id); + // if ( is_object( $post ) && $post->post_type == 'xarxaprod-convo' ){ + // $terms = wp_get_object_terms( $post->ID, 'xarxaprod-convo-category' ); + // if( $terms ){ + // return str_replace( '%xarxaprod-convo-category%' , $terms[0]->slug , $post_link ); + // } + // } + // return $post_link; + // } + // add_filter( 'post_type_link', 'xarxaprod_wpplugin_convocatoria_and_category_permalink', 1, 2 ); diff --git a/xarxaprod-wp-plugin.php b/xarxaprod-wp-plugin.php index e568c1c..c4bf000 100644 --- a/xarxaprod-wp-plugin.php +++ b/xarxaprod-wp-plugin.php @@ -67,10 +67,18 @@ include( XARXAPROD_WPPLUGIN_PATH . 'includes/stylesheet-admin.php'); //include( XARXAPROD_WPPLUGIN_PATH . 'includes/stylesheet-public.php'); } +// post type ajuts include( XARXAPROD_WPPLUGIN_PATH . 'includes/custom-post-type-ajuts.php'); include( XARXAPROD_WPPLUGIN_PATH . 'includes/custom-field-ajuts-filter.php'); include( XARXAPROD_WPPLUGIN_PATH . 'includes/custom-field-ajuts-filter-function-frontend.php'); +// post type convocatories +include( XARXAPROD_WPPLUGIN_PATH . 'includes/custom-post-type-convos.php'); + +//include( XARXAPROD_WPPLUGIN_PATH . 'includes/custom-field-convos-filter.php'); +// +//include( XARXAPROD_WPPLUGIN_PATH . 'includes/custom-field-convos-filter-function-frontend.php'); + include( XARXAPROD_WPPLUGIN_PATH . 'includes/register-plugin-scripts.php');