/
class-fw-extension-styling.php
191 lines (157 loc) · 4.58 KB
/
class-fw-extension-styling.php
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
<?php if ( ! defined( 'FW' ) ) {
die( 'Forbidden' );
}
class FW_Extension_Styling extends FW_Extension {
/** @var FW_Form */
private $form;
/** @var array */
private static $user_options = array();
/**
* @internal
*/
public function _init() {
self::$user_options = $this->get_options('appearance-settings');
if (!empty(self::$user_options)) {
if ( is_admin() ) {
$this->form = new FW_Form( $this->get_name(), array(
'render' => array( $this, '_form_render' ),
'validate' => array( $this, '_form_validate' ),
'save' => array( $this, '_form_save' )
) );
$this->add_admin_actions();
} else {
$this->add_theme_actions();
}
}
add_action('fw_option_types_init', array($this, '_action_option_types_init'));
}
public function _action_option_types_init() {
require_once dirname( __FILE__ ) . '/includes/option-types/style/class-fw-option-type-style.php';
}
/**
* @return string
*/
public function get_menu_slug()
{
return 'theme-styling';
}
protected function add_admin_actions() {
add_action( 'fw_' . $this->get_name() . '_form_save', array( $this, '_admin_action_generate_css' ) );
add_action( 'admin_menu', array( $this, '_admin_action_add_menu' ), 20 );
}
protected function add_theme_actions() {
add_action( 'wp_head', array( $this, '_theme_action_print_css' ), 99 );
}
/**
* @internal
*/
public function _admin_action_add_menu() {
$default_args = array(
'page_title' => __( 'Theme Styling', 'fw' ),
'menu_title' => __( 'Styling', 'fw' ),
'menu_slug' => $this->get_menu_slug(),
);
$filtered_args = apply_filters( 'fw_ext_' . $this->get_name() . '_page_' . $default_args['menu_slug'], $default_args );
$args = array_merge( $default_args, $filtered_args );
add_theme_page(
$args['page_title'],
$args['menu_title'],
'manage_options',
$args['menu_slug'],
array( $this, 'render_styling_settings_page' )
);
return true;
}
public function render_styling_settings_page() {
echo '<div class="wrap" style="opacity:0">';
echo '<h2>' . __( 'Theme Styling', 'fw' ) . '</h2><br/>';
$this->form->render();
echo '</div>';
}
/**
* @internal
*/
public function _form_render( $data ) {
$this->add_admin_static();
$options = array(
array(
'custom_css' => array(
'title' => false,
'type' => 'box',
'options' => self::$user_options
)
)
);
$values = FW_Request::POST( FW_Option_Type::get_default_name_prefix(), fw_get_db_extension_data( $this->get_name(), 'options' ) );
echo fw()->backend->render_options( $options, $values );
$data['submit']['html'] = '<button class="button-primary button-large">' . __( 'Save', 'fw' ) . '</button>';
unset( $options );
return $data;
}
/**
* @internal
* @param $errors
* @return array
*/
public function _form_validate( $errors ) {
if (!current_user_can('manage_options')) {
$errors[] = __('You have no permission to change Styling options', 'fw');
}
return $errors;
}
/**
* @internal
*/
public function _form_save( $data ) {
fw_set_db_extension_data( $this->get_name(), 'options', fw_get_options_values_from_input( $this->get_options('appearance-settings') ) );
do_action( 'fw_' . $this->get_name() . '_form_save' );
$data['redirect'] = fw_current_url();
return $data;
}
/*
* @internal
*/
private function add_admin_static() {
wp_enqueue_style(
'fw-extension-' . $this->get_name() . '-styles',
$this->get_declared_URI( '/static/css/styles.css' ),
array(),
fw()->manifest->get_version()
);
wp_enqueue_script(
'fw-extension-' . $this->get_name(),
$this->get_declared_URI('/static/js/scripts.js'),
array('jquery'),
fw()->manifest->get_version(),
true
);
}
/**
* Triggers when the extension settings are saved,
* it generates css from the styling settings and stores it
* @internal
*/
public function _admin_action_generate_css() {
$theme_options = fw_extract_only_options( $this->get_options('appearance-settings') );
$saved_data = fw_get_db_extension_data( $this->get_name(), 'options' );
$css_for_style_options = FW_Styling_Css_Generator::get_css( $theme_options, $saved_data );
fw_set_db_extension_data( $this->get_name(), 'css', $css_for_style_options );
}
/**
* Prints the css generated from the styling settings
* @internal
*/
public function _theme_action_print_css() {
$css = fw_get_db_extension_data( $this->get_name(), 'css' );
if ( ! empty( $css ) ) {
echo $css;
}
}
/**
* {@inheritdoc}
*/
public function _get_link()
{
return menu_page_url($this->get_menu_slug(), false);
}
}