Skip to content

Commit

Permalink
Hello Bar syndication
Browse files Browse the repository at this point in the history
Signed-off-by: James Hunt <[email protected]>
  • Loading branch information
thetwopct committed Sep 20, 2024
1 parent f0e7f12 commit 7f6a3ff
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,6 @@ public function custom_lf_event_column( $column, $post_id ) {
}
break;

// $yes = '<span class="dashicons dashicons-yes-alt">c</span>';

// displays if logo is present.
case 'lf_event_logo':
echo get_post_meta( $post_id, 'lf_event_logo', true ) ? '<span class="dashicons dashicons-yes-alt" style="color:green"></span>' : '<span class="dashicons dashicons-no-alt" style="color:red"></span>';
Expand Down Expand Up @@ -646,4 +644,86 @@ public function register_lf_rest_routes() {
public function set_post_preview_expiry() {
return 7 * DAY_IN_SECONDS;
}

/**
* Generate Hello Bar JS for syndication.
*/
public function generate_hello_bar_js() {
$options = get_option( 'lf-mu' );
$show_hello_bar = ( isset( $options['show_hello_bar'] ) && ! empty( $options['show_hello_bar'] ) ) ? 1 : 0;
$hello_bar_content = isset( $options['hello_bar_content'] ) ? wp_kses_post( $options['hello_bar_content'] ) : '';
$hello_bar_text = isset( $options['hello_bar_text'] ) ? esc_attr( $options['hello_bar_text'] ) : '#FFFFFF';
$hello_bar_bg = isset( $options['hello_bar_bg'] ) ? esc_attr( $options['hello_bar_bg'] ) : '#0175e4';

$js_content = "
(function() {
document.addEventListener('DOMContentLoaded', function() {
var hB = document.createElement('div');
hB.classList.add('cncf-hello-bar');
hB.style.cssText = '
position: relative;
top: 0;
left: 0;
width: 100%;
background-color: {$hello_bar_bg};
color: {$hello_bar_text};
text-align: center;
padding: 6.4px;
font-family: Clarity City,-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Roboto,Ubuntu,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol;
font-weight: 400;
font-size: 14.4px;
letter-spacing: 0.24px;
line-height: 1.15;
opacity: 0;
';
hB.innerHTML = `{$hello_bar_content}`;
document.body.insertBefore(hB, document.body.firstChild);
var style = document.createElement('style');
style.innerHTML = `
.cncf-hello-bar a {
color: inherit;
font-weight: 400;
text-decoration: underline;
text-underline-position: under;
transition: all 0.1s ease;
}
.cncf-hello-bar a:hover {
text-decoration: none;
text-underline-position: unset;
}
`;
document.head.appendChild(style);
hB.style.opacity = '1';
});
})();
";

$js_content = $this->minify_js( $js_content );

// If we want to turn off the bar, remove all the content in it.
if ( ! $show_hello_bar ) {
$js_content = '';
}

$upload_dir = wp_upload_dir();
$file_path = $upload_dir['basedir'] . '/hello-bar.js';

if ( file_put_contents( $file_path, $js_content ) === false ) {
error_log( 'Failed to write hello-bar.js' );
}
}

/**
* Quick and dirty minify
*
* @param string $js Content.
*/
protected function minify_js( $js ) {
$js = preg_replace( '#/\*.*?\*/#s', '', $js );
$js = preg_replace( '#(?<!:)//.*#', '', $js );
$js = str_replace( array( "\n", "\r", "\t" ), '', $js );
$js = preg_replace( '/\s+/', ' ', $js );
return $js;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@

$hello_bar_text = ( isset( $options['hello_bar_text'] ) && ! empty( $options['hello_bar_text'] ) ) ? esc_attr( $options['hello_bar_text'] ) : '';

$upload_dir = wp_upload_dir();
$hello_bar_js_file_path = $upload_dir['basedir'] . '/hello-bar.js';
$hello_bar_js_file_url = $upload_dir['baseurl'] . '/hello-bar.js';

$header_image_id = ( isset( $options['header_image_id'] ) && ! empty( $options['header_image_id'] ) ) ? absint( $options['header_image_id'] ) : '';

$header_cta_text = ( isset( $options['header_cta_text'] ) && ! empty( $options['header_cta_text'] ) ) ? esc_attr( $options['header_cta_text'] ) : '';
Expand Down Expand Up @@ -191,6 +195,28 @@
</div>
</td>
</tr>
<tr>
<th scope="row">
<label for="hello_bar_syndication">Hello Bar
Syndication</label>
</th>
<td colspan="2">

<?php
if ( $hello_bar_js_file_path ) {
?>
<p
style="margin-bottom: 5px;">Use the below script on your websites to embed the Hello Bar.</p>
<input type="text" disabled style="width:100%;"
name="hello-bar-url"
value='<script src="<?php echo esc_attr( $hello_bar_js_file_url ); ?>"></script>' /> <?php // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript ?>
<p
style="margin-top: 5px;">Note: A <i>change</i> to the Hello Bar settings is required to regenerate the JS file. To hide the Hello Bar on syndicated sites, choose to not Show the Hello Bar.</p>
<?php
}
?>
</td>
</tr>
</tbody>
</table>
<hr />
Expand Down Expand Up @@ -509,7 +535,8 @@ class="clear_upload_image_button button"
</td>
</tr>
<tr>
<th scope="row"><label for="social_twitter">X (formerly Twitter)</label>
<th scope="row"><label for="social_twitter">X (formerly
Twitter)</label>
</th>
<td>
<input type="text" class="social_twitter regular-text"
Expand Down Expand Up @@ -606,12 +633,12 @@ class="promotion_cta_text regular-small-text"
CTA Link</label>
</th>
<td>
<input type="text"
<input type="text"
class="promotion_cta_link regular-text"
id="<?php echo esc_html( $this->plugin_name ); ?>-promotion_cta_link"
name="<?php echo esc_html( $this->plugin_name ); ?>[promotion_cta_link]"
value="<?php echo esc_url( $promotion_cta_link ); ?>"
placeholder="https://training.linuxfoundation.org/cyber-monday-cncf-2023/" />
placeholder="https://training.linuxfoundation.org/cyber-monday-cncf-2023/" />
</td>
</tr>
</tbody>
Expand Down Expand Up @@ -688,12 +715,12 @@ class="promotion_cta_text2 regular-small-text"
CTA Link</label>
</th>
<td>
<input type="text"
<input type="text"
class="promotion_cta_link2 regular-text"
id="<?php echo esc_html( $this->plugin_name ); ?>-promotion_cta_link2"
name="<?php echo esc_html( $this->plugin_name ); ?>[promotion_cta_link2]"
value="<?php echo esc_url( $promotion_cta_link2 ); ?>"
placeholder="https://training.linuxfoundation.org/cyber-monday-cncf-2023/" />
placeholder="https://training.linuxfoundation.org/cyber-monday-cncf-2023/" />
</td>
</tr>
</tbody>
Expand Down Expand Up @@ -789,33 +816,39 @@ class="clear_upload_image_button button"
</td>
</tr>
<tr>
<th scope="row"><label for="google_maps_api_key">Google Maps API
<th scope="row"><label for="google_maps_api_key">Google Maps
API
key for geocoding</label>
</th>
<td>
<input type="text" class="google_maps_api_key regular-text"
<input type="text"
class="google_maps_api_key regular-text"
id="<?php echo esc_html( $this->plugin_name ); ?>-google_maps_api_key"
name="<?php echo esc_html( $this->plugin_name ); ?>[google_maps_api_key]"
value="<?php echo esc_attr( $google_maps_api_key ); ?>" />
</td>
</tr>
<tr>
<th scope="row"><label for="google_maps_api_public_key">Google Maps API
<th scope="row"><label
for="google_maps_api_public_key">Google Maps API
key for public map js</label>
</th>
<td>
<input type="text" class="google_maps_api_public_key regular-text"
<input type="text"
class="google_maps_api_public_key regular-text"
id="<?php echo esc_html( $this->plugin_name ); ?>-google_maps_api_public_key"
name="<?php echo esc_html( $this->plugin_name ); ?>[google_maps_api_public_key]"
value="<?php echo esc_attr( $google_maps_api_public_key ); ?>" />
</td>
</tr>
<tr>
<th scope="row"><label for="community_api_key">Community Site API
<th scope="row"><label for="community_api_key">Community
Site API
key</label>
</th>
<td>
<input type="text" class="community_api_key regular-text"
<input type="text"
class="community_api_key regular-text"
id="<?php echo esc_html( $this->plugin_name ); ?>-community_api_key"
name="<?php echo esc_html( $this->plugin_name ); ?>[community_api_key]"
value="<?php echo esc_attr( $community_api_key ); ?>" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ private function define_admin_hooks() {
$this->loader->add_filter( 'pre_get_posts', $plugin_admin, 'set_events_admin_order' );

$this->loader->add_filter( 'ppp_nonce_life', $plugin_admin, 'set_post_preview_expiry' );

$this->loader->add_action( 'update_option_lf-mu', $plugin_admin, 'generate_hello_bar_js', 10, 3 );
}

/**
Expand Down

0 comments on commit 7f6a3ff

Please sign in to comment.