self::POST_TYPE, 'orderby' => 'menu_order', 'order' => 'ASC', 'posts_per_page' => -1, 'suppress_filters' => false, ] ); if ( ! empty( $banks ) ) { foreach ( $banks as &$post ) { $post->acf = get_fields( $post->ID ); } unset( $post ); } } if ( $forceArrays ) { $arrays = $banks; array_walk( $arrays, function ( &$val ) { $val = (array) $val; } ); return $arrays; } return $banks; } public static function get_query( $zip ) { static $queries; if ( empty( $zip ) || ! ( $canton = DB::get_canton_by_zip( $zip ) ) ) { return false; } if ( is_array( $queries ) && array_key_exists( $canton['ID'], $queries ) ) { $queries[$canton['ID']]->rewind_posts(); return $queries[$canton['ID']]; } $query = new \WP_Query( [ 'post_type' => self::POST_TYPE, 'orderby' => 'menu_order', 'order' => 'ASC', 'posts_per_page' => -1, 'suppress_filters' => false, 'meta_query' => [ [ 'key' => 'bank_cantons', 'value' => '"' . $canton['ID'] . '"', 'compare' => 'LIKE' ] ], ] ); if ( $query->have_posts() ) { foreach ( $query->posts as &$post ) { $post->acf = get_fields( $post->ID ); } unset( $post ); } $queries[$canton['ID']] = $query; return $query; } public static function fetch_company_type( \WP_Post $post, array $all_data, $lang = null ) { if ( null === $lang ) { $lang = FQP::get_curr_lang(); } $entry_types = DB::get_entry_types(); $entry_type = $all_data['step1']['entry_type']; $index = "type_{$entry_type}"; if ('y' == $entry_types[$entry_type]['hasBacking']) { $backing_type = $all_data['step1']['backing_type']; $index .= "_{$backing_type}"; } if ( ! array_key_exists( $index, $post->acf['bank_company_types']) ) { return false; } return $post->acf['bank_company_types'][$index]; } public static function fetch_discounts( \WP_Post $post ) { $discounts = []; foreach ( $post->acf['bank_company_types'] as $key => $data ) { $key = str_replace( 'type', '$discounts', $key ); $key = preg_replace( '/_(\d+)_(\d+)$/', '[$1][$2]', $key); // ag, gmbh $key = preg_replace( '/_(\d+)$/', '[$1]', $key); // ef, kg $key .= " = {$data['discount']};"; eval($key); } return $discounts; } public static function register_post_type() { register_post_type( self::POST_TYPE, [ 'labels' => array( 'name' => 'Cantonal Partners', 'singular_name' => 'Cantonal Partner', 'add_new' => 'Add New Cantonal Partner', 'add_new_item' => 'New Cantonal Partner', 'edit_item' => 'Edit Cantonal Partner', 'new_item' => 'New Cantonal Partner', 'view_item' => 'View Cantonal Partner', 'search_items' => 'Search Cantonal Partner', 'not_found' => 'Cantonal Partner was not found', 'not_found_in_trash' => 'No Cantonal Partner was found in Trash', 'parent_item_colon' => '', 'menu_name' => 'Cantonal Partners', ), 'description' => '', 'public' => false, 'show_ui' => true, 'show_in_menu' => false, 'capability_type' => 'post', 'hierarchical' => false, 'supports' => ['title', 'page-attributes'], 'taxonomies' => [], 'has_archive' => false, 'menu_icon' => plugins_url( 'img/icon.png', FQP_PLUGIN_BASE_NAME ), ] ); } public static function acf_load_cantons_field_choices( $field ) { $field['choices'] = []; if ( $cantons = DB::get_cantons() ) { $field['choices'] = $cantons; } return $field; } public static function acf_load_partners_field_choices( $field ) { $field['choices'] = []; if ( $partners = DB::get_partners() ) { foreach ($partners as $partner) { $field['choices'][$partner->ID] = $partner->post_title; } } return $field; } public static function enqueue_scripts( array $script_options ) { //wp_enqueue_script( 'fasoon_quote_step6_products', plugins_url( '/js/fasoon-quote-step6-products.js', FQP_PLUGIN_BASE_NAME ), [ 'jquery', 'fasoon_quote_step6' ] ); //wp_localize_script( 'fasoon_quote_step6_products', 'fasoon_quote_step6_products_vars', $script_options ); } public static function admin_list() { add_filter( 'manage_' . self::POST_TYPE . '_posts_columns', [ self::class, 'posts_columns' ] ); // add custom column add_filter( 'manage_edit-' . self::POST_TYPE . '_sortable_columns', [ self::class, 'sortable_columns' ] ); // mark custom column as sorted add_action( 'manage_' . self::POST_TYPE . '_posts_custom_column' , [ self::class, 'posts_custom_column' ] ); // output custom column value add_action( 'pre_get_posts', [ self::class, 'posts_orderby' ] ); // change default order } public static function posts_columns( $columns ) { $last_value = end( $columns ); $last_key = key( $columns ); unset( $columns[$last_key] ); $columns['menu_order'] = fqp__( 'Order' ); $columns[$last_key] = $last_value; return $columns; } public static function sortable_columns( $columns ) { $columns['menu_order'] = 'menu_order'; return $columns; } public static function posts_custom_column( $column ) { global $post; switch ( $column ) { case 'menu_order': echo $post->menu_order; break; } } public static function posts_orderby( \WP_Query $query ) { if ( ! $query->is_main_query() || self::POST_TYPE != $query->get( 'post_type' ) ) { return; // Nothing to do } $orderby = strtolower( $query->get( 'orderby' ) ); if ( in_array( $orderby, [ '', 'menu_order' ] ) ) { $order = ( 'DESC' == strtoupper( $query->get('order') ) ) ? 'DESC' : 'ASC'; $query->set( 'orderby', 'menu_order' ); $query->set( 'order', $order ); } } }