自定义Wordpress插件崩溃前端

时间:2016-05-12 17:19:31

标签: php wordpress

我有一个自定义插件,以前编码到functions.php并且工作正常。我提取了代码,我正在创建一个独立的插件。当我这样做时,网站的后端很好,插件可以完成应该做的事情,但是前端显示500错误。

<?php
function add_bulk_coupons() {
$prod = new MM_Product();
$selectedProds = array();

$id = 0; 

if(isset($p->id))
{
    $id = $p->id;
}

$coupon = new MM_Coupon($id);
$prods = $coupon->getProducts();

if(!$coupon->isValid())
{
    $coupon->setStartDate(Date("m/d/Y"));
}

$editable = "";

if(MM_Coupon::isBeingUsed($coupon->getId()))
{
    $editable = "disabled='disabled'";
}

$products = MM_HtmlUtils::createCheckboxGroup(MM_Product::getAll(),         "mm_products", $prods);

/*
//testing coupon restr
$x = 30;
while($x <= 1029):

    global $wpdb;
    $sql = "INSERT INTO mm_coupon_restrictions (coupon_id, product_id)     VALUES ('".$x."','1')";
    $wpdb->query( $wpdb->prepare( $sql ) );

    $x++;
endwhile;
*/
if(isset($_POST) && !empty($_POST)):
    global $wpdb;
    $coupon_variables = '';
    foreach($_POST as $key=>$value):
        if($key == 'number_of_coupons' || $key == 'mm_products' || $key ==     'coupon_name' || $key == 'coupon_code') continue;
        if($value == '') $value = NULL;
        if($key == 'end_date' || $key == 'start_date'):
            $value = date("Y-m-d H:i:s", strtotime($value));
        endif;
        if($coupon_variables==''):
            $coupon_variables .= "'".$value."'";
        else:
            $coupon_variables .= ','."'".$value."'";
        endif;
    endforeach;
    $x = 1;
    while($x <= $_POST['number_of_coupons']):
        $coupon_name = $_POST['coupon_name'].$x;
        $rand = rand(10000000, 90000000);
        $coupon_code = $_POST['coupon_code'].$rand;

        $sql = "INSERT INTO mm_coupons (coupon_name, coupon_code,     coupon_type, coupon_value, coupon_value_currency, description, quantity, start_date, end_date, recurring_billing_setting, is_gift, gift_user_id, gift_order_item_id, date_modified, date_added) VALUES ('".$coupon_name."','".$coupon_code."',".$coupon_variables.")";
        $wpdb->query( $wpdb->prepare( $sql ) );
        $sql = "SELECT id FROM mm_coupons ORDER BY id DESC LIMIT 1";
        $theid = $wpdb->get_results( $sql );
        //print_r( $theid );
        $x++;
    endwhile;
    echo '<div class="success" style="color: #000;background-color: #ebf8a4;border-color: #a2d246;margin: 1em 0 1em;border: 1px solid;background-repeat: no-repeat;background-position: 10px 50%;padding: 10px 10px 10px 10px;-moz-border-radius: 5px;-webkit-border-radius: 5px;border-radius: 5px;-moz-box-shadow: 0 1px 1px #fff inset;-webkit-box-shadow: 0 1px 1px #fff inset;box-shadow: 0 1px 1px #fff inset;">Successfully Created '.$_POST['number_of_coupons'].' Coupons. <a href="https://www.hurlbutvisuals.com/blog/wp-admin/admin.php?page=product_settings&module=coupons">View Them Now</a></div>';
endif;
?>
<form action="" method="post">
    <div id="mm-coupons-container">
        <br />

        <h3>Create your coupons</h3>
        <table cellspacing="10">
            <tr>
                <td width="140">Name Prefix*</td>
                <td><input type='hidden' id='id' value='<?php echo $coupon->getId(); ?>' />
                    <input id="mm_coupon_name" name="coupon_name" type="text" value='<?php echo $coupon->getCouponName(); ?>' style='width:300px;' />
                </td>
            </tr>
            <tr>
                <td width="140">Coupon Code Prefix*</td>
                <td>
                    <input id="mm_coupon_code" name="coupon_code" <?php echo $editable; ?>  type="text" value='<?php echo strtoupper($coupon->getCouponCode()); ?>' />
                </td>
            </tr>
            <tr>
                <td width="140">Discount*</td>
                <td>
                    <select id="mm_coupon_type" name="coupon_type" <?php echo $editable; ?> onchange="mmjs.typeChangeHandler();">
                        <option value='<?php echo MM_Coupon::$TYPE_PERCENTAGE; ?>' <?php echo (($coupon->getCouponType() == MM_Coupon::$TYPE_PERCENTAGE)?"selected":""); ?>>% Off</option>  
                        <option value='<?php echo MM_Coupon::$TYPE_DOLLAR; ?>' <?php echo (($coupon->getCouponType() == MM_Coupon::$TYPE_DOLLAR)?"selected":""); ?>>$ Off</option>  
                        <option value='<?php echo MM_Coupon::$TYPE_FREE; ?>' <?php echo (($coupon->getCouponType() == MM_Coupon::$TYPE_FREE)?"selected":""); ?>>Free</option>                   
                    </select>
                    <input id="mm_coupon_value" name="coupon_value" <?php echo $editable; ?> type="text" value='<?php echo $coupon->getCouponValue(); ?>' style="width:85px;" />
                    <input type="hidden" name="coupon_value_currency" value='' />
                    <input type="hidden" name="description" value='' />
                </td>
            </tr>
            <tr>
                <td>Number of coupons to create</td>
                <td>
                    <input id="mm_quantity" name="quantity" type="hidden" style='width: 80px;' value='1' />
                    <input id="mm_quantity" name="number_of_coupons" type="text" style='width: 80px;' value='<?php echo $coupon->getQuantity(); ?>' />
                </td>
            </tr>
            <tr>
                <td valign="top" width="140">Restrictions</td>
                <td>
                    <table>
                        <tr>
                            <td width="140">Start Date</td>
                            <td>
                                <?php echo MM_Utils::getCalendarIcon(); ?>
                                <input id="mm_start_date" name="start_date" type="text" value='<?php echo $coupon->getStartDate(); ?>' style="width:110px;" />
                            </td>
                        </tr>
                        <tr>
                            <td>End Date</td>
                            <td>
                                <?php echo MM_Utils::getCalendarIcon(); ?>
                                <input id="mm_end_date" name="end_date" type="text" value='<?php echo $coupon->getEndDate(); ?>' style="width:110px;" />
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                <div style="width: 98%; margin-top: 8px;" class="mm-divider"></div>
                </td>
            </tr>
            <tr>
                <td width="140" style="vertical-align:top;">Subscription Options</td>
                <td>
                    <div id="mm_subscription_options_section">
                        <p style="margin:0px 0px 5px 0px;"><input id="mm_recurring_setting_first" <?php echo $editable; ?> name="recurring_billing_setting" type="radio" value='first' <?php echo (($coupon->getRecurringBillingSetting()=="first")?"checked":""); ?> /> Apply discount to the first charge only</p>
                        <p style="margin:10px 0px 0px 0px;"><input id="mm_recurring_setting_all" <?php echo $editable; ?> name="recurring_billing_setting" type="radio" value='all' <?php echo (($coupon->getRecurringBillingSetting()=="all")?"checked":""); ?> /> Apply discount to all charges</p>
                    </div>
                    <input type="hidden" name="is_gift" value='0' />
                    <input type="hidden" name="gift_user_id" value='0' />
                    <input type="hidden" name="gift_order_item_id" value='0'     />

                    <input type="hidden" name="date_modified" value='<?php     echo date("Y-m-d H:i:s"); ?>' />
                    <input type="hidden" name="date_added" value='<?php echo date("Y-m-d H:i:s"); ?>' />
                </td>
            </tr>

            <tr>
                <td colspan="2">
                <div style="width: 98%; margin-top: 8px;" class="mm-divider"></div>
                </td>
            </tr>
                <tr valign='top'>
                    <td valign='top'>Valid Products</td>
                    <td valign='top'>
                        <div style="overflow: auto; height: 165px; width: 250px; border: 1px solid #ccc;">
                        <?php echo $products; ?>
                        </div>
                    </td>
                </tr>
            </table>
        </div>

        <script type='text/javascript'>
            jQuery(document).ready(function(){
                jQuery("#mm_start_date").datepicker();
                jQuery("#mm_end_date").datepicker();
            });
        </script>

        <div class="mm-dialog-footer-container">
        <div class="mm-dialog-button-container">
            <button class="mm-ui-button blue">Save Coupon</button>
        </div>
    </div>
</form>
<?php
}

0 个答案:

没有答案