first commit

This commit is contained in:
CHIEFSOFT\ameye
2023-11-30 13:20:54 -05:00
commit e9e5c0546c
5833 changed files with 1801865 additions and 0 deletions
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,47 @@
( function( $ ) {
'use strict';
$( function() {
/*
* AJAX Reset cache
*/
$( document ).on( 'click dblclick', 'a[href*="action=powerkit_reset_cache"]', function( e ) {
var $this = this;
$.ajax( {
type: 'POST',
url: window.pk_connect.ajax_url + '?action=powerkit_reset_cache',
data: {
page: $( $this ).attr( 'href' ).split( 'page=' ).pop().split( '&' ).shift(),
},
beforeSend: function() {
$( $this ).siblings( '.spinner, .status' ).remove();
$( $this ).after( '<span class="spinner is-active"></span>' );
},
success: function( data ) {
$( $this ).after( '<span class="status dashicons dashicons-yes"></span>' );
},
error: function() {
$( $this ).after( '<span class="status dashicons dashicons-no-alt"></span>' );
},
complete: function() {
$( $this ).siblings( '.spinner' ).remove();
setTimeout( function() {
$( $this ).siblings( '.status' ).fadeOut();
}, 2000 );
}
} );
// Refresh customizer.
if ( $( 'body' ).hasClass( 'wp-customizer' ) ) {
wp.customize.previewer.refresh();
}
return false;
} );
} );
} )( jQuery );
@@ -0,0 +1,159 @@
<?php
/**
* Fonts
*
* @package Powerkit
* @subpackage Extensions
*/
if ( class_exists( 'Powerkit_Module' ) ) {
/**
* Init module
*/
class Powerkit_Fonts extends Powerkit_Module {
/**
* Register module
*/
public function register() {
$this->name = 'fonts';
$this->slug = 'fonts';
$this->type = 'extension';
$this->category = 'basic';
$this->public = false;
$this->enabled = false;
}
/**
* Initialize module
*/
public function initialize() {
add_action( 'admin_menu', array( $this, 'register_options_page' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
add_filter( 'powerkit_fonts_choices', array( $this, 'kirki_fonts_choices' ) );
}
/**
* Register admin page
*
* @since 1.0.0
*/
public function register_options_page() {
add_theme_page( esc_html__( 'Fonts', 'powerkit' ), esc_html__( 'Fonts', 'powerkit' ), 'manage_options', powerkit_get_page_slug( $this->slug ), array( $this, 'settings_page' ) );
}
/**
* Build admin page
*
* @since 1.0.0
*/
public function settings_page() {
powerkit_uuid_hash();
// Check wpnonce.
if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'] ) ) { // Input var ok; sanitization ok.
return;
}
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You do not have sufficient rights to view this page.', 'powerkit' ) );
}
?>
<div class="wrap">
<h1 class="wp-heading-inline"><?php esc_html_e( 'Fonts', 'powerkit' ); ?></h1>
<hr class="wp-header-end">
<div class="settings">
<?php $fonts_settings = apply_filters( 'powerkit_fonts_register_settings', array() ); ?>
<?php if ( $fonts_settings ) : ?>
<div class="tabs">
<?php
$first_settings = current( $fonts_settings );
$tab = sanitize_title( isset( $_REQUEST['tab'] ) ? $_REQUEST['tab'] : $first_settings['id'] ); // Input var ok; sanitization ok.
?>
<nav class="nav-tab-wrapper">
<?php
foreach ( $fonts_settings as $item ) {
$class = ( $item['id'] === $tab ) ? 'nav-tab-active' : '';
printf( '<a class="nav-tab %4$s" href="%1$s&tab=%2$s">%3$s</a>',
esc_url( powerkit_get_page_url( $this->slug, 'themes' ) ), esc_attr( $item['id'] ), esc_html( $item['name'] ), esc_attr( $class )
);
}
?>
</nav>
<?php
foreach ( $fonts_settings as $item ) {
if ( $item['id'] === $tab ) {
?>
<div id="tab-<?php echo esc_attr( $item['id'] ); ?>" class="tab-wrap">
<?php
if ( is_callable( $item['function'] ) ) {
call_user_func( $item['function'] );
}
?>
</div>
<?php
}
}
?>
</div>
<?php endif; ?>
</div>
</div>
<?php
}
/**
* Add support wp-custom-fonts in Kirki.
*
* @param array $settings Pre settings.
*/
public function kirki_fonts_choices( $settings = array() ) {
$fonts_list = apply_filters( 'powerkit_fonts_list', array() );
if ( ! $fonts_list ) {
return $settings;
}
$fonts_settings = array(
'fonts' => array(
'google' => array(),
'families' => isset( $fonts_list['families'] ) ? $fonts_list['families'] : null,
'variants' => isset( $fonts_list['variants'] ) ? $fonts_list['variants'] : null,
),
);
$fonts_settings = array_merge( (array) $fonts_settings, (array) $settings );
return $fonts_settings;
}
/**
* Register the stylesheets and JavaScript for the admin area.
*
* @param string $page Current page.
*/
public function admin_enqueue_scripts( $page ) {
if ( 'appearance_page_' . powerkit_get_page_slug( $this->slug ) === $page ) {
wp_enqueue_media();
wp_enqueue_script( 'admin-powerkit-fonts', plugin_dir_url( __FILE__ ) . 'js/admin-powerkit-fonts.js', array( 'jquery' ), powerkit_get_setting( 'version' ), false );
wp_localize_script( 'admin-powerkit-fonts', 'powerkitFonts', array(
'delete' => esc_html__( 'Are you sure you want to delete this font and all the files downloaded from it?', 'powerkit' ),
'title' => esc_html__( 'Select or Upload Font', 'powerkit' ),
'button' => esc_html__( 'Use This File', 'powerkit' ),
) );
}
}
}
new Powerkit_Fonts();
}
@@ -0,0 +1,101 @@
( function( $ ) {
'use strict';
$( function() {
/* Set all variables to be used in scope */
var powerkitFontsMediaFrame;
/* Add Font Link */
$( document ).on( 'click', '.upload-font-container .upload-font-link', function( event ) {
event.preventDefault();
var parentContainer = $( this ).parents( '.upload-font-container' );
// Options.
var options = {
title: powerkitFonts.title,
button: {
text: powerkitFonts.button,
},
library: {
type: parentContainer.data( 'type' )
},
multiple: false
};
powerkitFontsMediaFrame = wp.media( options );
// When an image is selected in the media frame...
powerkitFontsMediaFrame.on( 'select', function() {
// Get media attachment details from the frame state.
var attachment = powerkitFontsMediaFrame.state().get( 'selection' ).first().toJSON();
parentContainer.find( '.filename' ).val( attachment.filename ).change();
parentContainer.find( '.uploaded-font-id' ).val( attachment.id ).change();
parentContainer.find( '.upload-font-link' ).addClass( 'hidden' );
parentContainer.find( '.delete-font-link' ).removeClass( 'hidden' );
powerkitFontsMediaFrame.close();
} );
powerkitFontsMediaFrame.open();
} );
/* Delete Font Link */
$( document ).on( 'click', '.upload-font-container .delete-font-link', function( event ) {
event.preventDefault();
var parentContainer = $( this ).parents( '.upload-font-container' );
parentContainer.find( '.filename' ).val( '' ).change();
parentContainer.find( '.uploaded-font-id' ).val( '' ).change();
parentContainer.find( '.upload-font-link' ).removeClass( 'hidden' );
parentContainer.find( '.delete-font-link' ).addClass( 'hidden' );
} );
/* Delete alert */
$( document ).on( 'click', '.powerkit-fonts-delete', function( event ) {
return confirm( powerkitFonts.delete ) ? true : false;
} );
/* View code */
$( document ).find( '.powerkit-fonts-view-code' ).each( function( index, el ) {
var obj = $( el ).closest( 'tr' ).next();
// Reset td padding.
$( obj ).find( 'td' ).css( 'padding', 0 );
// Show tr.
$( obj ).removeClass( 'hidden' );
// Set height textarea.
$( obj ).find( 'textarea' ).css( 'min-height', '110px' ).height( 110 );
} );
/* View code slide toggle */
$( document ).on( 'click', '.powerkit-fonts-view-code', function( event ) {
$( this ).closest( 'tr' ).prevAll().find( '.template-box' ).slideUp();
$( this ).closest( 'tr' ).next().nextAll().find( '.template-box' ).slideUp();
$( this ).closest( 'tr' ).next().find( '.template-box' ).slideToggle();
return false;
} );
/* View code outside */
$( document ).on( 'click', function( event ) {
if ( !$( event.target ).closest( '.wp-list-table' ).length ) {
$( '.template-box' ).slideUp();
}
} );
} );
} )( jQuery );
@@ -0,0 +1,309 @@
<?php
/**
* Gallery
*
* @package Powerkit
* @subpackage Extensions
*/
if ( class_exists( 'Powerkit_Module' ) ) {
/**
* Init module
*/
class Powerkit_Gallery extends Powerkit_Module {
/**
* Register module
*/
public function register() {
$this->name = 'gallery';
$this->slug = 'gallery';
$this->type = 'extension';
$this->category = 'basic';
$this->public = false;
$this->enabled = false;
}
/**
* Initialize module
*/
public function initialize() {
add_action( 'admin_init', array( $this, 'gallery_init' ) );
add_action( 'post_gallery', array( $this, 'change_post_gallery' ), 10, 3 );
add_filter( 'jetpack_gallery_types', array( $this, 'jetpack_gallery_types' ) );
// Force Jetpack compatibility with our galleries.
add_filter( 'jp_carousel_force_enable', '__return_true' );
}
/**
* Gallery Shortcode Function
*
* @param string $output The current output.
* @param array $attr Attributes from the gallery shortcode.
* @param int $instance Numeric ID of the gallery shortcode instance.
*/
public function change_post_gallery( $output, $attr, $instance ) {
// Make sure we're overwriting only galleries with either layout or type attributes.
if ( ! ( isset( $attr['layout'] ) || isset( $attr['type'] ) ) ) {
return;
}
// Support for the deprecated layout attribute.
if ( isset( $attr['layout'] ) ) {
if ( 'grid' === $attr['layout'] ) {
$attr['type'] = 'default';
} else {
$attr['type'] = $attr['layout'];
}
}
if ( ! isset( $attr['type'] ) ) {
return;
}
// Return if type is neither justified nor slider.
$gallery_types = apply_filters( 'powerkit_gallery_types', array() );
if ( ! array_key_exists( $attr['type'], (array) $gallery_types ) ) {
return '';
}
global $post;
$atts = shortcode_atts(
array(
'order' => 'ASC',
'orderby' => 'menu_order ID',
'id' => $post ? $post->ID : 0,
'itemtag' => 'figure',
'icontag' => 'div',
'captiontag' => 'figcaption',
'columns' => 3,
'type' => 'default',
'size' => 'thumbnail',
'include' => '',
'exclude' => '',
'link' => '',
), $attr, 'gallery'
);
$settings = array(
'custom_class' => '',
'custom_attrs' => '',
);
$settings = apply_filters( 'powerkit_gallery_settings', $settings, $attr );
$id = intval( $atts['id'] );
if ( ! empty( $atts['include'] ) ) {
$_attachments = get_posts(
array(
'include' => $atts['include'],
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => $atts['order'],
'orderby' => $atts['orderby'],
)
);
$attachments = array();
foreach ( $_attachments as $key => $val ) {
$attachments[ $val->ID ] = $_attachments[ $key ];
}
} elseif ( ! empty( $atts['exclude'] ) ) {
$attachments = get_children(
array(
'post_parent' => $id,
'exclude' => $atts['exclude'],
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => $atts['order'],
'orderby' => $atts['orderby'],
)
);
} else {
$attachments = get_children(
array(
'post_parent' => $id,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => $atts['order'],
'orderby' => $atts['orderby'],
)
);
}
if ( empty( $attachments ) ) {
return '';
}
if ( is_feed() ) {
$output = "\n";
foreach ( $attachments as $att_id => $attachment ) {
$output .= wp_get_attachment_link( $att_id, $atts['size'], true ) . "\n";
}
return $output;
}
$itemtag = tag_escape( $atts['itemtag'] );
$captiontag = tag_escape( $atts['captiontag'] );
$custom_class = $settings['custom_class'];
$custom_attrs = $settings['custom_attrs'];
if ( 'justified' === $atts['type'] ) {
$captiontag = 'div';
}
$type = esc_attr( $atts['type'] );
$float = is_rtl() ? 'right' : 'left';
$selector = "gallery-{$instance}";
$size_class = sanitize_html_class( $atts['size'] );
$gallery_div = "<div id='$selector' class='gallery galleryid-{$id} gallery-type-{$type} gallery-size-{$size_class} {$custom_class}' {$custom_attrs}>";
$output = apply_filters( 'powerkit_gallery_style', $gallery_div );
$i = 0;
foreach ( $attachments as $id => $attachment ) {
$attr = ( trim( $attachment->post_excerpt ) ) ? array(
'aria-describedby' => "$selector-$id",
) : array();
// Add gallery type attribute.
$attr['data-gallery'] = $atts['type'];
if ( ! empty( $atts['link'] ) && 'file' === $atts['link'] ) {
$image_output = wp_get_attachment_link( $id, $atts['size'], false, false, false, $attr );
} elseif ( ! empty( $atts['link'] ) && 'none' === $atts['link'] ) {
$image_output = wp_get_attachment_image( $id, $atts['size'], false, $attr );
} else {
$image_output = wp_get_attachment_link( $id, $atts['size'], true, false, false, $attr );
}
$image_meta = wp_get_attachment_metadata( $id );
$output .= "<{$itemtag} class='gallery-item'>";
$output .= $image_output;
if ( $captiontag && trim( $attachment->post_excerpt ) ) {
$output .= "
<{$captiontag} class='caption wp-caption-text gallery-caption' id='$selector-$id'>
" . wptexturize( $attachment->post_excerpt ) . "
</{$captiontag}>";
}
$output .= "</{$itemtag}>";
}
$output .= '</div>';
return $output;
}
/**
* Add new gallery types if Jetpack is not installed
*/
public function gallery_init() {
add_action( 'print_media_templates', array( $this, 'print_media_templates' ) );
}
/**
* Add new gallery types to Jetpack if it's installed
*
* @param array $types Existing Jetpack gallery types.
*/
public function jetpack_gallery_types( $types ) {
$gallery_types = apply_filters(
'powerkit_gallery_types', array(
'default' => esc_html__( 'Default', 'powerkit' ),
)
);
$types = array_merge( $types, $gallery_types );
return $types;
}
/**
* Add Gallery Type Dropdown
*/
public function print_media_templates() {
// Return if Jetpack's Tiled Gallery.
if ( class_exists( 'Jetpack' ) && Jetpack::is_module_active( 'tiled-gallery' ) ) {
return '';
}
$gallery_types = apply_filters(
'powerkit_gallery_types', array(
'default' => esc_html__( 'Default', 'powerkit' ),
)
);
$default_gallery_type = apply_filters( 'powerkit_default_gallery_type', 'default' );
?>
<script type="text/html" id="tmpl-pk-gallery-settings">
<label class="setting">
<span><?php esc_html_e( 'Type', 'powerkit' ); ?></span>
<select class="type" name="type" data-setting="type">
<?php foreach ( $gallery_types as $value => $caption ) : ?>
<option value="<?php echo esc_attr( $value ); ?>"><?php echo esc_html( $caption ); ?></option>
<?php endforeach; ?>
</select>
</label>
</script>
<script>
jQuery( document ).ready( function() {
_.extend( wp.media.gallery.defaults, {
type: '<?php echo esc_attr( $default_gallery_type ); ?>'
} );
// join default gallery settings template with yours -- store in list
if ( !wp.media.gallery.templates ) wp.media.gallery.templates = [ 'gallery-settings' ];
wp.media.gallery.templates.push( 'pk-gallery-settings' );
// loop through list -- allowing for other templates/settings
wp.media.view.Settings.Gallery = wp.media.view.Settings.Gallery.extend( {
template: function( view ) {
var output = '';
for ( var i in wp.media.gallery.templates ) {
output += wp.media.template( wp.media.gallery.templates[ i ] )( view );
}
return output;
},
render: function() {
var $el = this.$el;
wp.media.view.Settings.prototype.render.apply( this, arguments );
// Hide the Columns setting for all types except Default
$el.find( 'select[name=type]' ).on( 'change', function () {
var columnSetting = $el.find( 'select[name=columns]' ).closest( 'label.setting' );
if ( 'default' === jQuery( this ).val() || 'thumbnails' === jQuery( this ).val() ) {
columnSetting.show();
} else {
columnSetting.hide();
}
} ).change();
return this;
}
} );
} );
</script>
<?php
}
}
new Powerkit_Gallery();
}
@@ -0,0 +1,266 @@
<?php
/**
* Social Follow
*
* @package Powerkit
* @subpackage Extensions
*/
if ( class_exists( 'Powerkit_Module' ) ) {
/**
* Init module
*/
class Powerkit_Social_Follow extends Powerkit_Module {
/**
* Register module
*/
public function register() {
$this->name = 'Social Follow';
$this->slug = 'social_follow';
$this->type = 'extension';
$this->category = 'basic';
$this->public = false;
$this->enabled = true;
}
/**
* Transient Prefix
*
* @since 1.0.0
* @var string $cache_timeout Cache Timeout (minutes).
*/
private $trans_prefix = 'powerkit_social_follow_';
/**
* Initialize module
*/
public function initialize() {
// Reset cache.
add_filter( 'powerkit_reset_cache', array( $this, 'register_reset_cache' ) );
add_filter( 'powerkit_ajax_reset_cache', array( $this, 'register_reset_cache' ) );
}
/**
* Register Reset Cache
*
* @since 1.0.0
* @param array $list Change list reset cache.
* @access private
*/
public function register_reset_cache( $list ) {
$slug = powerkit_get_page_slug( $this->slug );
$list[ $slug ] = array(
'powerkit_social_follow',
);
return $list;
}
/**
* Return social follow
*
* @param string $name Name of social network.
* @param string $username Username of social network.
* @param string $type Type of social network.
*/
public function get_data( $name, $username, $type = null ) {
$social_function = sprintf( 'get_%s', $name );
if ( ! $username ) {
return;
}
if ( method_exists( $this, $social_function ) ) {
return $this->$social_function( $username, $type );
}
}
/**
* Get youtube data.
*
* @param string $username Username of social network.
* @param string $type Type of social network.
*/
public function get_youtube( $username, $type = null ) {
$result = array();
// Get data for social network.
$counter = new Powerkit_Links_Social_Counter();
$counter->trans_prefix = $this->trans_prefix;
$counter->config['youtube_slug'] = $username;
$counter->config['youtube_channel_type'] = $type;
$data = $counter->get_count( 'youtube', true );
// Followers.
// Manual Count Override.
$count_override = get_option( 'powerkit_social_links_youtube_override' );
if ( $count_override ) {
$result['followers'] = $count_override;
} elseif ( isset( $data['count'] ) && ! isset( $data['error'] ) ) {
$result['followers'] = $data['count'];
} elseif ( isset( $data['error'] ) ) {
powerkit_alert_warning( $data['error'] );
}
// Avatar.
if ( isset( $data['data'] ) && is_object( $data['data'] ) && isset( $data['data']->items[0]->snippet->thumbnails->default->url ) ) {
$result['avatar_1x'] = $data['data']->items[0]->snippet->thumbnails->default->url;
}
if ( isset( $data['data'] ) && is_object( $data['data'] ) && isset( $data['data']->items[0]->snippet->thumbnails->medium->url ) ) {
$result['avatar_2x'] = $data['data']->items[0]->snippet->thumbnails->medium->url;
}
// Username.
if ( isset( $data['data'] ) && is_object( $data['data'] ) && isset( $data['data']->items[0]->snippet->title ) ) {
$result['text'] = $data['data']->items[0]->snippet->title;
}
// Link.
if ( 'channel' === $type ) {
$result['link'] = sprintf( 'https://www.youtube.com/channel/%s/', $username );
} else {
$result['link'] = sprintf( 'https://www.youtube.com/user/%s/', $username );
}
return $result;
}
/**
* Get facebook data.
*
* @param string $username Username of social network.
* @param string $type Type of social network.
*/
public function get_facebook( $username, $type = null ) {
$result = array();
// Get data for social network.
$counter = new Powerkit_Links_Social_Counter();
$counter->trans_prefix = $this->trans_prefix;
$counter->config['facebook_user'] = $username;
$data = $counter->get_count( 'facebook', true );
// Followers.
// Manual Count Override.
$count_override = get_option( 'powerkit_social_links_facebook_override' );
if ( $count_override ) {
$result['followers'] = $count_override;
} elseif ( isset( $data['count'] ) && ! isset( $data['error'] ) ) {
$result['followers'] = $data['count'];
} elseif ( isset( $data['error'] ) ) {
powerkit_alert_warning( $data['error'] );
}
// Link.
$result['link'] = sprintf( 'https://facebook.com/%s/', $username );
// Avatar.
$result['avatar_1x'] = "https://graph.facebook.com/{$username}/picture?width=80&height=80";
$result['avatar_2x'] = "https://graph.facebook.com/{$username}/picture?width=160&height=160";
// Username.
$result['text'] = $username;
return $result;
}
/**
* Get instagram data.
*
* @param string $username Username of social network.
* @param string $type Type of social network.
*/
public function get_instagram( $username, $type = null ) {
$result = array();
// Get data for social network.
$counter = new Powerkit_Links_Social_Counter();
$counter->trans_prefix = $this->trans_prefix . md5( ( $username ) . powerkit_connect( 'instagram_app_access_token' ) );
$counter->config['instagram_user'] = $username;
$data = $counter->get_count( 'instagram', true );
// Followers.
// Manual Count Override.
$count_override = get_option( 'powerkit_social_links_instagram_override' );
if ( $count_override ) {
$result['followers'] = $count_override;
} elseif ( isset( $data['count'] ) && ! isset( $data['error'] ) ) {
$result['followers'] = $data['count'];
} elseif ( isset( $data['error'] ) ) {
powerkit_alert_warning( $data['error'] );
}
// Avatar parse json.
if ( isset( $data['data'] ) && is_object( $data['data'] ) && isset( $data['data']->profile_picture_url ) ) {
$result['avatar_1x'] = $data['data']->profile_picture_url;
}
if ( isset( $data['data'] ) && is_object( $data['data'] ) && isset( $data['data']->profile_picture_url ) ) {
$result['avatar_2x'] = $data['data']->profile_picture_url;
}
// Avatar parse html.
if ( isset( $data['data'] ) && is_array( $data['data'] ) && isset( $data['data']['entry_data']['ProfilePage'][0]['graphql']['user']['profile_pic_url'] ) ) {
$result['avatar_1x'] = $data['data']['entry_data']['ProfilePage'][0]['graphql']['user']['profile_pic_url'];
}
if ( isset( $data['data'] ) && is_array( $data['data'] ) && isset( $data['data']['entry_data']['ProfilePage'][0]['graphql']['user']['profile_pic_url_hd'] ) ) {
$result['avatar_2x'] = $data['data']['entry_data']['ProfilePage'][0]['graphql']['user']['profile_pic_url_hd'];
}
/* Manual Override */
if ( get_option( 'powerkit_connect_instagram_custom_followers' ) ) {
$result['followers'] = (int) get_option( 'powerkit_connect_instagram_custom_followers' );
}
if ( get_option( 'powerkit_connect_instagram_custom_avatar' ) ) {
$result['avatar_1x'] = get_option( 'powerkit_connect_instagram_custom_avatar' );
}
if ( get_option( 'powerkit_connect_instagram_custom_avatar' ) ) {
$result['avatar_2x'] = get_option( 'powerkit_connect_instagram_custom_avatar' );
}
// Link.
$result['link'] = sprintf( 'https://www.instagram.com/%s/', $username );
// Username.
$result['text'] = $username;
return $result;
}
}
/**
* Get social follow
*
* @param string $name Name of social network.
* @param string $username Username of social network.
* @param string $type Type of social network.
*/
function powerkit_get_social_follow( $name, $username, $type = null ) {
$object = new Powerkit_Social_Follow();
return $object->get_data( $name, $username, $type );
}
new Powerkit_Social_Follow();
}
@@ -0,0 +1,228 @@
<?php
/**
* Twitter API
*
* @package Powerkit
* @subpackage Extensions
*/
if ( class_exists( 'Powerkit_Module' ) ) {
/**
* Init module
*/
class Powerkit_Twitter_API extends Powerkit_Module {
/**
* Register module
*/
public function register() {
$this->name = 'Twitter API';
$this->slug = 'twitter-api';
$this->type = 'extension';
$this->category = 'basic';
$this->public = false;
$this->enabled = false;
}
/**
* The base_url.
*
* @var string $base_url The base_url.
*/
protected $base_url;
/**
* The get_fields.
*
* @var string $get_fields The get_fields.
*/
private $get_fields;
/**
* The oauth.
*
* @var string $oauth The oauth.
*/
private $oauth;
/**
* The header.
*
* @var string $header The header.
*/
private $header;
/**
* The json.
*
* @var string $json The json.
*/
public $json;
/**
* Init API
*
* @param array $request_settings All necessary tokens for OAuth connection.
* @param string $feed_type Type of Twitter feed.
*/
public function init( $request_settings, $feed_type ) {
$this->consumer_key = $request_settings['consumer_key'];
$this->consumer_secret = $request_settings['consumer_secret'];
$this->access_token = $request_settings['access_token'];
$this->access_token_secret = $request_settings['access_token_secret'];
$this->feed_type = $feed_type;
}
/**
* Sets the complete url for our API endpoint. GET fields will be added later
*/
public function set_url_base() {
switch ( $this->feed_type ) {
case 'hometimeline':
$this->base_url = 'https://api.twitter.com/1.1/statuses/home_timeline.json';
break;
case 'search':
$this->base_url = 'https://api.twitter.com/1.1/search/tweets.json';
break;
default:
$this->base_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
}
}
/**
* Encodes an array of GET field data into html characters for including in a URL
*
* @param array $get_fields Array of GET fields that are compatible with the Twitter API.
*/
public function set_get_fields( array $get_fields ) {
$url_string = '?';
$length = count( $get_fields );
$j = 1;
foreach ( $get_fields as $key => $value ) {
$url_string .= rawurlencode( $key ) . '=' . rawurlencode( $value );
if ( $j != $length ) {
$url_string .= '&';
}
$j++;
}
$this->get_fields = $url_string;
}
/**
* Uses the OAuth data to build the base string needed to create the
* OAuth signature to be used in the header of the request
*
* @param array $oauth Oauth data without the signature.
*/
private function build_base_string( $oauth ) {
$base_string = array();
ksort( $oauth );
// Start forming the header string by creating a numeric index array with each part of the header string it's own element in the array.
foreach ( $oauth as $key => $value ) {
$base_string[] = rawurlencode( $key ) . '=' . rawurlencode( $value );
}
// Convert the array of values into a single encoded string and return.
return 'GET&' . rawurlencode( $this->base_url ) . '&' . rawurlencode( implode( '&', $base_string ) );
}
/**
* Builds the OAuth data array that is used to authenticate the connection
* to the Twitter API
*/
public function build_oauth() {
$oauth = array(
'oauth_consumer_key' => $this->consumer_key,
'oauth_nonce' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_token' => $this->access_token,
'oauth_timestamp' => time(),
'oauth_version' => '1.0',
);
$getfields = str_replace( '?', '', explode( '&', $this->get_fields ) );
// Add the get fields to the oauth associative array to be formed into the header string eventually.
foreach ( $getfields as $getfield ) {
$split = explode( '=', $getfield );
if ( isset( $split[1] ) ) {
$oauth[ $split[0] ] = urldecode( $split[1] );
}
}
// The OAuth signature for Twitter is a hashed, encoded version of the base url, 4 different keys.
$base_string = $this->build_base_string( $oauth );
$composite_key = rawurlencode( $this->consumer_secret ) . '&' . rawurlencode( $this->access_token_secret );
$oauth_signature = base64_encode( hash_hmac( 'sha1', $base_string, $composite_key, true ) );
$oauth['oauth_signature'] = $oauth_signature;
$this->oauth = $oauth;
}
/**
* Since the OAuth data is passed in a url, special characters need to be encoded
*/
private function encode_header() {
$header = 'Authorization: OAuth ';
$values = array();
// Each element of the header needs to have it's special characters encoded for passing through a url.
foreach ( $this->oauth as $key => $value ) {
if ( in_array(
$key,
array(
'oauth_consumer_key',
'oauth_nonce',
'oauth_signature',
'oauth_signature_method',
'oauth_timestamp',
'oauth_token',
'oauth_version',
)
) ) {
$values[] = "$key=\"" . rawurlencode( $value ) . '"';
}
}
$header .= implode( ', ', $values );
$this->header = $header;
}
/**
* Attempts to connect to the Twitter api using WP_HTTP class
*
* @param string $url The complete api endpoint url.
*/
private function wp_http_request( $url ) {
$args = array(
'headers' => $this->header,
'timeout' => 60,
'sslverify' => false,
);
return wp_remote_get( $url, $args );
}
/**
* Uses the data created and gathered up to this point to make the actual connection
* to the Twitter API. It first tests whether or not a curl connection is possible,
* followed by file_get_contents connection, then defaults to the WordPress WP_HTTP object
*
* @return mixed|string raw json data retrieved from the API request
*/
public function perform_request() {
$url = $this->base_url . $this->get_fields;
$this->build_oauth();
$this->encode_header();
$this->json = $this->wp_http_request( $url );
return $this;
}
}
}
@@ -0,0 +1,122 @@
<?php
/**
* Gallery
*
* @package Powerkit
* @subpackage Extensions
*/
if ( class_exists( 'Powerkit_Module' ) ) {
/**
* Init module
*/
class Powerkit_User extends Powerkit_Module {
/**
* Register module
*/
public function register() {
$this->name = 'user';
$this->slug = 'user';
$this->type = 'extension';
$this->category = 'basic';
$this->public = false;
$this->enabled = true;
}
/**
* Initialize module
*/
public function initialize() {
// Helpers Functions for the module.
require_once dirname( __FILE__ ) . '/helpers/helper-powerkit-user.php';
// Filters.
add_filter( 'init', array( $this, 'powerkit_guest_support_meta' ) );
add_filter( 'pre_get_avatar', array( $this, 'powerkit_guest_support_avatar' ), 10, 3 );
add_filter( 'author_link', array( $this, 'powerkit_guest_support_link' ), 9999, 3 );
}
/**
* Add support meta info for guest authors.
*/
public function powerkit_guest_support_meta() {
$fields = array(
'user_url',
'url',
'display_name',
'first_name',
'last_name',
'user_email',
'website',
'description',
);
foreach ( $fields as $field ) {
/**
* Filters the value of the requested user metadata.
*
* @param string $value The value of the metadata.
* @param int $user_id The user ID for the value.
* @param int|bool $original The original user ID, as passed to the function.
*/
add_filter( 'get_the_author_' . $field, function( $value, $user_id, $original = null ) use ( $field ) {
if ( powerkit_is_guest( $user_id ) && empty( $value ) ) {
$guest_value = powerkit_get_guest_meta( $field, $user_id );
if ( $guest_value ) {
$value = $guest_value;
}
}
return $value;
}, 10, 3 );
}
}
/**
* Add support avatar for guest authors.
*
* @param string $link The URL to the author's page.
* @param int $author_id The author's id.
* @param string $author_nicename The author's nice name.
*/
public function powerkit_guest_support_link( $link, $author_id, $author_nicename ) {
global $wp_rewrite;
if ( powerkit_is_guest( $author_id ) ) {
$guest = powerkit_get_guest_meta( 'user_login', $author_id );
if ( $guest ) {
$link = $wp_rewrite->get_author_permastruct();
$link = str_replace( '%author%', $guest, $link );
$link = home_url( user_trailingslashit( $link ) );
}
}
return $link;
}
/**
* Add support avatar for guest authors.
*
* @param string $avatar HTML for the user's avatar. Default null.
* @param mixed $id_or_email The Gravatar to retrieve.
* @param array $args Arguments passed to get_avatar_url(), after processing.
*/
public function powerkit_guest_support_avatar( $avatar, $id_or_email, $args ) {
if ( powerkit_is_guest( $id_or_email ) && empty( $avatar ) ) {
$guest_avatar = coauthors_get_avatar( (object) array(
'ID' => $id_or_email,
'type' => 'guest-author',
), $args['size'] );
if ( $guest_avatar ) {
$avatar = $guest_avatar;
}
}
return $avatar;
}
}
new Powerkit_User();
}
@@ -0,0 +1,163 @@
<?php
/**
* Helpers to empower authors and users
*
* @package Powerkit
* @subpackage Modules/Helper
*/
/**
* Check enabled CoAuthors.
*/
function powerkit_coauthors_enabled() {
return class_exists( 'CoAuthors_Plus' );
}
/**
* Retrieve list of guests.
*/
function powerkit_get_guests() {
$list = wp_cache_get( 'powerkit_get_guests' );
if ( ! $list ) {
$list = array();
$query = new WP_Query();
$guests = $query->query( array(
'post_type' => 'guest-author',
'post_status' => 'publish',
'posts_per_page' => -1,
) );
foreach ( $guests as $guest ) {
array_push( $list, $guest );
}
wp_cache_set( 'powerkit_get_guests', $list, 'powerkit', 1 );
}
return $list;
}
/**
* Determines whether a user is a guest.
*
* @param int $id The id of guest.
*/
function powerkit_is_guest( $id ) {
if ( ! powerkit_coauthors_enabled() ) {
return false;
}
if ( ! is_numeric( $id ) ) {
return false;
}
$guests = powerkit_get_guests();
foreach ( $guests as $guest ) {
if ( (int) $id === (int) $guest->ID ) {
return true;
}
}
}
/**
* Retrieve guest info by a given field.
*
* @param string $field The field of guest.
* @param int $id The id of guest.
*/
function powerkit_get_guest_meta( $field, $id ) {
$field = str_replace( array( 'user_url', 'url' ), 'website', $field );
return get_post_meta( $id, 'cap-' . $field, true );
}
/**
* Retrieve list of users and guests.
*/
function powerkit_get_users() {
$users = wp_cache_get( 'powerkit_get_users' );
if ( ! $users ) {
$args = array(
'orderby' => 'display_name',
'capability' => array( 'edit_posts' ),
);
// Capability queries were only introduced in WP 5.9.
if ( version_compare( $GLOBALS['wp_version'], '5.9', '<' ) ) {
$args['who'] = 'authors';
unset( $args['capability'] );
}
$users = get_users( apply_filters( 'powerkit_get_users_args', $args ) );
if ( powerkit_coauthors_enabled() ) {
$guests = powerkit_get_guests();
foreach ( $guests as $guest ) {
$guest->first_name = powerkit_get_guest_meta( 'first_name', $guest->ID );
$guest->last_name = powerkit_get_guest_meta( 'last_name', $guest->ID );
$guest->user_email = powerkit_get_guest_meta( 'user_email', $guest->ID );
$guest->website = powerkit_get_guest_meta( 'website', $guest->ID );
$user = (object) array(
'ID' => $guest->ID,
'display_name' => $guest->post_title,
'user_registered' => $guest->post_date,
'user_login' => $guest->first_name,
'user_nicename' => $guest->last_name,
'user_email' => $guest->user_email,
'user_url' => $guest->website,
'user_pass' => '',
'user_activation_key' => '',
'user_status' => 0,
);
array_push( $users, $user );
}
}
wp_cache_set( 'powerkit_get_users', $users, 'powerkit', 1 );
}
usort( $users, function( $a, $b ) {
if ( function_exists( 'mb_strtolower' ) ) {
return strcmp( mb_strtolower( $a->display_name ), mb_strtolower( $b->display_name ) );
} else {
return strcmp( strtolower( $a->display_name ), strtolower( $b->display_name ) );
}
} );
return $users;
}
/**
* Check the user's belonging to the post.
*
* @param int $author_id The ID of author.
* @param int $post_id The ID of post.
*/
function powerkit_check_post_author( $author_id, $post_id ) {
if ( (string) get_post_field( 'post_author', $post_id ) === (string) $author_id ) {
return true;
}
if ( powerkit_coauthors_enabled() ) {
$coauthors = (array) get_coauthors();
foreach ( $coauthors as $coauthor ) {
if ( isset( $coauthor->ID ) && (string) $coauthor->ID === (string) $author_id ) {
return true;
}
}
}
}