Changeset 2851530
- Timestamp:
- 01/19/2023 10:42:51 PM (19 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
wp-inci/tags/1.6.0/blocks/product.php
r2809050 r2851530 10 10 */ 11 11 12 12 13 use Carbon_Fields\Block; 13 14 use Carbon_Fields\Carbon_Fields; 14 15 use Carbon_Fields\Field; 16 15 17 16 18 require_once WPINCI_BASE_PATH . '/public/class-wp-inci-frontend.php'; … … 21 23 * @return void 22 24 */ 23 function Wi_Load_cb() 24 { 25 Carbon_Fields::boot(); 25 function wi_load_cb() { 26 Carbon_Fields::boot(); 26 27 } 27 28 28 add_action( 'after_setup_theme', 'Wi_Load_cb');29 add_action(); 29 30 30 31 /** … … 33 34 * @return array 34 35 */ 35 function Wi_Get_products(): array 36 { 36 function wi_get_products() { 37 37 38 $results = [];38 ; 39 39 40 41 'post_type'=> array( 'product' ),42 43 'order'=> 'ASC',44 'ordeby' => 'title' 45 40 $args = array( 41 => array( 'product' ), 42 'post_status' => 'publish', 43 => 'ASC', 44 'ordeby' => 'title', 45 ); 46 46 47 $the_query = new WP_Query($args);47 ); 48 48 49 if ($the_query->have_posts() ) {49 $the_query->have_posts() ) { 50 50 51 while ( $the_query->have_posts() ) { 52 $the_query->the_post(); 53 $results[ get_the_ID() ] = get_the_title(); 54 } 51 while ( $the_query->have_posts() ) { 52 $the_query->the_post(); 53 $results[ get_the_ID() ] = get_the_title(); 54 } 55 } 55 56 56 } 57 wp_reset_postdata(); 57 58 58 wp_reset_postdata(); 59 60 return $results; 59 return $results; 61 60 } 62 61 … … 66 65 * @return void 67 66 */ 68 function Wi_Product_block()69 { 70 Block::make(__('Product', 'wp-inci')) 71 ->add_fields(72 [ 73 Field::make('text', 'title', __('Custom title', 'wp-inci'))->set_help_text('Leave blank to show the product title'),74 Field::make('select', 'product', __('Select product', 'wp-inci'))->add_options('wi_get_products'),75 Field::make('checkbox', 'linked', __('Show link', 'wp-inci'))->set_option_value('yes'),76 Field::make('checkbox', 'list', __('Hide ingredient list', 'wp-inci'))->set_option_value('yes'), 77 Field::make('checkbox', 'safety', __('Hide safety', 'wp-inci'))->set_option_value('yes')->set_conditional_logic(78 [ 79 [ 80 'field' => 'list',81 'value' => false,82 ] 83 ] 84 ), 85 ] 86 87 ->set_category('wp-inci')88 ->set_icon('wp-inci') 89 90 function ( 91 $fields 92 ) {93 ?>67 function 68 Block::make( __( 'Product', 'wp-inci' ) ) 69 ->add_fields( 70 ( 71 Field::make( 'text', 'title', __( 'Custom title', 'wp-inci' ) )->set_help_text( 'Leave blank to show the product title' ), 72 ), 73 ), 74 ), 75 Field::make( 'checkbox', 'safety', __( 'Hide safety', 'wp-inci' ) )->set_option_value( 'yes' )->set_conditional_logic( 76 ( 77 array( 78 'field' => 'list', 79 , 80 , 81 ) 82 ), 83 ) 84 ) 85 ) 86 ) 87 // @codingStandardsIgnoreStart 88 ->set_render_callback( 89 function ( $fields, $attributes, $inner_blocks ) { 90 $output = '<div class="block"> 91 92 94 93 95 <div class="block"> 94 $start = '<h3>'; 95 $end = '</h3>'; 96 $title = esc_html( get_the_title( $fields['product'] ) ); 96 97 97 <div class="block__product"> 98 <div class="wp-inci"> 99 <?php 100 $start = '<h3>'; 101 $end = '</h3>'; 102 $title = esc_html(get_the_title($fields['product'])); 98 if ( isset( $fields['title'] ) && '' !== $fields['title'] ) { 99 $title = esc_html( $fields['title'] ); 100 } 103 101 104 if (isset($fields['title']) && '' !== $fields['title'] ) { 105 $title = esc_html($fields['title']); 106 } 102 if ( $fields['linked'] ) { 103 $start = '<h3><a title="' . $title . '" href="' . get_permalink( $fields['product'] ) . '">'; 104 $end = '</a></h3>'; 105 } 107 106 108 if ($fields['linked'] ) { 109 $start = '<h3><a title="' . $title . '" href="' . get_permalink($fields['product']) . '">'; 110 $end = '</a></h3>'; 111 } 107 $output .= $start . $title . $end; 112 108 113 echo $start . $title . $end;109 ; 114 110 115 $safety = 'true'; 111 if ( isset( $fields['safety'] ) && true === $fields['safety'] ) { 112 $safety = 'false'; 113 } 116 114 117 if (isset($fields['safety']) && true === $fields['safety'] ) { 118 $safety = 'false'; 119 } 115 if ( ! $fields['list'] ) { 116 $output .= ( Wp_Inci_Frontend::get_instanceFrontend() )->getIngredientsTable( 117 $fields['product'], 118 $safety 119 ); 120 } 121 $output .= '</div> 122 </div> 123 </div>'; 120 124 121 if (! $fields['list'] ) { 122 echo ( Wp_Inci_Frontend::getInstanceFrontend() )->getIngredientsTable( 123 $fields['product'], 124 $safety 125 ); 126 } 127 ?> 128 </div> 129 </div> 130 </div> 131 132 <?php 133 } 134 ); 125 echo $output; 126 } 127 // @codingStandardsIgnoreEnd 128 ); 135 129 } 136 130 137 add_action( 'carbon_fields_register_fields', 'Wi_Product_block');131 add_action();
Note: See TracChangeset
for help on using the changeset viewer.