setup.class.php 25 KB

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