File manager - Edit - /home/colomboelectrici/public_html/wp-content/plugins/page-generator-pro/includes/global/shortcode.php
Back
<?php /** * Shortcode class * * @package Page_Generator_Pro * @author Tim Carr * @version 1.0 */ class Page_Generator_Pro_Shortcode { /** * Holds the class object. * * @since 1.1.3 * * @var object */ public static $instance; /** * Holds the base object. * * @since 1.2.1 * * @var object */ public $base; /** * Constructor * * @since 1.0.0 */ function __construct() { // Hooks and Filters add_action( 'wp_enqueue_scripts', array( $this, 'scripts_css' ) ); // Register shortcodes $this->add_shortcodes(); } /** * Registers the shortcodes used by this plugin * * @since 1.2.0 */ public function add_shortcodes() { add_shortcode( 'page-generator-pro-500px', array( $this, 'five_hundred_px' ) ); add_shortcode( 'page-generator-pro-google-maps', array( $this, 'google_maps' ) ); add_shortcode( 'page-generator-pro-google-map', array( $this, 'google_maps' ) ); add_shortcode( 'page-generator-pro-wikipedia', array( $this, 'wikipedia' ) ); add_shortcode( 'page-generator-pro-yelp', array( $this, 'yelp' ) ); add_shortcode( 'page-generator-pro-youtube', array( $this, 'youtube' ) ); } /** * Register or enqueue any JS and CSS * * @since 1.1.0 */ public function scripts_css() { // Get base instance $this->base = Page_Generator_Pro::get_instance(); // If a Google Maps API key has been specified, use it instead of the class default. $google_maps_api_key = Page_Generator_Pro_Settings::get_instance()->get_setting( $this->base->plugin->name . '-google', 'google_maps_api_key' ); $google_maps_api_js_disabled = Page_Generator_Pro_Settings::get_instance()->get_setting( $this->base->plugin->name . '-google', 'google_maps_api_js_disabled' ); // JS wp_enqueue_script( 'jquery' ); // Only enqueue Google Maps Library if it's not disabled in the Plugin Settings if ( ! $google_maps_api_js_disabled ) { wp_enqueue_script( $this->base->plugin->name . '-google-maps', 'https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false' . ( ! empty( $google_maps_api_key ) ? '&key=' . $google_maps_api_key : '' ), false, null, true ); } wp_enqueue_script( $this->base->plugin->name . '-frontend', $this->base->plugin->url . 'assets/js/min/frontend-min.js', array( 'jquery' ), $this->base->plugin->version, true ); // CSS wp_enqueue_style( $this->base->plugin->name . '-frontend', $this->base->plugin->url . 'assets/css/frontend.css' ); } /** * 500px Shortcode * * @since 1.0 * * @param array $atts Shortcode Attributes * @return string Output */ public function five_hundred_px( $atts ) { // Get base instance $this->base = Page_Generator_Pro::get_instance(); // Try to get the post ID // If we're generating a new page, this might not be possible global $post; if ( ! empty( $post ) ) { $post_id = $post->ID; } else { $post_id = 0; } // Parse shortcode attributes, defining fallback defaults if required $atts = shortcode_atts( array( 'image_size_id' => 600, 'term' => '', 'location' => 0, ), $atts, $this->base->plugin->name . '-500px' ); // Import an image into WordPress now // Setup 500px API instance $instance_500px = Page_Generator_Pro_500px::get_instance(); // If our featured image term is a location, get its latitude and longitude now if ( $atts['location'] ) { // Get geo instance $geo_instance = Page_Generator_Pro_Geo::get_instance(); // If a Google Geocoding API key has been specified, use it instead of the class default. $google_geocoding_api_key = Page_Generator_Pro_Settings::get_instance()->get_setting( 'page-generator-pro-google', 'google_geocoding_api_key' ); if ( ! empty( $google_geocoding_api_key ) ) { $geo_instance->api_key = $google_geocoding_api_key; } // Get lat/lng $lat_lng = $geo_instance->google_get_lat_lng( $atts['location'] ); } else { $lat_lng = false; } // If latitude and longitude returned an error, don't show an image at all if ( is_wp_error( $lat_lng ) ) { return ''; } // Run images query $images = $instance_500px->photos_search( $atts['image_size_id'], 100, $atts['term'], $lat_lng ); if ( is_wp_error( $images ) || ! is_array( $images ) ) { // Couldn't fetch an image from 500px, so don't show an image at all. return ''; } // Pick an image at random from the resultset $image_index = rand( 0, 99 ); // Import the first image $image_id = Page_Generator_Pro_Import::get_instance()->import_remote_image( $images[ $image_index ]['url'], $post_id, $images[ $image_index ]['title'], $images[ $image_index ]['caption'] ); // Return the image HTML tag return wp_get_attachment_image( $image_id, 'full' ); } /** * Google Maps Shortcode * * @since 1.0 * * @param array $atts Shortcode Attributes * @return string Output */ public function google_maps( $atts ) { // Get base instance $this->base = Page_Generator_Pro::get_instance(); // Parse shortcode attributes, defining fallback defaults if required $atts = shortcode_atts( array( 'location' => '', 'zoom' => 16, // Higher number = more zoomed in ), $atts, $this->base->plugin->name . '-google-maps' ); // Get geo instance $geo_instance = Page_Generator_Pro_Geo::get_instance(); // If a Google Geocoding API key has been specified, use it instead of the class default. $google_geocoding_api_key = Page_Generator_Pro_Settings::get_instance()->get_setting( 'page-generator-pro-google', 'google_geocoding_api_key' ); if ( ! empty( $google_geocoding_api_key ) ) { $geo_instance->api_key = $google_geocoding_api_key; } // Get latitude and longitude $lat_lng = $geo_instance->google_get_lat_lng( $atts['location'] ); if ( is_wp_error( $lat_lng ) ) { return ''; } // Return map markup return '<div class="' . $this->base->plugin->name . '-map" style="width:100%;height:250px;" data-address="' . $atts['location'] . '" data-lat="' . (string) $lat_lng['lat'] . '" data-lng="' . (string) $lat_lng['lng'] . '" data-zoom="' . $atts['zoom'] . '"></div>'; } /** * Wikipedia Shortcode * * @since 1.0.0 * * @param array $atts Shortcode Attributes * @return string Output */ public function wikipedia( $atts ) { // Check DOMDocument installed if ( ! class_exists( 'DOMDocument' ) ) { return ''; } // Get base instance $this->base = Page_Generator_Pro::get_instance(); // Parse shortcode attributes, defining fallback defaults if required $atts = shortcode_atts( array( 'term' => '', 'language' => 'en', 'sections' => 2, // Number of wikipedia sections to include ), $atts, $this->base->plugin->name . '-wikipedia' ); // Check if an article exists on Wikipedia for the given term $response = wp_remote_get( 'http://' . $atts['language'] . '.wikipedia.org/w/api.php?section=0&action=parse&page=' . $atts['term'] . '&format=json&prop=text&redirects' ); // Bail if an error occured if ( is_wp_error( $response ) ) { return ''; } if ( $response['response']['code'] != 200 ) { return ''; } // Check status $body = wp_remote_retrieve_body( $response ); $check = json_decode( $body ); if ( isset( $check->error ) ) { return ''; } // Get data from Wikipedia $url = 'http://' . $atts['language'] . '.wikipedia.org/wiki/' . str_replace(' ', '_', $atts['term'] ); $response = wp_remote_get( $url ); // Bail if an error occured if ( is_wp_error( $response ) ) { return ''; } if ( $response['response']['code'] != 200 ) { return ''; } // Get body $body = wp_remote_retrieve_body( $response ); if ( empty( $body ) ) { return ''; } // Parse into DOMDocument $dom = new DOMDocument(); $dom->preserveWhiteSpace = false; // Convert encoding to UTF-8 if php-mbstring is installed if ( function_exists( 'mb_convert_encoding' ) ) { @$dom->loadHTML( mb_convert_encoding( $body, 'HTML-ENTITIES', 'UTF-8' ) ); } else { // Cannot guarantee this works as mb_convert_encoding is not available @$dom->loadHTML( $body ); } // Extract paragraphs that contain the main content $paragraphs = $dom->getElementById( 'mw-content-text' )->getElementsByTagName( 'p' ); // Build paragraphs $x = 1; $build = ''; foreach( $paragraphs as $paragraph ) { // Skip if this entire paragraph's nodeValue matches the keyword // e.g. the city name if ( strpos( $atts['term'], trim( $paragraph->nodeValue ) ) !== false ) { continue; } if ( $x <= $atts['sections'] ) { $build .= "<p>" . $paragraph->nodeValue . "</p>"; $x++; } else { break; } } // Check WikiPedia returned content in our build process if ( empty( $build ) ) { return ''; } // Return string return '<div class="' . $this->base->plugin->name . '-wikipedia">' . $build . '</div>'; } /** * Yelp Shortcode * * @since 1.0.0 * * @param array $atts Shortcode Attributes * @return string Output */ public function yelp( $atts ) { // Get base instance $this->base = Page_Generator_Pro::get_instance(); // Get instances $settings_instance = Page_Generator_Pro_Settings::get_instance(); // Parse shortcode attributes, defining fallback defaults if required $atts = shortcode_atts( array( 'term' => '', 'location' => '', 'limit' => 5, ), $atts, $this->base->plugin->name . '-yelp' ); // Send request to Yelp API $yelp = Page_Generator_Pro_Yelp::get_instance(); $results = $yelp->businesses_search( $atts['term'], $atts['location'], $atts['limit'] ); // Check for errors from the Yelp API if ( is_wp_error( $results ) ) { if ( is_user_logged_in() ) { // Display error return 'Yelp API: ' . $results->get_error_message(); } // Just return a blank string return ''; } // Check if any businesses were found if ( ! isset( $results->businesses ) || count( $results->businesses ) == 0 ) { return ''; } // Iterate through results, building HTML $html = '<div class="' . $this->base->plugin->name . '-yelp">'; foreach ( $results->businesses as $count => $business ) { $html .= '<div class="business"> <div class="name">' . $business->name . '</div> <div class="rating"> <img src="' .$this->base->plugin->url . '/assets/images/yelp-ratings/' . $business->rating . '.png" />' . $business->review_count . ' ' . __( 'reviews', $this->base->plugin->name ) . ' </div> <div class="categories">'; // Categories $total_categories = count( $business->categories ); foreach ( $business->categories as $cCount => $category ) { $html .= $category->title; if ( ( $cCount + 1 ) != $total_categories ) { $html .= ', '; } } $html .= ' </div> <div class="phone">' . $business->phone . '</div> <div class="address">'; // Address $total_address_lines = count( $business->location->display_address ); foreach ( $business->location->display_address as $aCount => $address ) { $html .= $address; if ( ( $aCount + 1 ) != $total_address_lines ) { $html .= ', '; } } $html .= ' </div> </div>'; // Check if limit reached if ( ( $count + 1 ) == $atts['limit'] ) { break; } } // Add Yelp logo. Meets display requirements // http://www.yelp.co.uk/developers/getting_started/display_requirements $html .= '<a href="http://www.yelp.co.uk" target="_blank"><img src="http://s3-media1.ak.yelpcdn.com/assets/2/www/img/55e2efe681ed/developers/yelp_logo_50x25.png" /></a> </div>'; // Return HTML return $html; } /** * YouTube Shortcode * * @since 1.2.0 * * @param array $atts Shortcode Attributes * @return string Output */ public function youtube( $atts ) { // Get base instance $this->base = Page_Generator_Pro::get_instance(); // Try to get the post ID // If we're generating a new page, this might not be possible global $post; if ( ! empty( $post ) ) { $post_id = $post->ID; } else { $post_id = 0; } // Parse shortcode attributes, defining fallback defaults if required $atts = shortcode_atts( array( 'term' => '', 'location' => 0, ), $atts, $this->base->plugin->name . '-youtube' ); // If our term is a location, get its latitude and longitude now if ( $atts['location'] ) { // Setup instance $geo_instance = Page_Generator_Pro_Geo::get_instance(); // If a Google Geocoding API key has been specified, use it instead of the class default. $google_geocoding_api_key = Page_Generator_Pro_Settings::get_instance()->get_setting( $this->base->plugin->name . '-google', 'google_geocoding_api_key' ); if ( ! empty( $google_geocoding_api_key ) ) { $geo_instance->api_key = $google_geocoding_api_key; } $lat_lng = $geo_instance->google_get_lat_lng( $atts['location'] ); } else { $lat_lng = false; } // Setup YouTube instance $youtube = Page_Generator_Pro_Youtube::get_instance(); // If a YouTube Data API key has been specified, use it instead of the class default. $youtube_data_api_key = Page_Generator_Pro_Settings::get_instance()->get_setting( $this->base->plugin->name . '-google', 'youtube_data_api_key' ); if ( ! empty( $youtube_data_api_key ) ) { $youtube->api_key = $youtube_data_api_key; } // Run query $videos = $youtube->search( $atts['term'], $lat_lng ); if ( is_wp_error( $videos ) || ! is_array( $videos ) ) { // Couldn't fetch videos from YouTube, so don't show a video at all. return ''; } // Pick a video at random from the resultset $video_index = rand( 0, ( count( $videos ) - 1 ) ); // Return video URL return $videos[ $video_index ]['url']; } /** * Returns the singleton instance of the class. * * @since 1.1.3 * * @return object Class. */ public static function get_instance() { if ( ! isset( self::$instance ) && ! ( self::$instance instanceof self ) ) { self::$instance = new self; } return self::$instance; } }
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.06 |
proxy
|
phpinfo
|
Settings