如何在Nextpost ver中添加“Change Package”按钮。 4及更高?

I need this feature for account with active subscription and for expired accounts.

enter image description here

Update: Fount a solution, description below.

Fount a solution.

Add "Change Package" button for Settings page enter code here and for Renew page in Nextpost 4.0 and higher

Nextpost 4.0 and higher

  • Adds a "Change Package" button to Profile Settings page
  • Adds a "Change Package" button to Renew page (when subscription expired)
  • Checks for account count and just offers to change into packages where the allowed account count >= the existing accounts
  • Added extra security to prevent someone of downgrading with more accounts then allowed

4 Files to Patch (Reqiured)

/app/views/fragments/profile.fragment.php
/app/views/fragments/expired.fragment.php
/app/views/fragments/renew.fragment.php
/app/controllers/RenewController.php

Additional Files to Patch (for translation)

/app/locale/(Language-You-Need)/messages.po

Make a Backup of the Orginal Files!


Join Facebook Facebook Group "CodingMatters Nextpost" for more infos: https://www.facebook.com/groups/1545362275579082/


File profile.fragment.php in Line 228

From

<a class="small button" href="<?= APPURL."/renew" ?>"><?= __("Renew Account") ?></a>

To

<a class="small button" href="<?= APPURL."/renew" ?>"><?= __("Renew Account") ?></a>
<a class="small button" href="<?= APPURL."/renew?change=true" ?>"><?= __("Change Package") ?></a>

File expired.fragment.php in Line 34

From

<a class="small button" href="<?= APPURL."/renew" ?>"><?= __('Renew Account') ?></a>

To

<a class="small button" href="<?= APPURL."/renew" ?>"><?= __('Renew Account') ?></a>
<a class="small button" href="<?= APPURL."/renew?change=true" ?>"><?= __("Change Package") ?></a>

Add translation of "Change Package" to your messages.po

Example: Let's translate "Change Package" text to Russian in /app/locale/ru-RU/messages.po Just add new line with:

#: 
msgid "Change Package"
msgstr "Изменить тариф"

File renew.fragment.php in Line 201

From

<?php foreach ($Packages->getDataAs("Package") as $p): ?>

To

<?php foreach ($Packages->getDataAs("Package") as $p): ?>
<?php if(isset($PackageAvailable) && in_array($p, $PackageAvailable)){ ?>

File renew.fragment.php in Line 379

From

<?php endforeach; ?>

To

<?php } endforeach; ?>

File RenewController.php

Find function "public function process" and replace with:

public function process()
    {
        $AuthUser = $this->getVariable("AuthUser");
        $Route = $this->getVariable("Route");
        $EmailSettings = \Controller::model("GeneralData", "email-settings");

        if (!$AuthUser) {
            header("Location: ".APPURL."/login");
            exit;
        } else if (
            !$AuthUser->isAdmin() && 
            !$AuthUser->isEmailVerified() &&
            $EmailSettings->get("data.email_verification")) 
        {
            header("Location: ".APPURL."/profile?a=true");
            exit;
        }


        // Get active modules to be displayed in pricing table
        $Plugins = \Controller::model("Plugins");
        $Plugins->where("is_active", 1)
                ->whereIn("idname", [
                    "auto-follow", "auto-unfollow", "auto-like",
                    "auto-comment", "welcomedm", "auto-repost"
                ])->fetchData();

        $ActivePackage = Controller::model("Package", $AuthUser->get("package_id"));

        // Get Packages...
        $Packages = Controller::model("Packages");

        if(Input::get("package") >= 1){
            $SecurityAlert = true;
        }else{  
            $SecurityAlert = false;
        }

        if(Input::get("change") == true || Input::get("package") >= 1 || !$ActivePackage->get("is_public") ){
            $PackageAvailable = [];

            // Get Accounts...
            $Accounts = \Controller::model("Accounts");
            $Accounts->where("user_id", "=", $AuthUser->get("id"))
                     ->orderBy("id","DESC")
                     ->fetchData();

            $UserAccounts = count($Accounts->getDataAs("Account"));         

            $Packages->where("monthly_price", ">", 0)
                     ->orderBy("id","ASC")
                     ->fetchData();

            foreach ($Packages->getDataAs("Package") AS $p){                
                $PackageSettings = json_decode($p->get("settings"));                    
                if($PackageSettings->max_accounts >= $UserAccounts || $PackageSettings->max_accounts < 0){
                    if($p->get("is_public") == true || $AuthUser->get("package_id") == $p->get("id")){
                        array_push($PackageAvailable,$p);

                        if($SecurityAlert == true && $p->get("id") == Input::get("package"))
                        {
                            $SecurityAlert = false;
                        }
                    }
                }           
            }

        }else{      
            $Packages->where("is_public", "=", 1)
                     ->where("monthly_price", ">", 0)
                     ->orderBy("id","ASC")
                     ->fetchData();                 
        }

        // Security Check
        if($SecurityAlert){
            header("Location: ".APPURL);
            exit;
        }

        if($ActivePackage->isAvailable() && Input::get("package") >= 1){        
            $SelectedPackage = Controller::model("Package", Input::get("package"));     
        } else if ($ActivePackage->isAvailable()) {
            $SelectedPackage = $ActivePackage;
        } else {
            $SelectedPackage = Controller::model("Package", Input::get("package"));
        }

        if ($SelectedPackage->get("monthly_price") <= 0 || Input::get("change") == true) {          
            $SelectedPackage = Controller::model("Package"); 
        }



        // Set variables
        $this->setVariable("Packages", $Packages)
             ->setVariable("ActivePackage", $ActivePackage)
             ->setVariable("SelectedPackage", $SelectedPackage)
             ->setVariable("Plugins", $Plugins)
             ->setVariable("Settings", Controller::model("GeneralData", "settings"))
             ->setVariable("Integrations", Controller::model("GeneralData", "integrations"));

             if(isset($PackageAvailable)){
                $this->setVariable("PackageAvailable", $PackageAvailable);  
             }

        $this->checkRecurringPayments();

        if (Input::post("action") == "pay") {
            $this->pay();
        } 

        $this->view("renew");
    }

File RenewController.php

Find "pay" function and replace line:

if (!$SelectedPackage->get("is_public") || $SelectedPackage->get("monthly_price") <= 0) {

To

if (!$SelectedPackage->get("is_public") && $SelectedPackage->get("id") != $AuthUser->get("package_id") || $SelectedPackage->get("monthly_price") <= 0 ) {