| 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 |
<custom-fields> |
| 53 |
<custom-field action="copy">_recipe_settings</custom-field> |
| 54 |
</custom-fields> |
| 55 |
<custom-types> |
| 56 |
<custom-type translate="1">cp_recipe</custom-type> |
| 57 |
</custom-types> |
| 58 |
<taxonomies> |
| 59 |
<taxonomy translate="1">cp_recipe_category</taxonomy> |
| 60 |
</taxonomies> |
| 61 |
<admin-texts> |
| 62 |
'); |
| 63 |
echo esc_html( " <key name=\"cooked_settings\">" ) . "<br>"; |
| 64 |
foreach ( $cooked_tabs_fields as $key => $val ): |
| 65 |
echo esc_html( " <key name=\"" . esc_attr( $key ) . "\">" ) . "<br>"; |
| 66 |
foreach ( $val['fields'] as $sub_key => $sub_val ): |
| 67 |
echo esc_html( " <key name=\"" . esc_attr( $sub_key ) . "\" />" ) . "<br>"; |
| 68 |
endforeach; |
| 69 |
echo esc_html( " </key>" ) . "<br>"; |
| 70 |
endforeach; |
| 71 |
echo esc_html( " </key>" ); |
| 72 |
echo esc_html(' |
| 73 |
</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 |
if ( print_images[i].closest && print_images[i].closest('#cooked-print-logo') ) { |
| 196 |
continue; |
| 197 |
} |
| 198 |
print_images[i].style.display = 'block'; |
| 199 |
} |
| 200 |
} else { |
| 201 |
document.getElementsByClassName('cooked-post-featured-image')[0].style.display = 'none'; |
| 202 |
var print_images = document.getElementsByTagName('img'); |
| 203 |
for (var i = 0, len = print_images.length; i < len; i++) { |
| 204 |
if ( print_images[i].closest && print_images[i].closest('#cooked-print-logo') ) { |
| 205 |
continue; |
| 206 |
} |
| 207 |
print_images[i].style.display = 'none'; |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
if (printOpt.id == "print_options_ingredients" && typeof document.getElementsByClassName('cooked-recipe-ingredients')[0] != 'undefined') { |
| 213 |
if ( printOpt.checked ){ |
| 214 |
document.getElementsByClassName('cooked-recipe-ingredients')[0].style.display = 'block'; |
| 215 |
} else { |
| 216 |
document.getElementsByClassName('cooked-recipe-ingredients')[0].style.display = 'none'; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
if (printOpt.id == "print_options_directions" && typeof document.getElementsByClassName('cooked-recipe-directions')[0] != 'undefined' ) { |
| 221 |
if ( printOpt.checked ){ |
| 222 |
document.getElementsByClassName('cooked-recipe-directions')[0].style.display = 'block'; |
| 223 |
} else { |
| 224 |
document.getElementsByClassName('cooked-recipe-directions')[0].style.display = 'none'; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
if (printOpt.id == "print_options_notes" && typeof document.getElementsByClassName('cooked-recipe-notes')[0] != 'undefined' ) { |
| 229 |
if ( printOpt.checked ){ |
| 230 |
document.getElementsByClassName('cooked-recipe-notes')[0].style.display = 'block'; |
| 231 |
} else { |
| 232 |
document.getElementsByClassName('cooked-recipe-notes')[0].style.display = 'none'; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
} |
| 237 |
|
| 238 |
</script><?php |
| 239 |
} |
| 240 |
|
| 241 |
// Store the message |
| 242 |
public static function set_transient_message($message) { |
| 243 |
$user_id = get_current_user_id(); |
| 244 |
if ( !empty( $message )) { |
| 245 |
if ( !empty( $user_id ) ) { |
| 246 |
set_transient('cooked_user_' . $user_id . '_message', $message, 60); // Expires in 60 seconds |
| 247 |
} else { |
| 248 |
// For guests |
| 249 |
self::$guest_message_id = uniqid(); |
| 250 |
set_transient('cooked_guest_' . self::$guest_message_id . '_message', $message, 60); // Expires in 60 seconds |
| 251 |
return self::$guest_message_id; |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
// Retrieve and delete the message |
| 257 |
public static function get_and_delete_transient_message($guest_message_id = false) { |
| 258 |
$user_id = get_current_user_id(); |
| 259 |
if ( !empty( $user_id ) ) { |
| 260 |
$message = get_transient('cooked_user_' . $user_id . '_message'); |
| 261 |
if ( !empty( $message ) ) { |
| 262 |
delete_transient('cooked_user_' . $user_id . '_message'); |
| 263 |
return $message; |
| 264 |
} |
| 265 |
} else { |
| 266 |
// For guests - use provided guest_message_id or fall back to static property |
| 267 |
$message_id = $guest_message_id ?: self::$guest_message_id; |
| 268 |
if ( $message_id ) { |
| 269 |
$message = get_transient('cooked_guest_' . $message_id . '_message'); |
| 270 |
if ( !empty( $message ) ) { |
| 271 |
delete_transient('cooked_guest_' . $message_id . '_message'); |
| 272 |
self::$guest_message_id = false; |
| 273 |
return $message; |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
return false; |
| 278 |
} |
| 279 |
} |
| 280 |
|