type ) { case 0: case 1: //Text $type = 'text'; switch ( $newfield->format ) { default: case 'text': $fieldvalue = ATKPTools::get_post_parameter( ATKP_PRODUCT_POSTTYPE . '_' . $fieldname, 'string' ); break; case 'stars': case 'number': $fieldvalue = ATKPTools::get_post_parameter( ATKP_PRODUCT_POSTTYPE . '_' . $fieldname, 'double' ); break; case 'url': $fieldvalue = ATKPTools::get_post_parameter( ATKP_PRODUCT_POSTTYPE . '_' . $fieldname, 'url' ); break; } break; case 2: //multiline $fieldvalue = ATKPTools::get_post_parameter( ATKP_PRODUCT_POSTTYPE . '_' . $fieldname, 'multistring' ); break; case 3: //dropdown $fieldvalue = ATKPTools::get_post_parameter( ATKP_PRODUCT_POSTTYPE . '_' . $fieldname, 'string' ); break; case 4: //yesno $fieldvalue = ATKPTools::get_post_parameter( ATKP_PRODUCT_POSTTYPE . '_' . $fieldname, 'string' ); switch ( $fieldvalue ) { default: case '': $fieldvalue = null; break; case '0': $fieldvalue = '0'; break; case '1': $fieldvalue = '1'; break; } break; case 5: //html $fieldvalue = ATKPTools::get_post_parameter( ATKP_PRODUCT_POSTTYPE . '_' . $fieldname, 'html' ); break; default: /* translators: %s: field type */ throw new exception( esc_html( sprintf( __( 'unknown newfield->type: %s', 'affiliate-toolkit-starter' ), $newfield->type ) ) ); } return $fieldvalue; } public function create_control( $newfield, $controlname, $value, $isnewfield = false, $generaterandom = true ) { $result = ''; if ( $newfield != null ) { $newfield->isnewfield = $isnewfield; } $value = apply_filters( 'atkp_backend_control_value', $value, $newfield ); switch ( $newfield->type ) { default: case 1: //Text $type = 'text'; $attributes = ''; switch ( $newfield->format ) { case 'text': $type = 'text'; break; case 'number': $type = 'number'; $attributes = ' step="0.01" '; break; case 'url': $type = 'url'; break; case 'email': $type = 'email'; break; case 'date': $type = 'date'; break; case 'stars': $type = 'number'; $attributes = ' step="0.5" min="0" max="5" '; break; } $result = ' '; break; case 2: //multiline $result = ''; break; case 3: //dropdown if ( $isnewfield ) { $values = explode( "\n", $newfield->values ); } else { $values = explode( ';', $newfield->format ); } $result = ''; break; case 4: //yesno $result .= ''; break; case 5: //html ob_start(); wp_editor( $value, $controlname.($generaterandom ? random_int(1, 9999) : ''), array( 'media_buttons' => false, 'textarea_name' => $controlname, 'textarea_rows' => 5, ) ); $result = ob_get_contents(); ob_end_clean(); break; } // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output is built with proper escaping (esc_attr, esc_textarea) throughout the method echo $result; } public function get_minmaxvalue( $newfield, $order = 'ASC' ) { $minvalue = 0; global $wpdb; if ( $newfield->name == 'price' ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared -- Custom table query for price range, no user input. $results = $wpdb->get_results( ' SELECT min(products.salepricefloat) as minprice, max(products.salepricefloat) as maxprice FROM ' . $wpdb->prefix . 'posts posts inner join ' . $wpdb->prefix . 'atkp_products products on products.product_id = posts.id WHERE posts.post_type in ("atkp_product") and posts.post_status in ("draft","publish")' ); if ( count( $results ) > 0 ) { return $order == 'ASC' ? $results[0]->minprice : $results[0]->maxprice; } return 0; } if ( ! $newfield->isnewfield ) { $fieldname = ATKP_PRODUCT_POSTTYPE . '_customfield_' . $newfield->name; } else { $fieldname = ATKP_PRODUCT_POSTTYPE . '_cf_' . $newfield->name; } $args = array( 'post_type' => ATKP_PRODUCT_POSTTYPE, 'post_status' => array( 'publish' ), 'orderby' => 'meta_value_num', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key 'meta_key' => $fieldname, 'order' => $order, 'limit' => 1, ); $args = apply_filters( 'atkp_product_filter_getminmaxvalue', $args, $fieldname ); $the_query = new WP_Query( $args ); while ( $the_query->have_posts() ) { try { $the_query->the_post(); $prd = $the_query->post; $minvalue = ATKPTools::get_post_setting( $prd->ID, $fieldname ); break; } catch ( Exception $e ) { //TODO: logfile, falls ein wert nicht geparst werden kann? } } wp_reset_postdata(); return $minvalue; } public function get_meta_values( $fieldname, $type = 'post', $status = 'publish' ) { global $wpdb; if ( $fieldname == ATKP_PRODUCT_POSTTYPE . '_manufacturer' ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared -- Custom table query for manufacturer list, no user input. $results = $wpdb->get_results( ' SELECT products.manufacturer FROM ' . $wpdb->prefix . 'posts posts inner join ' . $wpdb->prefix . 'atkp_products products on products.product_id = posts.id WHERE posts.post_type in ("atkp_product") and posts.post_status in ("draft","publish") group by products.manufacturer' ); $simple = array(); foreach ( $results as $id ) { $simple[] = $id->manufacturer; } return $simple; } if ( $fieldname == ATKP_PRODUCT_POSTTYPE . '_brand' ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared -- Custom table query for brand list, no user input. $results = $wpdb->get_results( ' SELECT products.brand FROM ' . $wpdb->prefix . 'posts posts inner join ' . $wpdb->prefix . 'atkp_products products on products.product_id = posts.id WHERE posts.post_type in ("atkp_product") and posts.post_status in ("draft","publish") group by products.brand' ); $simple = array(); foreach ( $results as $id ) { $simple[] = $id->brand; } return $simple; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Meta values query for filter controls $r = $wpdb->get_col( $wpdb->prepare( " SELECT pm.meta_value FROM {$wpdb->postmeta} pm LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id WHERE pm.meta_key = %s AND p.post_status = %s AND p.post_type = %s ", $fieldname, $status, $type ) ); $simple = array(); foreach ( $r as $key => $id ) { $simple[] = $id; } return $simple; } public function get_pricecontrol( $filterparams ) { //range $pricefield = new atkp_udfield(); $pricefield->id = uniqid(); $pricefield->caption = 'Price'; $pricefield->name = 'price'; $pricefield->type = 1; $pricefield->format = 'number'; $pricefield->prefix = get_option( ATKP_PLUGIN_PREFIX . '_searchform_price_prefix', '€' ); $pricefield->suffix = get_option( ATKP_PLUGIN_PREFIX . '_searchform_price_suffix', '' ); return $this->create_frontendcontrol( $pricefield, null, 'price', '', false, $filterparams );; } public function get_custom_frontend_control( $fieldname2, $filterparams = null ) { $newfields = atkp_udfield::load_fields(); foreach ( $newfields as $newfield ) { $fieldname = 'customfield_' . $newfield->name; if ( $fieldname != $fieldname2 ) { continue; } switch ( $newfield->type ) { case 3: case 4: //dropdown & yes no return $this->create_frontendcontrol( $newfield, null, $fieldname, '', false, $filterparams ); break; case 1: //range if ( $newfield->format == 'number' ) { return $this->create_frontendcontrol( $newfield, null, $fieldname, '', false, $filterparams ); } break; } } $groups = ATKPTools::get_fieldgroups(); foreach ( $groups as $group ) { $fields = ATKPTools::get_post_setting( $group->ID, ATKP_FIELDGROUP_POSTTYPE . '_fields' ); if ( $fields != null ) { foreach ( $fields as $field ) { $fieldname = 'cf_' . $field->name; if ( $fieldname != $fieldname2 ) { continue; } switch ( $field->type ) { case 3: case 4: //dropdown & yes no return $this->create_frontendcontrol( $field, null, $fieldname, '', true, $filterparams ); break; case 1: //range if ( $field->format == 'number' ) { return $this->create_frontendcontrol( $field, null, $fieldname, '', true, $filterparams ); } break; } } } } $taxonomies = atkp_udtaxonomy::load_taxonomies(); if ( $taxonomies != null ) { foreach ( $taxonomies as $taxonomy ) { if ( ! $taxonomy->issystemfield ) { if ( $taxonomy->isnewtax ) { $fieldname = 'ct_' . $taxonomy->name; } else { $fieldname = 'customtaxonomy_' . $taxonomy->name; } } else { if ( $taxonomy->ismanufacturer ) { $fieldname = 'manufacturer'; } else if ( $taxonomy->isauthor ) { $fieldname = 'author'; } else if ( $taxonomy->isbrand ) { $fieldname = 'brand'; } else if ( $taxonomy->isproductcategory ) { $fieldname = 'productcategory'; } else { $fieldname = $taxonomy->name; } } if ( $fieldname != $fieldname2 ) { continue; } //combo mit dropdown return $this->create_frontendcontrol( null, $taxonomy, $fieldname, '', false, $filterparams ); } } return ''; } public function create_frontendcontrol( $newfield, $taxonomy, $controlname, $value, $isnewfield = false, $filterparams = null ) { $result = ''; if ( $newfield != null ) { $newfield->isnewfield = $isnewfield; } $filterparams = apply_filters( 'atkp_frontend_control_params', $filterparams, $newfield ); if ( $taxonomy != null ) { /* translators: %s: field name */ $caption = sprintf( __( 'select %s', 'affiliate-toolkit-starter' ), $taxonomy->caption ); $intvals = isset( $filterparams[ $controlname ] ) ? ( is_array( $filterparams[ $controlname ] ) ? $filterparams[ $controlname ] : array_map( 'intval', explode( ',', $filterparams[ $controlname ] ) ) ) : null; $result .= ''; } else if ( $newfield != null ) { switch ( $newfield->type ) { case 1: //Text $type = 'text'; switch ( $newfield->format ) { case 'text': break; case 'number': $maxvalue = $this->get_minmaxvalue( $newfield, $order = 'DESC' ); $minvalue = 0; if ( isset( $filterparams[ 'min' . $controlname ] )) { $minvalue_value = intval( $filterparams[ 'min' . $controlname ] ) ; } else { $minvalue_value = $minvalue; } if ( isset( $filterparams[ 'max' . $controlname ] ) ) { $maxvalue_value = intval( $filterparams[ 'max' . $controlname ] ); } else { if($maxvalue == 0) $maxvalue_value = 100; else $maxvalue_value = $maxvalue; } $result = '
'; break; case 'url': break; case 'stars': $minvalue_value = $minvalue = 0; $maxvalue_value = $maxvalue = 5; $result = ''; break; } break; case 2: //multiline //$result = ''; break; case 3: //dropdown if ( $isnewfield ) { $values = explode( "\n", $newfield->values ); } else { $values = explode( ';', $newfield->format ); } /* translators: %s: field name */ $caption = sprintf( __( 'select %s', 'affiliate-toolkit-starter' ), $newfield->caption ); $stringvals = isset( $filterparams[ $controlname ] ) ? ( is_array( $filterparams[ $controlname ] ) ? $filterparams[ $controlname ] : explode( ',', $filterparams[ $controlname ] ) ) : null; $result = ''; break; case 4: //yesno $boolval = isset( $filterparams[ $controlname ] ) ? boolval( $filterparams[ $controlname ] ) : false; $result .= '