- made hooks documentation;

- some optimizations and make single functions for some hooks;
This commit is contained in:
nikitozzzzzzz
2018-03-02 09:55:49 +02:00
parent 6fc6db7886
commit 3d19aa00b8
64 changed files with 7287 additions and 1790 deletions
+140 -88
View File
@@ -4,105 +4,157 @@ namespace um\core;
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
if ( ! class_exists( 'User_posts' ) ) {
class User_posts {
function __construct() {
add_action('um_profile_content_posts', array(&$this, 'add_posts') );
add_action('um_profile_content_comments', array(&$this, 'add_comments') );
add_action('um_ajax_load_posts__um_load_posts', array(&$this, 'load_posts') );
add_action('um_ajax_load_posts__um_load_comments', array(&$this, 'load_comments') );
}
/***
*** @dynamic load of posts
***/
function load_posts( $args ) {
$array = explode(',', $args );
$post_type = $array[0];
$posts_per_page = $array[1];
$offset = $array[2];
$author = $array[3];
$offset_n = $posts_per_page + $offset;
UM()->shortcodes()->modified_args = "$post_type,$posts_per_page,$offset_n,$author";
UM()->shortcodes()->loop = UM()->query()->make("post_type=$post_type&posts_per_page=$posts_per_page&offset=$offset&author=$author");
UM()->shortcodes()->load_template('profile/posts-single');
}
/***
*** @dynamic load of comments
***/
function load_comments( $args ) {
$array = explode(',', $args );
$post_type = $array[0];
$posts_per_page = $array[1];
$offset = $array[2];
$author = $array[3];
$offset_n = $posts_per_page + $offset;
UM()->shortcodes()->modified_args = "$post_type,$posts_per_page,$offset_n,$author";
UM()->shortcodes()->loop = UM()->query()->make("post_type=$post_type&number=$posts_per_page&offset=$offset&user_id=$author");
UM()->shortcodes()->load_template('profile/comments-single');
}
/***
*** @add posts
***/
function add_posts() {
UM()->shortcodes()->load_template('profile/posts');
}
/**
* Class User_posts
* @package um\core
*/
class User_posts {
/***
*** @add comments
***/
function add_comments() {
UM()->shortcodes()->load_template('profile/comments');
}
/**
* User_posts constructor.
*/
function __construct() {
add_action( 'um_profile_content_posts', array( &$this, 'add_posts' ) );
add_action( 'um_profile_content_comments', array( &$this, 'add_comments' ) );
/***
*** @count posts
***/
function count_user_posts_by_type( $user_id= '', $post_type = 'post' ) {
global $wpdb;
if ( !$user_id )
$user_id = um_user('ID');
add_action( 'um_ajax_load_posts__um_load_posts', array( &$this, 'load_posts' ) );
add_action( 'um_ajax_load_posts__um_load_comments', array( &$this, 'load_comments' ) );
}
if ( !$user_id ) return 0;
$where = get_posts_by_author_sql( $post_type, true, $user_id );
$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );
/**
* Dynamic load of posts
*
* @param array $args
*/
function load_posts( $args ) {
$array = explode(',', $args );
$post_type = $array[0];
$posts_per_page = $array[1];
$offset = $array[2];
$author = $array[3];
return apply_filters('um_pretty_number_formatting', $count);
}
$offset_n = $posts_per_page + $offset;
/***
*** @count comments
***/
function count_user_comments( $user_id = null ) {
global $wpdb;
if ( !$user_id )
$user_id = um_user('ID');
UM()->shortcodes()->modified_args = "$post_type,$posts_per_page,$offset_n,$author";
if ( !$user_id ) return 0;
UM()->shortcodes()->loop = UM()->query()->make("post_type=$post_type&posts_per_page=$posts_per_page&offset=$offset&author=$author");
$count = $wpdb->get_var("SELECT COUNT(comment_ID) FROM " . $wpdb->comments. " WHERE user_id = " . $user_id . " AND comment_approved = '1'");
UM()->shortcodes()->load_template('profile/posts-single');
}
return apply_filters('um_pretty_number_formatting', $count);
}
/**
* Dynamic load of comments
*
* @param $args
*/
function load_comments( $args ) {
$array = explode(',', $args );
$post_type = $array[0];
$posts_per_page = $array[1];
$offset = $array[2];
$author = $array[3];
}
$offset_n = $posts_per_page + $offset;
UM()->shortcodes()->modified_args = "$post_type,$posts_per_page,$offset_n,$author";
UM()->shortcodes()->loop = UM()->query()->make("post_type=$post_type&number=$posts_per_page&offset=$offset&user_id=$author");
UM()->shortcodes()->load_template('profile/comments-single');
}
/**
* Add posts
*/
function add_posts() {
UM()->shortcodes()->load_template( 'profile/posts' );
}
/**
* Add comments
*/
function add_comments() {
UM()->shortcodes()->load_template( 'profile/comments' );
}
/**
* Count posts by type
*
* @param string $user_id
* @param string $post_type
*
* @return int|string
*/
function count_user_posts_by_type( $user_id= '', $post_type = 'post' ) {
global $wpdb;
if ( !$user_id )
$user_id = um_user( 'ID' );
if ( !$user_id ) return 0;
$where = get_posts_by_author_sql( $post_type, true, $user_id );
$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );
return $this->pretty_number_formatting( $count );
}
/**
* Count comments
*
* @param int|null $user_id
*
* @return int|string
*/
function count_user_comments( $user_id = null ) {
global $wpdb;
if ( !$user_id )
$user_id = um_user('ID');
if ( !$user_id ) return 0;
$count = $wpdb->get_var("SELECT COUNT(comment_ID) FROM " . $wpdb->comments. " WHERE user_id = " . $user_id . " AND comment_approved = '1'");
return $this->pretty_number_formatting( $count );
}
/**
* @param int $count
*
* @return string
*/
function pretty_number_formatting( $count ) {
/**
* UM hook
*
* @type filter
* @title um_pretty_number_formatting
* @description Change User Posts count value
* @input_vars
* [{"var":"$count","type":"int","desc":"Posts Count"}]
* @change_log
* ["Since: 2.0"]
* @usage
* <?php add_filter( 'um_pretty_number_formatting', 'function_name', 10, 1 ); ?>
* @example
* <?php
* add_filter( 'um_pretty_number_formatting', 'my_pretty_number_formatting', 10, 1 );
* function my_pretty_number_formatting( $count ) {
* // your code here
* return $count;
* }
* ?>
*/
return apply_filters( 'um_pretty_number_formatting', $count );
}
}
}