*/ add_theme_support( 'automatic-feed-links' ); /* add title-tag */ add_theme_support( 'title-tag' ); /* add menu */ register_nav_menu( 'primary-menu', __('Primary Menu', 'float') ); /* add post thumbnail */ add_theme_support( 'post-thumbnails' ); } } add_action( 'init', 'float_theme_setup' ); /** * init widget */ if ( !function_exists('float_widgets_init') ) { function float_widgets_init() { $sidebar = array( 'name' => __('Main Sidebar', 'float'), 'id' => 'main-sidebar', 'description' => __('Default sidebar', 'float'), 'class' => 'main-sidebar', 'before_title' => '

', 'after_title' => '

' ); register_sidebar( $sidebar ); } } add_action( 'widgets_init', 'float_widgets_init' ); /** * Enqueue scripts and styles. */ function float_scripts() { wp_register_script( 'app-main-script', get_template_directory_uri() . '/dist/app.js', array('jquery'), '1.0.0', true ); wp_localize_script('app-main-script', 'ajax_object', array('ajax_url' => admin_url( 'admin-ajax.php'), 'google_client_id' => defined("GOOGLE_CLIENT_APP_ID") ? GOOGLE_CLIENT_APP_ID : "343605975098-i4p86p4la6lpff0rmsq3mg36vi9do9rb.apps.googleusercontent.com" )); wp_localize_script('app-main-script', 'contact_settings', array( 'sg_contact' => defined("SG_CONTACT_LIST") ? SG_CONTACT_LIST : "f6ff7833-d6cf-425e-a426-395ac201b794", 'us_contact' => defined("US_CONTACT_LIST") ? US_CONTACT_LIST : "a646ee53-dfec-4a6a-a0c3-102c6840df06", 'default_list' => defined("DEFAULT_CONTACT_LIST") ? DEFAULT_CONTACT_LIST : "6e36b912-7cc3-4fb2-ba50-a664fbefb11b" )); wp_enqueue_script( 'app-main-script' ); } add_action( 'wp_enqueue_scripts', 'float_scripts' ); function float_styles() { wp_register_style( 'app-main-style', get_template_directory_uri() . '/style.css', array(), '1.0.0', 'all' ); wp_enqueue_style( 'app-main-style' ); } add_action( 'wp_enqueue_scripts', 'float_styles' ); /** * set up menu */ if ( !function_exists('float_menu') ) { function float_menu($menu) { $menu = array( 'theme_location' => $menu, 'container' => false, 'menu_class' => 'navbar-nav ml-auto', 'depth' => 5, 'fallback_cb' => false, 'walker' => new Bootstrap_Menu_Walker() ); wp_nav_menu( $menu ); } } /** * add more menu item */ function add_signup_link( $items, $args ) { if ($args->theme_location == 'primary-menu') { $items .= '
  • Sign up
  • '; } return $items; } add_filter( 'wp_nav_menu_items', 'add_signup_link', 10, 2 ); /** * process ajax call newsletter subcribe */ function subcribe_news_letter_ajax() { return subscribeUser(); } add_action('wp_ajax_subcribe_news_letter_ajax', 'subcribe_news_letter_ajax'); add_action('wp_ajax_nopriv_subcribe_news_letter_ajax', 'subcribe_news_letter_ajax'); /** * add action for ajax call newsletter subcribe */ add_action('mc4wp_form_respond', function($form) { die(json_encode($form)); }); /** * add filter for shortcode_atts_arve */ function float_filter_detect_youtube_loop($atts) { if ( 'youtube' !== $atts['provider'] || ( empty( $atts['url'] ) && empty( $atts['id'] ) ) ) { return $atts; } if ( empty( $atts['url'] ) ) { # Not a url but it will work $url = str_replace( array( '&playlist=', '&playlist=' ), '?playlist=', $atts['id'] ); } else { $url = $atts['url']; } $query_array = arve_url_query_array( $url ); if (!isset($atts['loop']) || (isset($atts['loop']) && $atts['loop'] != 'yes')) { return $atts; } $atts['id'] = strtok( $atts['id'], '?' ); $atts['id'] = strtok( $atts['id'], '&' ); if ( empty( $query_array['playlist'] ) ) { $atts['parameters'] .= 'playlist=' . $atts['id']; } else { $atts['parameters'] .= 'playlist=' . $query_array['playlist']; } return $atts; } add_filter( 'shortcode_atts_arve', 'float_filter_detect_youtube_loop', -8 ); /** * set Popular Posts by Views */ function float_set_post_views($postID) { $count_key = 'float_post_views_count'; $count = get_post_meta($postID, $count_key, true); if (empty($count)) { delete_post_meta($postID, $count_key); add_post_meta($postID, $count_key, 1); } else { $count++; update_post_meta($postID, $count_key, $count); } } // Remove issues with prefetching adding extra views remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); /** * display the post view count */ function float_get_post_views($postID){ $count_key = 'float_post_views_count'; $count = get_post_meta($postID, $count_key, true); if($count == ''){ delete_post_meta($postID, $count_key); add_post_meta($postID, $count_key, '0'); return 0; } return $count; } /** * process signup form */ function float_signup_ajax() { // Verify nonce // if( !isset( $_POST['float_signup_user_nonce'] ) || !wp_verify_nonce( $_POST['float_signup_user_nonce'], 'float_signup_user' ) ) { // die( 'Ooops, something went wrong, please try again later.' ); // } // Post values $fullname = trim($_POST['full_name']); $email = trim($_POST['email']); // validate form $errors = []; if (empty($email)) { $errors['email'][] = 'This field is required.'; } if (!is_email($email)) { $errors['email'][] = 'Please enter a valid email address.'; } if (email_exists($email)) { $errors['email'][] = 'Sorry, that email address is already used!'; } $userdata = [ 'user_login' => $email, 'user_email' => $email, 'display_name' => $fullname, 'role' => 'subscriber' ]; // Return if (!empty($errors)) { $respone_data = [ 'success' => false, 'message' => $errors ]; } else { $user_id = wp_insert_user( $userdata ); if( !is_wp_error($user_id) ) { $respone_data = [ 'success' => true, 'message' => 'We have created an account for you.' ]; } else { $respone_data = [ 'success' => false, 'message' => $user_id->get_error_message() ]; } } $respone_data = json_encode($respone_data); die($respone_data); } /** * Detect user location based on user's IP */ function get_user_location() { $ip = get_user_ip(); $result = [ 'country'=>'', 'city'=>'', 'timezone' => 'Asia/Singapore', 'region_name'=>'', 'country_code'=>'', 'lat'=>'', 'lng'=>'' ]; $ip_data = @json_decode(file_get_contents('http://www.geoplugin.net/json.gp?ip='.$ip)); if($ip_data && $ip_data->geoplugin_countryName != null){ $result['country'] = $ip_data->geoplugin_countryName; $result['city'] = $ip_data->geoplugin_city; $result['timezone'] = $ip_data->geoplugin_timezone; $result['region_name'] = $ip_data->geoplugin_region; $result['country_code'] = $ip_data->geoplugin_countryCode; $result['lat'] = $ip_data->geoplugin_latitude; $result['lng'] = $ip_data->geoplugin_longitude; } return $result; } function get_user_ip() { $client = @$_SERVER['HTTP_CLIENT_IP']; $forward = @$_SERVER['HTTP_X_FORWARDED_FOR']; $remote = @$_SERVER['REMOTE_ADDR']; if(filter_var($client, FILTER_VALIDATE_IP)){ $ip = $client; }elseif(filter_var($forward, FILTER_VALIDATE_IP)){ $ip = $forward; }else{ $ip = $remote; } return $ip; } function get_latest_articles($request) { $limit = isset($_GET['limit']) ? intval(trim($_GET['limit'])) : 10; $page = isset($_GET['page']) ? intval(trim($_GET['page'])) : 1; if (!isset($limit) || !is_int($limit) || $limit <= 0) { $limit = 10; // Set limit to by 10 as default } if (!isset($page) || !is_int($page) || $page <= 0) { $page = 1; // Set default page } $query = new WP_Query( array( 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => $limit, 'paged' => $page, 'orderby' => 'date', 'order' => 'DESC' ) ); if ( !$query->have_posts() ) { return new WP_Error( 'empty_post', 'there is no post ', array('status' => 404) ); } // add meta to post $newPost = array(); foreach ( $query->posts as $key => $post ) { add_meta_value_to_posts( $post ); array_push( $newPost, $post ); } wp_reset_postdata(); // Add co-authors to post if exists foreach ( $newPost as $post ) { $co_authors = get_coauthors( $post->ID ); $result = []; foreach ( $co_authors as $co_author ) { if (get_class($co_author) !== 'WP_User') { array_push($result, $co_author->display_name); continue; } array_push($result, $co_author->data->display_name); } if ( !empty($result) ) { $post->co_authors = $result; } } $response = new WP_REST_Response( $newPost ); $response->set_status(200); return $response; } function get_article_by_id($request) { $post_id = isset($request['id']) && intval($request['id']) ? intval($request['id']) : NULL; if (empty($post_id)) { return new WP_Error( 'error', 'missing post id params ', array('status' => 400) ); } $post = get_post( $post_id ); if (empty($post)) { return new WP_Error( 'empty_post', 'there is no post ', array('status' => 404) ); } add_meta_value_to_posts( $post ); // Add co-authors to post if exists $co_authors = get_coauthors( $post->ID ); $result = []; foreach ( $co_authors as $co_author ) { if (get_class($co_author) !== 'WP_User') { array_push($result, $co_author->display_name); continue; } array_push($result, $co_author->data->display_name); } if ( !empty($result) ) { $post->co_authors = $result; } $response = new WP_REST_Response($post); $response->set_status(200); return $response; } function subscribeUser() { // Post values $name = trim($_POST['name']); $first_name = trim($_POST['first_name']); $last_name = trim($_POST['last_name']); $email = trim($_POST['email']); $phone = trim($_POST['phone']); // $list_id = '6e36b912-7cc3-4fb2-ba50-a664fbefb11b'; // validate form $errors = []; if (empty($email)) { $errors['email'][] = 'Email is required.'; } else if (!is_email($email)) { $errors['email'][] = 'Please enter a valid email address.'; } if (isset($errors) && count($errors)) { die(json_encode(['success' => NULL, 'errors' => $errors])); } $data = [ 'email' => $email, 'first_name' => $first_name, 'last_name' => $last_name, 'phone' => $phone, 'name' => $name, 'list_id' => $list_id, ]; $api = curl_init(); // initialize cURL connection $baseUrl = 'https://float.sg/api'; $api_url = $baseUrl . '/newsletter-subscribe'; curl_setopt($api, CURLOPT_URL, $api_url); curl_setopt($api, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer 99dfe35fcb7de1ee')); curl_setopt($api, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0'); curl_setopt($api, CURLOPT_RETURNTRANSFER, true); // return the API response curl_setopt($api, CURLOPT_CUSTOMREQUEST, 'POST'); // method POST curl_setopt($api, CURLOPT_TIMEOUT, 10); curl_setopt($api, CURLOPT_POST, true); curl_setopt($api, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($api, CURLOPT_POSTFIELDS, json_encode($data) ); // send data in json $result = curl_exec($api); die($result); } add_action('rest_api_init', function () { register_rest_route( 'float/v1', 'latest-articles',array( 'methods' => 'GET', 'callback' => 'get_latest_articles', 'permission_callback' => '__return_true' )); register_rest_route( 'float/v1', 'articles/(?P\d+)',array( 'methods' => 'GET', 'callback' => 'get_article_by_id', 'permission_callback' => '__return_true' )); }); add_action('wp_ajax_float_signup_ajax', 'float_signup_ajax'); add_action('wp_ajax_nopriv_float_signup_ajax', 'float_signup_ajax'); // PRIVATE functions function prepare_get_posts_query($args) { $query = 'SELECT posts.*, pm.meta_key, pm.meta_value, u.display_name from wp_posts posts LEFT JOIN wp_posts as temp on (temp.post_parent = posts."ID") LEFT JOIN wp_postmeta pm ON (temp."ID" = pm.post_id) LEFT JOIN wp_users u ON (u."ID" = posts.post_author) WHERE posts."ID" IN ('; $limit = 10; $limit_query = 'LIMIT 10'; $offset_query = 'OFFSET 0'; $sub_query = 'SELECT "ID" FROM wp_posts p WHERE p.post_status = \'publish\' AND p.post_type= \'post\''; if (isset($args['id'])) { $sub_query .= ' AND p."ID" = ' . $args['id']; } if (isset($args['limit'])) { $limit = $args['limit']; $limit_query = 'LIMIT ' . $limit; } if (isset($args['page'])) { $offset_query = 'OFFSET ' . ($args['page'] - 1) * $limit; } $sub_query .= ' ORDER BY p.post_date DESC ' . $limit_query . ' ' . $offset_query . ')'; $query .= $sub_query . ' GROUP BY posts."ID", pm.meta_key, pm.meta_value, u.display_name ORDER BY posts.post_date DESC '; return $query; } /** * Additional meta data to post * @param WP_POST &$post */ function add_meta_value_to_posts( &$post ) { if ( !$post ) { return; } // get all custom fields if ( function_exists( 'get_fields' ) ) { $post_meta = get_fields( $post->ID ); if ( $post_meta ) { foreach ( $post_meta as $key => $meta ) { if ( !empty($meta) && is_string($meta) && preg_match( '/^http|^https:\/\/.*?/m', $meta ) ) { $meta = encodeURI( $meta ); } $post->$key = $meta; } } } // get post author $post->display_name = get_the_author_meta( 'display_name', $post->post_author ); // get featured image $post->featured_image = encodeURI( get_the_post_thumbnail_url( $post ) ); $post->_wp_attachment_metadata = get_post_meta( $post->ID, '_wp_attachment_metadata', true ); } function handle_get_posts_by_query($query) { global $wpdb; $posts = $wpdb->get_results($query); if (empty($posts)) { return []; } $posts = json_decode(json_encode($posts), true); // Parse data for response $results = []; foreach($posts as $p) { $data = json_decode(json_encode($p), true); $data['content'] = apply_filters('the_content', $p['post_content']); $id = $data['ID']; $meta_key = $data['meta_key']; $meta_value = $data['meta_value']; $featured_image = NULL; if ($meta_key == '_wp_attached_file') { $featured_image = 'wp-content/uploads/'.$meta_value; } if (isset($results[$id])) { $results[$id][$meta_key] = $meta_value; if ($featured_image) { $results[$id]['featured_image'] = $featured_image; } continue; } // If post does not appear in list yet. // Add new post to the results. unset($data['meta_key']); unset($data['meta_value']); $data[$meta_key] = $meta_value; if ($featured_image) { $data['featured_image'] = $featured_image; } $results[$id] = $data; } return array_values($results); } function encodeURI($url) { // http://php.net/manual/en/function.rawurlencode.php // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURI $unescaped = array( '%2D'=>'-','%5F'=>'_','%2E'=>'.','%21'=>'!', '%7E'=>'~', '%2A'=>'*', '%27'=>"'", '%28'=>'(', '%29'=>')' ); $reserved = array( '%3B'=>';','%2C'=>',','%2F'=>'/','%3F'=>'?','%3A'=>':', '%40'=>'@','%26'=>'&','%3D'=>'=','%2B'=>'+','%24'=>'$' ); $score = array( '%23'=>'#' ); return strtr(rawurlencode($url), array_merge($reserved,$unescaped,$score)); } function add_meta_tags() { global $post; if ( is_singular() ) { $des_post = strip_tags( $post->post_content ); $des_post = strip_shortcodes( $des_post ); $des_post = str_replace( array("\n", "\r", "\t"), ' ', $des_post ); $des_post = trim( $des_post ); $des_post = mb_substr( $des_post, 0, 300, 'utf8' ); echo '' . "\n"; echo '' . "\n"; echo '' . "\n"; echo '' . "\n"; echo '' . "\n"; echo '' . "\n"; echo '' . "\n"; echo '' . "\n"; echo '' . "\n"; } if ( is_home() ) { echo '' . "\n"; echo '' . "\n"; echo '' . "\n"; echo '' . "\n"; echo '' . "\n"; } if ( is_category() ) { $des_cat = strip_tags(category_description()); echo '' . "\n"; } } add_action('wp_head', 'add_meta_tags');