rapid-addon.php 31 KB
Newer Older
imac's avatar
imac committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
<?php

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,
		);	

		function __construct($name, $slug) {

			$this->name = $name;
			$this->slug = $slug;

		}

		function set_import_function($name) {
			$this->import_function = $name;
		}

		function set_post_saved_function($name) {
			$this->post_saved_function = $name;
		}

		function is_active_addon($post_type = null) {
			
			if ( ! is_plugin_active('wp-all-import-pro/wp-all-import-pro.php') and ! is_plugin_active('wp-all-import/plugin.php') ){
				return 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 ($addon_active){
				
				$current_theme = wp_get_theme();

				$parent_theme = $current_theme->parent();				

				$theme_name = $current_theme->get('Name');
				
				$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 ! empty($this->active_plugins) ){

					include_once( ABSPATH . 'wp-admin/includes/plugin.php' );

					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_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) {

			$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);

			$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;

		}

		/**
		* 
		* 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();

			foreach ($this->fields as $field_slug => $field_params) {
				if (in_array($field_params['type'], array('title', 'plain_text'))) 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;
			}			

			if ( ! empty($this->options) ){
				foreach ($this->options as $slug => $value) {
					$options_arr[$slug] = $value;
				}
			}

			$options_arr[$this->slug]   = $options_list;
			$options_arr['rapid_addon'] = plugin_basename( __FILE__ );

			return $options_arr;

		}

		function wpai_api_options($all_options) {

			$all_options = $all_options + $this->options_array();

			return $all_options;

		}


		function wpai_api_register($addons) {

			if (empty($addons[$this->slug])) {
				$addons[$this->slug] = 1;
			}

			return $addons;

		}


		function wpai_api_parse($functions) {

			$functions[$this->slug] = array($this, 'parse');
			return $functions;

		}

		function wpai_api_post_saved($functions){
			$functions[$this->slug] = array($this, 'post_saved');
			return $functions;
		}


		function wpai_api_import($functions) {

			$functions[$this->slug] = array($this, 'import');
			return $functions;

		}

		function post_saved( $importData ){

			if (is_callable($this->post_saved_function))
				call_user_func($this->post_saved_function, $importData['pid'], $importData['import'], $importData['logger']);
			
		}

		function import($importData, $parsedData) {

			if (!$this->is_active_addon($importData['post_type'])) {
				return;
			}

			$import_options = $importData['import']['options'][$this->slug];

	//		echo "<pre>";
	//		print_r($import_options);
	//		echo "</pre>";

			if ( ! empty($parsedData) )	{

				$this->logger = $importData['logger'];

				$post_id = $importData['pid'];
				$index = $importData['i'];

				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

							$image_url_or_path = $parsedData[$field_slug][$index];

							$download = $import_options['download_image'][$field_slug];

							$uploaded_image = PMXI_API::upload_image($post_id, $image_url_or_path, $download, $importData['logger'], true);

							$data[$field_slug] = array(
								"attachment_id" => $uploaded_image,
								"image_url_or_path" => $image_url_or_path,
								"download" => $download
							);

							break;

						case 'file':

							$image_url_or_path = $parsedData[$field_slug][$index];

							$download = $import_options['download_image'][$field_slug];

							$uploaded_file = PMXI_API::upload_image($post_id, $image_url_or_path, $download, $importData['logger'], true, "", "files");

							$data[$field_slug] = array(
								"attachment_id" => $uploaded_file,
								"image_url_or_path" => $image_url_or_path,
								"download" => $download
							);

							break;
						
						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 ($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 (!empty($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']);
			}

		}


		function wpai_api_metabox($post_type, $current_values) {

			if (!$this->is_active_addon($post_type)) {
				return;
			}

			echo $this->helper_metabox_top($this->name);

			$visible_fields = 0;

			foreach ($this->fields as $field_slug => $field_params) {
				if ($field_params['is_sub_field']) continue;
				$visible_fields++;
			}

			$counter = 0;

			foreach ($this->fields as $field_slug => $field_params) {				

				// do not render sub fields
				if ($field_params['is_sub_field']) continue;		

				$counter++;		

				$this->render_field($field_params, $field_slug, $current_values, $visible_fields == $counter);										

				//if ( $field_params['type'] != 'accordion' ) echo "<br />";				

			}

			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] = ''; }

			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]
					)
				);

			} 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]
					)
				);

			} 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'] == 'title'){
				?>
				<h4 class="wpallimport-add-on-options-title"><?php _e($field_params['name'], 'wp_all_import_plugin'); ?><?php if ( ! empty($field_params['tooltip'])): ?><a href="#help" class="wpallimport-help" title="<?php echo $field_params['tooltip']; ?>" style="position:relative; top: -1px;">?</a><?php endif; ?></h4>				
				<?php

			} else if($field_params['type'] == 'plain_text'){
				if ($field_params['is_html']):					
					echo $field_params['name'];				
				else:
					?>
					<p style="margin: 0 0 12px 0;"><?php echo $field_params['name'];?></p>
					<?php
				endif;
			}


		}
		/**
		*
		* Helper function for nested radio fields
		*
		*/
		function get_sub_fields($field_params, $field_slug, $current_values){
			$sub_fields = array();	
			if ( ! empty($field_params['enum_values']) ){										
				foreach ($field_params['enum_values'] as $key => $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();
			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']],
							'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']],
							'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' => $current_values[$this->slug]['download_image'][$sub_field['slug']],
							'field_key' => $sub_field['slug'],
							'addon_prefix' => $this->slug,
							'is_main_field' => $sub_field['is_main_field']
						)
					);
				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' => $current_values[$this->slug]['download_image'][$sub_field['slug']],
							'field_key' => $sub_field['slug'],
							'addon_prefix' => $this->slug,
							'is_main_field' => $sub_field['is_main_field']
						)
					);
					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' => $current_values[$this->slug]['mapping'][$sub_field['slug']],
							'xpath' => $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;

			return $this->add_field(sanitize_key($text) . time() . uniqid() . count($this->fields), $text, 'plain_text', null, "", $is_html);			

		}			

		function helper_metabox_top($name) {

			return '
			<style type="text/css">
				.wpallimport-plugin .wpallimport-addon div.input {
					margin-bottom: 15px;
				}
				.wpallimport-plugin .wpallimport-addon .custom-params tr td.action{
					width: auto !important;
				}
				.wpallimport-plugin .wpallimport-addon .wpallimport-custom-fields-actions{
					right:0 !important;
				}
				.wpallimport-plugin .wpallimport-addon table tr td.wpallimport-enum-input-wrapper{
					width: 80%;
				}
				.wpallimport-plugin .wpallimport-addon table tr td.wpallimport-enum-input-wrapper input{
					width: 100%;
				}
				.wpallimport-plugin .wpallimport-addon .wpallimport-custom-fields-actions{
					float: right;	
					right: 30px;
					position: relative;				
					border: 1px solid #ddd;
					margin-bottom: 10px;
				}
				
				.wpallimport-plugin .wpallimport-addon .wpallimport-sub-options {
					margin-bottom: 15px;				
					margin-top: -16px;	
				}
				.wpallimport-plugin .wpallimport-addon .wpallimport-sub-options .wpallimport-content-section{
					padding-bottom: 8px;
					margin:0; 
					border: none;
					padding-top: 1px;
					background: #f1f2f2;				
				}		
				.wpallimport-plugin .wpallimport-addon .wpallimport-sub-options .wpallimport-collapsed-header{
					padding-left: 13px;
				}
				.wpallimport-plugin .wpallimport-addon .wpallimport-sub-options .wpallimport-collapsed-header h3{
					font-size: 14px;
					margin: 6px 0;
				}

				.wpallimport-plugin .wpallimport-addon .wpallimport-sub-options-full-width{
					bottom: -40px;
					margin-bottom: 0;
					margin-left: -25px;
					margin-right: -25px;
					position: relative;						
				}		
				.wpallimport-plugin .wpallimport-addon .wpallimport-sub-options-full-width .wpallimport-content-section{					
					margin:0; 					
					border-top:1px solid #ddd; 
					border-bottom: none; 
					border-right: none; 
					border-left: none; 
					background: #f1f2f2;									
				}					
				.wpallimport-plugin .wpallimport-addon .wpallimport-sub-options-full-width .wpallimport-collapsed-header h3{					
					margin: 14px 0;
				}

				.wpallimport-plugin .wpallimport-addon .wpallimport-dependent-options{
					margin-left: 1px;
					margin-right: -1px;					
				}		
				.wpallimport-plugin .wpallimport-addon .wpallimport-dependent-options .wpallimport-content-section{
					border: 1px solid #ddd;
					border-top: none;
				}
				.wpallimport-plugin .wpallimport-addon .wpallimport-full-with-bottom{
					margin-left: -25px; 
					margin-right: -25px;
				}
				.wpallimport-plugin .wpallimport-addon .wpallimport-full-with-not-bottom{
					margin: 25px -1px 25px 1px;
				}
				.wpallimport-plugin .wpallimport-addon .wpallimport-full-with-not-bottom .wpallimport-content-section{
					border: 1px solid #ddd;
				}
				.wpallimport-plugin .wpallimport-addon .wpallimport-add-on-options-title{
					font-size: 14px;
  					margin: 45px 0 15px 0;
				}
			</style>
			<div class="wpallimport-collapsed wpallimport-section wpallimport-addon '.$this->slug.' closed">
				<div class="wpallimport-content-section">
					<div class="wpallimport-collapsed-header">
						<h3>'.__($name,'pmxi_plugin').'</h3>	
					</div>
					<div class="wpallimport-collapsed-content" style="padding: 0;">
						<div class="wpallimport-collapsed-content-inner">
							<table class="form-table" style="max-width:none;">
								<tr>
									<td colspan="3">';
		}

		function helper_metabox_bottom() {

			return '				</td>
								</tr>
							</table>
						</div>
					</div>
				</div>
			</div>';

		}

		/**
		*
		* simply add an additional section for attachments
		*
		*/
		function import_files( $slug, $title ){
			$this->import_images( $slug, $title, 'files');
		}

		/**
		*
		* simply add an additional section 
		*
		*/
		function import_images( $slug, $title, $type = 'images' ){
			
			if ( empty($title) or empty($slug) ) return;

			$section_slug = 'pmxi_' . $slug; 

			$this->image_sections[] = array(
				'title' => $title,
				'slug'  => $section_slug,
				'type'  => $type
			);			
			
			foreach ($this->image_options as $option_slug => $value) {
				$this->add_option($section_slug . $option_slug, $value);
			}

			if (count($this->image_sections) > 1){
				add_filter('wp_all_import_is_show_add_new_images', array($this, 'filter_is_show_add_new_images'), 10, 2);
			}

			add_filter('wp_all_import_is_allow_import_images', array($this, 'is_allow_import_images'), 10, 2);			
			
			if (function_exists($slug)) add_action( $section_slug, $slug, 10, 4);
		}			
			/**
			*
			* filter to allow import images for free edition of WP All Import
			*
			*/
			function is_allow_import_images($is_allow, $post_type){
				return ($this->is_active_addon($post_type)) ? true : $is_allow;
			}

		/**
		*
		* filter to control additional images sections
		*
		*/
		function additional_sections($sections){
			if ( ! empty($this->image_sections) ){
				foreach ($this->image_sections as $add_section) {
					$sections[] = $add_section;
				}
			}
			
			return $sections;
		}
			/**
			*
			* remove the 'Don't touch existing images, append new images' when more than one image section is in use.
			*
			*/
			function filter_is_show_add_new_images($is_show, $post_type){
				return ($this->is_active_addon($post_type)) ? false : $is_show;
			}

		/**
		*
		* disable the default images section
		*
		*/		
		function disable_default_images($post_type = false){
									
			add_filter('wp_all_import_is_images_section_enabled', array($this, 'is_enable_default_images_section'), 10, 2);

		}
			function is_enable_default_images_section($is_enabled, $post_type){						
				
				return ($this->is_active_addon($post_type)) ? false : true;
								
			}

		function helper_parse($parsingData, $options) {

			extract($parsingData);

			$data = array(); // parsed data

			if ( ! empty($import->options[$this->slug])){

				$this->logger = $parsingData['logger'];

				$cxpath = $xpath_prefix . $import->xpath;

				$tmp_files = array();

				foreach ($options[$this->slug] as $option_name => $option_value) {					
					if ( isset($import->options[$this->slug][$option_name]) and $import->options[$this->slug][$option_name] != '') {						
						if ($import->options[$this->slug][$option_name] == "xpath") {
							if ($import->options[$this->slug]['xpaths'][$option_name] == ""){
								$count and $this->data[$option_name] = array_fill(0, $count, "");
							} else {
								$data[$option_name] = XmlImportParser::factory($xml, $cxpath, $import->options[$this->slug]['xpaths'][$option_name], $file)->parse($records);
								$tmp_files[] = $file;						
							}
						} 
						else {							
							$data[$option_name] = XmlImportParser::factory($xml, $cxpath, $import->options[$this->slug][$option_name], $file)->parse();
							$tmp_files[] = $file;
						}


					} else {
						$data[$option_name] = array_fill(0, $count, "");
					}

				}

				foreach ($tmp_files as $file) { // remove all temporary files created
					unlink($file);
				}

			}

			return $data;
		}


		function can_update_meta($meta_key, $import_options) {

			//echo "<pre>";
			//print_r($import_options['options']);
			//echo "</pre>";
			
			$import_options = $import_options['options'];

			if ($import_options['update_all_data'] == 'yes') return true;

			if ( ! $import_options['is_update_custom_fields'] ) return false;			

			if ($import_options['update_custom_fields_logic'] == "full_update") return true;
			if ($import_options['update_custom_fields_logic'] == "only" and ! empty($import_options['custom_fields_list']) and is_array($import_options['custom_fields_list']) and in_array($meta_key, $import_options['custom_fields_list']) ) return true;
			if ($import_options['update_custom_fields_logic'] == "all_except" and ( empty($import_options['custom_fields_list']) or ! in_array($meta_key, $import_options['custom_fields_list']) )) return true;

			return false;

		}

		function can_update_taxonomy($tax_name, $import_options) {

			//echo "<pre>";
			//print_r($import_options['options']);
			//echo "</pre>";
			
			$import_options = $import_options['options'];

			if ($import_options['update_all_data'] == 'yes') return true;

			if ( ! $import_options['is_update_categories'] ) return false;			

			if ($import_options['update_categories_logic'] == "full_update") return true;
			if ($import_options['update_categories_logic'] == "only" and ! empty($import_options['taxonomies_list']) and is_array($import_options['taxonomies_list']) and in_array($tax_name, $import_options['taxonomies_list']) ) return true;
			if ($import_options['update_categories_logic'] == "all_except" and ( empty($import_options['taxonomies_list']) or ! in_array($tax_name, $import_options['taxonomies_list']) )) return true;

			return false;

		}

		function can_update_image($import_options) {

			$import_options = $import_options['options'];

			if ($import_options['update_all_data'] == 'yes') return true;

			if (!$import_options['is_update_images']) return false;			

			if ($import_options['is_update_images']) return true;			

			return false;
		}


		function admin_notice_ignore() {
			if (isset($_GET[$this->slug.'_ignore']) && '0' == $_GET[$this->slug.'_ignore'] ) {
				update_option($this->slug.'_ignore', 'true');
			}
		}

		function display_admin_notice() {


			if ($this->notice_text) {
				$notice_text = $this->notice_text;
			} else {
				$notice_text = $this->name.' requires WP All Import <a href="http://www.wpallimport.com/" target="_blank">Pro</a> or <a href="http://wordpress.org/plugins/wp-all-import" target="_blank">Free</a>.';
			}

			if (!get_option(sanitize_key($this->slug).'_notice_ignore')) {

				?>

	    		<div class="error notice is-dismissible wpallimport-dismissible" style="margin-top: 10px;" rel="<?php echo sanitize_key($this->slug); ?>">
	    		    <p><?php _e(
		    		    	sprintf(
	    			    		$notice_text,
	    			    		'?'.$this->slug.'_ignore=0'
	    			    	), 
	    		    		'rapid_addon_'.$this->slug
	    		    	); ?></p>
			    </div>

				<?php

			}

		}

		/*
		*
		* $conditions - array('themes' => array('Realia'), 'plugins' => array('plugin-directory/plugin-file.php', 'plugin-directory2/plugin-file.php')) 
		*
		*/
		function admin_notice($notice_text = '', $conditions = array()) {

			$is_show_notice = false;

			include_once( ABSPATH . 'wp-admin/includes/plugin.php' );

			if ( ! is_plugin_active('wp-all-import-pro/wp-all-import-pro.php') and ! is_plugin_active('wp-all-import/plugin.php') ){
				$is_show_notice = true;
			}

			// Supported Themes
			if ( ! $is_show_notice and ! empty($conditions['themes']) ){

				$themeInfo    = wp_get_theme();
				$parentInfo = $themeInfo->parent();				
				$currentTheme = $themeInfo->get('Name');
				
				$is_show_notice = in_array($currentTheme, $conditions['themes']) ? false : true;				

				if ( $is_show_notice and $parentInfo ){
					$parent_theme = $parentInfo->get('Name');
					$is_show_notice = in_array($parent_theme, $conditions['themes']) ? false : true;					
				}

			}			

			// Required Plugins
			if ( ! $is_show_notice and ! empty($conditions['plugins']) ){				

				$requires_counter = 0;
				foreach ($conditions['plugins'] as $plugin) {
					if ( is_plugin_active($plugin) ) $requires_counter++;
				}

				if ($requires_counter != count($conditions['plugins'])){ 					
					$is_show_notice = true;			
				}

			}

			if ( $is_show_notice ){

				if ( $notice_text != '' ) {
					$this->notice_text = $notice_text;
				}

				add_action('admin_notices', array($this, 'display_admin_notice'));
			}

		}

		function log( $m = false){		

			$m and $this->logger and call_user_func($this->logger, $m);

		}
	}	

}