-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpocitadlo-libi-se.php
281 lines (249 loc) · 9.58 KB
/
pocitadlo-libi-se.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
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
<?php
/**
* Plugin Name: Počítadlo Líbí se
* Plugin URI: https://www.seznam.cz/
* Description: Integrace sociálního tlačítka Líbí se od Seznam.cz.
* Version: 1.0.0
* Requires at least: 4.7
* Requires PHP: 7.0
* Author: Seznam.cz
* Author URI: https://www.seznam.cz/
* License: ISC
* License URI: https://opensource.org/licenses/ISC
*/
require_once __DIR__ . '/pocitadlo-libi-se-common/index.php';
use function Cz\Seznam\PocitadloLibiSe\renderButton;
use function Cz\Seznam\PocitadloLibiSe\renderButtonScript;
use Cz\Seznam\PocitadloLibiSe\ButtonColorVariable;
use Cz\Seznam\PocitadloLibiSe\ButtonElementAttributeName;
use Cz\Seznam\PocitadloLibiSe\ButtonLayout;
use Cz\Seznam\PocitadloLibiSe\ButtonSize;
add_action( 'wp_head', function () {
$styles = file_get_contents( __DIR__ . '/pocitadlo-libi-se-common/pocitadlolibise.css' );
echo "<style>\n$styles</style>\n";
echo renderButtonScript() . "\n";
} );
(function () {
$PAGE_ID = 'cz-seznam-pocitadlolibise';
$OPTION_NAME = "$PAGE_ID-options";
final class ButtonPosition {
const CONTENT_START = 'content_start';
const CONTENT_END = 'content_end';
const CONTENT_START_AND_END = 'content_start_and_end';
private function __construct() {}
}
$DEFAULT_OPTIONS = [
ButtonElementAttributeName::LAYOUT => ButtonLayout::SEAMLESS,
ButtonElementAttributeName::SIZE => ButtonSize::SMALL,
'button_position' => ButtonPosition::CONTENT_END,
ButtonColorVariable::PRIMARY_COLOR => '#111111',
ButtonColorVariable::BACKGROUND_COLOR => '#ffffff',
ButtonColorVariable::HOVER_COLOR => '#888888',
ButtonColorVariable::COUNT_COLOR => '#888888',
ButtonColorVariable::ACTIVE_COLOR => '#de0000',
ButtonElementAttributeName::ANALYTICS_HIT_PAYLOAD => '',
];
$PLACEHOLDER = '<span></span>';
add_filter( 'the_content', function ($content) use ($OPTION_NAME, $DEFAULT_OPTIONS, $PLACEHOLDER) {
if ( is_main_query() && in_the_loop() && is_single() ) {
$options = get_option( $OPTION_NAME, $DEFAULT_OPTIONS );
$buttonOptions = [
ButtonElementAttributeName::ENTITY_ID => get_permalink(),
ButtonElementAttributeName::LAYOUT => $options[ ButtonElementAttributeName::LAYOUT ],
ButtonElementAttributeName::SIZE => $options[ ButtonElementAttributeName::SIZE ],
ButtonElementAttributeName::ANALYTICS_HIT_PAYLOAD => $options[
ButtonElementAttributeName::ANALYTICS_HIT_PAYLOAD
],
'colors' => [
ButtonColorVariable::PRIMARY_COLOR => $options[ ButtonColorVariable::PRIMARY_COLOR ],
ButtonColorVariable::BACKGROUND_COLOR => $options[ ButtonColorVariable::BACKGROUND_COLOR ],
ButtonColorVariable::HOVER_COLOR => $options[ ButtonColorVariable::HOVER_COLOR ],
ButtonColorVariable::COUNT_COLOR => $options[ ButtonColorVariable::COUNT_COLOR ],
ButtonColorVariable::ACTIVE_COLOR => $options[ ButtonColorVariable::ACTIVE_COLOR ],
],
];
if (
$options[ 'button_position' ] === ButtonPosition::CONTENT_START ||
$options[ 'button_position' ] === ButtonPosition::CONTENT_START_AND_END
) {
$socialButton = renderButton(
array_merge(
$buttonOptions,
[ ButtonElementAttributeName::ANALYTICS_HIT_BUTTON_POSITION => ButtonPosition::CONTENT_START ]
),
$PLACEHOLDER
);
$content = "<div class=\"seznam-pocitadlo-libi-se\">$socialButton</div>\n$content";
}
if (
$options[ 'button_position' ] === ButtonPosition::CONTENT_END ||
$options[ 'button_position' ] === ButtonPosition::CONTENT_START_AND_END
) {
$socialButton = renderButton(
array_merge(
$buttonOptions,
[ ButtonElementAttributeName::ANALYTICS_HIT_BUTTON_POSITION => ButtonPosition::CONTENT_END ]
),
$PLACEHOLDER
);
$content = "$content\n<div class=\"seznam-pocitadlo-libi-se\">$socialButton</div>";
}
return $content;
}
return $content;
} );
$settings_section_header = function ($args) {
echo "<a id=\"" . esc_attr( $args['id'] ) . "\"></p>";
};
$settings_page = function () use ($PAGE_ID) {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
if ( isset( $_GET['settings-updated'] ) ) {
add_settings_error(
'cz_seznam_pocitadlolibise_messages',
'cz_seznam_pocitadlolibise_message',
'Nastavení bylo uloženo.',
'success'
);
}
settings_errors( 'cz_seznam_pocitadlolibise_messages' );
?>
<div class="wrap">
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
<form action="options.php" method="post">
<?php
settings_fields( $PAGE_ID );
// (sections are registered for "wporg", each field is registered to a specific section)
do_settings_sections( $PAGE_ID );
// output save settings button
submit_button( 'Uložit nastavení' );
?>
</form>
</div>
<?php
};
$settings_field_input = function ($args) use ($OPTION_NAME, $DEFAULT_OPTIONS) {
$options = get_option( $OPTION_NAME, $DEFAULT_OPTIONS );
?>
<input
id="<?php echo esc_attr( $args['label_for'] ); ?>"
name="<?php echo esc_attr( $OPTION_NAME ) ?>[<?php echo esc_attr( $args['label_for'] ); ?>]"
type="<?php echo esc_attr( $args['type'] ) ?>"
value="<?php echo esc_attr( $options[ $args['label_for'] ] ) ?>"
>
<?php
};
$settings_field_select = function ($args) use ($OPTION_NAME, $DEFAULT_OPTIONS) {
$options = get_option( $OPTION_NAME, $DEFAULT_OPTIONS );
?>
<select
id="<?php echo esc_attr( $args['label_for'] ); ?>"
name="<?php echo esc_attr( $OPTION_NAME ) ?>[<?php echo esc_attr( $args['label_for'] ); ?>]"
>
<?php foreach ( $args['options'] as $value => $label ) {
?>
<option
value="<?php echo esc_attr( $value ) ?>"
<?php echo isset( $options[ $args['label_for'] ] ) ? ( selected( $options[ $args['label_for'] ], $value, false ) ) : ( '' ); ?>
>
<?php echo esc_html( $label ) ?>
</option>
<?php
} ?>
</select>
<?php
};
add_action( 'admin_init', function () use (
$PAGE_ID,
$OPTION_NAME,
$DEFAULT_OPTIONS,
$settings_section_header,
$settings_field_input,
$settings_field_select
) {
$SETTINGS_SECTION = 'cz_seznam_pocitadlolibise';
register_setting( $PAGE_ID, $OPTION_NAME, [
'type' => 'array',
'description' => 'Configuration for the Líbí se social button by Seznam.cz',
'show_in_rest' => false,
'default' => $DEFAULT_OPTIONS,
] );
add_settings_section(
$SETTINGS_SECTION,
'Nastavení vzhledu',
$settings_section_header,
$PAGE_ID
);
$szn_add_settings_field = function ($name, $label, $callback, $options) use ($PAGE_ID, $SETTINGS_SECTION) {
add_settings_field(
$name,
$label,
$callback,
$PAGE_ID,
$SETTINGS_SECTION,
array_merge(
[
'label_for' => $name,
],
$options
)
);
};
$szn_add_settings_input = function ($name, $label, $type) use ($szn_add_settings_field, $settings_field_input) {
$szn_add_settings_field($name, $label, $settings_field_input, [ 'type' => $type ]);
};
$szn_add_settings_select = function ($name, $label, $options) use (
$szn_add_settings_field,
$settings_field_select
) {
$szn_add_settings_field($name, $label, $settings_field_select, [ 'options' => $options ]);
};
$szn_add_settings_select(
ButtonElementAttributeName::LAYOUT,
'Šablona vzhledu',
[
ButtonLayout::SEAMLESS => 'Obarvitelná',
ButtonLayout::BUTTON_COUNT => 'Tlačítko',
ButtonLayout::BOX_COUNT => 'Box',
]
);
$szn_add_settings_select(
ButtonElementAttributeName::SIZE,
'Velikost',
[
ButtonSize::MINIMALISTIC => 'Minimalistická (pouze obarvitelný vzhled)',
ButtonSize::SMALL => 'Malá',
ButtonSize::LARGE => 'Velká (nepodporuje obarvitelný vzhled)',
]
);
$szn_add_settings_select(
'button_position',
'Umístění tlačítka',
[
ButtonPosition::CONTENT_START => 'Začátek příspěvku',
ButtonPosition::CONTENT_END => 'Konec příspěvku',
ButtonPosition::CONTENT_START_AND_END => 'Začátek i konec příspěvku',
]
);
$szn_add_settings_input(ButtonColorVariable::PRIMARY_COLOR, 'Základní barva textu', 'color');
$szn_add_settings_input(ButtonColorVariable::BACKGROUND_COLOR, 'Barva pozadí', 'color');
$szn_add_settings_input(ButtonColorVariable::HOVER_COLOR, 'Barva textu při najetí myší', 'color');
$szn_add_settings_input(ButtonColorVariable::COUNT_COLOR, 'Barva počtu hlasů', 'color');
$szn_add_settings_input(ButtonColorVariable::ACTIVE_COLOR, 'Barva textu po zahlasování', 'color');
$szn_add_settings_input(ButtonElementAttributeName::ANALYTICS_HIT_PAYLOAD, 'Dodatečná data pro analytiku', 'text');
} );
add_action( 'admin_menu', function () use ($PAGE_ID, $settings_page) {
add_options_page(
'Počítadlo Líbí se',
'Počítadlo Líbí se',
'manage_options',
$PAGE_ID,
$settings_page
);
} );
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), function ($links) use ($PAGE_ID) {
$links[] = '<a href="' . esc_url( get_admin_url( null, 'options-general.php?page=' . $PAGE_ID ) ) . '">Nastavení</a>';
return $links;
} );
})();