868497fd4f39bd46b509e5368028c138b2baac25.svn-base 51.8 KB
Newer Older
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 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996
<?php if ( ! defined( 'ABSPATH' ) ) {
	die;
} // Cannot access pages directly.
/**
 * Last edit: 2020-05-21
 *
 * INFOS AND TODOS:
 *
 * IDEAS
 * - import options from file
 * - chunk upload
 */
/**
 * ToDos:
 * - possibility to override indluded files from path
 */
/**
 * Available fields:
 * - ACE field
 * - attached
 * - backup
 * - button
 * - botton_bar
 * - card
 * - checkbox
 * - color
 * - content
 * - date
 * - editor
 * - gallery
 * - group/accordion item
 * - hidden
 * - image
 * - image_select
 * - meta
 * - notice
 * - number
 * - password
 * - radio
 * - range
 * - select
 * - switcher
 * - tab
 * - tap_list
 * - text
 * - textarea
 * - typography
 * - upload
 * - video mp4/oembed
 */
/**
 * Standard args for all field:
 * - type
 * - id
 * - title
 *   - description
 * - class
 * - attributes
 * - before
 * - after
 */
if ( ! class_exists( 'Exopite_Simple_Options_Framework' ) ) :

	class Exopite_Simple_Options_Framework {

		/**
		 *
		 * dirname
		 * @access public
		 * @var string
		 *
		 */
		public $dirname = '';

		/**
		 *
		 * unique
		 * @access public
		 * @var string
		 *
		 */
		public $unique = '';

		/**
		 *
		 * notice
		 * @access public
		 * @var boolean
		 *
		 */
		public $notice = false;

		/**
		 *
		 * settings
		 * @access public
		 * @var array
		 *
		 */
		public $config = array();

		/**
		 *
		 * options
		 * @access public
		 * @var array
		 *
		 */
		public $fields = array();
		public $elements = array();

		public $is_multilang = null;

		public $multilang = false;
		public $lang_default;
		public $lang_current;

		public $languages = array();

		public $version;

		// public $debug = true;
		public $debug = false;

		/**
		 *
		 * options store
		 * @access public
		 * @var array
		 *
		 */
		public $db_options = array();

		/**
		 * Sets the type to  metabox|menu
		 * @var string
		 */
		private $type;

		/**
		 * @var object WP_Error
		 */
		protected $errors;

		/**
		 * @var array required fields for $type = menu
		 */
		protected $required_keys_all_types = array();

		/**
		 * @var array required fields for $type = menu
		 */
		protected $required_keys_menu = array( 'id', 'menu_title', 'plugin_basename' );

		/**
		 * @var array required fields for $type = metabox
		 */
		protected $required_keys_metabox = array( 'id', 'post_types', 'title', 'capability' );

		public function __construct( $config, $elements ) {

			// If we are not in admin area exit.
			if ( ! is_admin() ) {
				return;
			}

			$this->version = '20191203';

			// TODO: Do sanitize $config['id']
			$this->unique = $config['id'];

			// Filter for override every exopite $config and $fields
			// $this->config = apply_filters( 'exopite_sof_config', $config );
			// $this->elements = apply_filters( 'exopite_sof_options', $fields );

			// Filter for override $config and $fields with respect to $config and $fields
			$this->config = apply_filters( 'exopite_sof_config_' . $this->unique, $config );
			$this->elements = apply_filters( 'exopite_sof_options_' . $this->unique, $elements );

			// now is_menu() and is_metabox() available

			$this->load_textdomain();

			$this->get_fields();

			$this->check_required_configuration_keys();

			$this->load_classes();

			$this->set_properties();

			$this->setup_multilang();

			$this->include_field_classes();

			$this->define_shared_hooks();

			$this->define_hooks();

		}

		public function get_mo_file() {
			$path = wp_normalize_path( dirname( __FILE__ ) ) . '/lang';
			$domain = 'exopite-sof';
			if ( function_exists( 'determine_locale' ) ) {
				$locale = determine_locale();
			} else {
				$locale = get_locale();
			}

			return $path . '/' . $domain . '-' . $locale . '.mo';
		}

		public function load_textdomain() {

			$mofile = $this->get_mo_file();
			load_textdomain( 'exopite-sof', $mofile );

		}

		protected function setup_multilang() {

			// Srt Defaults for all cases
			$multilang_defaults = Exopite_Simple_Options_Framework_Helper::get_language_defaults();

			if ( is_array( $multilang_defaults ) ) {

				$this->config['multilang'] = $this->multilang = $multilang_defaults;

				$this->lang_current = $this->multilang['current'];
				$this->lang_default = $this->multilang['default'];
				$this->languages    = $this->multilang['languages'];

			}

		}

		/**
		 * Return the array of languages except current language
		 *
		 * @return array $languages_except_current
		 *
		 */
		public function languages_except_current_language() {

			$all_languages = $this->languages;

			if ( empty( $all_languages ) ) {
				return $all_languages;
			}

			$languages_except_current = array_diff( $all_languages, array( $this->lang_current ) );

			unset( $all_languages );

			return $languages_except_current;

		}

		/**
		 * Checks for required keys in configuration array
		 * and throw admin error if a required key is missing
		 */
		protected function check_required_configuration_keys() {
			// instantiate the Wp_Error for $this->errors
			$this->errors = new WP_Error();

			$required_key_array = $this->required_keys_all_types;

			if ( $this->is_menu() ) {
				$required_key_array = $this->required_keys_menu;
			}

			if ( $this->is_metabox() ) {
				$required_key_array = $this->required_keys_metabox;
			}

			// Loop through all required keys array to check if every required key is set.
			if ( ! empty( $required_key_array ) && ! empty( $this->config ) ) {

				foreach ( $required_key_array as $key ) :

					if ( ! array_key_exists( $key, $this->config ) ) {
						// Add error message to the WP_Error object
						$this->errors->add( "missing_config_key_{$key}", sprintf( eac_attr__( "%s is missing in the configuration array", 'exopite-sof' ), $key ) );
					}

				endforeach;

				$errors_array = $this->errors->get_error_messages();

				// if the errors are logged, add the admin display hook
				if ( ! empty( $errors_array ) ) {
					add_action( 'admin_notices', array( $this, 'display_admin_error' ) );
				}

			} // ! empty( $required_key_array )

		} //check_required_keys()

		/**
		 * Set Properties of the class
		 */
		protected function set_properties() {

			if ( isset( $this->config['type'] ) ) {
				$this->set_type( $this->config['type'] );
			}

			// Parse the configuration against default values for Menu
			if ( $this->is_menu() ) {
				$default_menu_config = $this->get_config_default_menu();
				$this->config        = wp_parse_args( $this->config, $default_menu_config );

				// override option type to nullify 'simple' even if added
				$this->config['options'] = ''; // so, even if options is 'simple', we make it non-simple

			}

			// Parse the configuration against default values for Metabox
			if ( $this->is_metabox() ) {
				$default_metabox_config = $this->get_config_default_metabox();
				$this->config           = wp_parse_args( $this->config, $default_metabox_config );

				// This override s done for testing active plugin for qTranX and Wp-Multilang
				if ( $this->is_special_multilang_active() ) {

					$this->config['multilang'] = true; // so, even if multilingual was TRUE, we make it FALSE
					$this->config['options']   = false; // We can only save data for special languages in non-simple options

				} else {
					// override multilang true so that we dont save meta for language
					$this->config['multilang'] = false; // so, even if multilingual was TRUE, we make it FALSE

				}

			}

			$this->set_is_multilang_from_config();

			$this->config['is_options_simple'] = ( $this->is_options_simple() ) ? true : false;

			$this->dirname = wp_normalize_path( dirname( __FILE__ ) );
		}

		public function is_special_multilang_active() {

			return Exopite_Simple_Options_Framework_Helper::is_special_multilang_plugin_active();

		}

		public function display_error() {
			add_action( 'admin_notices', array( $this, 'display_admin_error' ) );
		}

		public function display_admin_error() {

			$class        = 'notice notice-error';
			$message      = '';
			$errors_array = $this->errors->get_error_messages();


			if ( ! empty( $errors_array ) ) {
				// Get the error messages from the array
				$message .= esc_html( implode( ', ', $errors_array ) );
			} else {
				// if no message is set, throw generic error message
				$message .= eac_attr__( 'Irks! An un-known error has occurred.', 'exopite-sof' );
			}

			printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) );
		}

		/**
		 * Register all of the hooks shared by all $type  metabox | menu
		 */
		protected function define_shared_hooks() {

			// Upload hooks are only required for both,
			Exopite_Simple_Options_Framework_Upload::add_hooks();

			//scripts and styles
			add_action( 'admin_enqueue_scripts', array( $this, 'load_scripts_styles' ) );

			/**
			 * Add "code" plugin for TinyMCE
			 * @link https://www.tinymce.com/docs/plugins/code/
			 */
			add_filter( 'mce_external_plugins', array( $this, 'mce_external_plugins' ) );

		}//define_shared_hooks()

		protected function define_hooks() {

			if ( $this->is_menu() ) {

				$this->define_menu_hooks();

			} elseif ( $this->is_metabox() ) {

				$this->define_metabox_hooks();

			}

		}

		/**
		 * Register all of the hooks related to 'menu' functionality
		 *
		 * @access   protected
		 */
		protected function define_menu_hooks() {

			/**
			 * Load options only if menu
			 * on metabox, page id is not yet available
			 */
			$this->db_options = apply_filters( 'exopite_sof_menu_get_options', get_option( $this->unique ), $this->unique );


			add_action( 'admin_init', array( $this, 'register_setting' ) );
			add_action( 'admin_menu', array( $this, 'add_admin_menu' ) );
			add_action( 'wp_ajax_exopite-sof-export-options', array( $this, 'export_options' ) );
			add_action( 'wp_ajax_exopite-sof-import-options', array( $this, 'import_options' ) );
			add_action( 'wp_ajax_exopite-sof-reset-options', array( $this, 'reset_options' ) );

			if ( isset( $this->config['plugin_basename'] ) && ! empty( $this->config['plugin_basename'] ) ) {
				add_filter( 'plugin_action_links_' . $this->config['plugin_basename'], array(
					$this,
					'plugin_action_links'
				) );
			}

		}

		/**
		 * Register all of the hooks related to 'metabox' functionality
		 *
		 * @access   protected
		 */
		protected function define_metabox_hooks() {

			/**
			 * Add metabox and register custom fields
			 *
			 * @link https://code.tutsplus.com/articles/rock-solid-wordpress-30-themes-using-custom-post-types--net-12093
			 */
			add_action( 'admin_init', array( $this, 'add_meta_box' ) );
			add_action( 'save_post', array( $this, 'save' ) );

		}

		/**
		 * Sets the $type property
		 *
		 * @param string $config_type
		 */
		protected function set_type( $config_type ) {

			$config_type = sanitize_key( $config_type );

			switch ( $config_type ) {
				case ( 'menu' ):
					$this->type = 'menu';
					break;

				case ( 'metabox' ):
					$this->type = 'metabox';
					break;

				default:
					$this->type = '';
			}

		}

		/**
		 * @return bool true if its a metabox type
		 */
		protected function is_metabox() {

			return ( $this->type === 'metabox' ) ? true : false;
		}

		/**
		 * @return bool true if its a metabox type
		 */
		protected function is_menu() {

			return ( $this->type === 'menu' ) ? true : false;
		}

		/**
		 * @return bool true if its a 'options'   => 'simple' in $config
		 */
		protected function is_options_simple() {

			// 'options'    => 'simple' in $config only required for metabox type
			return ( ! $this->is_menu() && isset( $this->config['options'] ) && $this->config['options'] === 'simple' ) ? true : false;
		}

		protected function set_is_multilang_from_config() {

			/**
			 * We need to store the value from $config to a property, so that we can keep track of the actual input
			 */

			/**
			 * Make sure if metabox and simple options are activated,
			 * then multilang is disabled.
			 * Enabled multilang is only required for qTranslate-X.
			 */
			if ( $this->is_metabox() && $this->is_options_simple() ) {
				$this->is_multilang = $this->config['is_multilang'] = false;
			}

			$this->is_multilang = $this->config['is_multilang'] = isset( $this->config['multilang'] ) && ( $this->config['multilang'] === true ) ? true : false;

		}

		/**
		 * @return bool true if multilang is set to true
		 */
		protected function is_multilang() {

			return $this->is_multilang;

		}

		/**
		 * @return bool true if its menu options
		 */
		protected function is_menu_page_loaded() {

			$current_screen = get_current_screen();

			return substr( $current_screen->id, - strlen( $this->unique ) ) === $this->unique;

		}

		/**
		 * check if the admin screen is of the post_type defined in config
		 * @return bool true if its menu options
		 */
		protected function is_metabox_enabled_post_type() {

			if ( ! isset( $this->config['post_types'] ) ) {
				return false;
			}

			$current_screen = get_current_screen();

			$post_type_loaded = $current_screen->id;

			return ( in_array( $post_type_loaded, $this->config['post_types'] ) ) ? true : false;

		}


		// for TinyMCE Code Plugin
		public function mce_external_plugins( $plugins ) {
			$url             = $this->get_url( $this->dirname );
			$base            = trailingslashit( join( '/', array( $url, 'assets' ) ) );
			$plugins['code'] = $base . 'plugin.code.min.js';

			return $plugins;
		}

		public function import_options() {

			$retval = 'error';

			$nonce_v = wp_verify_nonce( $_POST['wpnonce'], 'exopite_sof_backup' );
			$nonce = $_POST['wpnonce'];

			if ( isset( $_POST['unique'] ) && ! empty( $_POST['value'] ) && isset( $_POST['wpnonce'] ) && wp_verify_nonce( $_POST['wpnonce'], 'exopite_sof_backup' ) ) {

				$option_key = sanitize_key( $_POST['unique'] );

				//Using json_decode
				$value = json_decode( stripslashes( $_POST['value'] ), true );

				if ( is_array( $value ) ) {

					update_option( $option_key, $value );
					$retval = 'success';

				}

			}

			die( $retval );

		}

		public function export_options() {

			if ( isset( $_GET['export'] ) && isset( $_GET['wpnonce'] ) && wp_verify_nonce( $_GET['wpnonce'], 'exopite_sof_backup' ) ) {

				$option_key = sanitize_key( $_GET['export'] );

				header( 'Content-Type: plain/text' );
				header( 'Content-disposition: attachment; filename=exopite-sof-options-' . gmdate( 'd-m-Y' ) . '.txt' );
				header( 'Content-Transfer-Encoding: binary' );
				header( 'Pragma: no-cache' );
				header( 'Expires: 0' );

				// Using json_encode()
				echo json_encode( get_option( $option_key ) );

			}

			die();
		}

		public function reset_options() {

			$retval = 'error';

			if ( isset( $_POST['unique'] ) && isset( $_POST['wpnonce'] ) && wp_verify_nonce( $_POST['wpnonce'], 'exopite_sof_backup' ) ) {

				delete_option( sanitize_key( $_POST['unique'] ) );

				$retval = 'success';

			}

			die( $retval );
		}

		/**
		 * Load classes
		 */
		public function load_classes() {

			require_once 'multilang-class.php';
			require_once 'fields-class.php';
			require_once 'upload-class.php';
			require_once 'sanitize-class.php';

		}

		/**
		 * Get url from path
		 * works only for local urls
		 *
		 * @param  string $path the path
		 *
		 * @return string   the generated url
		 */
		public function get_url( $path = '' ) {

			$url = str_replace(
				wp_normalize_path( untrailingslashit( ABSPATH ) ),
				site_url(),
				$path
			);

			return $url;
		}

		public function locate_template( $type ) {

			/**
			 * Ideas:
			 * - May extend this with override.
			 */
			// This should be the name of directory in theme/ child theme
			$override_dir_name = 'exopite';
			$fields_dir_name   = 'fields';

			$template = join( DIRECTORY_SEPARATOR, array( $this->dirname, $fields_dir_name, $type . '.php' ) );

			return $template;

		}

		/**
		 * Register "settings" for plugin option page in plugins list
		 *
		 * @param array $links plugin links
		 *
		 * @return array possibly modified $links
		 */
		public function plugin_action_links( $links ) {
			/**
			 *  Documentation : https://codex.wordpress.org/Plugin_API/Filter_Reference/plugin_action_links_(plugin_file_name)
			 */

			// BOOL of settings is given true | false
			if ( is_bool( $this->config['settings_link'] ) ) {

				// FALSE: If it is false, no need to go further
				if ( ! $this->config['settings_link'] ) {
					return $links;
				}

				// TRUE: if Settings link is not defined, lets create one
				if ( $this->config['settings_link'] ) {

					$options_base_file_name = sanitize_file_name( $this->config['parent'] );

					$options_page_id = $this->unique;

					$settings_link = "{$options_base_file_name}?page={$options_page_id}";

					$settings_link_array = array(
						'<a href="' . admin_url( $settings_link ) . '">' . __( 'Settings', '' ) . '</a>',
					);

					return array_merge( $settings_link_array, $links );
				}
			} // if ( is_bool( $this->config['settings_link'] ) )

			// URL of settings is given
			if ( ! is_bool( $this->config['settings_link'] ) && ! is_array( $this->config['settings_link'] ) ) {
				$settings_link = esc_url( $this->config['settings_link'] );

				return array_merge( $settings_link, $links );
			}

			// Array of settings_link is given
			if ( is_array( $this->config['settings_link'] ) ) {


				$settings_links_config_array = $this->config['settings_link'];
				$settings_link_array         = array();

				foreach ( $settings_links_config_array as $link ) {

					$link_text         = isset( $link['text'] ) ? sanitize_text_field( $link['text'] ) : __( 'Settings', '' );
					$link_url_un_clean = isset( $link['url'] ) ? $link['url'] : '#';

					$link_type = isset( $link['type'] ) ? sanitize_key( $link['type'] ) : 'default';

					switch ( $link_type ) {
						case ( 'external' ):
							$link_url = esc_url_raw( $link_url_un_clean );
							break;

						case ( 'file' ):
							$link_url = admin_url( sanitize_file_name( $link_url_un_clean ) );
							break;

						default:

							if ( $this->config['submenu'] ) {

								$options_base_file_name = sanitize_file_name( $this->config['parent'] );

								$options_base_file_name_extension = pathinfo( parse_url( $options_base_file_name )['path'], PATHINFO_EXTENSION );

								if ( $options_base_file_name_extension === 'php' ) {
									$options_base = $options_base_file_name;
								} else {
									$options_base = 'admin.php';
								}
								$options_page_id = $this->unique;

								$settings_link = "{$options_base}?page={$options_page_id}";

								$link_url = admin_url( $settings_link );

							} else {
								$settings_link = "?page={$this->unique}";
								$link_url      = admin_url( $settings_link );
							}

					}

					$settings_link_array[] = '<a href="' . $link_url . '">' . $link_text . '</a>';

				}

				return array_merge( $settings_link_array, $links );

			} // if ( is_array( $this->config['settings_link'] ) )

			// if nothing is returned so far, return original $links
			return $links;

		}

		/**
		 * Get default config for menu
		 * @return array $default
		 */
		public function get_config_default_menu() {

			$default = array(
				//
				'parent'        => 'options-general.php',
				'menu'          => 'plugins.php', // For backward compatibility
				'menu_title'    => __( 'Plugin Options', 'exopite-options-framework' ),
				// Required for submenu
				'submenu'       => false,
				//The name of this page
				'title'         => __( 'Plugin Options', 'exopite-options-framework' ),
				// The capability needed to view the page
				'capability'    => 'manage_options',
				'settings_link' => true,
				'tabbed'        => true,
				'position'      => 100,
				'icon'          => '',
				'multilang'     => true,
				'options'       => false
			);

			return apply_filters( 'exopite_sof_filter_config_default_menu_array', $default );
		}

		/**
		 * Get default config for metabox
		 * @return array $default
		 */
		public function get_config_default_metabox() {

			$default = array(

				'title'      => '',
				'post_types' => array( 'post' ),
				'context'    => 'advanced',
				'priority'   => 'default',
				'capability' => 'edit_posts',
				'tabbed'     => true,
				'options'    => false,
				'multilang'  => false

			);

			return apply_filters( 'exopite_sof_filter_config_default_metabox_array', $default );
		}

		/* Create a meta box for our custom fields */
		public function add_meta_box() {

			add_meta_box(
				$this->unique,
				$this->config['title'],
				array( $this, 'display_page' ),
				$this->config['post_types'],
				$this->config['context'],
				$this->config['priority']
			);

		}

		/**
		 * Register settings for plugin option page with a callback to save
		 */
		public function register_setting() {

			register_setting( $this->unique, $this->unique, array( $this, 'save' ) );

		}

		/**
		 * Register plugin option page
		 */
		public function add_admin_menu() {

			// Is it a main menu or sub_menu
			if ( ! (bool) $this->config['submenu'] ) {

//				$default['icon']     = '';
//				$default['position'] = 100;
//				$default['menu']     = 'Plugin menu';

//				$this->config = wp_parse_args( $this->config, $default );

				$menu = add_menu_page(
					$this->config['title'],
					$this->config['menu_title'],
					$this->config['capability'],
					$this->unique, //slug
					array( $this, 'display_page' ),
					$this->config['icon'],
					$this->config['position']
				);

			} else {

//				$this->config = wp_parse_args( $this->config, $default );

				$submenu = add_submenu_page(
					$this->config['parent'],
					$this->config['title'],
					$this->config['title'],
					$this->config['capability'],
					$this->unique, // slug
                    array( $this, 'display_page' ),
                    $this->config['position']
				);

			}
		}

		/**
		 * Load scripts and styles
		 *
		 * @hooked  admin_enqueue_scripts
		 *
		 * @param string hook name
		 */
		public function load_scripts_styles( $hook ) {

			// return if not admin
			if ( ! is_admin() ) {
				return;
			}

			/**
			 * Load Scripts for only Menu page
			 */
			if ( $this->is_menu_page_loaded() ):
				// TODO: Shift Scripts from all $type to this section

			endif; //$this->is_menu_page_loaded()


			/**
			 * Load Scripts for metabox that have enabled metabox using Exopite framework
			 */
			if ( $this->is_metabox_enabled_post_type() ):
				// TODO: Shift Scripts from all $type to this section

			endif; // $this->is_metabox_enabled_post_type()


			/**
			 * Load Scripts shared by all $type
			 */
			if ( $this->is_menu_page_loaded() || $this->is_metabox_enabled_post_type() ) :

				$url  = $this->get_url( $this->dirname );
				$base = trailingslashit( join( '/', array( $url, 'assets' ) ) );

				if ( ! wp_style_is( 'font-awesome' ) || ! wp_style_is( 'font-awesome-470' ) || ! wp_style_is( 'FontAwesome' ) ) {

					/* Get font awsome */
					wp_enqueue_style( 'font-awesome-470', $base . 'font-awesome-4.7.0/font-awesome.min.css', array(), $this->version, 'all' );

				}

				/**
				 * jquery-ui-core is built into WordPress
				 *
				 * @link https://developer.wordpress.org/reference/functions/wp_enqueue_script/
				 */
				wp_enqueue_style( 'jquery-ui-core' );
				// wp_enqueue_style( 'jquery-ui', $base . 'jquery-ui.css', array(), '1.8.24', 'all' );
				wp_enqueue_style( 'exopite-simple-options-framework', $base . 'styles.css', array(), $this->version, 'all' );

				// Add jQuery form scripts for menu options AJAX save
				wp_enqueue_script( 'jquery-form' );

				// Add the date picker script
				wp_enqueue_script( 'jquery-ui-datepicker' );

				// wp_enqueue_script( 'jquery-ui-sortable' );

				$scripts_styles = array(
					array(
						'name' => 'jquery-interdependencies',
						'fn'   => 'jquery.interdependencies.min.js',
						'dep'  => array(
							'jquery',
							'jquery-ui-datepicker',
							'wp-color-picker'
						),
					),
					array(
						'name' => 'jquery.sortable',
						'fn'   => 'html5sortable.min.js',
						'dep'  => array(
							'jquery',
						),
					),
					array(
						'name' => 'exopite-simple-options-framework-js',
						'fn'   => 'scripts.min.js',
						'dep'  => array(
							'jquery',
							'jquery-ui-datepicker',
							'wp-color-picker',
							'jquery-interdependencies'
						),
					),
				);

				foreach ( $scripts_styles as $item ) {
					wp_enqueue_script( $item['name'], $base . $item['fn'], $item['dep'], $this->version, true );
				}

				/**
				 * Enqueue class scripts
				 * with this, only enqueue scripts if class/field is used
				 */
				$this->enqueue_field_classes();

			endif; //$this->is_menu_page_loaded() || $this->is_metabox_enabled_post_type()

		}

		/**
		 * Save options or metabox to meta
		 *
		 * @return mixed
		 *
		 * @hooked  'save_post' for metabox
		 * @hooked  register_setting() for menu
		 *
		 */
		public function save( $posted_data ) {

			// Is user has ability to save?
			if ( ! current_user_can( $this->config['capability'] ) ) {
				return null;
			}

			//TODO Verify nonce

			$valid   = array();
			$post_id = null;

			/**
			 * @var $section_fields_with_values it will hold the sanitized fields => value
			 */
			$section_fields_with_values = array();

			// Specific to Menu Options
			if ( $this->is_menu() ) {

				/**
				 * Import options should not be checked.
				 */
				if ( isset( $_POST['action'] ) && sanitize_key( $_POST['action'] ) === 'exopite-sof-import-options' ) {
					return apply_filters( 'exopite_sof_import_options', $posted_data, $this->unique );
				}

				// Preserve values start with "_".
                $options = get_option( $this->unique );
                if ( is_array( $options ) ) {

                    foreach ( $options as $key => $value ) {

                        if ( substr( $key, 0, 1 ) === '_' ) {

                            $valid[ $key ] = $value;

                        }

                    }

                }

			}

			// Specific to Metabox
			if ( $this->is_metabox() ) {

				// if this is metabox, $posted_data is post_id we are saving
				$post_id = $posted_data;
				if ( $this->is_options_simple() ) {
					$posted_data = $_POST;
				} else {

					if ( isset( $_POST[ $this->unique ] ) ) {
						$posted_data = $_POST[ $this->unique ];
					} else {
						return false;
					}

				}

				if ( $posted_data === null ) return;

				// Stop WP from clearing custom fields on autosave
				if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
					return null;
				}

				// Prevent quick edit from clearing custom fields
				if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
					return null;
				}

				// If the post type is not in our post_type array, do nothing
				if ( ! in_array( get_post_type( $post_id ), $this->config['post_types'] ) ) {
					return null;
				}

			}

			/**
			 * This must be running on post meta too because of the qTranslate-X.
			 * Maybe check first if qTranslate-X is installed and active?
			 */
			// Preserve other language setting
			// Get current field value, make sure we are not override other language values

			if ( $this->is_multilang() ) {

				$other_languages = $this->languages_except_current_language();

				foreach ( $other_languages as $language ) {

					if ( $this->is_metabox() ) {
						// required for qTranslate-X and WP Multilang
						$post_meta = get_post_meta( $post_id );

						if ( ! empty( $post_meta ) ) {
							$options_array_from_post_meta = maybe_unserialize( $post_meta[ $this->unique ][0] );
							if ( is_array( $options_array_from_post_meta ) ) {
								$section_fields_with_values[ $language ] = $options_array_from_post_meta[ $language ];
							}
						}
					}

					if ( $this->is_menu() ) {
						$section_fields_with_values[ $language ] = $this->db_options[ $language ];
					}
				}
			}

			$sanitizer = new Exopite_Simple_Options_Framework_Sanitize( $this->is_multilang(), $this->lang_current, $this->config, $this->fields );
			if ( $this->is_multilang() ) {
				$section_fields_with_values[ $this->lang_current ] = $sanitizer->get_sanitized_values( $this->fields, $posted_data[$this->lang_current] );
			} else {
				$section_fields_with_values = $sanitizer->get_sanitized_values( $this->fields, $posted_data );
			}

            // Add values here, which start with '_', to skip sanitization.
            // This values does not come from the framework.
            foreach ( $valid as $key => $value ) {
                if ( ! isset( $section_fields_with_values[ $key ] ) ) {
                    $section_fields_with_values[ $key ] = $value;
                }
            }

			/**
			 * The idea here is that, this hook run on both.
			 */
			do_action( 'exopite_sof_do_save_options', $section_fields_with_values, $this->unique, $post_id );
			$valid = apply_filters( 'exopite_sof_save_options', $section_fields_with_values, $this->unique, $post_id );

			if ( $this->is_menu() ) {

				// These actions and filters only for options menu
				$valid = apply_filters( 'exopite_sof_save_menu_options', $section_fields_with_values, $this->unique );
				do_action( 'exopite_sof_do_save_menu_options', $section_fields_with_values, $this->unique );

				return $valid;
			}

			if ( $this->is_metabox() ) {

				// When we click on "New Post" (CPT), then $post is not available, so we need to check if it is set
				/**
				 * NEED TESTING!
				 * I'm not sure about this, need check. I think post ID IS available because saving custom field
				 * should be after post is inserted.
				 */
				if ( isset( $post_id ) ) {

					$valid = apply_filters( 'exopite_sof_save_meta_options', $section_fields_with_values, $this->unique, $post_id );
					do_action( 'exopite_sof_do_save_meta_options', $section_fields_with_values, $this->unique, $post_id );

					if ( $this->is_options_simple() ) {

						foreach ( $valid as $key => $value ) {

							update_post_meta( $post_id, $key, $value );

						}
					} else {
						update_post_meta( $post_id, $this->unique, $valid );
					}

				}

			}

			unset( $post_id, $valid, $posted_data, $section_fields_with_values, $section_fields_current_lang );

		}

		//DEGUB
		public function write_log( $type, $log_line ) {

			$hash        = '';
			$fn          = plugin_dir_path( __FILE__ ) . '/' . $type . $hash . '.log';
			$log_in_file = file_put_contents( $fn, date( 'Y-m-d H:i:s' ) . ' - ' . $log_line . PHP_EOL, FILE_APPEND );

		}

		/**
		 * Loop fileds based on field from user
		 *
		 * @param $callbacks
		 */
		public function loop_fields( $callbacks ) {

			if ( ! is_array( $this->fields ) ) return;

			foreach ( $this->fields as $section ) {

				// before
				if ( $callbacks['before'] ) {
					call_user_func( array( $this, $callbacks['before'] ), $section );
				}

				if ( ! isset( $section['fields'] ) || ! is_array( $section['fields'] ) ) {
					continue;
				}

				foreach ( $section['fields'] as $field ) {

					// If has subfields
					if ( ( $callbacks['main'] == 'include_field_class' || $callbacks['main'] == 'enqueue_field_class' ) && isset( $field['fields'] ) ) {

						foreach ( $field['fields'] as $subfield ) {

							if ( $callbacks['main'] ) {
								call_user_func( array( $this, $callbacks['main'] ), $subfield );
							}

						}

					}

					if ( $callbacks['main'] ) {
						call_user_func( array( $this, $callbacks['main'] ), $field );
					}

					// main

				}

				// after
				if ( $callbacks['after'] ) {
					call_user_func( array( $this, $callbacks['after'] ) );
				}
			}

		}

		/**
		 * @link https://thisinterestsme.com/php-using-recursion-print-values-multidimensional-array/
		 */
		public function recursive_walk( $array, &$fields ){

			foreach( $array as $key => $value ) {

				if ( is_array( $value ) ) {

					if ( isset( $value['type'] ) ) {

						if ( ! in_array( $value['type'], $fields ) && ! empty( $value['type'] ) ) {

							$temp_array = array(
								'type' 	=> $value['type'],
							);

							if ( isset( $value['id'] ) ) {
								$temp_array['id'] = $value['id'];
							}

							$fields[ $value['type'] ] = $temp_array;

						}

						if ( $value['type'] == 'editor' && isset( $value['editor'] ) ) {

							if ( ! isset( $fields[ $value['type'] ]['editor'] ) || ! is_array( $fields[ $value['type'] ]['editor'] ) ) {
								$fields[ $value['type'] ]['editor'] = array();
							}

							if ( ! in_array( $value['editor'], $fields[ $value['type'] ]['editor'] ) ) {
								$fields[ $value['type'] ]['editor'][] = $value['editor'];
							}

						}

					}

					$this->recursive_walk( $value, $fields );

				}

			}

			return $fields;

		}

		/**
		 * Loop and add callback to include and enqueue
		 */
		public function include_field_classes() {

			if ( empty( $this->fields ) ) {
				return;
			}

			$fields = array();
			$fields = $this->recursive_walk( $this->fields, $fields );

			foreach ( $fields as $field => $args ) {

				$this->include_field_class( $field );

			}

		}

		/**
		 * Loop and add callback to include and enqueue
		 */
		public function enqueue_field_classes() {

			if ( empty( $this->fields ) ) {
				return;
			}

			$fields = array();
			$fields = $this->recursive_walk( $this->fields, $fields );

			foreach ( $fields as $field => $args ) {

				$this->enqueue_field_class( $args );

			}

		}

		/**
		 * Include field classes
		 * and enqueue they scripts
		 */
		public function include_field_class( $field ) {

            if ( is_array( $field ) && isset( $field['type'] ) ) {
                $field = $field['type'];
            }

			$class = 'Exopite_Simple_Options_Framework_Field_' . $field;

			if ( ! class_exists( $class ) ) {

				$field_filename = $this->locate_template( $field );

				if ( file_exists( $field_filename ) ) {

					require_once join( DIRECTORY_SEPARATOR, array(
						$this->dirname,
						'fields',
						$field . '.php'
					) );

				}

			}

		}

		/**
		 * Include field classes
		 * and enqueue they scripts
		 */
		public function enqueue_field_class( $field ) {

			$class = 'Exopite_Simple_Options_Framework_Field_' . $field['type'];

			if ( class_exists( $class ) && method_exists( $class, 'enqueue' ) ) {

				$args = array(
					'plugin_sof_url'  => plugin_dir_url( __FILE__ ),
					'plugin_sof_path' => plugin_dir_path( __FILE__ ),
					'field'           => $field,
				);

				$class::enqueue( $args );

			}

		}

		/**
		 * Generate files
		 *
		 * @param  array $field field args
		 *
		 * @echo string   generated HTML for the field
		 */
		public function add_field( $field, $value = null ) {

			do_action( 'exopite_sof_before_generate_field', $field, $this->config );
			do_action( 'exopite_sof_before_add_field', $field, $this->config );

			$output     = '';
			$class      = 'Exopite_Simple_Options_Framework_Field_' . $field['type'];
			$depend     = '';
			$wrap_class = ( ! empty( $field['wrap_class'] ) ) ? ' ' . $field['wrap_class'] : '';
			$hidden     = ( $field['type'] == 'hidden' ) ? ' hidden' : '';
			$sub        = ( ! empty( $field['sub'] ) ) ? 'sub-' : '';

			/**
			 * Add editor name to classes for styling purposes.
			 */
			if (  $field['type'] == 'editor' ) {

				if ( ! isset( $field['editor'] ) || $field['editor'] == 'tinymce' ) {
					$wrap_class .= ' exopite-sof-tinymce-editor';
				} elseif ( isset( $field['editor'] ) && $field['editor'] == 'trumbowyg' ) {
					$wrap_class .= ' exopite-sof-trumbowyg-editor';
				}

			}

			if ( ! empty( $field['dependency'] ) ) {
				$hidden = ' hidden';
				$depend .= ' data-' . $sub . 'controller="' . $field['dependency'][0] . '"';
				$depend .= ' data-' . $sub . 'condition="' . $field['dependency'][1] . '"';
				$depend .= ' data-' . $sub . 'value="' . $field['dependency'][2] . '"';
			}

			if ( ! isset( $field['pseudo'] ) || ! $field['pseudo'] ) {
				$output .= '<div class="exopite-sof-field exopite-sof-field-' . $field['type'] . $wrap_class . $hidden . '"' . $depend . '>';
			}

			if ( isset( $field['title'] ) ) {

				$output .= '<h4 class="exopite-sof-title">';

				$output .= $field['title'];

				if ( ! empty( $field['description'] ) ) {
					$output .= '<p class="exopite-sof-description">' . $field['description'] . '</p>';
				}

				$output .= '</h4>'; // exopite-sof-title
				$output .= '<div class="exopite-sof-fieldset">';
			}

			if ( class_exists( $class ) ) {

				if ( empty( $value ) && $value !== 0 && $value !== '0' ) {

					// NEW

					if ( ( isset( $field['sub'] ) && ! empty( $field['sub'] ) ) || $this->is_menu() ) {
						$value = $this->get_value( $this->db_options, $field  );
					}

					if ( $this->is_metabox() ) {

						if ( $this->is_options_simple() ) {

							$meta = get_post_meta( get_the_ID() );

							/**
							 * get_post_meta return empty on non existing meta
							 * we need to check if meta key is exist to return null,
							 * because default value can only display if value is null
							 * on empty vlaue - may saved by the user - should display empty.
							 */
							if ( isset( $field['id'] ) && isset( $meta[ $field['id'] ] ) ) {

								$value = array_shift( $meta[ $field['id'] ] );

							} else {
								$value = null;
							}

						} else {

							$meta  = get_post_meta( get_the_ID(), $this->unique, true );
							$value = $this->get_value( $meta, $field  );

						}

					}

				}

				ob_start();
				$element = new $class( $field, $value, $this->unique, $this->config, $this->multilang );
				$element->output();
				$output .= ob_get_clean();

			} else {

				$output .= '<div class="danger unknown">';
				$output .= esc_attr__( 'ERROR:', 'exopite-simple-options' ) . ' ';
				$output .= esc_attr__( 'This field class is not available!', 'exopite-simple-options' );
				$output .= ' <i>(' . $field['type'] . ')</i>';
				$output .= '</div>';

			}

			if ( isset( $field['title'] ) ) {
				$output .= '</div>';
			} // exopite-sof-fieldset

			if ( ! isset( $field['pseudo'] ) || ! $field['pseudo'] ) {
				$output .= '<div class="clearfix"></div>';
				$output .= '</div>'; // exopite-sof-field
			}

			do_action( 'exopite_sof_after_generate_field', $field, $this->config );

			echo apply_filters( 'exopite_sof_add_field', $output, $field, $this->config );

			do_action( 'exopite_sof_after_add_field', $field, $this->config );

		}

		public function get_value( $options, $field  ) {

			/**
			 * IF MULTILANG
			 * - get option[current-lang][id]
			 * - but not the get option[default-lang][id] <- because if no options in current lang, should display default
			 * NOT MULTILANG OR MULTILANG NOT EXIST THEN TRY
			 * - get option[id]
			 */

			$value = null;

			if ( ! isset( $field['id'] ) ) {
				return $value;
			}

			if ( isset( $options[ $this->lang_current ][ $field['id'] ] ) ) {

				$value = $options[ $this->lang_current ][ $field['id'] ];

			} elseif ( $value === null && isset( $options[ $field['id'] ] ) ) {

				$value = $options[ $field['id'] ];

			}

			/**
			 * Use this filter, like:
			 *
			 * add_filter( 'exopite_sof_field_value', 'prefix_exopite_sof_field_value', 10, 5 );
			 * public function prefix_exopite_sof_field_value( $value, $unique, $options, $field ) {
			 *
			 *	   if ( $unique == $this->plugin_name && $field['id'] == 'your-field-id' ) {
			 *		   // do the magic ;)
			 *	   }
			 *
			 *	   return $value;
			 * }
			 */
			return apply_filters( 'exopite_sof_field_value', $value, $this->unique, $options, $field );

		}

		/**
		 * Display form and header for options page
		 * for metabox no need to do this.
		 */
		public function display_options_page_header() {

			echo '<form method="post" action="options.php" enctype="multipart/form-data" name="' . $this->unique . '" class="exopite-sof-form-js ' . $this->unique . '-form" data-save="' . esc_attr__( '正在保存...', 'exopite-sof' ) . '" data-saved="' . esc_attr__( '保存成功', 'exopite-sof' ) . '">';

			settings_fields( $this->unique );
			do_settings_sections( $this->unique );
			$current_language_title = '';
			if ( $this->is_multilang() ) {
				$current_language_title = apply_filters( 'exopite_sof_title_language_notice', $this->lang_current );
				$current_language_title = ' [ ' . $current_language_title . ' ]';
			}

			echo '<header class="exopite-sof-header exopite-sof-header-js">';
			echo '<h1>' . $this->config['title'] . $current_language_title . '</h1>';

			// echo '<span class="exopite-sof-search-wrapper"><input type="text" class="exopite-sof-search"></span>';

			echo '<fieldset><span class="exopite-sof-ajax-message"></span>';
			submit_button( esc_attr__( '保存设置', 'exopite-sof' ), 'primary ' . 'exopite-sof-submit-button-js', $this->unique . '-save', false, array() );
			echo '</fieldset>';
			echo '</header>';

		}

		/**
		 * Display form and footer for options page
		 * for metabox no need to do this.
		 */
		public function display_options_page_footer() {

			echo '<footer class="exopite-sof-footer-js exopite-sof-footer">';

			echo '<fieldset><span class="exopite-sof-ajax-message"></span>';
			submit_button( esc_attr__( '保存设置', 'exopite-sof' ), 'primary ' . 'exopite-sof-submit-button-js', '', false, array() );
			echo '</fieldset>';

			echo '</footer>';

			echo '</form>';

		}

		/**
		 * Display section header, only first is visible on start
		 */
		public function display_options_section_header( $section ) {

			$visibility = ' hide';
			if ( $section === reset( $this->fields ) ) {
				$visibility = '';
			}

            $section_name = ( isset( $section['name'] ) ) ? $section['name'] : '';
            $section_icon = ( isset( $section['icon'] ) ) ? $section['icon'] : '';

			echo '<div class="exopite-sof-section exopite-sof-section-' . $section_name . $visibility . '">';

			if ( isset( $section['title'] ) && ! empty( $section['title'] ) ) {

				$icon_before = '';
				if ( strpos( $section_icon, 'dashicon' ) !== false ) {
					$icon_before = 'dashicons-before ';
				} elseif ( strpos( $section_icon, 'fa' ) !== false ) {
					$icon_before = 'fa-before ';
				}

				echo '<h2 class="exopite-sof-section-header" data-section="' . $section_name . '"><span class="' . $icon_before . $section_icon . '"></span>' . $section['title'] . '</h2>';

			}

		}

		/**
		 * Display section footer
		 */
		public function display_options_section_footer() {

			echo '</div>'; // exopite-sof-section

		}

		public function get_fields() {

			if ( ! $this->elements ) return;

			$fields = array();

			foreach ( $this->elements as $key => $value ) {

				if( isset( $value['sections'] ) ) {

					foreach ( $value['sections'] as $section ) {

						if( isset( $section['fields'] ) ) {
							$fields[] = $section;
						}

					}

				} else {

					if( isset( $value['fields'] ) ) {
						$fields[] = $value;
					}

				}

			}

			$this->fields = $fields;
		}

		public function get_menu_item_icons( $section ) {

			if ( strpos( $section['icon'], 'dashicon' ) !== false ) {
				echo '<span class="exopite-sof-nav-icon dashicons-before ' . $section['icon'] . '"></span>';
			} elseif ( strpos( $section['icon'], 'fa' ) !== false ) {
				echo '<span class="exopite-sof-nav-icon fa-before ' . $section['icon'] . '" aria-hidden="true"></span>';
			}

		}

		public function get_menu_item( $section, $active = '', $force_hidden ) {

			// $active = '';
			// if ( $section === reset( $this->fields ) ) {
			// 	$active = ' active';
			// }

			$depend = '';
			$hidden = ( $force_hidden ) ? ' hidden' : '';

			// Dependency for tabs too
			if ( ! empty( $section['dependency'] ) ) {
				$hidden = ' hidden';
				$depend = ' data-controller="' . $section['dependency'][0] . '"';
				$depend .= ' data-condition="' . $section['dependency'][1] . '"';
				$depend .= ' data-value="' . $section['dependency'][2] . '"';
			}

            $section_name = ( isset( $section['name'] ) ) ? $section['name'] : '';

			echo '<li  class="exopite-sof-nav-list-item' . $active . $hidden . '"' . $depend . ' data-section="' . $section_name . '">';
			echo '<span class="exopite-sof-nav-list-item-title">';
			$this->get_menu_item_icons( $section );
			echo $section['title'];
			echo '</span>';
			echo '</li>';

		}

		public function get_menu() {

			// $fields = array();

			echo '<div class="exopite-sof-nav"><ul class="exopite-sof-nav-list">';

			foreach ( $this->elements as $key => $value ) {

				$active = '';
				reset( $this->elements );
				if ( $key === key($this->elements ) ) {
					$active = ' active';
				}


				if( isset( $value['sections'] ) ) {

					echo '<li  class="exopite-sof-nav-list-parent-item' . $active . '">';
					echo '<span class="exopite-sof-nav-list-item-title">';
					$this->get_menu_item_icons( $value );
					echo $value['title'];
					echo '</span>';
					echo '<ul style="display:none;">';

					foreach ( $value['sections'] as $section ) {

						if( isset( $section['fields'] ) ) {

							$this->get_menu_item( $section, $active, false );

						}

					}

					echo '</ul>';
					echo '</li>';

				} else {

					if( isset( $value['fields'] ) ) {

						$this->get_menu_item( $value, $active, false );

					}

				}

			}

			echo '</ul></div>';

		}

		public function display_debug_infos() {

			echo '<pre>MULTILANG<br>';
			var_export( $this->config['multilang'] );
			echo '</pre>';

			echo '<pre>IS_SIMPLE:<br>';
			var_export( $this->is_options_simple() );
			echo '</pre>';

			echo '<pre>IS_MULTILANG<br>';
			var_export( $this->is_multilang() );
			echo '</pre>';

			echo '<pre>IS_SPECIAL_MULTILANG_PLUGIN_ACTIVE<br>';
			var_export( $this->is_special_multilang_active() );
			echo '</pre>';

			echo '<pre>DB_OPTIONS<br>';
			var_export( $this->db_options );
			echo '</pre>';

		}

		/**
		 * Display form form either options page or metabox
		 */
		public function display_page() {

			do_action( 'exopite_simple_options_framework_form_' . $this->config['type'] . '_before' );

			settings_errors();

			echo '<div class="exopite-sof-wrapper exopite-sof-wrapper-' . $this->config['type'] . ' ' . $this->unique . '-options">';

			switch ( $this->config['type'] ) {
				case 'menu':
					add_action( 'exopite_sof_display_page_header', array(
						$this,
						'display_options_page_header'
					), 10, 1 );
					do_action( 'exopite_sof_display_page_header', $this->config );
					break;

				case 'metabox':
					/**
					 * Get options
					 * Can not get options in __consturct, because there, the_ID is not yet available.
					 *
					 * Only if options is not simple, if yes, then value determinated when field are displayed.
					 * Simple options is stored az induvidual meta key, value pair, otherwise it is stored in an array.
					 *
					 * I implemented this option because it is possible to search in serialized (array) post meta:
					 * @link https://wordpress.stackexchange.com/questions/16709/meta-query-with-meta-values-as-serialize-arrays
					 * @link https://stackoverflow.com/questions/15056407/wordpress-search-serialized-meta-data-with-custom-query
					 * @link https://www.simonbattersby.com/blog/2013/03/querying-wordpress-serialized-custom-post-data/
					 *
					 * but there is no way to sort them with wp_query or SQL.
					 * @link https://wordpress.stackexchange.com/questions/87265/order-by-meta-value-serialized-array/87268#87268
					 * "Not in any reliable way. You can certainly ORDER BY that value but the sorting will use the whole serialized string,
					 * which will give * you technically accurate results but not the results you want. You can't extract part of the string
					 * for sorting within the query itself. Even if you wrote raw SQL, which would give you access to database functions like
					 * SUBSTRING, I can't think of a dependable way to do it. You'd need a MySQL function that would unserialize the value--
					 * you'd have to write it yourself.
					 * Basically, if you need to sort on a meta_value you can't store it serialized. Sorry."
					 *
					 * It is possible to get all required posts and store them in an array and then sort them as an array,
					 * but what if you want multiple keys/value pair to be sorted?
					 *
					 * UPDATE
					 * it is maybe possible:
					 * @link http://www.russellengland.com/2012/07/how-to-unserialize-data-using-mysql.html
					 * but it is waaay more complicated and less documented as meta query sort and search.
					 * It should be not an excuse to use it, but it is not as reliable as it should be.
					 *
					 * @link https://wpquestions.com/Order_by_meta_key_where_value_is_serialized/7908
					 * "...meta info serialized is not a good idea. But you really are going to lose the ability to query your
					 * data in any efficient manner when serializing entries into the WP database.
					 *
					 * The overall performance saving and gain you think you are achieving by serialization is not going to be noticeable to
					 * any major extent. You might obtain a slightly smaller database size but the cost of SQL transactions is going to be
					 * heavy if you ever query those fields and try to compare them in any useful, meaningful manner.
					 *
					 * Instead, save serialization for data that you do not intend to query in that nature, but instead would only access in
					 * a passive fashion by the direct WP API call get_post_meta() - from that function you can unpack a serialized entry
					 * to access its array properties too."
					 */
					if ( $this->config['type'] == 'metabox' && ( ! isset( $this->config['options'] ) || $this->config['options'] != 'simple' ) ) {
						$meta_options     = get_post_meta( get_the_ID(), $this->unique, true );
						$this->db_options = apply_filters( 'exopite-simple-options-framework-meta-get-options', $meta_options, $this->unique, get_the_ID() );
					}

					if ( $this->debug ) {
						$meta_options = get_post_meta( get_the_ID() );
						echo '<pre>POST_META<br>';
						var_export( $meta_options );
						echo '</pre>';
					}

					break;
			}

			if ( isset( $this->config['multilang'] ) && is_array( $this->config['multilang'] ) ) {

				switch ( $this->config['type'] ) {
					case 'menu':
						$current_language = $this->config['multilang']['current'];
						break;

					case 'metabox':
						// Pages/Posts can not have "All languages" displayed, then default will be displayed
						$current_language = ( $this->config['multilang']['current'] == 'all' ) ? $this->config['multilang']['default'] : $this->config['multilang']['current'];
						break;
				}

				/**
				 * ToDos:
				 * - check this value, shouldn't be hard coded.
				 */
				$current_language = 'en';
				/**
				 * Current language need to pass to save function, if "All languages" seleted, WPML report default
				 * on save hook.
				 */
				echo '<input type="hidden" name="_language" value="' . $current_language . '">';
			}

			$sections = count( $this->fields );

			$tabbed = ( $sections > 1 && $this->config['tabbed'] ) ? ' exopite-sof-content-nav exopite-sof-content-js' : '';

			if ( $this->debug ) {

				$this->display_debug_infos();

			}

			/**
			 * Generate fields
			 */
			// Generate tab navigation
			echo '<div class="exopite-sof-content' . $tabbed . '">';

			if ( ! empty( $tabbed ) ) {

				$this->get_menu();

			}

			echo '<div class="exopite-sof-sections">';

			// Generate fields
			$callbacks = array(
				'before' => 'display_options_section_header',
				'main'   => 'add_field',
				'after'  => 'display_options_section_footer'
			);

			$this->loop_fields( $callbacks );

			echo '</div>'; // sections
			echo '</div>'; // content
			if ( $this->config['type'] == 'menu' ) {

				add_action( 'exopite_sof_display_page_footer', array(
					$this,
					'display_options_page_footer'
				), 10, 1 );
				do_action( 'exopite_sof_display_page_footer', $this->config );

			}

			echo '</div>';

			do_action( 'exopite_sof_form_' . $this->config['type'] . '_after' );

		} // display_page()


	} //class

endif;

/**
 * Use:
 *  $options = ( function_exists( 'get_exopite_sof_option' ) ) ? get_exopite_sof_option( $this->plugin_name ) : get_option( $this->plugin_name );

 *  // OR
 *
 *  if ( function_exists( 'get_exopite_sof_option' ) ) {
 *      $options = get_exopite_sof_option( $this->plugin_name );
 *  } else {
 *      $options = get_option( $this->plugin_name );
 *  }
 */
if ( ! function_exists( 'get_exopite_sof_option' ) ) {
	function get_exopite_sof_option( $option_slug, $default = false  ) {

		/**
		 * save in cache?
		 * @link https://codex.wordpress.org/Transients_API
		 */

		if ( ! class_exists( 'Exopite_Simple_Options_Framework_Helper' ) ) {
			require_once 'multilang-class.php';
		}

		$language_defaults = Exopite_Simple_Options_Framework_Helper::get_language_defaults();

		$options = apply_filters( 'get_exopite_sof_option', get_option( $option_slug, $default ), $option_slug, $default );

		if ( isset( $options[$language_defaults['current']] ) ) {

			return $options[$language_defaults['current']];

		} elseif ( isset( $options[$language_defaults['default']] ) ) {

			return $options[$language_defaults['default']];

		} else {

			return $options;

		}

	}
}