Plugin Directory

Changeset 2851530

Timestamp:
01/19/2023 10:42:51 PM (19 months ago)
Author:
xlthlx
Message:

Version 1.6.0 fix

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wp-inci/tags/1.6.0/blocks/product.php

    r2809050 r2851530  
    1010 */
    1111
     12
    1213use Carbon_Fields\Block;
    1314use Carbon_Fields\Carbon_Fields;
    1415use Carbon_Fields\Field;
     16
    1517
    1618require_once WPINCI_BASE_PATH . '/public/class-wp-inci-frontend.php';
     
    2123 * @return void
    2224 */
    23 function Wi_Load_cb()
    24 {
    25     Carbon_Fields::boot();
     25function wi_load_cb() {
     26     Carbon_Fields::boot();
    2627}
    2728
    28 add_action('after_setup_theme', 'Wi_Load_cb');
     29add_action();
    2930
    3031/**
     
    3334 * @return array
    3435 */
    35 function Wi_Get_products(): array
    36 {
     36function wi_get_products() {
    3737
    38     $results = [];
     38;
    3939
    40     $args = array(
    41     'post_type' => array( 'product' ),
    42     'post_status' => 'publish',
    43     'order' => 'ASC',
    44     'ordeby' => 'title'
    45     );
     40$args = array(
     41 => array( 'product' ),
     42'post_status' => 'publish',
     43 => 'ASC',
     44        'ordeby'      => 'title',
     45);
    4646
    47     $the_query = new WP_Query($args);
     47);
    4848
    49     if ($the_query->have_posts() ) {
     49$the_query->have_posts() ) {
    5050
    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    }
    5556
    56     }
     57    wp_reset_postdata();
    5758
    58     wp_reset_postdata();
    59 
    60     return $results;
     59    return $results;
    6160}
    6261
     
    6665 * @return void
    6766 */
    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         ->set_render_callback(
    90             function (
    91                 $fields
    92             ) {
    93                 ?>
     67function
     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               
    9493
    95                  <div class="block">
     94                $start = '<h3>';
     95                $end   = '</h3>';
     96                $title = esc_html( get_the_title( $fields['product'] ) );
    9697
    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                }
    103101
    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                }
    107106
    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;
    112108
    113                 echo $start . $title . $end;
     109;
    114110
    115                 $safety = 'true';
     111                if ( isset( $fields['safety'] ) && true === $fields['safety'] ) {
     112                    $safety = 'false';
     113                }
    116114
    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>';
    120124
    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        );
    135129}
    136130
    137 add_action('carbon_fields_register_fields', 'Wi_Product_block');
     131add_action();
Note: See TracChangeset for help on using the changeset viewer.