'pb_codecanyon_widget', 'description' => __( 'Display CodeCanyon items with this widget', 'affiliate-codecanyon-widget' ) ); $control_ops = array( 'id_base' => 'pb_codecanyon_widget' ); parent::__construct( 'pb_codecanyon_widget', __( 'Affiliate CodeCanyon Widget', 'affiliate-codecanyon-widget' ), $widget_ops, $control_ops ); $this->cc_cats = array( array( 'name' => 'wordpress', 'title' => 'WordPress' ), array( 'name' => 'php-scripts', 'title' => 'PHP Scripts' ), array( 'name' => 'javascript', 'title' => 'JavaScript' ), array( 'name' => 'css', 'title' => 'CSS' ), array( 'name' => 'ecommerce', 'title' => 'eCommerce' ), array( 'name' => 'mobile', 'title' => 'Mobile' ), array( 'name' => 'html5', 'title' => 'HTML5' ), array( 'name' => 'skins', 'title' => 'Skins' ), array( 'name' => 'plugins', 'title' => 'Plugins' ), array( 'name' => 'apps', 'title' => 'Apps' ) ); $this->exclude = array(); if ( !is_admin() ) { add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) ); } else { add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); } $this->defaults = array( 'title' => 'CodeCanyon', 'description' => '', 'items_type' => array( 'wordpress' ), 'items_from' => 'user', 'user' => 'themepunch', 'num_items' => 9, 'orderby' => 'uploaded_on', 'ref' => 'pluginsbay', 'more_link_url' => 'https://codecanyon.net/user/themepunch/portfolio?ref=pluginsbay', 'more_link_txt' => __( 'View more', 'affiliate-codecanyon-widget' ), 'order' => 'desc', 'target' => '_blank', 'exclude' => '', 'nofollow' => 0, 'token' => '' ); //Allow themes or plugins to modify default parameters $this->defaults = apply_filters( 'pb_cc_widget_modify_defaults', $this->defaults ); } function widget( $args, $instance ) { extract( $args ); $instance = wp_parse_args( (array) $instance, $this->defaults ); $title = apply_filters( 'widget_title', $instance['title'] ); echo $before_widget; if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>

exclude = explode( ',', $instance['exclude'] ); $this->exclude = array_map( 'absint', $this->exclude ); } $this->token = $instance['token']; $items = array(); $order = false; switch ( $instance['items_from'] ) { case 'popular': $items = $this->get_popular_items( $instance['items_type'] ); break; case 'latest': $items = $this->get_latest_items( $instance['items_type'] ); break; default: if ( !empty( $instance['user'] ) ) { $users = array_map( 'trim', explode( ',', $instance['user'] ) ); $items = $this->get_items_from_users( $users, $instance['items_type'] ); } break; } if ( !empty( $items ) ): if( $instance['items_from'] == 'user'){ $this->orderby = $instance['orderby']; $this->items_order = $instance['order']; if ( $this->orderby != 'random' ) { usort( $items, array( $this, "cmp" ) ); } else { shuffle( $items ); } } $items = array_slice( $items, 0, absint( $instance['num_items'] ) ); $ref = !empty( $instance['ref'] ) ? '?ref='.$instance['ref'] : ''; $target = !empty( $instance['target'] ) ? $instance['target'] : '_blank'; $nofollow = $instance['nofollow'] ? 'rel="nofollow"' : ''; ?>

>

defaults ); ?>

'.__( 'Generate token', 'affiliate-codecanyon-widget' ).''; ?>


class="affiliate-codecanyon-widget-type" />
class="affiliate-codecanyon-widget-type" />
class="affiliate-codecanyon-widget-type" />

selected( $instance['items_from'], 'user', 'style="display:none;"', true ); ?>>

selected( $instance['items_from'], 'popular', 'style="display:none;"'); ?>>
cc_cats as $cat ) : ?> />

selected( $instance['items_from'], 'user', 'style="display:none;"', true ); ?>>

selected( $instance['items_from'], 'user', 'style="display:none;"', true ); ?>> /> />

has_token() ) { $headers = array(); $headers[] = 'Content-length: 0'; $headers['Content-type'] = 'application/json'; $headers['Authorization'] = 'Bearer '.$this->token; $args = array( 'headers' => $headers ); $url = 'https://api.envato.com/v1/market/'; } else { $url = 'http://marketplace.envato.com/api/v3/'; } $url .= $query; //print_r( $url ); $response = wp_remote_get( $url, $args ); //print_r( $response ); //print_r( $response ); if ( is_wp_error( $response ) || ( wp_remote_retrieve_response_code( $response ) != 200 ) ) { return false; } return json_decode( wp_remote_retrieve_body( $response ), true ); } function has_token() { return isset( $this->token ) && !empty( $this->token ); } function get_items_from_users( $users = array(), $type = array( 'wordpress' ) ) { $items = array(); foreach ( $users as $user ) { $cached = get_transient( $this->id_base.'_'.$user ); $item_data_ready = array(); if ( empty( $cached ) ) { $query = 'new-files-from-user:'.$user.',codecanyon.json'; $response = $this->envato_request( $query ); if ( !isset( $response['new-files-from-user'] ) || empty( $response['new-files-from-user'] ) ) { continue; } $item_data_ready = $response['new-files-from-user']; set_transient( $this->id_base.'_'.$user, $item_data_ready, DAY_IN_SECONDS ); } else { $item_data_ready = $cached; } $type_check = count( $type ) == count( $this->cc_cats ) ? false : true; foreach ( $item_data_ready as $item ) { if ( !in_array( $item['id'], $this->exclude ) ) { if ( $type_check ) { if ( $this->item_type_check( trim( $item['category'] ), $type ) ) { $items[] = $item; } } else { $items[] = $item; } } } } return $items; } function get_popular_items( $type = array( 'wordpress' ) ) { $type = array( 'wordpress' ); //Hardcoded to WordPres only, due to current CodeCanyon API limitations $items = array(); $cached = get_transient( $this->id_base.'_popular' ); $item_data_ready = array(); if ( empty( $cached ) ) { $query = 'popular:codecanyon.json'; $response = $this->envato_request( $query ); if ( !isset( $response['popular']['items_last_week'] ) || empty( $response['popular']['items_last_week'] ) ) { return $items; } $item_data_ready = $response['popular']['items_last_week']; set_transient( $this->id_base.'_popular', $item_data_ready, DAY_IN_SECONDS ); } else { $item_data_ready = $cached; } $type_check = count( $type ) == count( $this->cc_cats ) ? false : true; foreach ( $item_data_ready as $item ) { if ( !in_array( $item['id'], $this->exclude ) ) { if ( $type_check ) { if ( $this->item_type_check( trim( $item['category'] ), $type ) ) { $items[] = $item; } } else { $items[] = $item; } } } return $items; } function get_latest_items( $types = array( 'wordpress' ) ) { $items = array(); foreach ( $types as $type ) { $cached = get_transient( $this->id_base.'_'.$type ); $item_data_ready = array(); if ( empty( $cached ) ) { $query = 'new-files:codecanyon,'.$type.'.json'; $response = $this->envato_request( $query ); //print_r( $response['new-files'] ); if ( !isset( $response['new-files'] ) || empty( $response['new-files'] ) ) { continue; } $item_data_ready = $response['new-files']; set_transient( $this->id_base.'_'.$type, $item_data_ready, DAY_IN_SECONDS ); } else { $item_data_ready = $cached; } foreach ( $item_data_ready as $item ) { if ( !in_array( $item['id'], $this->exclude ) ) { $items[] = $item; } } } return $items; } function item_type_check( $category, $types ) { foreach ( $types as $type ) { if ( strpos( 'pb'.$category, $type ) ) { return true; } } return false; } function enqueue_styles() { wp_register_style( 'pluginsbay-codecanyon-widget', PB_CC_PLUGIN_URI . 'css/style.css', false, PB_CC_WIDGET_VER ); wp_enqueue_style( 'pluginsbay-codecanyon-widget' ); } function enqueue_scripts() { global $pagenow, $typenow; if( $pagenow == 'widgets.php' ){ wp_enqueue_script( 'pluginsbay-codecanyon-widget', PB_CC_PLUGIN_URI . 'js/admin.js', array('jquery') , PB_CC_WIDGET_VER ); } } function selected( $a, $b, $output, $reverse = false ) { if($reverse){ return (string) $a !== (string) $b ? $output : ''; } else { return (string) $a === (string) $b ? $output : ''; } } function cmp( $a, $b ) { if ( $this->orderby == 'last_update' || $this->orderby == 'uploaded_on' ) { if ( $this->items_order == 'desc' ) { return strcmp( strtotime( $b[$this->orderby] ), strtotime( $a[$this->orderby] ) ); } else { return strcmp( strtotime( $a[$this->orderby] ), strtotime( $b[$this->orderby] ) ); } } else { if ( $this->items_order == 'desc' ) { return $b[$this->orderby] > $a[$this->orderby] ? true : false; } else { return $b[$this->orderby] > $a[$this->orderby] ? false : true; } } } } ?>