Skip to content

Instantly share code, notes, and snippets.

@ikamal7
Created September 29, 2019 19:34
Show Gist options
  • Save ikamal7/7d6cbfc8073a56017436b257c9c06f82 to your computer and use it in GitHub Desktop.
Save ikamal7/7d6cbfc8073a56017436b257c9c06f82 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: WP Rating Reviews
* Plugin URI: http://imdev.xyz/plugins/wprr
* Description: Allows your visitors to leave business / product reviews.
* Version: 1.0.0
* Author: Kamal Hosen
* Author URI: http://imdev.xyz/
* Text Domain: wp-rating-reviews
* Domain Path: /languages/
* License: GNU General Public License v2 or later
*
* Copyright (c) 2019 Kamal Hosen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
Class WPRatingReviews {
public function __construct() {
add_action( 'plugins_loaded', array( $this, 'wprr_load_textdomain' ) );
add_action( 'admin_menu', array( $this, 'wprr_add_metabox' ) );
add_action( 'init', array( $this, 'wprr_register_post_type' ) );
add_action('admin_enqueue_scripts', array($this, 'wprr_admin_assets'));
//add_action('save_post', array($this, 'wprr_save_metabox'));
//add_action('save_post', array($this, 'wprr_insert_review'));
add_filter('the_content', array($this, 'wprr_frontend_form'));
}
function wprr_admin_assets(){
wp_enqueue_style('wprr_admin_css', plugin_dir_url(__FILE__).'/assets/css/wprr.css', null,time());
}
function wprr_insert_review(){
//if( wp_verify_nonce($_POST['_wpnonce'], 'wprr-frontend-post') ) {}
$review_name = isset( $_POST['reviewer'] );
$email_id = isset( $_POST['email_id'] );
$website = isset( $_POST['website'] );
$rating = isset( $_POST['rating'] );
$review = isset( $_POST['review'] );
$review_items = array(
'post_title' => $review_name,
'post_content' => $review,
'post_type' => 'wprr',
'post_status' => 'draft'
);
$post_id = wp_insert_post( $review_items, true );
//echo $post_id . $review_name . $email_id . $website;
update_post_meta( $post_id, 'reviewer', $review_name );
update_post_meta( $post_id, 'email_id', $email_id );
update_post_meta( $post_id, 'website', $website );
update_post_meta( $post_id, 'rating', $rating );
}
function wprr_frontend_form($content){
$post_id = get_the_ID();
$wpnoncefield = wp_nonce_field( 'wprr-frontend-post' );
if(is_single() || is_page()){
$form_markup = <<<EOD
<form method="POST" action="">
<label for="reviewer">Name</label><br>
<input type="text" name="reviewer" id="reviewer"><br>
<label for="email_id">Email</label><br>
<input type="email" name="email_id" id="email_id"><br>
<label for="website">Website</label><br>
<input type="text" name="website" id="website"><br>
<label for="rating">Rating</label><br>
<input type="text" name="rating" id="rating"><br>
<label for="review">Review</label><br>
<textarea name="review" id="review" cols="30" rows="2"></textarea><br>
<label for="image">Image</label><br>
<input type="file" name="image" id="image" accept="image/*"><br>
<input type="hidden" name="post_page_id" value="{$post_id}">
{$wpnoncefield}
<input type="submit" name="post" value="Send">
</form>
EOD;
return $content.$form_markup;
}
}
function wprr_save_metabox($post_id){
$reviewer = isset($_POST['reviewer']) ? $_POST['reviewer'] : '';
$email_id = isset($_POST['email_id']) ? $_POST['email_id'] : '';
$website = isset($_POST['website']) ? $_POST['website'] : '';
$rating = isset($_POST['rating']) ? $_POST['rating'] : '';
$admin = isset($_POST['admin']) ? $_POST['admin'] : '';
$post_page_id = isset($_POST['post_page_id']) ? $_POST['post_page_id'] : '';
update_post_meta($post_id, 'reviewer', $reviewer);
update_post_meta($post_id, 'email_id', $email_id);
update_post_meta($post_id, 'website', $website);
update_post_meta($post_id, 'rating', $rating);
update_post_meta($post_id, 'admin', $admin);
update_post_meta($post_id, 'post_page_id', $post_page_id);
}
function wprr_add_metabox() {
add_meta_box( 'wprr_metabox', __( 'WP Rating Reviews Setting', 'wp-rating-reviews' ), array(
$this,
'wprr_display_form'
), array( 'post', 'page' ) );
add_meta_box( 'wprr_metabox_cpt', __( 'WP Rating Reviews Meta', 'wp-rating-reviews' ), array(
$this,
'wprr_display_form_cpt'
), 'wprr' );
}
function wprr_display_form_cpt($post) {
$reviewer_meta = get_post_meta($post->ID,'reviewer', true);
$email_meta = get_post_meta($post->ID,'email_id', true);
$website_meta = get_post_meta($post->ID,'website', true);
$rating_meta = get_post_meta($post->ID,'rating', true);
$admin_meta = get_post_meta($post->ID,'admin', true);
$post_page_id = get_post_meta($post->ID,'post_page_id', true);
$ratings_value = array('1', '2', '3', '4', '5');
$id_lebel = __('Post/Page ID', 'wp-rating-reviews');
$reviewer = __('Reviewer Name', 'wp-rating-reviews');
$email_id = __('Email', 'wp-rating-reviews');
$website = __('Website', 'wp-rating-reviews');
$rating = __('Rating', 'wp-rating-reviews');
$admin = __('Admin Response', 'wp-rating-reviews');
$option_html = "<option value='0'>".__('Select rating', 'wp-rating-reviews')."</option>";
foreach ($ratings_value as $rating_value){
$selected = '';
if($rating_value == $rating_meta){
$selected = 'selected';
}
$option_html .= sprintf("<option %s value='%s'>%s start</option>", $selected, $rating_value, $rating_value );
}
$form_markup = <<<EOD
<div class="fields">
<div class="fields_wrap">
<div class="label_wrap">
<label for="post_page_id">{$id_lebel}</label>
</div>
<div class="input_wrap">
<input class="widefat" type="text" id="post_page_id" name="post_page_id" value="{$post_page_id}">
</div>
</div>
<div class="fields_wrap">
<div class="label_wrap">
<label for="reviewer">{$reviewer}</label>
</div>
<div class="input_wrap">
<input class="widefat" type="text" id="reviewer" name="reviewer" value="{$reviewer_meta}">
</div>
</div>
<div class="fields_wrap">
<div class="label_wrap">
<label for="email_id">{$email_id}</label>
</div>
<div class="input_wrap">
<input class="widefat" type="email" id="email_id" name="email_id" value="{$email_meta}">
</div>
</div>
<div class="fields_wrap">
<div class="label_wrap">
<label for="website">{$website}</label>
</div>
<div class="input_wrap">
<input class="widefat" type="text" id="website" name="website" value="{$website_meta}">
</div>
</div>
<div class="fields_wrap">
<div class="label_wrap">
<label for="rating">{$rating}</label>
</div>
<div class="input_wrap">
<select class="widefat" name="rating" id="rating">
{$option_html}
</select>
</div>
</div>
<div class="fields_wrap">
<div class="label_wrap">
<label for="admin">{$admin}</label>
</div>
<div class="input_wrap">
<textarea class="widefat" name="admin" id="admin" value="">{$admin_meta}</textarea>
</div>
</div>
</div>
EOD;
echo $form_markup;
}
function wprr_display_form() {
$show_form_label = __( 'Show review form' );
$show_review_label = __( 'Show review' );
$metabox_form = <<<EOD
<p>
<label for="show_form"><b>{$show_form_label}</b></label>
<select name="show_form" id="show_form">
<option value="1">Yes</option>
<option value="2">No</option>
</select>
</p>
<p>
<label for="show_review"><b>{$show_review_label}</b></label>
<select name="show_review" id="show_review">
<option value="1">Yes</option>
<option value="2">No</option>
</select>
</p>
EOD;
echo $metabox_form;
}
public function wprr_load_textdomain() {
load_plugin_textdomain( 'wp-rating-reviews', false, dirname( __FILE__ ) . '/languages' );
}
function wprr_register_post_type() {
register_post_type( 'wprr', array(
'labels' => array(
'name' => __( 'Reviews', 'wp-rating-reviews' ),
'singular_name' => __( 'Review', 'wp-rating-reviews' ),
'menu_name' => __( 'Reviews', 'wp-rating-reviews' ),
'add_new_item' => __( 'Add New Review', 'wp-rating-reviews' ),
'add_new' => __( 'Add New', 'wp-rating-reviews' ),
),
'public' => true,
'supports' => array( 'title', 'editor', 'thumbnail' ),
'menu_icon' => plugin_dir_url( __FILE__ ) . 'assets/img/wprr.png',
) );
}
}
new WPRatingReviews();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment