setup.class.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. <?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
  2. /**
  3. *
  4. * Setup Class
  5. *
  6. * @since 1.0.0
  7. * @version 1.0.0
  8. *
  9. */
  10. if ( ! class_exists( 'Sakurairo_CSF' ) ) {
  11. class Sakurairo_CSF {
  12. // Default constants
  13. public static $premium = false;
  14. public static $version = '2.2.7';
  15. public static $dir = '';
  16. public static $url = '';
  17. public static $css = '';
  18. public static $file = '';
  19. public static $enqueue = false;
  20. public static $webfonts = array();
  21. public static $subsets = array();
  22. public static $inited = array();
  23. public static $fields = array();
  24. public static $args = array(
  25. 'admin_options' => array(),
  26. 'customize_options' => array(),
  27. 'metabox_options' => array(),
  28. 'nav_menu_options' => array(),
  29. 'profile_options' => array(),
  30. 'taxonomy_options' => array(),
  31. 'widget_options' => array(),
  32. 'comment_options' => array(),
  33. 'shortcode_options' => array(),
  34. );
  35. // Shortcode instances
  36. public static $shortcode_instances = array();
  37. private static $instance = null;
  38. public static function init( $file = __FILE__ ) {
  39. // Set file constant
  40. self::$file = $file;
  41. // Set constants
  42. self::constants();
  43. // Include files
  44. self::includes();
  45. if ( is_null( self::$instance ) ) {
  46. self::$instance = new self();
  47. }
  48. return self::$instance;
  49. }
  50. // Initalize
  51. public function __construct() {
  52. // Init action
  53. do_action( 'csf_init' );
  54. // Setup textdomain
  55. self::textdomain();
  56. add_action( 'after_setup_theme', array( 'Sakurairo_CSF', 'setup' ) );
  57. add_action( 'init', array( 'Sakurairo_CSF', 'setup' ) );
  58. add_action( 'switch_theme', array( 'Sakurairo_CSF', 'setup' ) );
  59. add_action( 'admin_enqueue_scripts', array( 'Sakurairo_CSF', 'add_admin_enqueue_scripts' ) );
  60. add_action( 'wp_enqueue_scripts', array( 'Sakurairo_CSF', 'add_typography_enqueue_styles' ), 80 );
  61. add_action( 'wp_head', array( 'Sakurairo_CSF', 'add_custom_css' ), 80 );
  62. add_filter( 'admin_body_class', array( 'Sakurairo_CSF', 'add_admin_body_class' ) );
  63. }
  64. // Setup frameworks
  65. public static function setup() {
  66. // Welcome page
  67. self::include_plugin_file( 'views/welcome.php' );
  68. // Setup admin option framework
  69. $params = array();
  70. if ( class_exists( 'CSF_Options' ) && ! empty( self::$args['admin_options'] ) ) {
  71. foreach ( self::$args['admin_options'] as $key => $value ) {
  72. if ( ! empty( self::$args['sections'][$key] ) && ! isset( self::$inited[$key] ) ) {
  73. $params['args'] = $value;
  74. $params['sections'] = self::$args['sections'][$key];
  75. self::$inited[$key] = true;
  76. CSF_Options::instance( $key, $params );
  77. if ( ! empty( $value['show_in_customizer'] ) ) {
  78. $value['output_css'] = false;
  79. $value['enqueue_webfont'] = false;
  80. self::$args['customize_options'][$key] = $value;
  81. self::$inited[$key] = null;
  82. }
  83. }
  84. }
  85. }
  86. // Setup customize option framework
  87. $params = array();
  88. if ( class_exists( 'CSF_Customize_Options' ) && ! empty( self::$args['customize_options'] ) ) {
  89. foreach ( self::$args['customize_options'] as $key => $value ) {
  90. if ( ! empty( self::$args['sections'][$key] ) && ! isset( self::$inited[$key] ) ) {
  91. $params['args'] = $value;
  92. $params['sections'] = self::$args['sections'][$key];
  93. self::$inited[$key] = true;
  94. CSF_Customize_Options::instance( $key, $params );
  95. }
  96. }
  97. }
  98. // Setup metabox option framework
  99. $params = array();
  100. if ( class_exists( 'CSF_Metabox' ) && ! empty( self::$args['metabox_options'] ) ) {
  101. foreach ( self::$args['metabox_options'] as $key => $value ) {
  102. if ( ! empty( self::$args['sections'][$key] ) && ! isset( self::$inited[$key] ) ) {
  103. $params['args'] = $value;
  104. $params['sections'] = self::$args['sections'][$key];
  105. self::$inited[$key] = true;
  106. CSF_Metabox::instance( $key, $params );
  107. }
  108. }
  109. }
  110. // Setup nav menu option framework
  111. $params = array();
  112. if ( class_exists( 'CSF_Nav_Menu_Options' ) && ! empty( self::$args['nav_menu_options'] ) ) {
  113. foreach ( self::$args['nav_menu_options'] as $key => $value ) {
  114. if ( ! empty( self::$args['sections'][$key] ) && ! isset( self::$inited[$key] ) ) {
  115. $params['args'] = $value;
  116. $params['sections'] = self::$args['sections'][$key];
  117. self::$inited[$key] = true;
  118. CSF_Nav_Menu_Options::instance( $key, $params );
  119. }
  120. }
  121. }
  122. // Setup profile option framework
  123. $params = array();
  124. if ( class_exists( 'CSF_Profile_Options' ) && ! empty( self::$args['profile_options'] ) ) {
  125. foreach ( self::$args['profile_options'] as $key => $value ) {
  126. if ( ! empty( self::$args['sections'][$key] ) && ! isset( self::$inited[$key] ) ) {
  127. $params['args'] = $value;
  128. $params['sections'] = self::$args['sections'][$key];
  129. self::$inited[$key] = true;
  130. CSF_Profile_Options::instance( $key, $params );
  131. }
  132. }
  133. }
  134. // Setup taxonomy option framework
  135. $params = array();
  136. if ( class_exists( 'CSF_Taxonomy_Options' ) && ! empty( self::$args['taxonomy_options'] ) ) {
  137. $taxonomy = ( isset( $_GET['taxonomy'] ) ) ? sanitize_text_field( wp_unslash( $_GET['taxonomy'] ) ) : '';
  138. foreach ( self::$args['taxonomy_options'] as $key => $value ) {
  139. if ( ! empty( self::$args['sections'][$key] ) && ! isset( self::$inited[$key] ) ) {
  140. $params['args'] = $value;
  141. $params['sections'] = self::$args['sections'][$key];
  142. self::$inited[$key] = true;
  143. CSF_Taxonomy_Options::instance( $key, $params );
  144. }
  145. }
  146. }
  147. // Setup widget option framework
  148. if ( class_exists( 'CSF_Widget' ) && class_exists( 'WP_Widget_Factory' ) && ! empty( self::$args['widget_options'] ) ) {
  149. $wp_widget_factory = new WP_Widget_Factory();
  150. global $wp_widget_factory;
  151. foreach ( self::$args['widget_options'] as $key => $value ) {
  152. if ( ! isset( self::$inited[$key] ) ) {
  153. self::$inited[$key] = true;
  154. $wp_widget_factory->register( CSF_Widget::instance( $key, $value ) );
  155. }
  156. }
  157. }
  158. // Setup comment option framework
  159. $params = array();
  160. if ( class_exists( 'CSF_Comment_Metabox' ) && ! empty( self::$args['comment_options'] ) ) {
  161. foreach ( self::$args['comment_options'] as $key => $value ) {
  162. if ( ! empty( self::$args['sections'][$key] ) && ! isset( self::$inited[$key] ) ) {
  163. $params['args'] = $value;
  164. $params['sections'] = self::$args['sections'][$key];
  165. self::$inited[$key] = true;
  166. CSF_Comment_Metabox::instance( $key, $params );
  167. }
  168. }
  169. }
  170. // Setup shortcode option framework
  171. $params = array();
  172. if ( class_exists( 'CSF_Shortcoder' ) && ! empty( self::$args['shortcode_options'] ) ) {
  173. foreach ( self::$args['shortcode_options'] as $key => $value ) {
  174. if ( ! empty( self::$args['sections'][$key] ) && ! isset( self::$inited[$key] ) ) {
  175. $params['args'] = $value;
  176. $params['sections'] = self::$args['sections'][$key];
  177. self::$inited[$key] = true;
  178. CSF_Shortcoder::instance( $key, $params );
  179. }
  180. }
  181. // Once editor setup for gutenberg and media buttons
  182. if ( class_exists( 'CSF_Shortcoder' ) && ! empty( self::$shortcode_instances ) ) {
  183. foreach ( self::$shortcode_instances as $instance ) {
  184. if ( ! empty( $instance['show_in_editor'] ) ) {
  185. CSF_Shortcoder::once_editor_setup();
  186. break;
  187. }
  188. }
  189. }
  190. }
  191. do_action( 'csf_loaded' );
  192. }
  193. // Create options
  194. public static function createOptions( $id, $args = array() ) {
  195. self::$args['admin_options'][$id] = $args;
  196. }
  197. // Create customize options
  198. public static function createCustomizeOptions( $id, $args = array() ) {
  199. self::$args['customize_options'][$id] = $args;
  200. }
  201. // Create metabox options
  202. public static function createMetabox( $id, $args = array() ) {
  203. self::$args['metabox_options'][$id] = $args;
  204. }
  205. // Create menu options
  206. public static function createNavMenuOptions( $id, $args = array() ) {
  207. self::$args['nav_menu_options'][$id] = $args;
  208. }
  209. // Create shortcoder options
  210. public static function createShortcoder( $id, $args = array() ) {
  211. self::$args['shortcode_options'][$id] = $args;
  212. }
  213. // Create taxonomy options
  214. public static function createTaxonomyOptions( $id, $args = array() ) {
  215. self::$args['taxonomy_options'][$id] = $args;
  216. }
  217. // Create profile options
  218. public static function createProfileOptions( $id, $args = array() ) {
  219. self::$args['profile_options'][$id] = $args;
  220. }
  221. // Create widget
  222. public static function createWidget( $id, $args = array() ) {
  223. self::$args['widget_options'][$id] = $args;
  224. self::set_used_fields( $args );
  225. }
  226. // Create comment metabox
  227. public static function createCommentMetabox( $id, $args = array() ) {
  228. self::$args['comment_options'][$id] = $args;
  229. }
  230. // Create section
  231. public static function createSection( $id, $sections ) {
  232. self::$args['sections'][$id][] = $sections;
  233. self::set_used_fields( $sections );
  234. }
  235. // Set directory constants
  236. public static function constants() {
  237. // We need this path-finder code for set URL of framework
  238. $dirname = str_replace( '//', '/', wp_normalize_path( dirname( dirname( self::$file ) ) ) );
  239. $theme_dir = str_replace( '//', '/', wp_normalize_path( get_parent_theme_file_path() ) );
  240. $plugin_dir = str_replace( '//', '/', wp_normalize_path( WP_PLUGIN_DIR ) );
  241. $plugin_dir = str_replace( '/opt/bitnami', '/bitnami', $plugin_dir );
  242. $located_plugin = ( preg_match( '#'. self::sanitize_dirname( $plugin_dir ) .'#', self::sanitize_dirname( $dirname ) ) ) ? true : false;
  243. $directory = ( $located_plugin ) ? $plugin_dir : $theme_dir;
  244. $directory_uri = ( $located_plugin ) ? WP_PLUGIN_URL : get_parent_theme_file_uri();
  245. $foldername = str_replace( $directory, '', $dirname );
  246. $protocol_uri = ( is_ssl() ) ? 'https' : 'http';
  247. $directory_uri = set_url_scheme( $directory_uri, $protocol_uri );
  248. self::$dir = $dirname;
  249. self::$url = $directory_uri . $foldername;
  250. }
  251. // Include file helper
  252. public static function include_plugin_file( $file, $load = true ) {
  253. $path = '';
  254. $file = ltrim( $file, '/' );
  255. $override = apply_filters( 'csf_override', 'csf-override' );
  256. if ( file_exists( get_parent_theme_file_path( $override .'/'. $file ) ) ) {
  257. $path = get_parent_theme_file_path( $override .'/'. $file );
  258. } elseif ( file_exists( get_theme_file_path( $override .'/'. $file ) ) ) {
  259. $path = get_theme_file_path( $override .'/'. $file );
  260. } elseif ( file_exists( self::$dir .'/'. $override .'/'. $file ) ) {
  261. $path = self::$dir .'/'. $override .'/'. $file;
  262. } elseif ( file_exists( self::$dir .'/'. $file ) ) {
  263. $path = self::$dir .'/'. $file;
  264. }
  265. if ( ! empty( $path ) && ! empty( $file ) && $load ) {
  266. global $wp_query;
  267. if ( is_object( $wp_query ) && function_exists( 'load_template' ) ) {
  268. load_template( $path, true );
  269. } else {
  270. require_once( $path );
  271. }
  272. } else {
  273. return self::$dir .'/'. $file;
  274. }
  275. }
  276. // Is active plugin helper
  277. public static function is_active_plugin( $file = '' ) {
  278. return in_array( $file, (array) get_option( 'active_plugins', array() ) );
  279. }
  280. // Sanitize dirname
  281. public static function sanitize_dirname( $dirname ) {
  282. return preg_replace( '/[^A-Za-z]/', '', $dirname );
  283. }
  284. // Set url constant
  285. public static function include_plugin_url( $file ) {
  286. return esc_url( self::$url ) .'/'. ltrim( $file, '/' );
  287. }
  288. // Include files
  289. public static function includes() {
  290. // Helpers
  291. self::include_plugin_file( 'functions/actions.php' );
  292. self::include_plugin_file( 'functions/helpers.php' );
  293. self::include_plugin_file( 'functions/sanitize.php' );
  294. self::include_plugin_file( 'functions/validate.php' );
  295. // Includes free version classes
  296. self::include_plugin_file( 'classes/abstract.class.php' );
  297. self::include_plugin_file( 'classes/fields.class.php' );
  298. self::include_plugin_file( 'classes/admin-options.class.php' );
  299. // Includes premium version classes
  300. if ( self::$premium ) {
  301. self::include_plugin_file( 'classes/customize-options.class.php' );
  302. self::include_plugin_file( 'classes/metabox-options.class.php' );
  303. self::include_plugin_file( 'classes/nav-menu-options.class.php' );
  304. self::include_plugin_file( 'classes/profile-options.class.php' );
  305. self::include_plugin_file( 'classes/shortcode-options.class.php' );
  306. self::include_plugin_file( 'classes/taxonomy-options.class.php' );
  307. self::include_plugin_file( 'classes/widget-options.class.php' );
  308. self::include_plugin_file( 'classes/comment-options.class.php' );
  309. }
  310. // Include all framework fields
  311. $fields = apply_filters( 'csf_fields', array(
  312. 'accordion',
  313. 'background',
  314. 'backup',
  315. 'border',
  316. 'button_set',
  317. 'callback',
  318. 'checkbox',
  319. 'code_editor',
  320. 'color',
  321. 'color_group',
  322. 'content',
  323. 'date',
  324. 'datetime',
  325. 'dimensions',
  326. 'fieldset',
  327. 'gallery',
  328. 'group',
  329. 'heading',
  330. 'icon',
  331. 'image_select',
  332. 'link',
  333. 'link_color',
  334. 'map',
  335. 'media',
  336. 'notice',
  337. 'number',
  338. 'palette',
  339. 'radio',
  340. 'repeater',
  341. 'select',
  342. 'slider',
  343. 'sortable',
  344. 'sorter',
  345. 'spacing',
  346. 'spinner',
  347. 'subheading',
  348. 'submessage',
  349. 'switcher',
  350. 'tabbed',
  351. 'text',
  352. 'textarea',
  353. 'typography',
  354. 'upload',
  355. 'wp_editor',
  356. ) );
  357. if ( ! empty( $fields ) ) {
  358. foreach ( $fields as $field ) {
  359. if ( ! class_exists( 'CSF_Field_'. $field ) && class_exists( 'CSF_Fields' ) ) {
  360. self::include_plugin_file( 'fields/'. $field .'/'. $field .'.php' );
  361. }
  362. }
  363. }
  364. }
  365. // Setup textdomain
  366. public static function textdomain() {
  367. load_textdomain( 'sakurairo_csf', self::$dir .'/languages/'. get_locale() .'.mo' );
  368. }
  369. // Set all of used fields
  370. public static function set_used_fields( $sections ) {
  371. if ( ! empty( $sections['fields'] ) ) {
  372. foreach ( $sections['fields'] as $field ) {
  373. if ( ! empty( $field['fields'] ) ) {
  374. self::set_used_fields( $field );
  375. }
  376. if ( ! empty( $field['tabs'] ) ) {
  377. self::set_used_fields( array( 'fields' => $field['tabs'] ) );
  378. }
  379. if ( ! empty( $field['accordions'] ) ) {
  380. self::set_used_fields( array( 'fields' => $field['accordions'] ) );
  381. }
  382. if ( ! empty( $field['type'] ) ) {
  383. self::$fields[$field['type']] = $field;
  384. }
  385. }
  386. }
  387. }
  388. // Enqueue admin and fields styles and scripts
  389. public static function add_admin_enqueue_scripts() {
  390. if ( ! self::$enqueue ) {
  391. // Loads scripts and styles only when needed
  392. $wpscreen = get_current_screen();
  393. if ( ! empty( self::$args['admin_options'] ) ) {
  394. foreach ( self::$args['admin_options'] as $argument ) {
  395. if ( substr( $wpscreen->id, -strlen( $argument['menu_slug'] ) ) === $argument['menu_slug'] ) {
  396. self::$enqueue = true;
  397. }
  398. }
  399. }
  400. if ( ! empty( self::$args['metabox_options'] ) ) {
  401. foreach ( self::$args['metabox_options'] as $argument ) {
  402. if ( in_array( $wpscreen->post_type, (array) $argument['post_type'] ) ) {
  403. self::$enqueue = true;
  404. }
  405. }
  406. }
  407. if ( ! empty( self::$args['taxonomy_options'] ) ) {
  408. foreach ( self::$args['taxonomy_options'] as $argument ) {
  409. if ( in_array( $wpscreen->taxonomy, (array) $argument['taxonomy'] ) ) {
  410. self::$enqueue = true;
  411. }
  412. }
  413. }
  414. if ( ! empty( self::$shortcode_instances ) ) {
  415. foreach ( self::$shortcode_instances as $argument ) {
  416. if ( ( $argument['show_in_editor'] && $wpscreen->base === 'post' ) || $argument['show_in_custom'] ) {
  417. self::$enqueue = true;
  418. }
  419. }
  420. }
  421. if ( ! empty( self::$args['widget_options'] ) && ( $wpscreen->id === 'widgets' || $wpscreen->id === 'customize' ) ) {
  422. self::$enqueue = true;
  423. }
  424. if ( ! empty( self::$args['customize_options'] ) && $wpscreen->id === 'customize' ) {
  425. self::$enqueue = true;
  426. }
  427. if ( ! empty( self::$args['nav_menu_options'] ) && $wpscreen->id === 'nav-menus' ) {
  428. self::$enqueue = true;
  429. }
  430. if ( ! empty( self::$args['profile_options'] ) && ( $wpscreen->id === 'profile' || $wpscreen->id === 'user-edit' ) ) {
  431. self::$enqueue = true;
  432. }
  433. if ( ! empty( self::$args['comment_options'] ) && $wpscreen->id === 'comment' ) {
  434. self::$enqueue = true;
  435. }
  436. if ( $wpscreen->id === 'tools_page_csf-welcome' ) {
  437. self::$enqueue = true;
  438. }
  439. }
  440. if ( ! apply_filters( 'csf_enqueue_assets', self::$enqueue ) ) {
  441. return;
  442. }
  443. // Check for developer mode
  444. $min = ( self::$premium && SCRIPT_DEBUG ) ? '' : '.min';
  445. // Admin utilities
  446. wp_enqueue_media();
  447. // Wp color picker
  448. wp_enqueue_style( 'wp-color-picker' );
  449. wp_enqueue_script( 'wp-color-picker' );
  450. // Font awesome 4 and 5 loader
  451. if ( apply_filters( 'csf_fa4', false ) ) {
  452. wp_enqueue_style( 'csf-fa', 'https://cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome'. $min .'.css', array(), '4.7.0', 'all' );
  453. } else {
  454. wp_enqueue_style( 'csf-fa5', 'https://cdn.staticfile.org/font-awesome/5.15.4/css/all'. $min .'.css', array(), '5.15.5', 'all' );
  455. wp_enqueue_style( 'csf-fa5-v4-shims', 'https://cdn.staticfile.org/font-awesome/5.15.4/css/v4-shims'. $min .'.css', array(), '5.15.5', 'all' );
  456. }
  457. // Main style
  458. wp_enqueue_style( 'sakurairo_csf', self::include_plugin_url( 'assets/css/style'. $min .'.css' ), array(), self::$version, 'all' );
  459. // Main RTL styles
  460. if ( is_rtl() ) {
  461. wp_enqueue_style( 'csf-rtl', self::include_plugin_url( 'assets/css/style-rtl'. $min .'.css' ), array(), self::$version, 'all' );
  462. }
  463. // Main scripts
  464. wp_enqueue_script( 'csf-plugins', self::include_plugin_url( 'assets/js/plugins'. $min .'.js' ), array(), self::$version, true );
  465. wp_enqueue_script( 'sakurairo_csf', self::include_plugin_url( 'assets/js/main'. $min .'.js' ), array( 'csf-plugins' ), self::$version, true );
  466. // Main variables
  467. wp_localize_script( 'sakurairo_csf', 'csf_vars', array(
  468. 'color_palette' => apply_filters( 'csf_color_palette', array() ),
  469. 'i18n' => array(
  470. 'confirm' => esc_html__( 'Are you sure?', 'sakurairo_csf' ),
  471. 'typing_text' => esc_html__( 'Please enter %s or more characters', 'sakurairo_csf' ),
  472. 'searching_text' => esc_html__( 'Searching...', 'sakurairo_csf' ),
  473. 'no_results_text' => esc_html__( 'No results found.', 'sakurairo_csf' ),
  474. ),
  475. ) );
  476. // Enqueue fields scripts and styles
  477. $enqueued = array();
  478. if ( ! empty( self::$fields ) ) {
  479. foreach ( self::$fields as $field ) {
  480. if ( ! empty( $field['type'] ) ) {
  481. $classname = 'CSF_Field_' . $field['type'];
  482. if ( class_exists( $classname ) && method_exists( $classname, 'enqueue' ) ) {
  483. $instance = new $classname( $field );
  484. if ( method_exists( $classname, 'enqueue' ) ) {
  485. $instance->enqueue();
  486. }
  487. unset( $instance );
  488. }
  489. }
  490. }
  491. }
  492. do_action( 'csf_enqueue' );
  493. }
  494. // Add typography enqueue styles to front page
  495. public static function add_typography_enqueue_styles() {
  496. if ( ! empty( self::$webfonts ) ) {
  497. if ( ! empty( self::$webfonts['enqueue'] ) ) {
  498. $query = array();
  499. $fonts = array();
  500. foreach ( self::$webfonts['enqueue'] as $family => $styles ) {
  501. $fonts[] = $family . ( ( ! empty( $styles ) ) ? ':'. implode( ',', $styles ) : '' );
  502. }
  503. if ( ! empty( $fonts ) ) {
  504. $query['family'] = implode( '%7C', $fonts );
  505. }
  506. if ( ! empty( self::$subsets ) ) {
  507. $query['subset'] = implode( ',', self::$subsets );
  508. }
  509. $query['display'] = 'swap';
  510. wp_enqueue_style( 'csf-google-web-fonts', esc_url( add_query_arg( $query, '//fonts.googleapis.com/css' ) ), array(), null );
  511. }
  512. if ( ! empty( self::$webfonts['async'] ) ) {
  513. $fonts = array();
  514. foreach ( self::$webfonts['async'] as $family => $styles ) {
  515. $fonts[] = $family . ( ( ! empty( $styles ) ) ? ':'. implode( ',', $styles ) : '' );
  516. }
  517. wp_enqueue_script( 'csf-google-web-fonts', esc_url( '//ajax.googleapis.com/ajax/libs/webfont/1.6.26/webfont.js' ), array(), null );
  518. wp_localize_script( 'csf-google-web-fonts', 'WebFontConfig', array( 'google' => array( 'families' => $fonts ) ) );
  519. }
  520. }
  521. }
  522. // Add admin body class
  523. public static function add_admin_body_class( $classes ) {
  524. if ( apply_filters( 'csf_fa4', false ) ) {
  525. $classes .= 'csf-fa5-shims';
  526. }
  527. return $classes;
  528. }
  529. // Add custom css to front page
  530. public static function add_custom_css() {
  531. if ( ! empty( self::$css ) ) {
  532. echo '<style type="text/css">'. wp_strip_all_tags( self::$css ) .'</style>';
  533. }
  534. }
  535. // Add a new framework field
  536. public static function field( $field = array(), $value = '', $unique = '', $where = '', $parent = '' ) {
  537. // Check for unallow fields
  538. if ( ! empty( $field['_notice'] ) ) {
  539. $field_type = $field['type'];
  540. $field = array();
  541. $field['content'] = esc_html__( 'Oops! Not allowed.', 'sakurairo_csf' ) .' <strong>('. $field_type .')</strong>';
  542. $field['type'] = 'notice';
  543. $field['style'] = 'danger';
  544. }
  545. $depend = '';
  546. $visible = '';
  547. $unique = ( ! empty( $unique ) ) ? $unique : '';
  548. $class = ( ! empty( $field['class'] ) ) ? ' ' . esc_attr( $field['class'] ) : '';
  549. $is_pseudo = ( ! empty( $field['pseudo'] ) ) ? ' csf-pseudo-field' : '';
  550. $field_type = ( ! empty( $field['type'] ) ) ? esc_attr( $field['type'] ) : '';
  551. if ( ! empty( $field['dependency'] ) ) {
  552. $dependency = $field['dependency'];
  553. $depend_visible = '';
  554. $data_controller = '';
  555. $data_condition = '';
  556. $data_value = '';
  557. $data_global = '';
  558. if ( is_array( $dependency[0] ) ) {
  559. $data_controller = implode( '|', array_column( $dependency, 0 ) );
  560. $data_condition = implode( '|', array_column( $dependency, 1 ) );
  561. $data_value = implode( '|', array_column( $dependency, 2 ) );
  562. $data_global = implode( '|', array_column( $dependency, 3 ) );
  563. $depend_visible = implode( '|', array_column( $dependency, 4 ) );
  564. } else {
  565. $data_controller = ( ! empty( $dependency[0] ) ) ? $dependency[0] : '';
  566. $data_condition = ( ! empty( $dependency[1] ) ) ? $dependency[1] : '';
  567. $data_value = ( ! empty( $dependency[2] ) ) ? $dependency[2] : '';
  568. $data_global = ( ! empty( $dependency[3] ) ) ? $dependency[3] : '';
  569. $depend_visible = ( ! empty( $dependency[4] ) ) ? $dependency[4] : '';
  570. }
  571. $depend .= ' data-controller="'. esc_attr( $data_controller ) .'"';
  572. $depend .= ' data-condition="'. esc_attr( $data_condition ) .'"';
  573. $depend .= ' data-value="'. esc_attr( $data_value ) .'"';
  574. $depend .= ( ! empty( $data_global ) ) ? ' data-depend-global="true"' : '';
  575. $visible = ( ! empty( $depend_visible ) ) ? ' csf-depend-visible' : ' csf-depend-hidden';
  576. }
  577. // These attributes has been sanitized above.
  578. echo '<div class="csf-field csf-field-'. $field_type . $is_pseudo . $class . $visible .'"'. $depend .'>';
  579. if ( ! empty( $field_type ) ) {
  580. if ( ! empty( $field['title'] ) ) {
  581. echo '<div class="csf-title">';
  582. echo '<h4>'. $field['title'] .'</h4>';
  583. echo ( ! empty( $field['subtitle'] ) ) ? '<div class="csf-subtitle-text">'. $field['subtitle'] .'</div>' : '';
  584. echo '</div>';
  585. }
  586. echo ( ! empty( $field['title'] ) ) ? '<div class="csf-fieldset">' : '';
  587. $value = ( ! isset( $value ) && isset( $field['default'] ) ) ? $field['default'] : $value;
  588. $value = ( isset( $field['value'] ) ) ? $field['value'] : $value;
  589. $classname = 'CSF_Field_'. $field_type;
  590. if ( class_exists( $classname ) ) {
  591. $instance = new $classname( $field, $value, $unique, $where, $parent );
  592. $instance->render();
  593. } else {
  594. echo '<p>'. esc_html__( 'Field not found!', 'sakurairo_csf' ) .'</p>';
  595. }
  596. } else {
  597. echo '<p>'. esc_html__( 'Field not found!', 'sakurairo_csf' ) .'</p>';
  598. }
  599. echo ( ! empty( $field['title'] ) || ! empty( $field['fancy_title'] ) ) ? '</div>' : '';
  600. echo '<div class="clear"></div>';
  601. echo '</div>';
  602. }
  603. }
  604. }
  605. Sakurairo_CSF::init( __FILE__ );