diff --git a/rapid-addon.php b/rapid-addon.php index 15a7236..e7f613b 100644 --- a/rapid-addon.php +++ b/rapid-addon.php @@ -10,772 +10,772 @@ if (!class_exists('RapidAddon')) { - class RapidAddon { - - public $name; - public $slug; - public $fields; - public $options = array(); - public $accordions = array(); - public $image_sections = array(); - public $import_function; - public $post_saved_function; - public $notice_text; - public $logger = null; - public $when_to_run = false; - public $image_options = array( - 'download_images' => 'yes', - 'download_featured_delim' => ',', - 'download_featured_image' => '', - 'gallery_featured_image' => '', - 'gallery_featured_delim' => ',', - 'featured_image' => '', - 'featured_delim' => ',', - 'search_existing_images' => 1, - 'is_featured' => 0, - 'create_draft' => 'no', - 'set_image_meta_title' => 0, - 'image_meta_title_delim' => ',', - 'image_meta_title' => '', - 'set_image_meta_caption' => 0, - 'image_meta_caption_delim' => ',', - 'image_meta_caption' => '', - 'set_image_meta_alt' => 0, - 'image_meta_alt_delim' => ',', - 'image_meta_alt' => '', - 'set_image_meta_description' => 0, - 'image_meta_description_delim' => ',', - 'image_meta_description_delim_logic' => 'separate', - 'image_meta_description' => '', - 'auto_rename_images' => 0, - 'auto_rename_images_suffix' => '', - 'auto_set_extension' => 0, - 'new_extension' => '', - 'do_not_remove_images' => 1, - 'search_existing_images_logic' => 'by_url' - ); + class RapidAddon { + + public $name; + public $slug; + public $fields; + public $options = array(); + public $accordions = array(); + public $image_sections = array(); + public $import_function; + public $post_saved_function; + public $notice_text; + public $logger = null; + public $when_to_run = false; + public $image_options = array( + 'download_images' => 'yes', + 'download_featured_delim' => ',', + 'download_featured_image' => '', + 'gallery_featured_image' => '', + 'gallery_featured_delim' => ',', + 'featured_image' => '', + 'featured_delim' => ',', + 'search_existing_images' => 1, + 'is_featured' => 0, + 'create_draft' => 'no', + 'set_image_meta_title' => 0, + 'image_meta_title_delim' => ',', + 'image_meta_title' => '', + 'set_image_meta_caption' => 0, + 'image_meta_caption_delim' => ',', + 'image_meta_caption' => '', + 'set_image_meta_alt' => 0, + 'image_meta_alt_delim' => ',', + 'image_meta_alt' => '', + 'set_image_meta_description' => 0, + 'image_meta_description_delim' => ',', + 'image_meta_description_delim_logic' => 'separate', + 'image_meta_description' => '', + 'auto_rename_images' => 0, + 'auto_rename_images_suffix' => '', + 'auto_set_extension' => 0, + 'new_extension' => '', + 'do_not_remove_images' => 1, + 'search_existing_images_logic' => 'by_url' + ); private $active_post_types,$active_themes,$active_plugins = []; - protected $isWizard = true; + protected $isWizard = true; - function __construct($name, $slug) { - $this->name = $name; - $this->slug = $slug; - if (!empty($_GET['id'])){ - $this->isWizard = false; - } - } + function __construct($name, $slug) { + $this->name = $name; + $this->slug = $slug; + if (!empty($_GET['id'])){ + $this->isWizard = false; + } + } - function set_import_function($name) { - $this->import_function = $name; - } + function set_import_function($name) { + $this->import_function = $name; + } - function set_post_saved_function($name) { - $this->post_saved_function = $name; - } + function set_post_saved_function($name) { + $this->post_saved_function = $name; + } - function is_active_addon($post_type = null) { + function is_active_addon($post_type = null) { - if ( ! class_exists( 'PMXI_Plugin' ) ) { - return false; - } + if ( ! class_exists( 'PMXI_Plugin' ) ) { + return false; + } - $addon_active = false; + $addon_active = false; - if ($post_type !== null) { - if (@in_array($post_type, $this->active_post_types) or empty($this->active_post_types)) { - $addon_active = true; - } - } + if ($post_type !== null) { + if (@in_array($post_type, $this->active_post_types) or empty($this->active_post_types)) { + $addon_active = true; + } + } - if ($addon_active){ + if ($addon_active){ - $current_theme = wp_get_theme(); + $current_theme = wp_get_theme(); - $parent_theme = $current_theme->parent(); + $parent_theme = $current_theme->parent(); - $theme_name = $current_theme->get('Name'); + $theme_name = $current_theme->get('Name'); - $addon_active = (@in_array($theme_name, $this->active_themes) or empty($this->active_themes)) ? true : false; + $addon_active = (@in_array($theme_name, $this->active_themes) or empty($this->active_themes)) ? true : false; - if ( ! $addon_active and $parent_theme ){ - $parent_theme_name = $parent_theme->get('Name'); - $addon_active = (@in_array($parent_theme_name, $this->active_themes) or empty($this->active_themes)) ? true : false; + if ( ! $addon_active and $parent_theme ){ + $parent_theme_name = $parent_theme->get('Name'); + $addon_active = (@in_array($parent_theme_name, $this->active_themes) or empty($this->active_themes)) ? true : false; - } + } - if ( $addon_active and ! empty($this->active_plugins) ){ + if ( $addon_active and ! empty($this->active_plugins) ){ - include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); + include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); - foreach ($this->active_plugins as $plugin) { - if ( ! is_plugin_active($plugin) ) { - $addon_active = false; - break; - } - } - } + foreach ($this->active_plugins as $plugin) { + if ( ! is_plugin_active($plugin) ) { + $addon_active = false; + break; + } + } + } - } - - if ($this->when_to_run == "always") { - $addon_active = true; - } - - return apply_filters('rapid_is_active_add_on', $addon_active, $post_type, $this->slug); - } - - /** - * - * Add-On Initialization - * - * @param array $conditions - list of supported themes and post types - * - */ - function run($conditions = array()) { - - if (empty($conditions)) { - $this->when_to_run = "always"; - } - - @$this->active_post_types = ( ! empty($conditions['post_types'])) ? $conditions['post_types'] : array(); - @$this->active_themes = ( ! empty($conditions['themes'])) ? $conditions['themes'] : array(); - @$this->active_plugins = ( ! empty($conditions['plugins'])) ? $conditions['plugins'] : array(); - - add_filter('pmxi_addons', array($this, 'wpai_api_register')); - add_filter('wp_all_import_addon_parse', array($this, 'wpai_api_parse')); - add_filter('wp_all_import_addon_import', array($this, 'wpai_api_import')); - add_filter('wp_all_import_addon_saved_post', array($this, 'wpai_api_post_saved')); - add_filter('pmxi_options_options', array($this, 'wpai_api_options')); - add_filter('wp_all_import_image_sections', array($this, 'additional_sections'), 10, 1); - add_filter('pmxi_custom_types', array($this, 'filter_post_types'), 10, 2); - add_filter('pmxi_post_list_order', array($this,'sort_post_types'), 10, 1); - add_filter('wp_all_import_post_type_image', array($this, 'post_type_image'), 10, 1 ); - add_action('pmxi_extend_options_featured', array($this, 'wpai_api_metabox'), 10, 2); - add_action('admin_init', array($this, 'admin_notice_ignore')); - } - - function parse($data) { - - if ( ! $this->is_active_addon($data['import']->options['custom_type'])) return false; - - $parsedData = $this->helper_parse($data, $this->options_array()); - return $parsedData; - - } - - - function add_field($field_slug, $field_name, $field_type, $enum_values = null, $tooltip = "", $is_html = true, $default_text = '') { - - $field = array("name" => $field_name, "type" => $field_type, "enum_values" => $enum_values, "tooltip" => $tooltip, "is_sub_field" => false, "is_main_field" => false, "slug" => $field_slug, "is_html" => $is_html, 'default_text' => $default_text); - - $this->fields[$field_slug] = $field; - - if ( ! empty($enum_values) ){ - foreach ($enum_values as $key => $value) { - if (is_array($value)) - { - if ($field['type'] == 'accordion') - { - $this->fields[$value['slug']]['is_sub_field'] = true; - } - else - { - foreach ($value as $n => $param) { - if (is_array($param) and ! empty($this->fields[$param['slug']])){ - $this->fields[$param['slug']]['is_sub_field'] = true; - } - } - } - } - } - } + } - return $field; + if ($this->when_to_run == "always") { + $addon_active = true; + } - } + return apply_filters('rapid_is_active_add_on', $addon_active, $post_type, $this->slug); + } - function add_acf_field($field){ - $this->fields[$field->post_name] = array( - 'type' => 'acf', - 'field_obj' => $field - ); - } + /** + * + * Add-On Initialization + * + * @param array $conditions - list of supported themes and post types + * + */ + function run($conditions = array()) { - private $acfGroups = array(); + if (empty($conditions)) { + $this->when_to_run = "always"; + } - function use_acf_group($acf_group){ - $this->add_text( - '
-

'.$acf_group['title'].'

-
'); - $acf_fields = get_posts(array('posts_per_page' => -1, 'post_type' => 'acf-field', 'post_parent' => $acf_group['ID'], 'post_status' => 'publish', 'orderby' => 'menu_order', 'order' => 'ASC')); - if (!empty($acf_fields)){ - foreach ($acf_fields as $field) { - $this->add_acf_field($field); - } - } - $this->add_text('
'); - $this->acfGroups[] = $acf_group['ID']; - add_filter('wp_all_import_acf_is_show_group', array($this, 'acf_is_show_group'), 10, 2); - } - - function acf_is_show_group($is_show, $acf_group){ - return (in_array($acf_group['ID'], $this->acfGroups)) ? false : true; - } - - /** - * - * Add an option to WP All Import options list - * - * @param string $slug - option name - * @param string $default_value - default option value - * - */ - function add_option($slug, $default_value = ''){ - $this->options[$slug] = $default_value; - } - - function options_array() { - - $options_list = array(); - - if ( ! empty( $this->fields ) ) { - - foreach ($this->fields as $field_slug => $field_params) { - if (in_array($field_params['type'], array('title', 'plain_text', 'acf'))) continue; - $default_value = ''; - if (!empty($field_params['enum_values'])){ - foreach ($field_params['enum_values'] as $key => $value) { - $default_value = $key; - break; - } - } - $options_list[$field_slug] = $default_value; - } + @$this->active_post_types = ( ! empty($conditions['post_types'])) ? $conditions['post_types'] : array(); + @$this->active_themes = ( ! empty($conditions['themes'])) ? $conditions['themes'] : array(); + @$this->active_plugins = ( ! empty($conditions['plugins'])) ? $conditions['plugins'] : array(); - } + add_filter('pmxi_addons', array($this, 'wpai_api_register')); + add_filter('wp_all_import_addon_parse', array($this, 'wpai_api_parse')); + add_filter('wp_all_import_addon_import', array($this, 'wpai_api_import')); + add_filter('wp_all_import_addon_saved_post', array($this, 'wpai_api_post_saved')); + add_filter('pmxi_options_options', array($this, 'wpai_api_options')); + add_filter('wp_all_import_image_sections', array($this, 'additional_sections'), 10, 1); + add_filter('pmxi_custom_types', array($this, 'filter_post_types'), 10, 2); + add_filter('pmxi_post_list_order', array($this,'sort_post_types'), 10, 1); + add_filter('wp_all_import_post_type_image', array($this, 'post_type_image'), 10, 1 ); + add_action('pmxi_extend_options_featured', array($this, 'wpai_api_metabox'), 10, 2); + add_action('admin_init', array($this, 'admin_notice_ignore')); + } - if ( ! empty($this->options) ){ - foreach ($this->options as $slug => $value) { - $options_arr[$slug] = $value; - } - } + function parse($data) { - $options_arr[$this->slug] = $options_list; - $options_arr['rapid_addon'] = plugin_basename( __FILE__ ); + if ( ! $this->is_active_addon($data['import']->options['custom_type'])) return false; - return $options_arr; + $parsedData = $this->helper_parse($data, $this->options_array()); + return $parsedData; - } + } - function wpai_api_options($all_options) { - $all_options = $all_options + $this->options_array(); + function add_field($field_slug, $field_name, $field_type, $enum_values = null, $tooltip = "", $is_html = true, $default_text = '') { - return $all_options; + $field = array("name" => $field_name, "type" => $field_type, "enum_values" => $enum_values, "tooltip" => $tooltip, "is_sub_field" => false, "is_main_field" => false, "slug" => $field_slug, "is_html" => $is_html, 'default_text' => $default_text); - } + $this->fields[$field_slug] = $field; + if ( ! empty($enum_values) ){ + foreach ($enum_values as $key => $value) { + if (is_array($value)) + { + if ($field['type'] == 'accordion') + { + $this->fields[$value['slug']]['is_sub_field'] = true; + } + else + { + foreach ($value as $n => $param) { + if (is_array($param) and ! empty($this->fields[$param['slug']])){ + $this->fields[$param['slug']]['is_sub_field'] = true; + } + } + } + } + } + } - function wpai_api_register($addons) { + return $field; - if (empty($addons[$this->slug])) { - $addons[$this->slug] = 1; - } + } + + function add_acf_field($field){ + $this->fields[$field->post_name] = array( + 'type' => 'acf', + 'field_obj' => $field + ); + } + + private $acfGroups = array(); + + function use_acf_group($acf_group){ + $this->add_text( + '
+

'.$acf_group['title'].'

+
'); + $acf_fields = get_posts(array('posts_per_page' => -1, 'post_type' => 'acf-field', 'post_parent' => $acf_group['ID'], 'post_status' => 'publish', 'orderby' => 'menu_order', 'order' => 'ASC')); + if (!empty($acf_fields)){ + foreach ($acf_fields as $field) { + $this->add_acf_field($field); + } + } + $this->add_text('
'); + $this->acfGroups[] = $acf_group['ID']; + add_filter('wp_all_import_acf_is_show_group', array($this, 'acf_is_show_group'), 10, 2); + } - return $addons; + function acf_is_show_group($is_show, $acf_group){ + return (in_array($acf_group['ID'], $this->acfGroups)) ? false : true; + } - } + /** + * + * Add an option to WP All Import options list + * + * @param string $slug - option name + * @param string $default_value - default option value + * + */ + function add_option($slug, $default_value = ''){ + $this->options[$slug] = $default_value; + } + function options_array() { - function wpai_api_parse($functions) { + $options_list = array(); - $functions[$this->slug] = array($this, 'parse'); - return $functions; + if ( ! empty( $this->fields ) ) { - } + foreach ($this->fields as $field_slug => $field_params) { + if (in_array($field_params['type'], array('title', 'plain_text', 'acf'))) continue; + $default_value = ''; + if (!empty($field_params['enum_values'])){ + foreach ($field_params['enum_values'] as $key => $value) { + $default_value = $key; + break; + } + } + $options_list[$field_slug] = $default_value; + } - function wpai_api_post_saved($functions){ - $functions[$this->slug] = array($this, 'post_saved'); - return $functions; - } + } + if ( ! empty($this->options) ){ + foreach ($this->options as $slug => $value) { + $options_arr[$slug] = $value; + } + } - function wpai_api_import($functions) { + $options_arr[$this->slug] = $options_list; + $options_arr['rapid_addon'] = plugin_basename( __FILE__ ); - $functions[$this->slug] = array($this, 'import'); - return $functions; + return $options_arr; - } + } - function post_saved( $importData ){ + function wpai_api_options($all_options) { - if (is_callable($this->post_saved_function)) - call_user_func($this->post_saved_function, $importData['pid'], $importData['import'], $importData['logger']); + $all_options = $all_options + $this->options_array(); - } + return $all_options; - function import($importData, $parsedData) { + } - if (!$this->is_active_addon($importData['post_type'])) { - return; - } - $import_options = $importData['import']['options'][$this->slug]; + function wpai_api_register($addons) { - if ( ! empty($parsedData) ) { + if (empty($addons[$this->slug])) { + $addons[$this->slug] = 1; + } - $this->logger = $importData['logger']; + return $addons; - $post_id = $importData['pid']; - $index = $importData['i']; - $data = array(); - if (!empty($this->fields)){ - foreach ($this->fields as $field_slug => $field_params) { - if (in_array($field_params['type'], array('title', 'plain_text'))) continue; - switch ($field_params['type']) { + } - case 'image': - // import the specified image, then set the value of the field to the image ID in the media library + function wpai_api_parse($functions) { - $image_url_or_path = $parsedData[$field_slug][$index]; + $functions[$this->slug] = array($this, 'parse'); + return $functions; - if ( ! array_key_exists( $field_slug, $import_options['download_image'] ) ) { - continue 2; - } + } - $download = $import_options['download_image'][$field_slug]; + function wpai_api_post_saved($functions){ + $functions[$this->slug] = array($this, 'post_saved'); + return $functions; + } - $uploaded_image = PMXI_API::upload_image($post_id, $image_url_or_path, $download, $importData['logger'], true, "", "images", true, $importData['articleData']); - $data[$field_slug] = array( - "attachment_id" => $uploaded_image, - "image_url_or_path" => $image_url_or_path, - "download" => $download - ); + function wpai_api_import($functions) { - break; + $functions[$this->slug] = array($this, 'import'); + return $functions; - case 'file': + } - $image_url_or_path = $parsedData[$field_slug][$index]; + function post_saved( $importData ){ - if ( ! array_key_exists( $field_slug, $import_options['download_image'] ) ) { - continue 2; - } + if (is_callable($this->post_saved_function)) + call_user_func($this->post_saved_function, $importData['pid'], $importData['import'], $importData['logger']); - $download = $import_options['download_image'][$field_slug]; + } - $uploaded_file = PMXI_API::upload_image($post_id, $image_url_or_path, $download, $importData['logger'], true, "", "files", true, $importData['articleData']); + function import($importData, $parsedData) { - $data[$field_slug] = array( - "attachment_id" => $uploaded_file, - "image_url_or_path" => $image_url_or_path, - "download" => $download - ); + if (!$this->is_active_addon($importData['post_type'])) { + return; + } - break; + $import_options = $importData['import']['options'][$this->slug]; - default: - // set the field data to the value of the field after it's been parsed - $data[$field_slug] = $parsedData[$field_slug][$index]; - break; - } + if ( ! empty($parsedData) ) { - // apply mapping rules if they exist - if (!empty($import_options['mapping'][$field_slug])) { - $mapping_rules = json_decode($import_options['mapping'][$field_slug], true); + $this->logger = $importData['logger']; - if (!empty($mapping_rules) and is_array($mapping_rules)) { - foreach ($mapping_rules as $rule_number => $map_to) { - if (isset($map_to[trim($data[$field_slug])])){ - $data[$field_slug] = trim($map_to[trim($data[$field_slug])]); - break; - } - } - } - } - // -------------------- - } - } + $post_id = $importData['pid']; + $index = $importData['i']; + $data = array(); + if (!empty($this->fields)){ + foreach ($this->fields as $field_slug => $field_params) { + if (in_array($field_params['type'], array('title', 'plain_text'))) continue; + switch ($field_params['type']) { - call_user_func($this->import_function, $post_id, $data, $importData['import'], $importData['articleData'], $importData['logger']); - } + case 'image': - } + // import the specified image, then set the value of the field to the image ID in the media library + $image_url_or_path = $parsedData[$field_slug][$index]; - function wpai_api_metabox($post_type, $current_values) { + if ( ! array_key_exists( $field_slug, $import_options['download_image'] ) ) { + continue 2; + } - if (!$this->is_active_addon($post_type)) { - return; - } + $download = $import_options['download_image'][$field_slug]; - // Escaping is handled in 'helper_metabox_top' method. - echo $this->helper_metabox_top($this->name); + $uploaded_image = PMXI_API::upload_image($post_id, $image_url_or_path, $download, $importData['logger'], true, "", "images", true, $importData['articleData']); - $visible_fields = 0; + $data[$field_slug] = array( + "attachment_id" => $uploaded_image, + "image_url_or_path" => $image_url_or_path, + "download" => $download + ); - foreach ($this->fields as $field_slug => $field_params) { - if ($field_params['is_sub_field']) continue; - $visible_fields++; - } + break; - $counter = 0; + case 'file': - foreach ($this->fields as $field_slug => $field_params) { + $image_url_or_path = $parsedData[$field_slug][$index]; - // do not render sub fields - if ($field_params['is_sub_field']) continue; + if ( ! array_key_exists( $field_slug, $import_options['download_image'] ) ) { + continue 2; + } - $counter++; + $download = $import_options['download_image'][$field_slug]; - $this->render_field($field_params, $field_slug, $current_values, $visible_fields == $counter); + $uploaded_file = PMXI_API::upload_image($post_id, $image_url_or_path, $download, $importData['logger'], true, "", "files", true, $importData['articleData']); - } + $data[$field_slug] = array( + "attachment_id" => $uploaded_file, + "image_url_or_path" => $image_url_or_path, + "download" => $download + ); - // Static HTML is returned by method for display. - echo $this->helper_metabox_bottom(); + break; - if ( ! empty($this->image_sections) ){ - $is_images_section_enabled = apply_filters('wp_all_import_is_images_section_enabled', true, $post_type); - foreach ($this->image_sections as $k => $section) { - $section_options = array(); - foreach ($this->image_options as $slug => $value) { - $section_options[$section['slug'] . $slug] = $value; - } - if ( ! $is_images_section_enabled and ! $k ){ - $section_options[$section['slug'] . 'is_featured'] = 1; - } - PMXI_API::add_additional_images_section($section['title'], $section['slug'], $current_values, '', true, false, $section['type']); - } - } - - } - - function render_field($field_params, $field_slug, $current_values, $in_the_bottom = false){ - - if (!isset($current_values[$this->slug][$field_slug])) { - $current_values[$this->slug][$field_slug] = isset($field_params['default_text']) ? $field_params['default_text'] : ''; - } - - if ($field_params['type'] == 'text') { - - PMXI_API::add_field( - 'simple', - $field_params['name'], - array( - 'tooltip' => $field_params['tooltip'], - 'field_name' => $this->slug."[".$field_slug."]", - 'field_value' => ( $current_values[$this->slug][$field_slug] == '' && $this->isWizard ) ? $field_params['default_text'] : $current_values[$this->slug][$field_slug] - ) - ); - - } else if ($field_params['type'] == 'textarea') { - - PMXI_API::add_field( - 'textarea', - $field_params['name'], - array( - 'tooltip' => $field_params['tooltip'], - 'field_name' => $this->slug."[".$field_slug."]", - 'field_value' => ( $current_values[$this->slug][$field_slug] == '' && $this->isWizard ) ? $field_params['default_text'] : $current_values[$this->slug][$field_slug] - ) - ); - - } else if ($field_params['type'] == 'wp_editor') { - - PMXI_API::add_field( - 'wp_editor', - $field_params['name'], - array( - 'tooltip' => $field_params['tooltip'], - 'field_name' => $this->slug."[".$field_slug."]", - 'field_value' => ( $current_values[$this->slug][$field_slug] == '' && $this->isWizard ) ? $field_params['default_text'] : $current_values[$this->slug][$field_slug] - ) - ); - - } else if ($field_params['type'] == 'image' or $field_params['type'] == 'file') { - - if (!isset($current_values[$this->slug]['download_image'][$field_slug])) { $current_values[$this->slug]['download_image'][$field_slug] = ''; } - - PMXI_API::add_field( - $field_params['type'], - $field_params['name'], - array( - 'tooltip' => $field_params['tooltip'], - 'field_name' => $this->slug."[".$field_slug."]", - 'field_value' => $current_values[$this->slug][$field_slug], - 'download_image' => $current_values[$this->slug]['download_image'][$field_slug], - 'field_key' => $field_slug, - 'addon_prefix' => $this->slug - - ) - ); - - } else if ($field_params['type'] == 'radio') { - - if (!isset($current_values[$this->slug]['mapping'][$field_slug])) { $current_values[$this->slug]['mapping'][$field_slug] = array(); } - if (!isset($current_values[$this->slug]['xpaths'][$field_slug])) { $current_values[$this->slug]['xpaths'][$field_slug] = ''; } - - PMXI_API::add_field( - 'enum', - $field_params['name'], - array( - 'tooltip' => $field_params['tooltip'], - 'field_name' => $this->slug."[".$field_slug."]", - 'field_value' => $current_values[$this->slug][$field_slug], - 'enum_values' => $field_params['enum_values'], - 'mapping' => true, - 'field_key' => $field_slug, - 'mapping_rules' => $current_values[$this->slug]['mapping'][$field_slug], - 'xpath' => $current_values[$this->slug]['xpaths'][$field_slug], - 'addon_prefix' => $this->slug, - 'sub_fields' => $this->get_sub_fields($field_params, $field_slug, $current_values) - ) - ); - - } else if($field_params['type'] == 'accordion') { - - PMXI_API::add_field( - 'accordion', - $field_params['name'], - array( - 'tooltip' => $field_params['tooltip'], - 'field_name' => $this->slug."[".$field_slug."]", - 'field_key' => $field_slug, - 'addon_prefix' => $this->slug, - 'sub_fields' => $this->get_sub_fields($field_params, $field_slug, $current_values), - 'in_the_bottom' => $in_the_bottom - ) - ); - - } else if($field_params['type'] == 'acf') { - $fieldData = (!empty($field_params['field_obj']->post_content)) ? unserialize($field_params['field_obj']->post_content) : array(); - $fieldData['ID'] = $field_params['field_obj']->ID; - $fieldData['id'] = $field_params['field_obj']->ID; - $fieldData['label'] = $field_params['field_obj']->post_title; - $fieldData['key'] = $field_params['field_obj']->post_name; - if (empty($fieldData['name'])) $fieldData['name'] = $field_params['field_obj']->post_excerpt; - // This function is no longer used in recent versions of the ACF Import Add-On. - if (function_exists('pmai_render_field')) { - echo \wp_kses_post(pmai_render_field($fieldData, ( ! empty($current_values) ) ? $current_values : array() )); - } - } else if($field_params['type'] == 'title'){ - ?> -

?

- -

- $value) { - $sub_fields[$key] = array(); - if (is_array($value)){ - if ($field_params['type'] == 'accordion'){ - $sub_fields[$key][] = $this->convert_field($value, $current_values); - } - else - { - foreach ($value as $k => $sub_field) { - if (is_array($sub_field) and ! empty($this->fields[$sub_field['slug']])) - { - $sub_fields[$key][] = $this->convert_field($sub_field, $current_values); - } - } - } - } - } - } - return $sub_fields; - } - - function convert_field($sub_field, $current_values){ - $field = array(); - if (!isset($current_values[$this->slug][$sub_field['slug']])) { - $current_values[$this->slug][$sub_field['slug']] = isset($sub_field['default_text']) ? $sub_field['default_text'] : ''; - } - switch ($this->fields[$sub_field['slug']]['type']) { - case 'text': - $field = array( - 'type' => 'simple', - 'label' => $this->fields[$sub_field['slug']]['name'], - 'params' => array( - 'tooltip' => $this->fields[$sub_field['slug']]['tooltip'], - 'field_name' => $this->slug."[".$sub_field['slug']."]", - 'field_value' => ($current_values[$this->slug][$sub_field['slug']] == '' && $this->isWizard) ? $sub_field['default_text'] : $current_values[$this->slug][$sub_field['slug']], - 'is_main_field' => $sub_field['is_main_field'] - ) - ); - break; - case 'textarea': - $field = array( - 'type' => 'textarea', - 'label' => $this->fields[$sub_field['slug']]['name'], - 'params' => array( - 'tooltip' => $this->fields[$sub_field['slug']]['tooltip'], - 'field_name' => $this->slug."[".$sub_field['slug']."]", - 'field_value' => ($current_values[$this->slug][$sub_field['slug']] == '' && $this->isWizard) ? $sub_field['default_text'] : $current_values[$this->slug][$sub_field['slug']], - 'is_main_field' => $sub_field['is_main_field'] - ) - ); - break; - case 'wp_editor': - $field = array( - 'type' => 'wp_editor', - 'label' => $this->fields[$sub_field['slug']]['name'], - 'params' => array( - 'tooltip' => $this->fields[$sub_field['slug']]['tooltip'], - 'field_name' => $this->slug."[".$sub_field['slug']."]", - 'field_value' => ($current_values[$this->slug][$sub_field['slug']] == '' && $this->isWizard) ? $sub_field['default_text'] : $current_values[$this->slug][$sub_field['slug']], - 'is_main_field' => $sub_field['is_main_field'] - ) - ); - break; - case 'image': - $field = array( - 'type' => 'image', - 'label' => $this->fields[$sub_field['slug']]['name'], - 'params' => array( - 'tooltip' => $this->fields[$sub_field['slug']]['tooltip'], - 'field_name' => $this->slug."[".$sub_field['slug']."]", - 'field_value' => $current_values[$this->slug][$sub_field['slug']], - 'download_image' => null, - 'field_key' => $sub_field['slug'], - 'addon_prefix' => $this->slug, - 'is_main_field' => $sub_field['is_main_field'] - ) - ); - - if ( array_key_exists( 'download_image', $current_values[$this->slug] ) && array_key_exists( $sub_field['slug'], $current_values[$this->slug]['download_image'] ) ) { - $field['params']['download_image'] = $current_values[$this->slug]['download_image'][$sub_field['slug']]; - } - break; - case 'file': - $field = array( - 'type' => 'file', - 'label' => $this->fields[$sub_field['slug']]['name'], - 'params' => array( - 'tooltip' => $this->fields[$sub_field['slug']]['tooltip'], - 'field_name' => $this->slug."[".$sub_field['slug']."]", - 'field_value' => $current_values[$this->slug][$sub_field['slug']], - 'download_image' => null, - 'field_key' => $sub_field['slug'], - 'addon_prefix' => $this->slug, - 'is_main_field' => $sub_field['is_main_field'] - ) - ); - - if ( array_key_exists( 'download_image', $current_values[$this->slug] ) && array_key_exists( $sub_field['slug'], $current_values[$this->slug]['download_image'] ) ) { - $field['params']['download_image'] = $current_values[$this->slug]['download_image'][$sub_field['slug']]; - } - - break; - case 'radio': - $field = array( - 'type' => 'enum', - 'label' => $this->fields[$sub_field['slug']]['name'], - 'params' => array( - 'tooltip' => $this->fields[$sub_field['slug']]['tooltip'], - 'field_name' => $this->slug."[".$sub_field['slug']."]", - 'field_value' => $current_values[$this->slug][$sub_field['slug']], - 'enum_values' => $this->fields[$sub_field['slug']]['enum_values'], - 'mapping' => true, - 'field_key' => $sub_field['slug'], - 'mapping_rules' => isset($current_values[$this->slug]['mapping'][$sub_field['slug']]) ? $current_values[$this->slug]['mapping'][$sub_field['slug']] : array(), - 'xpath' => isset($current_values[$this->slug]['xpaths'][$sub_field['slug']]) ? $current_values[$this->slug]['xpaths'][$sub_field['slug']] : '', - 'addon_prefix' => $this->slug, - 'sub_fields' => $this->get_sub_fields($this->fields[$sub_field['slug']], $sub_field['slug'], $current_values), - 'is_main_field' => $sub_field['is_main_field'] - ) - ); - break; - case 'accordion': - $field = array( - 'type' => 'accordion', - 'label' => $this->fields[$sub_field['slug']]['name'], - 'params' => array( - 'tooltip' => $this->fields[$sub_field['slug']]['tooltip'], - 'field_name' => $this->slug."[".$sub_field['slug']."]", - 'field_key' => $sub_field['slug'], - 'addon_prefix' => $this->slug, - 'sub_fields' => $this->get_sub_fields($this->fields[$sub_field['slug']], $sub_field['slug'], $current_values), - 'in_the_bottom' => false - ) - ); - break; - default: - # code... - break; - } - return $field; - } - - /** - * - * Add accordion options - * - * - */ - function add_options( $main_field = false, $title = '', $fields = array() ){ - - if ( ! empty($fields) ) - { - - if ($main_field){ - - $main_field['is_main_field'] = true; - $fields[] = $main_field; + default: + // set the field data to the value of the field after it's been parsed + $data[$field_slug] = $parsedData[$field_slug][$index]; + break; + } - } + // apply mapping rules if they exist + if (!empty($import_options['mapping'][$field_slug])) { + $mapping_rules = json_decode($import_options['mapping'][$field_slug], true); + + if (!empty($mapping_rules) and is_array($mapping_rules)) { + foreach ($mapping_rules as $rule_number => $map_to) { + if (isset($map_to[trim($data[$field_slug])])){ + $data[$field_slug] = trim($map_to[trim($data[$field_slug])]); + break; + } + } + } + } + // -------------------- + } + } + + call_user_func($this->import_function, $post_id, $data, $importData['import'], $importData['articleData'], $importData['logger']); + } - return $this->add_field('accordion_' . $fields[0]['slug'], $title, 'accordion', $fields); + } - } - } + function wpai_api_metabox($post_type, $current_values) { - function add_title($title = '', $tooltip = ''){ + if (!$this->is_active_addon($post_type)) { + return; + } - if (empty($title)) return; + // Escaping is handled in 'helper_metabox_top' method. + echo $this->helper_metabox_top($this->name); - return $this->add_field(sanitize_key($title) . time(), $title, 'title', null, $tooltip); + $visible_fields = 0; - } + foreach ($this->fields as $field_slug => $field_params) { + if ($field_params['is_sub_field']) continue; + $visible_fields++; + } - function add_text($text = '', $is_html = false){ + $counter = 0; - if (empty($text)) return; + foreach ($this->fields as $field_slug => $field_params) { - $count = is_array($this->fields) ? count($this->fields) : 0; + // do not render sub fields + if ($field_params['is_sub_field']) continue; - return $this->add_field(sanitize_key($text) . time() . uniqid() . $count, $text, 'plain_text', null, "", $is_html); + $counter++; - } + $this->render_field($field_params, $field_slug, $current_values, $visible_fields == $counter); - function helper_metabox_top($name) { + } - return ' + // Static HTML is returned by method for display. + echo $this->helper_metabox_bottom(); + + if ( ! empty($this->image_sections) ){ + $is_images_section_enabled = apply_filters('wp_all_import_is_images_section_enabled', true, $post_type); + foreach ($this->image_sections as $k => $section) { + $section_options = array(); + foreach ($this->image_options as $slug => $value) { + $section_options[$section['slug'] . $slug] = $value; + } + if ( ! $is_images_section_enabled and ! $k ){ + $section_options[$section['slug'] . 'is_featured'] = 1; + } + PMXI_API::add_additional_images_section($section['title'], $section['slug'], $current_values, '', true, false, $section['type']); + } + } + + } + + function render_field($field_params, $field_slug, $current_values, $in_the_bottom = false){ + + if (!isset($current_values[$this->slug][$field_slug])) { + $current_values[$this->slug][$field_slug] = isset($field_params['default_text']) ? $field_params['default_text'] : ''; + } + + if ($field_params['type'] == 'text') { + + PMXI_API::add_field( + 'simple', + $field_params['name'], + array( + 'tooltip' => $field_params['tooltip'], + 'field_name' => $this->slug."[".$field_slug."]", + 'field_value' => ( $current_values[$this->slug][$field_slug] == '' && $this->isWizard ) ? $field_params['default_text'] : $current_values[$this->slug][$field_slug] + ) + ); + + } else if ($field_params['type'] == 'textarea') { + + PMXI_API::add_field( + 'textarea', + $field_params['name'], + array( + 'tooltip' => $field_params['tooltip'], + 'field_name' => $this->slug."[".$field_slug."]", + 'field_value' => ( $current_values[$this->slug][$field_slug] == '' && $this->isWizard ) ? $field_params['default_text'] : $current_values[$this->slug][$field_slug] + ) + ); + + } else if ($field_params['type'] == 'wp_editor') { + + PMXI_API::add_field( + 'wp_editor', + $field_params['name'], + array( + 'tooltip' => $field_params['tooltip'], + 'field_name' => $this->slug."[".$field_slug."]", + 'field_value' => ( $current_values[$this->slug][$field_slug] == '' && $this->isWizard ) ? $field_params['default_text'] : $current_values[$this->slug][$field_slug] + ) + ); + + } else if ($field_params['type'] == 'image' or $field_params['type'] == 'file') { + + if (!isset($current_values[$this->slug]['download_image'][$field_slug])) { $current_values[$this->slug]['download_image'][$field_slug] = ''; } + + PMXI_API::add_field( + $field_params['type'], + $field_params['name'], + array( + 'tooltip' => $field_params['tooltip'], + 'field_name' => $this->slug."[".$field_slug."]", + 'field_value' => $current_values[$this->slug][$field_slug], + 'download_image' => $current_values[$this->slug]['download_image'][$field_slug], + 'field_key' => $field_slug, + 'addon_prefix' => $this->slug + + ) + ); + + } else if ($field_params['type'] == 'radio') { + + if (!isset($current_values[$this->slug]['mapping'][$field_slug])) { $current_values[$this->slug]['mapping'][$field_slug] = array(); } + if (!isset($current_values[$this->slug]['xpaths'][$field_slug])) { $current_values[$this->slug]['xpaths'][$field_slug] = ''; } + + PMXI_API::add_field( + 'enum', + $field_params['name'], + array( + 'tooltip' => $field_params['tooltip'], + 'field_name' => $this->slug."[".$field_slug."]", + 'field_value' => $current_values[$this->slug][$field_slug], + 'enum_values' => $field_params['enum_values'], + 'mapping' => true, + 'field_key' => $field_slug, + 'mapping_rules' => $current_values[$this->slug]['mapping'][$field_slug], + 'xpath' => $current_values[$this->slug]['xpaths'][$field_slug], + 'addon_prefix' => $this->slug, + 'sub_fields' => $this->get_sub_fields($field_params, $field_slug, $current_values) + ) + ); + + } else if($field_params['type'] == 'accordion') { + + PMXI_API::add_field( + 'accordion', + $field_params['name'], + array( + 'tooltip' => $field_params['tooltip'], + 'field_name' => $this->slug."[".$field_slug."]", + 'field_key' => $field_slug, + 'addon_prefix' => $this->slug, + 'sub_fields' => $this->get_sub_fields($field_params, $field_slug, $current_values), + 'in_the_bottom' => $in_the_bottom + ) + ); + + } else if($field_params['type'] == 'acf') { + $fieldData = (!empty($field_params['field_obj']->post_content)) ? unserialize($field_params['field_obj']->post_content) : array(); + $fieldData['ID'] = $field_params['field_obj']->ID; + $fieldData['id'] = $field_params['field_obj']->ID; + $fieldData['label'] = $field_params['field_obj']->post_title; + $fieldData['key'] = $field_params['field_obj']->post_name; + if (empty($fieldData['name'])) $fieldData['name'] = $field_params['field_obj']->post_excerpt; + // This function is no longer used in recent versions of the ACF Import Add-On. + if (function_exists('pmai_render_field')) { + echo \wp_kses_post(pmai_render_field($fieldData, ( ! empty($current_values) ) ? $current_values : array() )); + } + } else if($field_params['type'] == 'title'){ + ?> +

?

+ +

+ $value) { + $sub_fields[$key] = array(); + if (is_array($value)){ + if ($field_params['type'] == 'accordion'){ + $sub_fields[$key][] = $this->convert_field($value, $current_values); + } + else + { + foreach ($value as $k => $sub_field) { + if (is_array($sub_field) and ! empty($this->fields[$sub_field['slug']])) + { + $sub_fields[$key][] = $this->convert_field($sub_field, $current_values); + } + } + } + } + } + } + return $sub_fields; + } + + function convert_field($sub_field, $current_values){ + $field = array(); + if (!isset($current_values[$this->slug][$sub_field['slug']])) { + $current_values[$this->slug][$sub_field['slug']] = isset($sub_field['default_text']) ? $sub_field['default_text'] : ''; + } + switch ($this->fields[$sub_field['slug']]['type']) { + case 'text': + $field = array( + 'type' => 'simple', + 'label' => $this->fields[$sub_field['slug']]['name'], + 'params' => array( + 'tooltip' => $this->fields[$sub_field['slug']]['tooltip'], + 'field_name' => $this->slug."[".$sub_field['slug']."]", + 'field_value' => ($current_values[$this->slug][$sub_field['slug']] == '' && $this->isWizard) ? $sub_field['default_text'] : $current_values[$this->slug][$sub_field['slug']], + 'is_main_field' => $sub_field['is_main_field'] + ) + ); + break; + case 'textarea': + $field = array( + 'type' => 'textarea', + 'label' => $this->fields[$sub_field['slug']]['name'], + 'params' => array( + 'tooltip' => $this->fields[$sub_field['slug']]['tooltip'], + 'field_name' => $this->slug."[".$sub_field['slug']."]", + 'field_value' => ($current_values[$this->slug][$sub_field['slug']] == '' && $this->isWizard) ? $sub_field['default_text'] : $current_values[$this->slug][$sub_field['slug']], + 'is_main_field' => $sub_field['is_main_field'] + ) + ); + break; + case 'wp_editor': + $field = array( + 'type' => 'wp_editor', + 'label' => $this->fields[$sub_field['slug']]['name'], + 'params' => array( + 'tooltip' => $this->fields[$sub_field['slug']]['tooltip'], + 'field_name' => $this->slug."[".$sub_field['slug']."]", + 'field_value' => ($current_values[$this->slug][$sub_field['slug']] == '' && $this->isWizard) ? $sub_field['default_text'] : $current_values[$this->slug][$sub_field['slug']], + 'is_main_field' => $sub_field['is_main_field'] + ) + ); + break; + case 'image': + $field = array( + 'type' => 'image', + 'label' => $this->fields[$sub_field['slug']]['name'], + 'params' => array( + 'tooltip' => $this->fields[$sub_field['slug']]['tooltip'], + 'field_name' => $this->slug."[".$sub_field['slug']."]", + 'field_value' => $current_values[$this->slug][$sub_field['slug']], + 'download_image' => null, + 'field_key' => $sub_field['slug'], + 'addon_prefix' => $this->slug, + 'is_main_field' => $sub_field['is_main_field'] + ) + ); + + if ( array_key_exists( 'download_image', $current_values[$this->slug] ) && array_key_exists( $sub_field['slug'], $current_values[$this->slug]['download_image'] ) ) { + $field['params']['download_image'] = $current_values[$this->slug]['download_image'][$sub_field['slug']]; + } + break; + case 'file': + $field = array( + 'type' => 'file', + 'label' => $this->fields[$sub_field['slug']]['name'], + 'params' => array( + 'tooltip' => $this->fields[$sub_field['slug']]['tooltip'], + 'field_name' => $this->slug."[".$sub_field['slug']."]", + 'field_value' => $current_values[$this->slug][$sub_field['slug']], + 'download_image' => null, + 'field_key' => $sub_field['slug'], + 'addon_prefix' => $this->slug, + 'is_main_field' => $sub_field['is_main_field'] + ) + ); + + if ( array_key_exists( 'download_image', $current_values[$this->slug] ) && array_key_exists( $sub_field['slug'], $current_values[$this->slug]['download_image'] ) ) { + $field['params']['download_image'] = $current_values[$this->slug]['download_image'][$sub_field['slug']]; + } + + break; + case 'radio': + $field = array( + 'type' => 'enum', + 'label' => $this->fields[$sub_field['slug']]['name'], + 'params' => array( + 'tooltip' => $this->fields[$sub_field['slug']]['tooltip'], + 'field_name' => $this->slug."[".$sub_field['slug']."]", + 'field_value' => $current_values[$this->slug][$sub_field['slug']], + 'enum_values' => $this->fields[$sub_field['slug']]['enum_values'], + 'mapping' => true, + 'field_key' => $sub_field['slug'], + 'mapping_rules' => isset($current_values[$this->slug]['mapping'][$sub_field['slug']]) ? $current_values[$this->slug]['mapping'][$sub_field['slug']] : array(), + 'xpath' => isset($current_values[$this->slug]['xpaths'][$sub_field['slug']]) ? $current_values[$this->slug]['xpaths'][$sub_field['slug']] : '', + 'addon_prefix' => $this->slug, + 'sub_fields' => $this->get_sub_fields($this->fields[$sub_field['slug']], $sub_field['slug'], $current_values), + 'is_main_field' => $sub_field['is_main_field'] + ) + ); + break; + case 'accordion': + $field = array( + 'type' => 'accordion', + 'label' => $this->fields[$sub_field['slug']]['name'], + 'params' => array( + 'tooltip' => $this->fields[$sub_field['slug']]['tooltip'], + 'field_name' => $this->slug."[".$sub_field['slug']."]", + 'field_key' => $sub_field['slug'], + 'addon_prefix' => $this->slug, + 'sub_fields' => $this->get_sub_fields($this->fields[$sub_field['slug']], $sub_field['slug'], $current_values), + 'in_the_bottom' => false + ) + ); + break; + default: + # code... + break; + } + return $field; + } + + /** + * + * Add accordion options + * + * + */ + function add_options( $main_field = false, $title = '', $fields = array() ){ + + if ( ! empty($fields) ) + { + + if ($main_field){ + + $main_field['is_main_field'] = true; + $fields[] = $main_field; + + } + + return $this->add_field('accordion_' . $fields[0]['slug'], $title, 'accordion', $fields); + + } + + } + + function add_title($title = '', $tooltip = ''){ + + if (empty($title)) return; + + return $this->add_field(sanitize_key($title) . time(), $title, 'title', null, $tooltip); + + } + + function add_text($text = '', $is_html = false){ + + if (empty($text)) return; + + $count = is_array($this->fields) ? count($this->fields) : 0; + + return $this->add_field(sanitize_key($text) . time() . uniqid() . $count, $text, 'plain_text', null, "", $is_html); + + } + + function helper_metabox_top($name) { + + return '