1
0

setup.class.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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. $theme_dir = str_replace( '/opt/bitnami', '/bitnami', $theme_dir );
  239. $plugin_dir = str_replace( '//', '/', wp_normalize_path( WP_PLUGIN_DIR ) );
  240. $plugin_dir = str_replace( '/opt/bitnami', '/bitnami', $plugin_dir );
  241. $located_plugin = ( preg_match( '#'. self::sanitize_dirname( $plugin_dir ) .'#', self::sanitize_dirname( $dirname ) ) ) ? true : false;
  242. $directory = ( $located_plugin ) ? $plugin_dir : $theme_dir;
  243. $directory_uri = ( $located_plugin ) ? WP_PLUGIN_URL : get_parent_theme_file_uri();
  244. $foldername = str_replace( $directory, '', $dirname );
  245. $protocol_uri = ( is_ssl() ) ? 'https' : 'http';
  246. $directory_uri = set_url_scheme( $directory_uri, $protocol_uri );
  247. self::$dir = $dirname;
  248. self::$url = $directory_uri . $foldername;
  249. }
  250. // Include file helper
  251. public static function include_plugin_file( $file, $load = true ) {
  252. $path = '';
  253. $file = ltrim( $file, '/' );
  254. $override = apply_filters( 'csf_override', 'csf-override' );
  255. if ( file_exists( get_parent_theme_file_path( $override .'/'. $file ) ) ) {
  256. $path = get_parent_theme_file_path( $override .'/'. $file );
  257. } elseif ( file_exists( get_theme_file_path( $override .'/'. $file ) ) ) {
  258. $path = get_theme_file_path( $override .'/'. $file );
  259. } elseif ( file_exists( self::$dir .'/'. $override .'/'. $file ) ) {
  260. $path = self::$dir .'/'. $override .'/'. $file;
  261. } elseif ( file_exists( self::$dir .'/'. $file ) ) {
  262. $path = self::$dir .'/'. $file;
  263. }
  264. if ( ! empty( $path ) && ! empty( $file ) && $load ) {
  265. global $wp_query;
  266. if ( is_object( $wp_query ) && function_exists( 'load_template' ) ) {
  267. load_template( $path, true );
  268. } else {
  269. require_once( $path );
  270. }
  271. } else {
  272. return self::$dir .'/'. $file;
  273. }
  274. }
  275. // Is active plugin helper
  276. public static function is_active_plugin( $file = '' ) {
  277. return in_array( $file, (array) get_option( 'active_plugins', array() ) );
  278. }
  279. // Sanitize dirname
  280. public static function sanitize_dirname( $dirname ) {
  281. return preg_replace( '/[^A-Za-z]/', '', $dirname );
  282. }
  283. // Set url constant
  284. public static function include_plugin_url( $file ) {
  285. return esc_url( self::$url ) .'/'. ltrim( $file, '/' );
  286. }
  287. // Include files
  288. public static function includes() {
  289. // Include common functions
  290. self::include_plugin_file( 'functions/actions.php' );
  291. self::include_plugin_file( 'functions/helpers.php' );
  292. self::include_plugin_file( 'functions/sanitize.php' );
  293. self::include_plugin_file( 'functions/validate.php' );
  294. // Include free version classes
  295. self::include_plugin_file( 'classes/abstract.class.php' );
  296. self::include_plugin_file( 'classes/fields.class.php' );
  297. self::include_plugin_file( 'classes/admin-options.class.php' );
  298. // Include all framework fields
  299. $fields = apply_filters( 'csf_fields', array(
  300. 'accordion',
  301. 'background',
  302. 'backup',
  303. 'border',
  304. 'button_set',
  305. 'callback',
  306. 'checkbox',
  307. 'code_editor',
  308. 'color',
  309. 'color_group',
  310. 'content',
  311. 'date',
  312. 'datetime',
  313. 'dimensions',
  314. 'fieldset',
  315. 'gallery',
  316. 'group',
  317. 'heading',
  318. 'icon',
  319. 'image_select',
  320. 'link',
  321. 'link_color',
  322. 'map',
  323. 'media',
  324. 'notice',
  325. 'number',
  326. 'palette',
  327. 'radio',
  328. 'repeater',
  329. 'select',
  330. 'slider',
  331. 'sortable',
  332. 'sorter',
  333. 'spacing',
  334. 'spinner',
  335. 'subheading',
  336. 'submessage',
  337. 'switcher',
  338. 'tabbed',
  339. 'text',
  340. 'textarea',
  341. 'typography',
  342. 'upload',
  343. 'wp_editor',
  344. ) );
  345. if ( ! empty( $fields ) ) {
  346. foreach ( $fields as $field ) {
  347. if ( ! class_exists( 'CSF_Field_'. $field ) && class_exists( 'CSF_Fields' ) ) {
  348. self::include_plugin_file( 'fields/'. $field .'/'. $field .'.php' );
  349. }
  350. }
  351. }
  352. }
  353. // Setup textdomain
  354. public static function textdomain() {
  355. load_textdomain( 'sakurairo_csf', self::$dir .'/languages/'. get_locale() .'.mo' );
  356. }
  357. // Set all of used fields
  358. public static function set_used_fields( $sections ) {
  359. if ( ! empty( $sections['fields'] ) ) {
  360. foreach ( $sections['fields'] as $field ) {
  361. if ( ! empty( $field['fields'] ) ) {
  362. self::set_used_fields( $field );
  363. }
  364. if ( ! empty( $field['tabs'] ) ) {
  365. self::set_used_fields( array( 'fields' => $field['tabs'] ) );
  366. }
  367. if ( ! empty( $field['accordions'] ) ) {
  368. self::set_used_fields( array( 'fields' => $field['accordions'] ) );
  369. }
  370. if ( ! empty( $field['elements'] ) ) {
  371. self::set_used_fields( array( 'fields' => $field['elements'] ) );
  372. }
  373. if ( ! empty( $field['type'] ) ) {
  374. self::$fields[$field['type']] = $field;
  375. }
  376. }
  377. }
  378. }
  379. // Enqueue admin and fields styles and scripts
  380. public static function add_admin_enqueue_scripts() {
  381. if ( ! self::$enqueue ) {
  382. // Loads scripts and styles only when needed
  383. $wpscreen = get_current_screen();
  384. if ( ! empty( self::$args['admin_options'] ) ) {
  385. foreach ( self::$args['admin_options'] as $argument ) {
  386. if ( substr( $wpscreen->id, -strlen( $argument['menu_slug'] ) ) === $argument['menu_slug'] ) {
  387. self::$enqueue = true;
  388. }
  389. }
  390. }
  391. if ( ! empty( self::$args['metabox_options'] ) ) {
  392. foreach ( self::$args['metabox_options'] as $argument ) {
  393. if ( in_array( $wpscreen->post_type, (array) $argument['post_type'] ) ) {
  394. self::$enqueue = true;
  395. }
  396. }
  397. }
  398. if ( ! empty( self::$args['taxonomy_options'] ) ) {
  399. foreach ( self::$args['taxonomy_options'] as $argument ) {
  400. if ( in_array( $wpscreen->taxonomy, (array) $argument['taxonomy'] ) ) {
  401. self::$enqueue = true;
  402. }
  403. }
  404. }
  405. if ( ! empty( self::$shortcode_instances ) ) {
  406. foreach ( self::$shortcode_instances as $argument ) {
  407. if ( ( $argument['show_in_editor'] && $wpscreen->base === 'post' ) || $argument['show_in_custom'] ) {
  408. self::$enqueue = true;
  409. }
  410. }
  411. }
  412. if ( ! empty( self::$args['widget_options'] ) && ( $wpscreen->id === 'widgets' || $wpscreen->id === 'customize' ) ) {
  413. self::$enqueue = true;
  414. }
  415. if ( ! empty( self::$args['customize_options'] ) && $wpscreen->id === 'customize' ) {
  416. self::$enqueue = true;
  417. }
  418. if ( ! empty( self::$args['nav_menu_options'] ) && $wpscreen->id === 'nav-menus' ) {
  419. self::$enqueue = true;
  420. }
  421. if ( ! empty( self::$args['profile_options'] ) && ( $wpscreen->id === 'profile' || $wpscreen->id === 'user-edit' ) ) {
  422. self::$enqueue = true;
  423. }
  424. if ( ! empty( self::$args['comment_options'] ) && $wpscreen->id === 'comment' ) {
  425. self::$enqueue = true;
  426. }
  427. if ( $wpscreen->id === 'tools_page_csf-welcome' ) {
  428. self::$enqueue = true;
  429. }
  430. }
  431. if ( ! apply_filters( 'csf_enqueue_assets', self::$enqueue ) ) {
  432. return;
  433. }
  434. // Check for developer mode
  435. $min = ( self::$debug && SCRIPT_DEBUG ) ? '' : '.min';
  436. // Admin utilities
  437. wp_enqueue_media();
  438. // Wp color picker
  439. wp_enqueue_style( 'wp-color-picker' );
  440. wp_enqueue_script( 'wp-color-picker' );
  441. // Font awesome 4 and 5 loader
  442. if ( apply_filters( 'csf_fa4', false ) ) {
  443. wp_enqueue_style( 'csf-fa', 'https://s4.zstatic.net/ajax/libs/font-awesome/4.7.0/css/font-awesome'. $min .'.css', array(), '4.7.0', 'all' );
  444. } else {
  445. wp_enqueue_style( 'csf-fa5', 'https://s4.zstatic.net/ajax/libs/font-awesome/5.15.4/css/all'. $min .'.css', array(), '5.15.5', 'all' );
  446. wp_enqueue_style( 'csf-fa5-v4-shims', 'https://s4.zstatic.net/ajax/libs/font-awesome/5.15.4/css/v4-shims'. $min .'.css', array(), '5.15.5', 'all' );
  447. }
  448. // Main style
  449. wp_enqueue_style( 'sakurairo_csf', self::include_plugin_url( 'assets/css/style'. $min .'.css' ), array(), self::$version, 'all' );
  450. // Main RTL styles
  451. if ( is_rtl() ) {
  452. wp_enqueue_style( 'csf-rtl', self::include_plugin_url( 'assets/css/style-rtl'. $min .'.css' ), array(), self::$version, 'all' );
  453. }
  454. // Main scripts
  455. wp_enqueue_script( 'csf-plugins', self::include_plugin_url( 'assets/js/plugins'. $min .'.js' ), array(), self::$version, true );
  456. wp_enqueue_script( 'sakurairo_csf', self::include_plugin_url( 'assets/js/main'. $min .'.js' ), array( 'csf-plugins' ), self::$version, true );
  457. // Main variables
  458. wp_localize_script( 'sakurairo_csf', 'csf_vars', array(
  459. 'color_palette' => apply_filters( 'csf_color_palette', array() ),
  460. 'i18n' => array(
  461. 'confirm' => esc_html__( 'Are you sure?', 'sakurairo_csf' ),
  462. 'typing_text' => esc_html__( 'Please enter %s or more characters', 'sakurairo_csf' ),
  463. 'searching_text' => esc_html__( 'Searching...', 'sakurairo_csf' ),
  464. 'no_results_text' => esc_html__( 'No results found.', 'sakurairo_csf' ),
  465. ),
  466. ) );
  467. // Enqueue fields scripts and styles
  468. $enqueued = array();
  469. if ( ! empty( self::$fields ) ) {
  470. foreach ( self::$fields as $field ) {
  471. if ( ! empty( $field['type'] ) ) {
  472. $classname = 'CSF_Field_' . $field['type'];
  473. if ( class_exists( $classname ) && method_exists( $classname, 'enqueue' ) ) {
  474. $instance = new $classname( $field );
  475. if ( method_exists( $classname, 'enqueue' ) ) {
  476. $instance->enqueue();
  477. }
  478. unset( $instance );
  479. }
  480. }
  481. }
  482. }
  483. do_action( 'csf_enqueue' );
  484. }
  485. // Add typography enqueue styles to front page
  486. public static function add_typography_enqueue_styles() {
  487. if ( ! empty( self::$webfonts ) ) {
  488. if ( ! empty( self::$webfonts['enqueue'] ) ) {
  489. $query = array();
  490. $fonts = array();
  491. foreach ( self::$webfonts['enqueue'] as $family => $styles ) {
  492. $fonts[] = $family . ( ( ! empty( $styles ) ) ? ':'. implode( ',', $styles ) : '' );
  493. }
  494. if ( ! empty( $fonts ) ) {
  495. $query['family'] = implode( '%7C', $fonts );
  496. }
  497. if ( ! empty( self::$subsets ) ) {
  498. $query['subset'] = implode( ',', self::$subsets );
  499. }
  500. $query['display'] = 'swap';
  501. wp_enqueue_style( 'csf-google-web-fonts', esc_url( add_query_arg( $query, '//fonts.googleapis.com/css' ) ), array(), null );
  502. }
  503. if ( ! empty( self::$webfonts['async'] ) ) {
  504. $fonts = array();
  505. foreach ( self::$webfonts['async'] as $family => $styles ) {
  506. $fonts[] = $family . ( ( ! empty( $styles ) ) ? ':'. implode( ',', $styles ) : '' );
  507. }
  508. wp_enqueue_script( 'csf-google-web-fonts', esc_url( '//ajax.googleapis.com/ajax/libs/webfont/1.6.26/webfont.js' ), array(), null );
  509. wp_localize_script( 'csf-google-web-fonts', 'WebFontConfig', array( 'google' => array( 'families' => $fonts ) ) );
  510. }
  511. }
  512. }
  513. // Add admin body class
  514. public static function add_admin_body_class( $classes ) {
  515. if ( apply_filters( 'csf_fa4', false ) ) {
  516. $classes .= 'csf-fa5-shims';
  517. }
  518. return $classes;
  519. }
  520. // Add custom css to front page
  521. public static function add_custom_css() {
  522. if ( ! empty( self::$css ) ) {
  523. echo '<style type="text/css">'. wp_strip_all_tags( self::$css ) .'</style>';
  524. }
  525. }
  526. // Add a new framework field
  527. public static function field( $field = array(), $value = '', $unique = '', $where = '', $parent = '' ) {
  528. // Check for unallow fields
  529. if ( ! empty( $field['_notice'] ) ) {
  530. $field_type = $field['type'];
  531. $field = array();
  532. $field['content'] = esc_html__( 'Oops! Not allowed.', 'sakurairo_csf' ) .' <strong>('. $field_type .')</strong>';
  533. $field['type'] = 'notice';
  534. $field['style'] = 'danger';
  535. }
  536. $depend = '';
  537. $visible = '';
  538. $unique = ( ! empty( $unique ) ) ? $unique : '';
  539. $class = ( ! empty( $field['class'] ) ) ? ' ' . esc_attr( $field['class'] ) : '';
  540. $is_pseudo = ( ! empty( $field['pseudo'] ) ) ? ' csf-pseudo-field' : '';
  541. $field_type = ( ! empty( $field['type'] ) ) ? esc_attr( $field['type'] ) : '';
  542. if ( ! empty( $field['dependency'] ) ) {
  543. $dependency = $field['dependency'];
  544. $depend_visible = '';
  545. $data_controller = '';
  546. $data_condition = '';
  547. $data_value = '';
  548. $data_global = '';
  549. if ( is_array( $dependency[0] ) ) {
  550. $data_controller = implode( '|', array_column( $dependency, 0 ) );
  551. $data_condition = implode( '|', array_column( $dependency, 1 ) );
  552. $data_value = implode( '|', array_column( $dependency, 2 ) );
  553. $data_global = implode( '|', array_column( $dependency, 3 ) );
  554. $depend_visible = implode( '|', array_column( $dependency, 4 ) );
  555. } else {
  556. $data_controller = ( ! empty( $dependency[0] ) ) ? $dependency[0] : '';
  557. $data_condition = ( ! empty( $dependency[1] ) ) ? $dependency[1] : '';
  558. $data_value = ( ! empty( $dependency[2] ) ) ? $dependency[2] : '';
  559. $data_global = ( ! empty( $dependency[3] ) ) ? $dependency[3] : '';
  560. $depend_visible = ( ! empty( $dependency[4] ) ) ? $dependency[4] : '';
  561. }
  562. $depend .= ' data-controller="'. esc_attr( $data_controller ) .'"';
  563. $depend .= ' data-condition="'. esc_attr( $data_condition ) .'"';
  564. $depend .= ' data-value="'. esc_attr( $data_value ) .'"';
  565. $depend .= ( ! empty( $data_global ) ) ? ' data-depend-global="true"' : '';
  566. $visible = ( ! empty( $depend_visible ) ) ? ' csf-depend-visible' : ' csf-depend-hidden';
  567. }
  568. // These attributes has been sanitized above.
  569. echo '<div class="csf-field csf-field-'. $field_type . $is_pseudo . $class . $visible .'"'. $depend .'>';
  570. if ( ! empty( $field_type ) ) {
  571. if ( ! empty( $field['title'] ) ) {
  572. echo '<div class="csf-title">';
  573. echo '<h4>'. $field['title'] .'</h4>';
  574. echo ( ! empty( $field['subtitle'] ) ) ? '<div class="csf-subtitle-text">'. $field['subtitle'] .'</div>' : '';
  575. echo '</div>';
  576. }
  577. echo ( ! empty( $field['title'] ) ) ? '<div class="csf-fieldset">' : '';
  578. $value = ( ! isset( $value ) && isset( $field['default'] ) ) ? $field['default'] : $value;
  579. $value = ( isset( $field['value'] ) ) ? $field['value'] : $value;
  580. $classname = 'CSF_Field_'. $field_type;
  581. if ( class_exists( $classname ) ) {
  582. $instance = new $classname( $field, $value, $unique, $where, $parent );
  583. $instance->render();
  584. } else {
  585. echo '<p>'. esc_html__( 'Field not found!', 'sakurairo_csf' ) .'</p>';
  586. }
  587. } else {
  588. echo '<p>'. esc_html__( 'Field not found!', 'sakurairo_csf' ) .'</p>';
  589. }
  590. echo ( ! empty( $field['title'] ) ) ? '</div>' : '';
  591. echo '<div class="clear"></div>';
  592. echo '</div>';
  593. }
  594. }
  595. }
  596. Sakurairo_CSF::init( __FILE__ );