jmlapam

utilities

Jan 5th, 2018
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.69 KB | None | 0 0
  1. <?php
  2.  
  3. namespace TokenToMe\TwitterCards;
  4.  
  5. if ( ! function_exists( 'add_action' ) ) {
  6.     header( 'Status: 403 Forbidden' );
  7.     header( 'HTTP/1.1 403 Forbidden' );
  8.     exit();
  9. }
  10.  
  11. class Utilities {
  12.  
  13.     /**
  14.      * @param $at
  15.      *
  16.      * @return bool|mixed
  17.      */
  18.     public static function remove_at( $at ) {
  19.  
  20.         if ( ! is_string( $at ) ) {
  21.             return false;
  22.         }
  23.  
  24.         $noat = str_replace( '@', '', $at );
  25.  
  26.         return $noat;
  27.     }
  28.  
  29.     /**
  30.      * Put some cache on request
  31.      * @return bool|mixed
  32.      * @author Julien Maury
  33.      */
  34.     public static function get_github_repositories() {
  35.  
  36.         if ( false === ( $data = get_site_transient( 'jm_github_repos' ) ) ) {
  37.  
  38.             $request = wp_remote_get( 'https://api.github.com/users/tweetpressfr/repos?sort=created' );
  39.  
  40.             if ( ! empty( $request ) && ! is_wp_error( $request ) && wp_remote_retrieve_response_code( $request ) === 200 ) {
  41.                 $data = set_site_transient( 'jm_github_repos', wp_remote_retrieve_body( $request ), WEEK_IN_SECONDS );// it's actually enough ^^
  42.             }
  43.  
  44.         }
  45.  
  46.         return $data;
  47.     }
  48.  
  49.     /**
  50.      * @return array
  51.      * @author Julien Maury
  52.      */
  53.     public static function get_keys() {
  54.         global $wpdb;
  55.         $keys = $wpdb->get_results( "SELECT DISTINCT meta_key from {$wpdb->postmeta}" );
  56.  
  57.         if ( empty( $keys ) ) {
  58.             return __return_empty_array();
  59.         }
  60.  
  61.         $keys = wp_list_pluck( $keys, 'meta_key', 'meta_key' );
  62.         array_unshift( $keys, __( 'Select' ) );
  63.  
  64.         return array_map( 'esc_attr', $keys );
  65.     }
  66.  
  67.     /**
  68.      * @param $lb
  69.      *
  70.      * @return string
  71.      */
  72.     public static function remove_lb( $lb ) {
  73.         $output = str_replace( array( "\r" . PHP_EOL, "\r" ), PHP_EOL, $lb );
  74.         $lines  = explode( PHP_EOL, $output );
  75.         $nolb   = array();
  76.         foreach ( $lines as $key => $line ) {
  77.             if ( ! empty( $line ) ) {
  78.                 $nolb[] = trim( $line );
  79.             }
  80.         }
  81.  
  82.         return implode( $nolb );
  83.     }
  84.  
  85.     /**
  86.      * @param $post_id
  87.      *
  88.      * @return string
  89.      */
  90.     public static function get_excerpt_by_id( $post_id ) {
  91.         $the_post    = get_post( $post_id );
  92.         $the_excerpt = $the_post->post_content; //Gets post_content to be used as a basis for the excerpt
  93.  
  94.         //kill shortcode
  95.         $shortcode_pattern = get_shortcode_regex();
  96.         $the_excerpt       = preg_replace( '/' . $shortcode_pattern . '/', '', $the_excerpt );
  97.  
  98.         // kill tags
  99.         $the_excerpt = strip_tags( $the_excerpt );
  100.  
  101.         return esc_attr( substr( $the_excerpt, 0, 200 ) ); // to prevent meta from being broken by e.g ""
  102.     }
  103.  
  104.     /**
  105.      * Allows us to get post types we want
  106.      * and make some show/hide
  107.      * @return array
  108.      */
  109.     public static function get_post_types() {
  110.         $cpts = get_option( 'jm_tc_cpt' );
  111.  
  112.         return empty( $cpts['twitterCardPt'] ) ? get_post_types( array( 'public' => true ) ) : array_values( $cpts['twitterCardPt'] );
  113.     }
  114.  
  115. }
Add Comment
Please, Sign In to add comment