File: /var/www/linde/wp-content/themes/linde/functions.php
<?php
/**
* Timber starter-theme
* https://github.com/timber/starter-theme
*
* @package WordPress
* @subpackage Timber
* @since Timber 0.1
*/
define('THEMEROOT', get_stylesheet_directory_uri() . "/");
define('FILEROOT', dirname(__DIR__) . '/' . get_option('stylesheet'));
/**
* If you are installing Timber as a Composer dependency in your theme, you'll need this block
* to load your dependencies and initialize Timber. If you are using Timber via the WordPress.org
* plug-in, you can safely delete this block.
*/
$composer_autoload = __DIR__ . '/vendor/autoload.php';
if ( file_exists( $composer_autoload ) ) {
require_once $composer_autoload;
$timber = new Timber\Timber();
}
/**
* This ensures that Timber is loaded and available as a PHP class.
* If not, it gives an error message to help direct developers on where to activate
*/
if ( ! class_exists( 'Timber' ) ) {
add_action(
'admin_notices',
function() {
echo '<div class="error"><p>Timber not activated. Make sure you activate the plugin in <a href="' . esc_url( admin_url( 'plugins.php#timber' ) ) . '">' . esc_url( admin_url( 'plugins.php' ) ) . '</a></p></div>';
}
);
add_filter(
'template_include',
function( $template ) {
return get_stylesheet_directory() . '/static/no-timber.html';
}
);
return;
}
add_filter('wpcf7_form_elements', 'contact_form_remove_checkbox_wrapping' );
function contact_form_remove_checkbox_wrapping( $content ) {
preg_match_all('/<span class="wpcf7-form-control-wrap[a-zA-Z ]*"><span class="wpcf7-form-control wpcf7-[checkbox|radio]+" id="([^\"]+)"><span class="[^\"]+">(<input type="[checkbox|radio]+" name="[^\"]+" value="[^\"]*" \/>)<span class="wpcf7-list-item-label">[^\"]+<\/span><\/span><\/span><\/span>/i', $content, $out );
if ( !empty( $out[0] ) ) {
$count = count( $out[1] );
for ( $i = 0; $i<$count; $i++ ) {
$out[2][ $i ] = str_replace(' value=', 'id="' . $out[1][ $i ] . '" value=', $out[2][$i] );
}
return str_replace( $out[0], $out[2], $content );
}
return $content;
}
/**
* Sets the directories (inside your theme) to find .twig attachments
*/
Timber::$dirname = array( 'templates', 'views' );
/**
* By default, Timber does NOT autoescape values. Want to enable Twig's autoescape?
* No prob! Just set this value to true
*/
Timber::$autoescape = false;
/**
* We're going to configure our theme inside of a subclass of Timber\Site
* You can move this to its own file and include here via php's include("MySite.php")
*/
class LindeSite extends Timber\Site {
/** Add timber support. */
public function __construct() {
add_action( 'after_setup_theme', array( $this, 'setup_menus' ) );
add_action( 'init', array( $this, 'setup_image_sizes' ) );
add_action( 'after_setup_theme', array( $this, 'theme_supports' ) );
add_filter( 'timber/context', array( $this, 'add_to_context' ) );
add_filter( 'timber/twig', array( $this, 'add_to_twig' ) );
add_action( 'wp_head', array( $this, 'linde_scripts_function' ) );
parent::__construct();
}
public function add_to_context( $context ) {
$context['menu'] = array();
$context['menu']['main'] = new Timber\Menu('primary');
$context['menu']['footer'] = new Timber\Menu('footer');
$context['footer_text'] = get_field('footer_text', 'option');
$context['footer_tagline'] = get_field('footer_tagline', 'option');
$context['social_icons'] = get_field('social_icons', 'option');
$context['general_form'] = get_field('cta_form_contact_form_7_code', 'option');
$context['modules_subtitle_text'] = get_field('modules_subtitle_text', 'option');
$context['contact_form_text'] = get_field('contact_form_text', 'option');
$context['ganalytics'] = get_field('google_analytics_tracking_ID', 'option');
$context['data_protection_page_link'] = get_permalink(get_field('data_protection_page', 'option'));
$context['terms_page_link'] = get_permalink(get_field('terms_page', 'option'));
$context['success_stories_page_link'] = get_permalink(get_field('success_stories_page', 'option'));
$context['thank_you_page_link'] = get_permalink(get_field('thank_you_page_link', 'option'));
$context['site'] = $this;
return $context;
}
public function setup_menus(){
register_nav_menus( array (
'primary' => __( 'Primary menu', 'linde' ),
'footer' => __( 'Footer menu', 'linde' )
) );
}
public function setup_image_sizes(){
add_image_size( "19x6_large", "1920", "600", true );
add_image_size( "19x10_large", "1920", "1072", true );
add_image_size( "19x10_medium_plus", "824", "460", true );
add_image_size( "19x10_medium", "412", "230", true );
add_image_size( "19x10_small", "206", "115", true );
add_image_size( "19x4_large", "1920", "400", true );
add_image_size( "53x32", "532", "299", true );
add_image_size( "72x68_large", "720", "668", true );
add_image_size( "72x60_large", "720", "600", true );
add_image_size( "72x68_medium", "360", "334", true );
add_image_size( "72x60_medium", "360", "300", true );
add_image_size( "82x46_large", "824", "460", true );
add_image_size( "82x46_medium", "412", "230", true );
add_image_size( "82x46_small", "225", "126", true );
}
public function theme_supports() {
// Make linde available for translation. Translations can be added to the /languages/ directory.
load_theme_textdomain( 'trex', get_template_directory() . '/languages' );
/*
* Let WordPress manage the document title.
* By adding theme support, we declare that this theme does not use a
* hard-coded <title> tag in the document head, and expect WordPress to
* provide it for us.
*/
add_theme_support( 'title-tag' );
/*
* Enable support for Post Thumbnails on posts and pages.
*
* @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
*/
add_theme_support( 'post-thumbnails' );
/*
* Switch default core markup for search form, comment form, and comments
* to output valid HTML5.
*/
add_theme_support(
'html5',
array(
'comment-form',
'comment-list',
'gallery',
'caption',
)
);
/*
* Enable support for Post Formats.
*
* See: https://codex.wordpress.org/Post_Formats
*/
add_theme_support(
'post-formats',
array(
'aside',
'image',
'video',
'quote',
'link',
'gallery',
'audio',
)
);
}
function linde_scripts_function() {
$html = '<script type="text/javascript">';
$html .= 'var ajaxurl = "' . admin_url('admin-ajax.php') . '";';
$html .= 'var themeurl = "' . get_template_directory_uri() . '";';
$html .= 'var siteurl = "' . get_site_url() . '";';
$html .= '</script>';
echo $html;
}
/** This is where you can add your own functions to twig.
*
* @param string $twig get extension.
*/
public function add_to_twig( $twig ) {
$twig->addExtension( new Twig\Extension\StringLoaderExtension() );
$twig->addFilter( new Timber\Twig_Filter( 'format_session', 'format_session' ) );
return $twig;
}
}
function format_session( $text ) {
return sprintf("%02d", $text);
return $text;
}
class ModuleTerm extends Timber\Term{
var $_coverImage = null;
var $_coverImageMobile = null;
var $_featuredImage = null;
public function coverImage() {
if (!$this->_coverImage) {
if ( isset( $this->cover_image ) && strlen( $this->cover_image ) ) {
$this->_coverImage = new Timber\Image( $this->cover_image );
}
}
if (!$this->_coverImage) {
$this->_coverImage = new Timber\Image( get_field('module_cover_image_default', 'option') );
}
return $this->_coverImage;
}
public function featuredImage() {
if (!$this->_featuredImage) {
if ( isset( $this->thumbnail_image ) && strlen( $this->thumbnail_image ) ) {
$this->_featuredImage = new Timber\Image( $this->thumbnail_image );
}
}
if (!$this->_featuredImage) {
$this->_featuredImage = new Timber\Image( get_field('module_featured_image_default', 'option') );
}
return $this->_featuredImage;
}
public function coverImageMobile() {
if (!$this->_coverImageMobile) {
if ( isset( $this->cover_image_mobile) && strlen( $this->cover_image_mobile ) ) {
$this->_coverImageMobile = new Timber\Image( $this->cover_image_mobile );
}
}
if (!$this->_coverImageMobile) {
$this->_coverImageMobile = new Timber\Image( get_field('module_cover_image_mobile_default', 'option') );
}
return $this->_coverImageMobile;
}
}
class PostCourse extends Timber\Post {
var $_coverImage = null;
var $_coverImageMobile = null;
var $_featuredImage = null;
public function coverImage() {
if (!$this->_coverImage) {
if ( isset( $this->cover_image ) && strlen( $this->cover_image ) ) {
$this->_coverImage = new Timber\Image( $this->cover_image );
}
}
if (!$this->_coverImage) {
$this->_coverImage = new Timber\Image( get_field('module_cover_image_default', 'option') );
}
return $this->_coverImage;
}
public function featuredImage() {
if (!$this->_featuredImage) {
if ( $this->thumbnail()) {
$this->_featuredImage = $this->thumbnail();
}
}
if (!$this->_featuredImage) {
$this->_featuredImage = new Timber\Image( get_field('course_featured_image_default', 'option') );
}
return $this->_featuredImage;
}
public function coverImageMobile() {
if (!$this->_coverImageMobile) {
if ( isset( $this->cover_image_mobile) && strlen( $this->cover_image_mobile ) ) {
$this->_coverImageMobile = new Timber\Image( $this->cover_image_mobile );
}
}
if (!$this->_coverImageMobile) {
$this->_coverImageMobile = new Timber\Image( get_field('module_cover_image_mobile_default', 'option') );
}
return $this->_coverImageMobile;
}
}
class PostFlexible extends Timber\Post {
public function blocks() {
$blocks = array();
foreach ( $this->meta( 'sections' ) as $section ) {
$elements = array();
foreach ( $section["elements"] as $element ) {
include 'templates/page-elements/' . $element['acf_fc_layout'] . '.php';
$elements[] = $element;
}
$section["elements"] = $elements;
$blocks[] = $section;
}
return $blocks;
}
}
new LindeSite();
//send the captcha token from the FE to this method, will return true if everything is valid, or false if captcha check fails
function check_captcha($token){
// Google reCAPTCHA secret key
$secretKey = '6Lc_PDYqAAAAAA76ccPpBk-ds8WLokYSPSPpEp7b';
// Verify the token with Google reCAPTCHA
$verifyUrl = 'https://www.google.com/recaptcha/api/siteverify';
$response = wp_remote_post($verifyUrl, [
'body' => [
'secret' => $secretKey,
'response' => $token,
'remoteip' => $_SERVER['REMOTE_ADDR']
]
]);
$responseBody = wp_remote_retrieve_body($response);
$result = json_decode($responseBody);
if (!$result->success || $result->score < 0.5) {
return false;
}else{
return true;
}
}
/** Contact Form AJAX */
add_action('wp_ajax_post_contact_form', 'post_contact_form');
add_action('wp_ajax_nopriv_post_contact_form', 'post_contact_form');
function post_contact_form() {
// The captcha token from the request
$token = isset($_POST['token']) ? sanitize_text_field($_POST['token']) : '';
$is_captcha_success = check_captcha($token);
if($is_captcha_success){
// Failure
wp_send_json_success(['result' => 'error', 'msg' => "There was an error trying to send your message."]);
return; // Stop further execution
}
$form_data = isset($_POST['data']) ? sanitize_text_field($_POST['data']) : '';
try {
$result = post('https://www.engineering.linde.com/l/310981/2020-03-31/lkl868', $form_data);
wp_send_json_success(['result' => 'success', 'msg' => "Thank you for reaching out! We'll get back to you soon!", 'resultMsg'=> $result, 'form_data' => $form_data]);
} catch (Exception $e) { // POST request failed
wp_send_json_success(['result' => 'error', 'msg' => "There was an error trying to send your message. Please try again."]);
}
}
/** Newsletter Form AJAX */
add_action('wp_ajax_post_newsletter_form', 'post_newsletter_form');
add_action('wp_ajax_nopriv_post_newsletter_form', 'post_newsletter_form');
function post_newsletter_form() {
// The captcha token from the request
$token = isset($_POST['token']) ? sanitize_text_field($_POST['token']) : '';
$is_captcha_success = check_captcha($token);
if($is_captcha_success){
// Failure
wp_send_json_success(['result' => 'error', 'msg' => "There was an error trying to process your request."]);
return; // Stop further execution
}
$form_data = isset($_POST['data']) ? sanitize_text_field($_POST['data']) : '';
try {
$result = post('https://www.engineering.linde.com/l/310981/2021-09-22/2kf3p7z', $form_data);
wp_send_json_success(['result' => 'success', 'msg' => "You have successfully subscribed to our newsletter.", 'resultMsg'=> $result, 'form_data' => $form_data]);
exit();
} catch (Exception $e) { // POST request failed
wp_send_json_success(['result' => 'error', 'msg' => "This email address has already subscribed."]);
}
wp_die();
}
/** Generic POST r quest helper for ajax form */
function post($url, $postStr = array()){
$options = array(
'http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postStr
)
);
$streamContext = stream_context_create($options);
$result = file_get_contents($url, false, $streamContext);
if($result === false){
$error = error_get_last();
throw new Exception('POST request failed: ' . $error['message']);
}
return $result;
}