Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added limit posts by post type #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions admin/dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
<input name="wpsite_limit_posts_settings_all_users" type="radio" value="capability" <?php echo isset($settings['all']) && $settings['all'] == 'capability' ? 'checked="checked"' : ''; ?>><label><?php _e('Role', self::$text_domain); ?></label><br />

<input name="wpsite_limit_posts_settings_all_users" type="radio" value="user" <?php echo isset($settings['all']) && $settings['all'] == 'user' ? 'checked="checked"' : ''; ?>><label><?php _e('User', self::$text_domain); ?></label>

<input name="wpsite_limit_posts_settings_all_users" type="radio" value="post_type" <?php echo isset($settings['all']) && $settings['all'] == 'post_type' ? 'checked="checked"' : ''; ?>><label><?php _e('Post Type', self::$text_domain); ?></label>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove this line since the Limit by Post Type option should be independent from the Limit by User

</td>
</th>
</tr>
Expand Down Expand Up @@ -96,6 +98,37 @@

?>

<!-- List all Post Types -->

<?php

$all_post_types_public = get_post_types(array('public'=> true),'names');
$all_post_types = array();
$post_types = array();

foreach ($all_post_types_public as $a){
if ($a != 'attachment'){
$all_post_types[] = $a;
}
}
foreach ($all_post_types as $post_type) {
$post_types[] = $post_type;
}

foreach ($post_types as $post_type) {
?><tr class="wpsite_limit_posts_post_type">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this class to allow the setting to show all the time.

<th class="wpsite_limit_posts_admin_table_th">
<label><?php _e($post_type, self::$text_domain); ?></label>
<td class="wpsite_limit_posts_admin_table_td">
<input id="wpsite_limit_posts_settings_<?php echo $post_type; ?>" name="wpsite_limit_posts_settings_<?php echo $post_type; ?>" type="text" size="10" value="<?php echo isset($settings['post_type_limit'][$post_type]) ? esc_attr($settings['post_type_limit'][$post_type]) : ''; ?>"><br/>
<em><?php _e("Default: -1 (i.e. umlimited)", self::$text_domain); ?></em>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make the default value -1 by changing this:

<?php echo isset($settings['post_type_limit'][$post_type]) ? esc_attr($settings['post_type_limit'][$post_type]) : ''; ?>

to this

<?php echo isset($settings['post_type_limit'][$post_type]) ? esc_attr($settings['post_type_limit'][$post_type]) : '-1'; ?>

</td>
</th>
</tr><?php
}

?>

</tbody>
</table>

Expand Down
19 changes: 16 additions & 3 deletions js/wpsite_limit_posts_admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,32 @@ jQuery(document).ready(function($) {
$("input:radio[name=wpsite_limit_posts_settings_all_users]").change(function(){
if ($(this).val() == 'capability') {
$(".wpsite_limit_posts_users").hide();
$(".wpsite_limit_posts_post_type").hide();
$(".wpsite_limit_posts_roles").show();
} else {
} else if ($(this).val() == 'user'){
$(".wpsite_limit_posts_users").show();
$(".wpsite_limit_posts_post_type").hide();
$(".wpsite_limit_posts_roles").hide();
}
else {
$(".wpsite_limit_posts_users").hide();
$(".wpsite_limit_posts_roles").hide();
$(".wpsite_limit_posts_post_type").show();
}
});

if ($("input:radio[name=wpsite_limit_posts_settings_all_users]:checked").val() == 'capability') {
$(".wpsite_limit_posts_users").hide();
$(".wpsite_limit_posts_roles").show();
} else {
$(".wpsite_limit_posts_post_type").hide();
} else if ($("input:radio[name=wpsite_limit_posts_settings_all_users]:checked").val() == 'user') {
$(".wpsite_limit_posts_users").show();
$(".wpsite_limit_posts_roles").hide();
$(".wpsite_limit_posts_post_type").hide();
}
else {
$(".wpsite_limit_posts_users").hide();
$(".wpsite_limit_posts_roles").hide();
$(".wpsite_limit_posts_post_type").show();
}

});
99 changes: 77 additions & 22 deletions wpsite_limit_posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ class WPsiteLimitPosts {
private static $default = array(
'all' => 'capability',
'all_limit' => array(),
'user_limit' => array()
'user_limit' => array(),
'post_type_limit' => array()
);

/**
Expand Down Expand Up @@ -126,43 +127,77 @@ static function wpsite_limit_posts_settings_link($links) {
*/
static function wpsite_stop_publish_post($data, $postarr) {

$user_data = get_userdata($data['post_author']);
$caps = $user_data->wp_capabilities;
$all_post_types_public = get_post_types(array('public'=> true),'names');
$all_post_types = array();
$post_types = array();

$settings = get_option('wpsite_limit_posts_settings');
foreach ($all_post_types_public as $a){
if ($a != 'attachment'){
$all_post_types[] = $a;
}
}
foreach ($all_post_types as $post_type) {
$post_types[] = $post_type;
}

/* Default values */
if (in_array(get_post_type(),$post_types)) {

if ($settings === false)
$settings = self::$default;
$user_data = get_userdata($data['post_author']);
$caps = $user_data->wp_capabilities;
$post_type_data = get_post_type();

global $wp_roles;
$settings = get_option('wpsite_limit_posts_settings');

if (!current_user_can('moderate_comments') && current_user_can('publish_posts')) {
/* Default values */

// Capabilities
if ($settings === false)
$settings = self::$default;

if (isset($settings['all']) && $settings['all'] == 'capability' && (int) $settings['all_limit'][implode(', ', $user_data->roles)] != -1) {
global $wp_roles;

if ($data['post_status'] == 'publish' && (int) $settings['all_limit'][implode(', ', $user_data->roles)] <= (int) count_user_posts($data['post_author']) && get_post_status($postarr['ID']) != 'publish') {
$data['post_status'] = 'limited';
}
if (!current_user_can('moderate_comments') && current_user_can('publish_posts')) {
// Capabilities

}
if (isset($settings['all']) && $settings['all'] == 'capability' && (int)$settings['all_limit'][implode(', ', $user_data->roles)] != -1) {

if ($data['post_status'] == 'publish' && (int)$settings['all_limit'][implode(', ', $user_data->roles)] <= (int)count_user_posts($data['post_author']) && get_post_status($postarr['ID']) != 'publish') {
$data['post_status'] = 'limited';
}

} // Users

else if (isset($settings['all']) && $settings['all'] == 'user' && (int)$settings['user_limit'][$data['post_author']] != -1) {
if ($data['post_status'] == 'publish' && (int)$settings['user_limit'][$data['post_author']] <= (int)count_user_posts($data['post_author']) && get_post_status($postarr['ID']) != 'publish') {
$data['post_status'] = 'limited';
}

// Users

else if (isset($settings['all']) && $settings['all'] == 'user' && (int) $settings['user_limit'][$data['post_author']] != -1) {
}
if (isset($settings['all']) && $settings['all'] == 'post_type' && (int)$settings['post_type_limit'][$post_type_data] != -1) {
$count_posts = wp_count_posts(get_post_type());
$published_posts = $count_posts->publish;

if ($data['post_status'] == 'publish' && (int)$settings['post_type_limit'][$post_type_data] <= (int)$published_posts && get_post_status($postarr['ID']) != 'publish') {
$data['post_status'] = 'limited';
}

if ($data['post_status'] == 'publish' && (int) $settings['user_limit'][$data['post_author']] <= (int) count_user_posts($data['post_author']) && get_post_status($postarr['ID']) != 'publish') {
$data['post_status'] = 'limited';
}
} else {
if (isset($settings['all']) && $settings['all'] == 'post_type' && (int)$settings['post_type_limit'][$post_type_data] != -1) {
$count_posts = wp_count_posts(get_post_type());
$published_posts = $count_posts->publish;

}
if ($data['post_status'] == 'publish' && (int)$settings['post_type_limit'][$post_type_data] <= (int)$published_posts && get_post_status($postarr['ID']) != 'publish') {
$data['post_status'] = 'limited';
}

}
}
return $data;
}
else{
return $data;
}

return $data;
}

/**
Expand Down Expand Up @@ -314,6 +349,26 @@ static function wpsite_limit_posts_admin_settings() {

}

$all_post_types_public = get_post_types(array('public'=> true),'names');
$all_post_types = array();
$post_types = array();

foreach ($all_post_types_public as $a){
if ($a != 'attachment'){
$all_post_types[] = $a;
}
}
foreach ($all_post_types as $post_type) {
$post_types[] = $post_type;
}
foreach ($post_types as $post_type) {
if (stripcslashes(sanitize_text_field($_POST['wpsite_limit_posts_settings_' . $post_type])) == '') {
$settings['post_type_limit'][$post_type] = -1;
} else {
$settings['post_type_limit'][$post_type] = isset($_POST['wpsite_limit_posts_settings_' . $post_type]) ? (int) stripcslashes(sanitize_text_field($_POST['wpsite_limit_posts_settings_' . $post_type])) : '-1';
}
}

update_option('wpsite_limit_posts_settings', $settings);
}

Expand Down