PluginProbe
Cooked – Recipe Management / 1.11.4
Cooked – Recipe Management v1.11.4
1.16.1 1.16.0 1.15.0 trunk 1.10.0 1.11.0 1.11.1 1.11.2 1.11.3 1.11.4 1.12.0 1.13.0 1.14.0 1.7.10 1.7.11 1.7.12 1.7.13 1.7.15.1 1.7.15.3 1.7.15.4 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 All 37 releases
cooked / includes / class.cooked-functions.php

class.cooked-functions.php in Cooked – Recipe Management 1.11.4, at includes/class.cooked-functions.php

274 lines 13.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Misc Functions
4 *
5 * @package Cooked
6 * @subpackage Misc Functions
7 * @since 1.0.0
8 */
9
10 // Exit if accessed directly
11 if ( ! defined( 'ABSPATH' ) ) exit;
12
13 class Cooked_Functions {
14
15 /**
16 * Guest message ID
17 *
18 * @var string
19 */
20 private static $guest_message_id = false;
21
22 public static function sanitize_text_field( $text ) {
23 $text = htmlentities( wp_unslash( $text ) );
24 $text = sanitize_text_field( $text );
25 return $text;
26 }
27
28 public static function array_splice_assoc( &$input, $offset, $length, $replacement = [] ) {
29 $replacement = (array) $replacement;
30
31 if ( is_array($input) ):
32 $key_indexes = array_flip(array_keys($input));
33 if (isset($input[$offset]) && is_string($offset)) {
34 $offset = $key_indexes[$offset];
35 $offset = (int) $offset + 1;
36 }
37 if (isset($input[$length]) && is_string($length)) {
38 $length = $key_indexes[$length] - $offset;
39 }
40
41 $input = array_slice($input, 0, $offset, TRUE)
42 + $replacement
43 + array_slice($input, $offset + $length, NULL, TRUE);
44 endif;
45 }
46
47 public static function wpml_xml( $cooked_tabs_fields ) {
48 echo '<div style="margin:30px 0 0 17px;">';
49 echo '<h3 style="width:70%;position:relative;z-index:2;padding:10px 0 12px;margin:0;font-size:1.2em;line-height:1.6;font-weight:600;color:#07a780">WPML Config XML</h3>';
50 echo '<pre style="color:#888;">';
51 echo esc_html('<wpml-config>
52 &nbsp;&nbsp;&nbsp;&nbsp;<custom-fields>
53 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<custom-field action="copy">_recipe_settings</custom-field>
54 &nbsp;&nbsp;&nbsp;&nbsp;</custom-fields>
55 &nbsp;&nbsp;&nbsp;&nbsp;<custom-types>
56 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<custom-type translate="1">cp_recipe</custom-type>
57 &nbsp;&nbsp;&nbsp;&nbsp;</custom-types>
58 &nbsp;&nbsp;&nbsp;&nbsp;<taxonomies>
59 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<taxonomy translate="1">cp_recipe_category</taxonomy>
60 &nbsp;&nbsp;&nbsp;&nbsp;</taxonomies>
61 &nbsp;&nbsp;&nbsp;&nbsp;<admin-texts>
62 ');
63 echo esc_html( "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<key name=\"cooked_settings\">" ) . "<br>";
64 foreach ( $cooked_tabs_fields as $key => $val ):
65 echo esc_html( "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<key name=\"" . esc_attr( $key ) . "\">" ) . "<br>";
66 foreach ( $val['fields'] as $sub_key => $sub_val ):
67 echo esc_html( "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<key name=\"" . esc_attr( $sub_key ) . "\" />" ) . "<br>";
68 endforeach;
69 echo esc_html( "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</key>" ) . "<br>";
70 endforeach;
71 echo esc_html( "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</key>" );
72 echo esc_html('
73 &nbsp;&nbsp;&nbsp;&nbsp;</admin-texts>
74 </wpml-config>');
75 echo '</pre>';
76 echo '</div>';
77 }
78
79 public static function hex2rgb( $hex ) {
80 list( $r,$g,$b ) = sscanf( $hex, "#%02x%02x%02x" );
81 $rgb_array = [$r, $g, $b];
82 return implode( ',',$rgb_array );
83 }
84
85 public static function parse_readme_changelog( $readme_url = false, $title = false ) {
86 ob_start();
87 include !$readme_url ? COOKED_DIR . 'readme.txt' : $readme_url;
88 $readme = ob_get_clean();
89
90 $readme = make_clickable(esc_html($readme));
91 $readme = preg_replace('/`(.*?)`/', '<code>\\1</code>', $readme);
92 //$readme = preg_replace( '/[\040]\*\*\NEW:\*\*/', '<strong class="new">' . __( 'New', 'cooked' ) . '</strong>', $readme);
93 //$readme = preg_replace( '/[\040]\*\*\TWEAK:\*\*/', '<strong class="tweak">' . __( 'Tweak', 'cooked' ) . '</strong>', $readme);
94 //$readme = preg_replace( '/[\040]\*\*\FIX:\*\*/', '<em class="fix">' . __( 'Fixed', 'cooked' ) . '</em>', $readme);
95 //$readme = preg_replace( '/[\040]\*\*\NEW\*\*/', '<strong class="new">' . __( 'New', 'cooked' ) . '</strong>', $readme);
96 //$readme = preg_replace( '/[\040]\*\*\TWEAK\*\*/', '<strong class="tweak">' . __( 'Tweak', 'cooked' ) . '</strong>', $readme);
97 //$readme = preg_replace( '/[\040]\*\*\FIX\*\*/', '<em class="fix">' . __( 'Fixed', 'cooked' ) . '</em>', $readme);
98 $readme = preg_replace( '/\*\*(.*?)\*\*/', '<strong>\\1</strong>', $readme);
99 $readme = preg_replace( '/\*(.*?)\*/', '<em>\\1</em>', $readme);
100 $readme = explode( '== Changelog ==', $readme );
101 $readme = $readme[1];
102 /* translators: a title for the "What's new in Cooked?" section. */
103 $whats_new_title = '<h4>' . ( $title ? esc_html( $title ) : apply_filters( 'cooked_whats_new_title', sprintf( __( "What's new in %s?", "cooked" ), 'Cooked ' . COOKED_VERSION ) ) ) . '</h4>';
104 $readme = preg_replace('/= (.*?) =/', $whats_new_title, $readme);
105 $readme = preg_replace("/\*+(.*)?/i","<ul class='cooked-whatsnew-list'><li>$1</li></ul>",$readme);
106 $readme = preg_replace("/(\<\/ul\>\n(.*)\<ul class=\'cooked-whatsnew-list\'\>*)+/","",$readme);
107 $readme = explode( $whats_new_title, $readme );
108 $readme = $whats_new_title . $readme[1];
109
110 return $readme;
111 }
112
113 public static function print_options() {
114 $default_print_options = apply_filters( 'cooked_default_print_options', [
115 'print_options_title' => 'checked',
116 'print_options_info' => '',
117 'print_options_excerpt' => '',
118 'print_options_images' => '',
119 'print_options_ingredients' => 'checked',
120 'print_options_directions' => 'checked',
121 'print_options_notes' => 'checked',
122 'print_options_nutrition' => '',
123 ]);
124
125 echo '<div id="cooked-print-options" class="cooked-clearfix">';
126
127 echo '<button class="cooked-button" onclick="window.print();">' . __( 'Print','cooked') . '</button>';
128 echo '<h3>' . __( 'Print Options:','cooked') . '</h3>';
129
130 echo '<input id="print_options_title" type="checkbox" name="print_options" value="1" ' . ( isset( $default_print_options['print_options_title'] ) ? $default_print_options['print_options_title'] : '' ) . ' /> <label for="print_options_title">' . __('Title','cooked') . '</label>';
131 echo '<input id="print_options_info" type="checkbox" name="print_options" value="1" ' . ( isset( $default_print_options['print_options_info'] ) ? $default_print_options['print_options_info'] : '' ) . ' /> <label for="print_options_info">' . __('Information','cooked') . '</label>';
132 echo '<input id="print_options_excerpt" type="checkbox" name="print_options" value="1" ' . ( isset( $default_print_options['print_options_excerpt'] ) ? $default_print_options['print_options_excerpt'] : '' ) . ' /> <label for="print_options_excerpt">' . __('Excerpt','cooked') . '</label>';
133 echo '<input id="print_options_images" type="checkbox" name="print_options" value="1" ' . ( isset( $default_print_options['print_options_images'] ) ? $default_print_options['print_options_images'] : '' ) . ' /> <label for="print_options_images">' . __('Images','cooked') . '</label>';
134 echo '<input id="print_options_ingredients" type="checkbox" name="print_options" value="1" ' . ( isset( $default_print_options['print_options_ingredients'] ) ? $default_print_options['print_options_ingredients'] : '' ) . ' /> <label for="print_options_ingredients">' . __('Ingredients','cooked') . '</label>';
135 echo '<input id="print_options_directions" type="checkbox" name="print_options" value="1" ' . ( isset( $default_print_options['print_options_directions'] ) ? $default_print_options['print_options_directions'] : '' ) . ' /> <label for="print_options_directions">' . __('Directions','cooked') . '</label>';
136 echo '<input id="print_options_notes" type="checkbox" name="print_options" value="1" ' . ( isset( $default_print_options['print_options_notes'] ) ? $default_print_options['print_options_notes'] : '' ) . ' /> <label for="print_options_notes">' . __('Notes','cooked') . '</label>';
137 echo '<input id="print_options_nutrition" type="checkbox" name="print_options" value="1" ' . ( isset( $default_print_options['print_options_nutrition'] ) ? $default_print_options['print_options_nutrition'] : '' ) . ' /> <label for="print_options_nutrition">' . __('Nutrition','cooked') . '</label>';
138
139 echo '</div>';
140 }
141
142 public static function print_options_js() {
143 ?><script type="text/javascript">
144
145 var print_options = document.getElementsByTagName('input');
146 for (var i = 0, len = print_options.length; i < len; i++) {
147 if ( print_options[i].getAttribute("name") == "print_options") {
148 update_print_options( print_options[i] );
149 }
150 }
151
152 document.addEventListener("click", function (e) {
153 update_print_options( e.target );
154 });
155
156 function update_print_options( printOpt ) {
157
158 if (printOpt.id == "print_options_title" && typeof document.getElementById('printTitle') != 'undefined') {
159 if ( printOpt.checked ){
160 document.getElementById('printTitle').style.display = 'block';
161 } else {
162 document.getElementById('printTitle').style.display = 'none';
163 }
164 }
165
166 if (printOpt.id == "print_options_nutrition" && typeof document.getElementsByClassName('cooked-nutrition-label')[0] != 'undefined') {
167 if ( printOpt.checked ){
168 document.getElementsByClassName('cooked-nutrition-label')[0].style.display = 'block';
169 } else {
170 document.getElementsByClassName('cooked-nutrition-label')[0].style.display = 'none';
171 }
172 }
173
174 if (printOpt.id == "print_options_info" && typeof document.getElementsByClassName('cooked-recipe-info')[0] != 'undefined') {
175 if ( printOpt.checked ){
176 document.getElementsByClassName('cooked-recipe-info')[0].style.display = 'block';
177 } else {
178 document.getElementsByClassName('cooked-recipe-info')[0].style.display = 'none';
179 }
180 }
181
182 if (printOpt.id == "print_options_excerpt" && typeof document.getElementsByClassName('cooked-recipe-excerpt')[0] != 'undefined') {
183 if ( printOpt.checked ){
184 document.getElementsByClassName('cooked-recipe-excerpt')[0].style.display = 'block';
185 } else {
186 document.getElementsByClassName('cooked-recipe-excerpt')[0].style.display = 'none';
187 }
188 }
189
190 if (printOpt.id == "print_options_images" && typeof document.getElementsByClassName('cooked-post-featured-image')[0] != 'undefined') {
191 if ( printOpt.checked ){
192 document.getElementsByClassName('cooked-post-featured-image')[0].style.display = 'block';
193 var print_images = document.getElementsByTagName('img');
194 for (var i = 0, len = print_images.length; i < len; i++) {
195 print_images[i].style.display = 'block';
196 }
197 } else {
198 document.getElementsByClassName('cooked-post-featured-image')[0].style.display = 'none';
199 var print_images = document.getElementsByTagName('img');
200 for (var i = 0, len = print_images.length; i < len; i++) {
201 print_images[i].style.display = 'none';
202 }
203 }
204 }
205
206 if (printOpt.id == "print_options_ingredients" && typeof document.getElementsByClassName('cooked-recipe-ingredients')[0] != 'undefined') {
207 if ( printOpt.checked ){
208 document.getElementsByClassName('cooked-recipe-ingredients')[0].style.display = 'block';
209 } else {
210 document.getElementsByClassName('cooked-recipe-ingredients')[0].style.display = 'none';
211 }
212 }
213
214 if (printOpt.id == "print_options_directions" && typeof document.getElementsByClassName('cooked-recipe-directions')[0] != 'undefined' ) {
215 if ( printOpt.checked ){
216 document.getElementsByClassName('cooked-recipe-directions')[0].style.display = 'block';
217 } else {
218 document.getElementsByClassName('cooked-recipe-directions')[0].style.display = 'none';
219 }
220 }
221
222 if (printOpt.id == "print_options_notes" && typeof document.getElementsByClassName('cooked-recipe-notes')[0] != 'undefined' ) {
223 if ( printOpt.checked ){
224 document.getElementsByClassName('cooked-recipe-notes')[0].style.display = 'block';
225 } else {
226 document.getElementsByClassName('cooked-recipe-notes')[0].style.display = 'none';
227 }
228 }
229
230 }
231
232 </script><?php
233 }
234
235 // Store the message
236 public static function set_transient_message($message) {
237 $user_id = get_current_user_id();
238 if ( !empty( $message )) {
239 if ( !empty( $user_id ) ) {
240 set_transient('cooked_user_' . $user_id . '_message', $message, 60); // Expires in 60 seconds
241 } else {
242 // For guests
243 self::$guest_message_id = uniqid();
244 set_transient('cooked_guest_' . self::$guest_message_id . '_message', $message, 60); // Expires in 60 seconds
245 return self::$guest_message_id;
246 }
247 }
248 }
249
250 // Retrieve and delete the message
251 public static function get_and_delete_transient_message($guest_message_id = false) {
252 $user_id = get_current_user_id();
253 if ( !empty( $user_id ) ) {
254 $message = get_transient('cooked_user_' . $user_id . '_message');
255 if ( !empty( $message ) ) {
256 delete_transient('cooked_user_' . $user_id . '_message');
257 return $message;
258 }
259 } else {
260 // For guests - use provided guest_message_id or fall back to static property
261 $message_id = $guest_message_id ?: self::$guest_message_id;
262 if ( $message_id ) {
263 $message = get_transient('cooked_guest_' . $message_id . '_message');
264 if ( !empty( $message ) ) {
265 delete_transient('cooked_guest_' . $message_id . '_message');
266 self::$guest_message_id = false;
267 return $message;
268 }
269 }
270 }
271 return false;
272 }
273 }
274