implode(',', wu_get_site_templates(array('fields' => 'ids'))), 'template_selection_type' => 'name', 'template_selection_template' => 'clean', 'cols' => 3, 'hide_template_selection_when_pre_selected' => false, ); } /** * List of keys of the default fields we want to display on the builder. * * @since 2.0.0 * @return array */ public function default_fields() { return array( // 'name', ); } /** * If you want to force a particular attribute to a value, declare it here. * * @since 2.0.0 * @return array */ public function force_attributes() { return array( 'id' => 'template_selection', 'name' => __('Template Selection', 'wp-ultimo'), 'required' => true, ); } /** * Returns the list of available pricing table templates. * * @since 2.0.0 * @return array */ public function get_template_selection_templates() { $available_templates = Field_Templates_Manager::get_instance()->get_templates_as_options('template_selection'); return $available_templates; } /** * Returns the list of additional fields specific to this type. * * @since 2.0.0 * @return array */ public function get_fields() { $editor_fields = array(); $editor_fields['cols'] = array( 'type' => 'hidden', ); $editor_fields['template_selection_type'] = array( 'type' => 'select', 'title' => __('Available templates', 'wp-ultimo'), 'desc' => __('How do you want to choose available which templates will be available.', 'wp-ultimo'), 'order' => 20, 'options' => array( 'name' => __('Select by names'), 'categories' => __('Select by categories'), 'all' => __('All templates'), ), 'html_attr' => array( 'v-model' => 'template_selection_type', ), ); $editor_fields['template_selection_categories'] = array( 'type' => 'select', 'title' => __('Template Categories', 'wp-ultimo'), 'placeholder' => __('e.g.: Landing Page, Health...', 'wp-ultimo'), 'desc' => __('Customers will be able to filter by categories during signup.', 'wp-ultimo'), 'order' => 21, 'options' => Site::get_all_categories(), 'html_attr' => array( 'data-selectize-categories' => 1, 'multiple' => 1, ), 'wrapper_html_attr' => array( 'v-show' => 'template_selection_type === "categories"', ), ); $editor_fields['template_selection_sites'] = array( 'type' => 'model', 'title' => __('Template Sites', 'wp-ultimo'), 'placeholder' => __('e.g. Template Site 1, My Agency', 'wp-ultimo'), 'desc' => __('Be sure to add the templates in the order you want them to show up.', 'wp-ultimo'), 'order' => 22, 'html_attr' => array( 'v-model' => 'template_selection_sites', 'data-model' => 'site', 'data-value-field' => 'blog_id', 'data-label-field' => 'title', 'data-search-field' => 'title', 'data-max-items' => 999, 'data-include' => implode( ',', wu_get_site_templates( array( 'fields' => 'blog_id', ) ) ), ), 'wrapper_html_attr' => array( 'v-show' => 'template_selection_type === \'name\'', ), ); $editor_fields['hide_template_selection_when_pre_selected'] = array( 'type' => 'toggle', 'title' => __('Hide when Pre-Selected', 'wp-ultimo'), 'desc' => __('Prevent customers from seeing this field when a template was already selected via the URL.', 'wp-ultimo'), 'tooltip' => __('If the template selection field is the only field in the current step, the step will be skipped.', 'wp-ultimo'), 'value' => 0, 'order' => 23, 'html_attr' => array( 'v-model' => 'hide_template_selection_when_pre_selected', ), ); $editor_fields['template_selection_template'] = array( 'type' => 'group', 'order' => 24, 'desc' => Field_Templates_Manager::get_instance()->render_preview_block('template_selection'), 'fields' => array( 'template_selection_template' => array( 'type' => 'select', 'title' => __('Template Selector Template', 'wp-ultimo'), 'placeholder' => __('Select your Template', 'wp-ultimo'), 'options' => array($this, 'get_template_selection_templates'), 'wrapper_classes' => 'wu-flex-grow', 'html_attr' => array( 'v-model' => 'template_selection_template', ), ), ), ); // @todo: re-add developer notes. // $editor_fields['_dev_note_develop_your_own_template_1'] = array( // 'type' => 'note', // 'order' => 99, // 'wrapper_classes' => 'sm:wu-p-0 sm:wu-block', // 'classes' => '', // 'desc' => sprintf('
%s
', __('Want to add customized template selection templates?
See how you can do that here.', 'wp-ultimo')), // ); return $editor_fields; } /** * Treat the attributes array to avoid reaching the input var limits. * * @since 2.0.0 * * @param array $attributes The attributes. * @return array */ public function reduce_attributes($attributes) { $array_sites = json_decode(json_encode($attributes['sites']), true); $attributes['sites'] = array_values(array_column($array_sites, 'blog_id')); return $attributes; } /** * Returns the field/element actual field array to be used on the checkout form. * * @since 2.0.0 * * @param array $attributes Attributes saved on the editor form. * @return array An array of fields, not the field itself. */ public function to_fields_array($attributes) { $checkout_fields = array(); $checkout_fields['template_id'] = array( 'type' => 'hidden', 'html_attr' => array( 'v-model' => 'template_id', ), ); /** * Hide when pre-selected. */ if (wu_should_hide_form_field($attributes)) { return $checkout_fields; } if (wu_get_isset($attributes, 'template_selection_template') === 'legacy') { wp_register_script('wu-legacy-signup', wu_get_asset('legacy-signup.js', 'js'), array('wu-functions'), wu_get_version()); wp_enqueue_script('wu-legacy-signup'); wp_enqueue_style('legacy-shortcodes', wu_get_asset('legacy-shortcodes.css', 'css'), array('dashicons'), wu_get_version()); } $site_list = $this->site_list($attributes); $customer_sites = array(); if (wu_get_setting('allow_own_site_as_template')) { $customer = wu_get_current_customer(); if ($customer) { $customer_sites = $customer->get_sites(array('fields' => 'ids')); $site_list = array_merge( $customer_sites, $site_list ); } } $sites = array_map('wu_get_site', $site_list); $sites = array_filter($sites); // Remove inactive sites $sites = array_filter($sites, fn($site) => $site->is_active()); $template_attributes = array( 'sites' => $sites, 'name' => $attributes['name'], 'cols' => $attributes['cols'], 'categories' => $attributes['template_selection_categories'] ?? \WP_Ultimo\Models\Site::get_all_categories($sites), 'customer_sites' => $customer_sites, ); $template_class = Field_Templates_Manager::get_instance()->get_template_class('template_selection', $attributes['template_selection_template']); $content = $template_class ? $template_class->render_container($template_attributes, $this) : __('Template does not exist.', 'wp-ultimo'); $checkout_fields[ $attributes['id'] ] = array( 'type' => 'note', 'desc' => $content, 'wrapper_classes' => $attributes['element_classes'], ); return $checkout_fields; } /** * Return site list according to selection type used. * * @param array $attributes Attributes saved on the editor form. * @return array Array of template ID's */ protected function site_list(array $attributes): array { $selection_type = wu_get_isset($attributes, 'template_selection_type', 'name'); if ($selection_type === 'name') { return explode(',', $attributes['template_selection_sites']); } if ($selection_type === 'all') { return wu_get_site_templates(array('fields' => 'blog_id')); } if ($selection_type === 'categories') { return array_column( \WP_Ultimo\Models\Site::get_all_by_categories( $attributes['template_selection_categories'], array( 'fields' => array('blog_id'), ), ), 'blog_id' ); } return explode(',', $attributes['template_selection_sites']); } }