Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[WordPress Plugin] MW WP Form Count Limiter
<?php
/*
* Plugin Name: MW WP Form Count Limiter
* Plugin URI: https://mt8.biz
* Description: MW WP Form のフォームごとに送信件数制限を設定できるようにするアドオン
* Version: 0.1
* Author: mt8
* Author URI: https://mt8.biz
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
$mwfcl = new MW_WP_Form_Count_Limiter();
$mwfcl->register_hooks();
class MW_WP_Form_Count_Limiter {
public function register_hooks() {
add_action( 'admin_init', array( $this, 'admin_init' ) );
add_filter( 'do_shortcode_tag', array( $this, 'do_shortcode_tag' ), 10, 4 );
}
public function admin_init() {
add_meta_box( 'mw_wp_form_count_limiter', 'Count Limiter', array( $this, 'add_meta_box' ), 'mw-wp-form', 'advanced', 'low' );
add_action( 'save_post', array( $this, 'save_post' ) );
}
public function add_meta_box() {
print $this->_get_meta_box_html();
}
private function _get_meta_box_html() {
global $pagenow;
$count = 0;
$text = '';
$target = '0';
if ( $pagenow == 'post.php' ) {
$p = filter_input( INPUT_GET, 'post' );
if ( $p ) {
$count = (int)get_post_meta( $p, 'wm_wp_form_count_limit', true );
$text = (string)get_post_meta( $p, 'wm_wp_form_count_end_text', true );
$target = (int)get_post_meta( $p, 'mw_wp_form_count_target', true );
}
}
ob_start();
?>
<p>
<label for="wm_wp_form_count_limit">Count</label>
<input type="number" min="0" max="999" id="wm_wp_form_count_limit" name="wm_wp_form_count_limit" value="<?php echo esc_attr( $count ); ?>" />
</p>
<p>
<label for="mw_wp_form_count_target">Target</label>
<input type="radio" id="mw_wp_form_count_target" name="mw_wp_form_count_target" value="0" <?php checked( $target, '0' ) ?>>TackingNo
<input type="radio" id="mw_wp_form_count_target" name="mw_wp_form_count_target" value="1" <?php checked( $target, '1' ) ?>>Form Data Count
</p>
<p>
<label for="wm_wp_form_count_end_text">End Text</label>
<?php wp_editor( $text, 'wm_wp_form_count_end_text' ) ?>
</p>
<input type="hidden" name="mw_wp_form_count_limiter" id="mw_wp_form_count_limiter" value="<?php echo wp_create_nonce( plugin_basename(__FILE__) ); ?>" />
<?php return ob_get_clean();
}
public function save_post( $post_id ) {
if ( ! wp_verify_nonce( filter_input( INPUT_POST, 'mw_wp_form_count_limiter' ), plugin_basename(__FILE__) )) {
return $post_id;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
if ( 'mw-wp-form' == filter_input( INPUT_POST, 'post_type' ) ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return $post_id;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
}
update_post_meta( $post_id, 'wm_wp_form_count_limit', (int)filter_input( INPUT_POST, 'wm_wp_form_count_limit' ) );
update_post_meta( $post_id, 'wm_wp_form_count_end_text', (string)filter_input( INPUT_POST, 'wm_wp_form_count_end_text' ) );
update_post_meta( $post_id, 'mw_wp_form_count_target', (int)filter_input( INPUT_POST, 'mw_wp_form_count_target' ) );
}
public function do_shortcode_tag( $output, $tag, $attr, $m ) {
if ( ! in_array( $tag, array( 'mwform_formkey' ) ) || ! array_key_exists( 'key', $attr ) ) {
return $output;
}
$key = (int)$attr['key'];
if ( ! $key ) {
return $output;
}
$p = get_post( $key );
if ( ! $p || $p->post_type != 'mw-wp-form' ) {
return $output;
}
//counter setting
$count = (int)get_post_meta( $key, 'wm_wp_form_count_limit', true );
$text = (string)get_post_meta( $key, 'wm_wp_form_count_end_text', true );
$target = (int)get_post_meta( $key, 'mw_wp_form_count_target', true );
if ( ! $count || trim( $text ) == '' ) {
return $output;
}
if ( $target == '0' ) {
$tracking_number = (int)get_post_meta( $key, 'tracking_number', true );
if ( ! $tracking_number ) {
return $output;
}
if ( $count < $tracking_number ) {
return apply_filters( 'the_content', $text );
}
}
if ( $target == '1' ) {
$query = new WP_Query(
array(
'post_type' => 'mwf_'.$key,
'posts_per_page' => -1
)
);
if ( $count <= $query->found_posts ) {
return apply_filters( 'the_content', $text );
}
}
//defaults
return $output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.