PluginProbe
Cooked – Recipe Management / 1.9.5
Cooked – Recipe Management v1.9.5
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.9.5, at includes/class.cooked-functions.php

251 lines 12.5 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 public static function sanitize_text_field( $text ) {
16 $text = htmlentities( stripslashes( $text ) );
17 $text = sanitize_text_field( $text );
18 return $text;
19 }
20
21 public static function array_splice_assoc( &$input, $offset, $length, $replacement = [] ) {
22 $replacement = (array) $replacement;
23
24 if ( is_array($input) ):
25 $key_indexes = array_flip(array_keys($input));
26 if (isset($input[$offset]) && is_string($offset)) {
27 $offset = $key_indexes[$offset];
28 $offset = (int) $offset + 1;
29 }
30 if (isset($input[$length]) && is_string($length)) {
31 $length = $key_indexes[$length] - $offset;
32 }
33
34 $input = array_slice($input, 0, $offset, TRUE)
35 + $replacement
36 + array_slice($input, $offset + $length, NULL, TRUE);
37 endif;
38 }
39
40 public static function wpml_xml( $cooked_tabs_fields ) {
41 echo '<div style="margin:30px 0 0 17px;">';
42 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>';
43 echo '<pre style="color:#888;">';
44 echo esc_html('<wpml-config>
45 &nbsp;&nbsp;&nbsp;&nbsp;<custom-fields>
46 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<custom-field action="copy">_recipe_settings</custom-field>
47 &nbsp;&nbsp;&nbsp;&nbsp;</custom-fields>
48 &nbsp;&nbsp;&nbsp;&nbsp;<custom-types>
49 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<custom-type translate="1">cp_recipe</custom-type>
50 &nbsp;&nbsp;&nbsp;&nbsp;</custom-types>
51 &nbsp;&nbsp;&nbsp;&nbsp;<taxonomies>
52 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<taxonomy translate="1">cp_recipe_category</taxonomy>
53 &nbsp;&nbsp;&nbsp;&nbsp;</taxonomies>
54 &nbsp;&nbsp;&nbsp;&nbsp;<admin-texts>
55 ');
56 echo esc_html( "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<key name=\"cooked_settings\">" ) . "<br>";
57 foreach ( $cooked_tabs_fields as $key => $val ):
58 echo esc_html( "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<key name=\"" . esc_attr( $key ) . "\">" ) . "<br>";
59 foreach ( $val['fields'] as $sub_key => $sub_val ):
60 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>";
61 endforeach;
62 echo esc_html( "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</key>" ) . "<br>";
63 endforeach;
64 echo esc_html( "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</key>" );
65 echo esc_html('
66 &nbsp;&nbsp;&nbsp;&nbsp;</admin-texts>
67 </wpml-config>');
68 echo '</pre>';
69 echo '</div>';
70 }
71
72 public static function hex2rgb( $hex ) {
73 list( $r,$g,$b ) = sscanf( $hex, "#%02x%02x%02x" );
74 $rgb_array = [$r, $g, $b];
75 return implode( ',',$rgb_array );
76 }
77
78 public static function parse_readme_changelog( $readme_url = false, $title = false ) {
79 ob_start();
80 include !$readme_url ? COOKED_DIR . 'readme.txt' : $readme_url;
81 $readme = ob_get_clean();
82
83 $readme = make_clickable(esc_html($readme));
84 $readme = preg_replace('/`(.*?)`/', '<code>\\1</code>', $readme);
85 //$readme = preg_replace( '/[\040]\*\*\NEW:\*\*/', '<strong class="new">' . __( 'New', 'cooked' ) . '</strong>', $readme);
86 //$readme = preg_replace( '/[\040]\*\*\TWEAK:\*\*/', '<strong class="tweak">' . __( 'Tweak', 'cooked' ) . '</strong>', $readme);
87 //$readme = preg_replace( '/[\040]\*\*\FIX:\*\*/', '<em class="fix">' . __( 'Fixed', 'cooked' ) . '</em>', $readme);
88 //$readme = preg_replace( '/[\040]\*\*\NEW\*\*/', '<strong class="new">' . __( 'New', 'cooked' ) . '</strong>', $readme);
89 //$readme = preg_replace( '/[\040]\*\*\TWEAK\*\*/', '<strong class="tweak">' . __( 'Tweak', 'cooked' ) . '</strong>', $readme);
90 //$readme = preg_replace( '/[\040]\*\*\FIX\*\*/', '<em class="fix">' . __( 'Fixed', 'cooked' ) . '</em>', $readme);
91 $readme = preg_replace( '/\*\*(.*?)\*\*/', '<strong>\\1</strong>', $readme);
92 $readme = preg_replace( '/\*(.*?)\*/', '<em>\\1</em>', $readme);
93 $readme = explode( '== Changelog ==', $readme );
94 $readme = $readme[1];
95 /* translators: a title for the "What's new in Cooked?" section. */
96 $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>';
97 $readme = preg_replace('/= (.*?) =/', $whats_new_title, $readme);
98 $readme = preg_replace("/\*+(.*)?/i","<ul class='cooked-whatsnew-list'><li>$1</li></ul>",$readme);
99 $readme = preg_replace("/(\<\/ul\>\n(.*)\<ul class=\'cooked-whatsnew-list\'\>*)+/","",$readme);
100 $readme = explode( $whats_new_title, $readme );
101 $readme = $whats_new_title . $readme[1];
102
103 return $readme;
104 }
105
106 public static function print_options() {
107 $default_print_options = apply_filters( 'cooked_default_print_options', [
108 'print_options_title' => 'checked',
109 'print_options_info' => '',
110 'print_options_excerpt' => '',
111 'print_options_images' => '',
112 'print_options_ingredients' => 'checked',
113 'print_options_directions' => 'checked',
114 'print_options_notes' => 'checked',
115 'print_options_nutrition' => '',
116 ]);
117
118 echo '<div id="cooked-print-options" class="cooked-clearfix">';
119
120 echo '<button class="cooked-button" onclick="window.print();">' . __( 'Print','cooked') . '</button>';
121 echo '<h3>' . __( 'Print Options:','cooked') . '</h3>';
122
123 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>';
124 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>';
125 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>';
126 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>';
127 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>';
128 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>';
129 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>';
130 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>';
131
132 echo '</div>';
133 }
134
135 public static function print_options_js() {
136
137 ?><script type="text/javascript">
138
139 var print_options = document.getElementsByTagName('input');
140 for (var i = 0, len = print_options.length; i < len; i++) {
141 if ( print_options[i].getAttribute("name") == "print_options") {
142 update_print_options( print_options[i] );
143 }
144 }
145
146 document.addEventListener("click", function (e) {
147 update_print_options( e.target );
148 });
149
150 function update_print_options( printOpt ) {
151
152 if (printOpt.id == "print_options_title" && typeof document.getElementById('printTitle') != 'undefined') {
153 if ( printOpt.checked ){
154 document.getElementById('printTitle').style.display = 'block';
155 } else {
156 document.getElementById('printTitle').style.display = 'none';
157 }
158 }
159
160 if (printOpt.id == "print_options_nutrition" && typeof document.getElementsByClassName('cooked-nutrition-label')[0] != 'undefined') {
161 if ( printOpt.checked ){
162 document.getElementsByClassName('cooked-nutrition-label')[0].style.display = 'block';
163 } else {
164 document.getElementsByClassName('cooked-nutrition-label')[0].style.display = 'none';
165 }
166 }
167
168 if (printOpt.id == "print_options_info" && typeof document.getElementsByClassName('cooked-recipe-info')[0] != 'undefined') {
169 if ( printOpt.checked ){
170 document.getElementsByClassName('cooked-recipe-info')[0].style.display = 'block';
171 } else {
172 document.getElementsByClassName('cooked-recipe-info')[0].style.display = 'none';
173 }
174 }
175
176 if (printOpt.id == "print_options_excerpt" && typeof document.getElementsByClassName('cooked-recipe-excerpt')[0] != 'undefined') {
177 if ( printOpt.checked ){
178 document.getElementsByClassName('cooked-recipe-excerpt')[0].style.display = 'block';
179 } else {
180 document.getElementsByClassName('cooked-recipe-excerpt')[0].style.display = 'none';
181 }
182 }
183
184 if (printOpt.id == "print_options_images" && typeof document.getElementsByClassName('cooked-post-featured-image')[0] != 'undefined') {
185 if ( printOpt.checked ){
186 document.getElementsByClassName('cooked-post-featured-image')[0].style.display = 'block';
187 var print_images = document.getElementsByTagName('img');
188 for (var i = 0, len = print_images.length; i < len; i++) {
189 print_images[i].style.display = 'block';
190 }
191 } else {
192 document.getElementsByClassName('cooked-post-featured-image')[0].style.display = 'none';
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 = 'none';
196 }
197 }
198 }
199
200 if (printOpt.id == "print_options_ingredients" && typeof document.getElementsByClassName('cooked-recipe-ingredients')[0] != 'undefined') {
201 if ( printOpt.checked ){
202 document.getElementsByClassName('cooked-recipe-ingredients')[0].style.display = 'block';
203 } else {
204 document.getElementsByClassName('cooked-recipe-ingredients')[0].style.display = 'none';
205 }
206 }
207
208 if (printOpt.id == "print_options_directions" && typeof document.getElementsByClassName('cooked-recipe-directions')[0] != 'undefined' ) {
209 if ( printOpt.checked ){
210 document.getElementsByClassName('cooked-recipe-directions')[0].style.display = 'block';
211 } else {
212 document.getElementsByClassName('cooked-recipe-directions')[0].style.display = 'none';
213 }
214 }
215
216 if (printOpt.id == "print_options_notes" && typeof document.getElementsByClassName('cooked-recipe-notes')[0] != 'undefined' ) {
217 if ( printOpt.checked ){
218 document.getElementsByClassName('cooked-recipe-notes')[0].style.display = 'block';
219 } else {
220 document.getElementsByClassName('cooked-recipe-notes')[0].style.display = 'none';
221 }
222 }
223
224 }
225
226 </script><?php
227
228 }
229
230 // Store the message
231 public static function set_transient_message($message) {
232 $user_id = get_current_user_id();
233 if ( !empty( $user_id ) && !empty( $message ) ) {
234 set_transient('cooked_user_' . $user_id . '_message', $message, 60); // Expires in 60 seconds
235 }
236 }
237
238 // Retrieve and delete the message
239 public static function get_and_delete_transient_message() {
240 $user_id = get_current_user_id();
241 if ( !empty( $user_id ) ) {
242 $message = get_transient('cooked_user_' . $user_id . '_message');
243 if ( !empty( $message ) ) {
244 delete_transient('cooked_user_' . $user_id . '_message');
245 return $message;
246 }
247 }
248 return false;
249 }
250 }
251