Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hello Bar Syndication #886

Closed
wants to merge 8 commits into from
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,129 @@ 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;
z-index: 9999;
transition: opacity 0.5s ease;
';

const subdomain = window.location.hostname.split('.')[0];
const hbContent = `{$hello_bar_content}`;
hB.innerHTML = hbContent.replace(/utm_source=www/g, 'utm_source=' + subdomain );

document.body.insertBefore(hB, document.body.firstChild);

var fixedNav = document.querySelector('.td-navbar');

var isNavFixed = function() {
return window.getComputedStyle(fixedNav).position === 'fixed';
};

var updateNavPosition = function() {
if (!fixedNav) return;

if (isNavFixed()) {
var hBHeight = hB.offsetHeight;
if (window.scrollY > 0 && window.scrollY < hBHeight) {
fixedNav.style.top = hBHeight - window.scrollY + 'px';
} else if (window.scrollY > hBHeight) {
fixedNav.style.top = '0';
} else {
fixedNav.style.top = hBHeight + 'px';
}
} else {
fixedNav.style.top = '0';
}
};

var resizeObserver = new ResizeObserver(function() {
updateNavPosition();
});

resizeObserver.observe(hB);

window.addEventListener('resize', () => updateNavPosition());
window.addEventListener('scroll', () => updateNavPosition());

updateNavPosition();

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, .cncf-hello-bar a:focus {
text-decoration: none;
text-underline-position: unset;
}
`;
document.head.appendChild(style);

setTimeout(function() {
hB.style.opacity = '1';
}, 5);
});
})();
";

$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 defer 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 @@ -86,9 +86,9 @@
// delete any KTP posts which aren't in $synced_ids.
$query = new WP_Query(
array(
'post_type' => 'lf_ktp',
'post__not_in' => $synced_ids,
'posts_per_page' => -1,
'post_type' => 'lf_ktp',
'post__not_in' => $synced_ids,
'posts_per_page' => -1,
)
);
while ( $query->have_posts() ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ function geocode_location( $id ) {
}

$args = array(
'post_type' => 'lf_person',
'title' => $p->name,
'post_status' => 'publish',
'numberposts' => 1,
'post_type' => 'lf_person',
'title' => $p->name,
'post_status' => 'publish',
'numberposts' => 1,
);

if ( $image_url ) {
Expand Down Expand Up @@ -203,9 +203,9 @@ function geocode_location( $id ) {
// delete any People posts which aren't in $synced_ids.
$query = new WP_Query(
array(
'post_type' => 'lf_person',
'post__not_in' => $synced_ids,
'posts_per_page' => -1,
'post_type' => 'lf_person',
'post__not_in' => $synced_ids,
'posts_per_page' => -1,
)
);
while ( $query->have_posts() ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,4 @@ public function sync_people( $request ) {

return new WP_REST_Response( array( 'Success' ), 200 );
}

/**
* Get hello bar data.
*
* @param WP_REST_Request $request Full data about the request.
* @return WP_Error|WP_REST_Response
*/
public function get_hello( $request ) {
$items = array();

$options = get_option( 'lf-mu' );
$items['show_hello_bar'] = ( isset( $options['show_hello_bar'] ) && ! empty( $options['show_hello_bar'] ) ) ? 1 : 0;
$items['hello_bar_content'] = ( isset( $options['hello_bar_content'] ) && ! empty( $options['hello_bar_content'] ) ) ? $options['hello_bar_content'] : '';
$items['hello_bar_bg'] = ( isset( $options['hello_bar_bg'] ) && ! empty( $options['hello_bar_bg'] ) ) ? esc_attr( $options['hello_bar_bg'] ) : '';
$items['hello_bar_text'] = ( isset( $options['hello_bar_text'] ) && ! empty( $options['hello_bar_text'] ) ) ? esc_attr( $options['hello_bar_text'] ) : '';

return new WP_REST_Response( $items, 200 );
}
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -661,8 +661,8 @@ public static function get_teamcloudnative() {
$query = new WP_Query( $args );
while ( $query->have_posts() ) {
$query->the_post();
$person_title = $post->post_title;
$person_image_url = wp_get_attachment_image_src( get_post_meta( get_the_ID(), 'lf_human_image', true ), 'newsroom-post-width' )[0];
$person_title = $post->post_title;
$person_image_url = wp_get_attachment_image_src( get_post_meta( get_the_ID(), 'lf_human_image', true ), 'newsroom-post-width' )[0];

$person_profile_link = get_post_meta( get_the_ID(), 'lf_human_post_url', true );
if ( ! $person_profile_link ) {
Expand Down