Created
March 29, 2024 02:44
-
-
Save wpeasy/8691772a4c2356f41e0a1a98ab9ec532 to your computer and use it in GitHub Desktop.
Bricks Live Update Options
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action('rest_api_init', 'wpe_register_live_options_rest_route'); | |
function wpe_register_live_options_rest_route() { | |
register_rest_route('v1/wpe_live_options', '/get_options', array( | |
'methods' => 'GET', | |
'callback' => 'mb_live_options_get_options', | |
'permission_callback' => 'wpe_verify_referrer_hostname' | |
)); | |
} | |
/* use for MetaBox */ | |
function mb_live_options_get_options($request) { | |
$options = get_option('live-options'); | |
return new WP_REST_Response( $options , 200); | |
} | |
/* use for ACF */ | |
function acf_live_options_get_options() { | |
// Ensure ACF is installed and active | |
if (function_exists('get_field')) { | |
// Define the associative array to store the options | |
$options = array(); | |
// Attempt to get all fields from the options page with the specified slug | |
$fields = acf_get_fields('group_660413af7a603'); | |
if ($fields) { | |
foreach ($fields as $field) { | |
// Add each field to the associative array | |
$name = $field['name']; | |
$options[$name] = get_field( $name, 'option'); | |
} | |
} | |
return new WP_REST_Response( $options , 200); | |
} else { | |
// ACF is not installed or active, return an empty array | |
return array(); | |
} | |
} | |
function wpe_verify_referrer_hostname($request) { | |
$referrer = wp_get_referer(); | |
$site_url = parse_url(get_site_url()); | |
if (parse_url($referrer, PHP_URL_HOST) === $site_url['host']) { | |
return true; | |
} | |
return new WP_Error('rest_forbidden', esc_html__('Unauthorized access, referrer does not match.', 'wpe'), array('status' => 403)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"content":[{"id":"liofbe","name":"section","parent":0,"children":["grszif","tzwpmo"],"settings":{}},{"id":"grszif","name":"code","parent":"liofbe","children":[],"settings":{"executeCode":true,"signature":"b93694e241067b5dae9ebdf4865fcafc","user_id":1,"time":1711679722,"code":"<script defer>\n; (() => {\n\n const pollInterval = 5000 /* ms */\n\n document.addEventListener('DOMContentLoaded', () => {\n const liveOptionElements = document.querySelectorAll('[data-live-option-id]');\n\n const updateElements = data => {\n liveOptionElements.forEach(el => {\n const el_name = el.dataset.liveOptionId;\n if (undefined !== data[el_name]) {\n el.innerHTML = data[el_name];\n }\n });\n }\n\n const get_data = () => {\n fetch('/wp-json/v1/wpe_live_options/get_options', {\n method: 'GET',\n headers: {\n 'Content-Type': 'application/json',\n },\n credentials: 'same-origin' // Ensure cookies for WordPress sites are sent with the request.\n })\n .then(response => {\n if (!response.ok) {\n throw new Error(`HTTP error! status: ${response.status}`);\n }\n return response.json();\n })\n .then(data => {\n updateElements(data);\n })\n .catch(error => {\n console.error('Error fetching the data: ', error);\n })\n .finally();\n\n };\n setInterval(() => {\n get_data();\n }, pollInterval);\n\n });\n\n})();\n</script>"}},{"id":"tzwpmo","name":"container","parent":"liofbe","children":["buxdrn"],"settings":{"_alignItems":"center"}},{"id":"buxdrn","name":"div","parent":"tzwpmo","children":["oziraj","ozgmxd","ftvzvd"],"settings":{"_display":"flex","_direction":"column","_alignItems":"center","_padding":{"top":"var(--space-m)","right":"var(--space-m)","bottom":"var(--space-m)","left":"var(--space-m)"},"_background":{"color":{"id":"bbcolors_neutral-ultra-dark-trans-90","name":"neutral-ultra-dark-trans-90","raw":"var(--neutral-ultra-dark-trans-90)"}},"_typography":{"color":{"id":"bbcolors_base-ultra-light","name":"base-ultra-light","raw":"var(--base-ultra-light)"}},"_border":{"radius":{"top":"var(--radius-m)","right":"var(--radius-m)","bottom":"var(--radius-m)","left":"var(--radius-m)"}},"_cssCustom":"#brxe-buxdrn {\n\tbox-shadow: var(--box-shadow-m);\n}"}},{"id":"oziraj","name":"heading","parent":"buxdrn","children":[],"settings":{"_attributes":[{"id":"ngfzlt","name":"data-live-option-id","value":"status_heading"}],"text":"{mb_live-options_status_heading}","tag":"h2"}},{"id":"ozgmxd","name":"text-basic","parent":"buxdrn","children":[],"settings":{"tag":"p","_attributes":[{"id":"xcbhwf","name":"data-live-option-id","value":"status_textarea"}],"text":"{mb_live-options_status_textarea}"}},{"id":"ftvzvd","name":"text-basic","parent":"buxdrn","children":[],"settings":{"tag":"p","_attributes":[{"id":"xcbhwf","name":"data-live-option-id","value":"status_level"}],"text":"{mb_live-options_status_level}"}}],"source":"bricksCopiedElements","sourceUrl":"https://dev.wpevolve.net","version":"1.9.7.1","globalClasses":[],"globalElements":[]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment