Plugin Directory

Changeset 2782716


Ignore:
Timestamp:
09/10/2022 08:11:46 AM (3 years ago)
Author:
pmpr
Message:

fix some bugs

Location:
pmpr-ir
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • pmpr-ir/tags/1.2.1/src/Pmpr/Plugin/Pmpr/Ajax.php

    r2781213 r2782716  
    179179                            && $manager->update($name, $message, $version)) {
    180180
    181                             $success = $this->parseConsoleResult($job, $message, $component);
    182                             if ($success) {
    183 
    184                                 Manipulate::updateComponentData($name, (array)$fetched);
     181                            if ($success = $this->parseConsoleResult($job, $message, $component)) {
     182
     183                                Manipulate::updateComponentData($name, wp_parse_args($fetched, Manipulate::getComponentData($name)));
    185184                            }
    186185                        } else {
  • pmpr-ir/tags/1.2.1/src/Pmpr/Plugin/Pmpr/Component/Item.php

    r2781213 r2782716  
    1212class Item implements ConstantInterface
    1313{
    14     /**
    15      * @var array|object
    16      */
    17     protected $data = [];
    18 
    19     /**
    20      * @var int|string|null
    21      */
    22     protected $rating = 0;
    23 
    24     /**
    25      * @var int|mixed|string
    26      */
    27     protected $ratingCount = 0;
    28 
    29     /**
    30      * @var int|mixed|string
    31      */
    32     protected $activeInstall = 0;
    33 
    34     /**
    35      * @var string|null
    36      */
    37     protected ?string $state = 'coming_soon';
    38 
    39     /**
    40      * @var string|null
    41      */
    42     protected ?string $type = null;
    43 
    44     /**
    45      * @var string|null
    46      */
    47     protected ?string $name = null;
    48 
    49     /**
    50      * @var string|null
    51      */
    52     protected ?string $title = null;
    53 
    54     /**
    55      * @var string|null
    56      */
    57     protected ?string $image = null;
    58 
    59     /**
    60      * @var string|null
    61      */
    62     protected ?string $version = null;
    63 
    64     /**
    65      * @var string
    66      */
    67     protected string $price = '0';
    68 
    69     /**
    70      * @var array|object|null
    71      */
    72     protected $backlinkModal = [];
    73 
    74     /**
    75      * @var string|null
    76      */
    77     protected ?string $imageDate = null;
    78 
    79     /**
    80      * @var string|null
    81      */
    82     protected ?string $lastUpdate = null;
    83 
    84     /**
    85      * @var string|null
    86      */
    87     protected ?string $permalink = null;
    88 
    89     /**
    90      * @var bool
    91      */
    92     protected bool $freeVersion = false;
    93 
    94     /**
    95      * @var string|null
    96      */
    97     protected ?string $newVersion = null;
    98 
    99     /**
    100      * @var string|null
    101      */
    102     protected ?string $publishDue = null;
    103 
    104     /**
    105      * @var string|null
    106      */
    107     protected ?string $description = null;
    108 
    109     /**
    110      * Item constructor.
    111      *
    112      * @param $component
    113      */
    114     public function __construct($component)
    115     {
    116         if (is_array($component)) {
    117 
    118             $this->data = $component;
    119             foreach ($component as $key => $value) {
    120 
    121                 $key = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $key))));
    122                 if ($value && property_exists($this, $key)) {
    123 
    124                     $this->{$key} = $value;
    125                 }
    126             }
    127         }
    128     }
    129 
    130     /**
    131      * @return array|object
    132      */
    133     public function getData()
    134     {
    135         return $this->data;
    136     }
    137 
    138     /**
    139      * @return int|mixed|string
    140      */
    141     public function getActiveInstall()
    142     {
    143         return $this->activeInstall;
    144     }
    145 
    146     /**
    147      * @return string|null
    148      */
    149     public function getState(): ?string
    150     {
    151         return $this->state;
    152     }
    153 
    154     /**
    155      * @return int|string|null
    156      */
    157     public function getRating()
    158     {
    159         return $this->rating;
    160     }
    161 
    162     /**
    163      * @return string|null
    164      */
    165     public function getVersion(): ?string
    166     {
    167         return $this->version;
    168     }
    169 
    170     /**
    171      * @return string|null
    172      */
    173     public function getNewVersion(): ?string
    174     {
    175         return $this->newVersion;
    176     }
    177 
    178     /**
    179      * @return string|null
    180      */
    181     public function getPublishDue(): ?string
    182     {
    183         return $this->publishDue;
    184     }
    185 
    186     /**
    187      * @return int|mixed|string
    188      */
    189     public function getRatingCount()
    190     {
    191         return $this->ratingCount;
    192     }
    193 
    194     /**
    195      * @return string|null
    196      */
    197     public function getLastUpdate(): ?string
    198     {
    199         return $this->lastUpdate;
    200     }
    201 
    202     /**
    203      * @return string|null
    204      */
    205     public function getPermalink(): ?string
    206     {
    207         return $this->permalink;
    208     }
    209 
    210     /**
    211      * @return string|null
    212      */
    213     public function getImage(): ?string
    214     {
    215         return $this->image;
    216     }
    217 
    218     /**
    219      * @return string|null
    220      */
    221     public function getImageDate(): ?string
    222     {
    223         return $this->imageDate;
    224     }
    225 
    226     /**
    227      * @return string|null
    228      */
    229     public function getTitle(): ?string
    230     {
    231         return $this->title;
    232     }
    233 
    234     /**
    235      * @return string|null
    236      */
    237     public function getType(): ?string
    238     {
    239         return $this->type;
    240     }
    241 
    242     /**
    243      * @return string|null
    244      */
    245     public function getSlug(): ?string
    246     {
    247         $type = '';
    248         switch ($this->getType()) {
    249             case self::MODULE:
    250                 $type = self::MDL;
    251                 break;
    252             case self::CUSTOM:
    253                 $type = self::CST;
    254                 break;
    255             case self::COVER:
    256                 $type = self::CVR;
    257                 break;
    258             case self::COMMON:
    259                 $type = self::CMN;
    260                 break;
    261         }
    262 
    263         $name = str_replace("wp-{$type}-", '', $this->getName());
    264         return "pr__{$type}__" . str_replace('-', '_', $name);
    265     }
    266 
    267     /**
    268      * @return bool
    269      */
    270     public function isInstalled(): bool
    271     {
    272         return Manipulate::isInstalled($this->getName());
    273     }
    274 
    275     /**
    276      * @return bool
    277      */
    278     public function isPublished(): bool
    279     {
    280         return $this->getState() === 'publish';
    281     }
    282 
    283     /**
    284      * @return bool
    285      */
    286     public function isComingSoon(): bool
    287     {
    288         return $this->getState() === 'coming_soon';
    289     }
    290 
    291     /**
    292      * @return string|null
    293      */
    294     public function getName(): ?string
    295     {
    296         return $this->name;
    297     }
    298 
    299     /**
    300      * @return string|null
    301      */
    302     public function getDescription(): ?string
    303     {
    304         return $this->description;
    305     }
    306 
    307     /**
    308      * @return string
    309      */
    310     public function getImageURL(): string
    311     {
    312         return Manipulate::maybeSaveAsset($this->getImage(), 'img/component/', $this->getName(), "-{$this->getImageDate()}");
    313     }
    314 
    315     /**
    316      * @return bool
    317      */
    318     public function hasFreeVersion(): bool
    319     {
    320         return $this->freeVersion;
    321     }
    322 
    323     /**
    324      * @return string
    325      */
    326     public function getPrice(): string
    327     {
    328         return $this->price;
    329     }
    330 
    331     /**
    332      * @return array|object|null
    333      */
    334     public function getBacklinkModal()
    335     {
    336         return $this->backlinkModal;
    337     }
    338 
    339     /**
    340      * @return array
    341      */
    342     public function getActions(): array
    343     {
    344         $name = $this->getName();
    345         if (!Manipulate::isInstalled($name)) {
    346 
    347             if ($this->isComingSoon()) {
    348 
    349                 $actions[] = $this->createAction([
    350                     'enable'    => false,
    351                     self::TITLE => __('Coming Soon', PR__PLG__PMPR),
    352                 ]);
    353             } else {
    354 
    355                 if ($backlinkModal = $this->getBacklinkModal() ) {
    356 
    357                     $actions[] = Manipulate::createModal([
    358                         self::ID            => $this->getName() . '_modal',
    359                         self::NAME          => 'install-component',
    360                         self::TOGGLE_TEXT   => __('Install', PR__PLG__PMPR),
    361                         self::MODAL_TITLE   => Manipulate::getArrayItem($backlinkModal, self::TITLE),
    362                         self::TOGGLE_ATTRS  => [
    363                             'class' => 'button button-default',
    364                         ],
    365                         self::MODAL_CONTETN => wpautop(Manipulate::getArrayItem($backlinkModal, self::TEXT)),
    366                         self::MODAL_BUTTONS => [
    367                             'close'       => [
    368                                 self::TITLE => __('Cancel', PR__PLG__PMPR),
    369                             ],
    370                             self::INSTALL => $this->createAction([
    371                                 self::JOB   => self::INSTALL,
    372                                 self::TITLE => __('Accept & Install', PR__PLG__PMPR),
    373                                 self::ATTRS => [
    374                                     'rol'   => 'button',
    375                                     'class' => 'pmpr-modal-button button button-secondary show',
    376                                 ],
    377                             ]),
    378                         ],
    379                     ]);
    380                 } else {
    381 
    382                     $actions[] = $this->createAction([
    383                         self::JOB   => self::INSTALL,
    384                         self::TITLE => __('Install', PR__PLG__PMPR),
    385                     ]);
    386                 }
    387             }
    388         } else {
    389 
    390             if ($this->hasUpdate()) {
    391 
    392                 $actions[] = $this->createAction([
    393                     self::JOB   => 'update',
    394                     self::TITLE => __('Update', PR__PLG__PMPR),
    395                 ]);
    396 
    397             } else if (Manipulate::isActive($name)) {
    398 
    399                 $actions[] = $this->createAction([
    400                     'enable'    => false,
    401                     self::TITLE => __('Active', PR__PLG__PMPR),
    402                 ]);
    403             } else {
    404 
    405                 $actions[] = $this->createAction([
    406                     self::JOB   => 'activate',
    407                     self::TITLE => __('Activate', PR__PLG__PMPR),
    408                 ]);
    409             }
    410         }
    411 
    412         if ($this->isPublished()
    413             && ($permalink = $this->getPermalink())) {
    414 
    415             $actions[] = Manipulate::createElement('a', [
    416                 'href'       => $permalink,
    417                 'aria-label' => sprintf(__('More information about %s', PR__PLG__PMPR), $name),
    418                 'data-title' => $name,
    419                 'target'     => '_blank',
    420             ], __('More Details', PR__PLG__PMPR));
    421 
    422             if ($price = $this->getPrice()) {
    423 
    424                 $actions[] = Manipulate::createStrong($price, [
    425                     'class' => 'font-16' . ($this->hasFreeVersion() ? ' text-danger' : ''),
    426                 ]);
    427             }
    428         }
    429 
    430         return $actions;
    431     }
    432 
    433     /**
    434      * @param array $args
    435      *
    436      * @return string
    437      */
    438     public function createAction(array $args = []): string
    439     {
    440         $job    = Manipulate::getArrayItem($args, self::JOB);
    441         $title  = Manipulate::getArrayItem($args, self::TITLE);
    442         $enable = Manipulate::getArrayItem($args, 'enable', true);
    443 
    444         $args = wp_parse_args($args, [
    445             self::VER   => $this->getVersion(),
    446             self::JOB   => $job,
    447             self::NAME  => $this->getName(),
    448             self::SLUG  => $this->getSlug(),
    449             self::TYPE  => $this->getType(),
    450             self::TITLE => $title,
    451             self::ATTRS => [
    452                 'rol'   => 'button',
    453                 'class' => 'button button-default' . ($enable ? '' : ' disabled'),
    454             ],
    455         ]);
    456 
    457         return Manipulate::createAction($args);
    458     }
    459 
    460     /**
    461      * @return bool
    462      */
    463     public function hasUpdate(): bool
    464     {
    465         $newVersion = $this->getNewVersion();
    466         // TODO: it's can get error, if data cleared by user and new data fetched
    467         return $newVersion && version_compare($newVersion, $this->getVersion(), '>');
    468     }
     14    /**
     15     * @var array|object
     16     */
     17    protected $data = [];
     18
     19    /**
     20     * @var int|string|null
     21     */
     22    protected $rating = 0;
     23
     24    /**
     25     * @var int|mixed|string
     26     */
     27    protected $ratingCount = 0;
     28
     29    /**
     30     * @var int|mixed|string
     31     */
     32    protected $activeInstall = 0;
     33
     34    /**
     35     * @var string|null
     36     */
     37    protected ?string $state = 'coming_soon';
     38
     39    /**
     40     * @var string|null
     41     */
     42    protected ?string $type = null;
     43
     44    /**
     45     * @var string|null
     46     */
     47    protected ?string $name = null;
     48
     49    /**
     50     * @var string|null
     51     */
     52    protected ?string $title = null;
     53
     54    /**
     55     * @var string|null
     56     */
     57    protected ?string $image = null;
     58
     59    /**
     60     * @var string|null
     61     */
     62    protected ?string $version = null;
     63
     64    /**
     65     * @var string
     66     */
     67    protected string $price = '0';
     68
     69    /**
     70     * @var array|object|null
     71     */
     72    protected $backlinkModal = [];
     73
     74    /**
     75     * @var string|null
     76     */
     77    protected ?string $imageDate = null;
     78
     79    /**
     80     * @var string|null
     81     */
     82    protected ?string $lastUpdate = null;
     83
     84    /**
     85     * @var string|null
     86     */
     87    protected ?string $permalink = null;
     88
     89    /**
     90     * @var bool
     91     */
     92    protected bool $freeVersion = false;
     93
     94    /**
     95     * @var string|null
     96     */
     97    protected ?string $newVersion = null;
     98
     99    /**
     100     * @var string|null
     101     */
     102    protected ?string $publishDue = null;
     103
     104    /**
     105     * @var string|null
     106     */
     107    protected ?string $description = null;
     108
     109    /**
     110     * Item constructor.
     111     *
     112     * @param $component
     113     */
     114    public function __construct($component)
     115    {
     116        if (is_array($component)) {
     117
     118            $this->data = $component;
     119            foreach ($component as $key => $value) {
     120
     121                $key = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $key))));
     122                if ($value && property_exists($this, $key)) {
     123
     124                    $this->{$key} = $value;
     125                }
     126            }
     127        }
     128    }
     129
     130    /**
     131     * @return array|object
     132     */
     133    public function getData()
     134    {
     135        return $this->data;
     136    }
     137
     138    /**
     139     * @return int|mixed|string
     140     */
     141    public function getActiveInstall()
     142    {
     143        return $this->activeInstall;
     144    }
     145
     146    /**
     147     * @return string|null
     148     */
     149    public function getState(): ?string
     150    {
     151        return $this->state;
     152    }
     153
     154    /**
     155     * @return int|string|null
     156     */
     157    public function getRating()
     158    {
     159        return $this->rating;
     160    }
     161
     162    /**
     163     * @return string|null
     164     */
     165    public function getVersion(): ?string
     166    {
     167        return $this->version;
     168    }
     169
     170    /**
     171     * @return string|null
     172     */
     173    public function getNewVersion(): ?string
     174    {
     175        return $this->newVersion;
     176    }
     177
     178    /**
     179     * @return string|null
     180     */
     181    public function getPublishDue(): ?string
     182    {
     183        return $this->publishDue;
     184    }
     185
     186    /**
     187     * @return int|mixed|string
     188     */
     189    public function getRatingCount()
     190    {
     191        return $this->ratingCount;
     192    }
     193
     194    /**
     195     * @return string|null
     196     */
     197    public function getLastUpdate(): ?string
     198    {
     199        return $this->lastUpdate;
     200    }
     201
     202    /**
     203     * @return string|null
     204     */
     205    public function getPermalink(): ?string
     206    {
     207        return $this->permalink;
     208    }
     209
     210    /**
     211     * @return string|null
     212     */
     213    public function getImage(): ?string
     214    {
     215        return $this->image;
     216    }
     217
     218    /**
     219     * @return string|null
     220     */
     221    public function getImageDate(): ?string
     222    {
     223        return $this->imageDate;
     224    }
     225
     226    /**
     227     * @return string|null
     228     */
     229    public function getTitle(): ?string
     230    {
     231        return $this->title;
     232    }
     233
     234    /**
     235     * @return string|null
     236     */
     237    public function getType(): ?string
     238    {
     239        return $this->type;
     240    }
     241
     242    /**
     243     * @return string|null
     244     */
     245    public function getSlug(): ?string
     246    {
     247        $type = '';
     248        switch ($this->getType()) {
     249            case self::MODULE:
     250                $type = self::MDL;
     251                break;
     252            case self::CUSTOM:
     253                $type = self::CST;
     254                break;
     255            case self::COVER:
     256                $type = self::CVR;
     257                break;
     258            case self::COMMON:
     259                $type = self::CMN;
     260                break;
     261        }
     262
     263        $name = str_replace("wp-{$type}-", '', $this->getName());
     264        return "pr__{$type}__" . str_replace('-', '_', $name);
     265    }
     266
     267    /**
     268     * @return bool
     269     */
     270    public function isInstalled(): bool
     271    {
     272        return Manipulate::isInstalled($this->getName());
     273    }
     274
     275    /**
     276     * @return bool
     277     */
     278    public function isPublished(): bool
     279    {
     280        return $this->getState() === 'publish';
     281    }
     282
     283    /**
     284     * @return bool
     285     */
     286    public function isComingSoon(): bool
     287    {
     288        return $this->getState() === 'coming_soon';
     289    }
     290
     291    /**
     292     * @return string|null
     293     */
     294    public function getName(): ?string
     295    {
     296        return $this->name;
     297    }
     298
     299    /**
     300     * @return string|null
     301     */
     302    public function getDescription(): ?string
     303    {
     304        return $this->description;
     305    }
     306
     307    /**
     308     * @return string
     309     */
     310    public function getImageURL(): string
     311    {
     312        return Manipulate::maybeSaveAsset($this->getImage(), 'img/component/', $this->getName(), "-{$this->getImageDate()}");
     313    }
     314
     315    /**
     316     * @return bool
     317     */
     318    public function hasFreeVersion(): bool
     319    {
     320        return $this->freeVersion;
     321    }
     322
     323    /**
     324     * @return string
     325     */
     326    public function getPrice(): string
     327    {
     328        return $this->price;
     329    }
     330
     331    /**
     332     * @return array|object|null
     333     */
     334    public function getBacklinkModal()
     335    {
     336        return $this->backlinkModal;
     337    }
     338
     339    /**
     340     * @return array
     341     */
     342    public function getActions(): array
     343    {
     344        $name = $this->getName();
     345        if (!Manipulate::isInstalled($name)) {
     346
     347            if ($this->isComingSoon()) {
     348
     349                $actions[] = $this->createAction([
     350                    'enable'    => false,
     351                    self::TITLE => __('Coming Soon', PR__PLG__PMPR),
     352                ]);
     353            } else {
     354
     355                if ($backlinkModal = $this->getBacklinkModal()) {
     356
     357                    $actions[] = Manipulate::createModal([
     358                        self::ID            => $this->getName() . '_modal',
     359                        self::NAME          => 'install-component',
     360                        self::TOGGLE_TEXT   => __('Install', PR__PLG__PMPR),
     361                        self::MODAL_TITLE   => Manipulate::getArrayItem($backlinkModal, self::TITLE),
     362                        self::TOGGLE_ATTRS  => [
     363                            'class' => 'button button-default',
     364                        ],
     365                        self::MODAL_CONTETN => wpautop(Manipulate::getArrayItem($backlinkModal, self::TEXT)),
     366                        self::MODAL_BUTTONS => [
     367                            'close'       => [
     368                                self::TITLE => __('Cancel', PR__PLG__PMPR),
     369                            ],
     370                            self::INSTALL => $this->createAction([
     371                                self::JOB   => self::INSTALL,
     372                                self::TITLE => __('Accept & Install', PR__PLG__PMPR),
     373                                self::ATTRS => [
     374                                    'rol'   => 'button',
     375                                    'class' => 'pmpr-modal-button button button-secondary show',
     376                                ],
     377                            ]),
     378                        ],
     379                    ]);
     380                } else {
     381
     382                    $actions[] = $this->createAction([
     383                        self::JOB   => self::INSTALL,
     384                        self::TITLE => __('Install', PR__PLG__PMPR),
     385                    ]);
     386                }
     387            }
     388        } else {
     389
     390            if ($this->hasUpdate()) {
     391
     392                $actions[] = $this->createAction([
     393                    self::JOB   => 'update',
     394                    self::TITLE => __('Update', PR__PLG__PMPR),
     395                ]);
     396
     397            } else if (Manipulate::isActive($name)) {
     398
     399                $actions[] = $this->createAction([
     400                    'enable'    => false,
     401                    self::TITLE => __('Active', PR__PLG__PMPR),
     402                ]);
     403            } else {
     404
     405                $actions[] = $this->createAction([
     406                    self::JOB   => 'activate',
     407                    self::TITLE => __('Activate', PR__PLG__PMPR),
     408                ]);
     409            }
     410        }
     411
     412        if ($this->isPublished()
     413            && ($permalink = $this->getPermalink())) {
     414
     415            $actions[] = Manipulate::createElement('a', [
     416                'href'       => $permalink,
     417                'aria-label' => sprintf(__('More information about %s', PR__PLG__PMPR), $name),
     418                'data-title' => $name,
     419                'target'     => '_blank',
     420            ], __('More Details', PR__PLG__PMPR));
     421
     422            if ($price = $this->getPrice()) {
     423
     424                $actions[] = Manipulate::createStrong($price, [
     425                    'class' => 'font-16' . ($this->hasFreeVersion() ? ' text-danger' : ''),
     426                ]);
     427            }
     428        }
     429
     430        return $actions;
     431    }
     432
     433    /**
     434     * @param array $args
     435     *
     436     * @return string
     437     */
     438    public function createAction(array $args = []): string
     439    {
     440        $job    = Manipulate::getArrayItem($args, self::JOB);
     441        $title  = Manipulate::getArrayItem($args, self::TITLE);
     442        $enable = Manipulate::getArrayItem($args, 'enable', true);
     443
     444        $args = wp_parse_args($args, [
     445            self::VER   => $this->getVersion(),
     446            self::JOB   => $job,
     447            self::NAME  => $this->getName(),
     448            self::SLUG  => $this->getSlug(),
     449            self::TYPE  => $this->getType(),
     450            self::TITLE => $title,
     451            self::ATTRS => [
     452                'rol'   => 'button',
     453                'class' => 'button button-default' . ($enable ? '' : ' disabled'),
     454            ],
     455        ]);
     456
     457        return Manipulate::createAction($args);
     458    }
     459
     460    /**
     461     * @return bool
     462     */
     463    public function hasUpdate(): bool
     464    {
     465        $has        = false;
     466        $version    = $this->getVersion();
     467        $newVersion = $this->getNewVersion();
     468        if ($version !== $newVersion) {
     469
     470            // TODO: it's can get error, if data cleared by user and new data fetched
     471            $has = $newVersion && version_compare($newVersion, $version, '>');
     472        }
     473        return $has;
     474    }
    469475}
  • pmpr-ir/trunk/src/Pmpr/Plugin/Pmpr/Ajax.php

    r2781204 r2782716  
    179179                            && $manager->update($name, $message, $version)) {
    180180
    181                             $success = $this->parseConsoleResult($job, $message, $component);
    182                             if ($success) {
    183 
    184                                 Manipulate::updateComponentData($name, (array)$fetched);
     181                            if ($success = $this->parseConsoleResult($job, $message, $component)) {
     182
     183                                Manipulate::updateComponentData($name, wp_parse_args($fetched, Manipulate::getComponentData($name)));
    185184                            }
    186185                        } else {
  • pmpr-ir/trunk/src/Pmpr/Plugin/Pmpr/Component/Item.php

    r2757352 r2782716  
    1212class Item implements ConstantInterface
    1313{
    14     /**
    15      * @var array|object
    16      */
    17     protected $data = [];
    18 
    19     /**
    20      * @var int|string|null
    21      */
    22     protected $rating = 0;
    23 
    24     /**
    25      * @var int|mixed|string
    26      */
    27     protected $ratingCount = 0;
    28 
    29     /**
    30      * @var int|mixed|string
    31      */
    32     protected $activeInstall = 0;
    33 
    34     /**
    35      * @var string|null
    36      */
    37     protected ?string $state = 'coming_soon';
    38 
    39     /**
    40      * @var string|null
    41      */
    42     protected ?string $type = null;
    43 
    44     /**
    45      * @var string|null
    46      */
    47     protected ?string $name = null;
    48 
    49     /**
    50      * @var string|null
    51      */
    52     protected ?string $title = null;
    53 
    54     /**
    55      * @var string|null
    56      */
    57     protected ?string $image = null;
    58 
    59     /**
    60      * @var string|null
    61      */
    62     protected ?string $version = null;
    63 
    64     /**
    65      * @var string
    66      */
    67     protected string $price = '0';
    68 
    69     /**
    70      * @var array|object|null
    71      */
    72     protected $backlinkModal = [];
    73 
    74     /**
    75      * @var string|null
    76      */
    77     protected ?string $imageDate = null;
    78 
    79     /**
    80      * @var string|null
    81      */
    82     protected ?string $lastUpdate = null;
    83 
    84     /**
    85      * @var string|null
    86      */
    87     protected ?string $permalink = null;
    88 
    89     /**
    90      * @var bool
    91      */
    92     protected bool $freeVersion = false;
    93 
    94     /**
    95      * @var string|null
    96      */
    97     protected ?string $newVersion = null;
    98 
    99     /**
    100      * @var string|null
    101      */
    102     protected ?string $publishDue = null;
    103 
    104     /**
    105      * @var string|null
    106      */
    107     protected ?string $description = null;
    108 
    109     /**
    110      * Item constructor.
    111      *
    112      * @param $component
    113      */
    114     public function __construct($component)
    115     {
    116         if (is_array($component)) {
    117 
    118             $this->data = $component;
    119             foreach ($component as $key => $value) {
    120 
    121                 $key = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $key))));
    122                 if ($value && property_exists($this, $key)) {
    123 
    124                     $this->{$key} = $value;
    125                 }
    126             }
    127         }
    128     }
    129 
    130     /**
    131      * @return array|object
    132      */
    133     public function getData()
    134     {
    135         return $this->data;
    136     }
    137 
    138     /**
    139      * @return int|mixed|string
    140      */
    141     public function getActiveInstall()
    142     {
    143         return $this->activeInstall;
    144     }
    145 
    146     /**
    147      * @return string|null
    148      */
    149     public function getState(): ?string
    150     {
    151         return $this->state;
    152     }
    153 
    154     /**
    155      * @return int|string|null
    156      */
    157     public function getRating()
    158     {
    159         return $this->rating;
    160     }
    161 
    162     /**
    163      * @return string|null
    164      */
    165     public function getVersion(): ?string
    166     {
    167         return $this->version;
    168     }
    169 
    170     /**
    171      * @return string|null
    172      */
    173     public function getNewVersion(): ?string
    174     {
    175         return $this->newVersion;
    176     }
    177 
    178     /**
    179      * @return string|null
    180      */
    181     public function getPublishDue(): ?string
    182     {
    183         return $this->publishDue;
    184     }
    185 
    186     /**
    187      * @return int|mixed|string
    188      */
    189     public function getRatingCount()
    190     {
    191         return $this->ratingCount;
    192     }
    193 
    194     /**
    195      * @return string|null
    196      */
    197     public function getLastUpdate(): ?string
    198     {
    199         return $this->lastUpdate;
    200     }
    201 
    202     /**
    203      * @return string|null
    204      */
    205     public function getPermalink(): ?string
    206     {
    207         return $this->permalink;
    208     }
    209 
    210     /**
    211      * @return string|null
    212      */
    213     public function getImage(): ?string
    214     {
    215         return $this->image;
    216     }
    217 
    218     /**
    219      * @return string|null
    220      */
    221     public function getImageDate(): ?string
    222     {
    223         return $this->imageDate;
    224     }
    225 
    226     /**
    227      * @return string|null
    228      */
    229     public function getTitle(): ?string
    230     {
    231         return $this->title;
    232     }
    233 
    234     /**
    235      * @return string|null
    236      */
    237     public function getType(): ?string
    238     {
    239         return $this->type;
    240     }
    241 
    242     /**
    243      * @return string|null
    244      */
    245     public function getSlug(): ?string
    246     {
    247         $type = '';
    248         switch ($this->getType()) {
    249             case self::MODULE:
    250                 $type = self::MDL;
    251                 break;
    252             case self::CUSTOM:
    253                 $type = self::CST;
    254                 break;
    255             case self::COVER:
    256                 $type = self::CVR;
    257                 break;
    258             case self::COMMON:
    259                 $type = self::CMN;
    260                 break;
    261         }
    262 
    263         $name = str_replace("wp-{$type}-", '', $this->getName());
    264         return "pr__{$type}__" . str_replace('-', '_', $name);
    265     }
    266 
    267     /**
    268      * @return bool
    269      */
    270     public function isInstalled(): bool
    271     {
    272         return Manipulate::isInstalled($this->getName());
    273     }
    274 
    275     /**
    276      * @return bool
    277      */
    278     public function isPublished(): bool
    279     {
    280         return $this->getState() === 'publish';
    281     }
    282 
    283     /**
    284      * @return bool
    285      */
    286     public function isComingSoon(): bool
    287     {
    288         return $this->getState() === 'coming_soon';
    289     }
    290 
    291     /**
    292      * @return string|null
    293      */
    294     public function getName(): ?string
    295     {
    296         return $this->name;
    297     }
    298 
    299     /**
    300      * @return string|null
    301      */
    302     public function getDescription(): ?string
    303     {
    304         return $this->description;
    305     }
    306 
    307     /**
    308      * @return string
    309      */
    310     public function getImageURL(): string
    311     {
    312         return Manipulate::maybeSaveAsset($this->getImage(), 'img/component/', $this->getName(), "-{$this->getImageDate()}");
    313     }
    314 
    315     /**
    316      * @return bool
    317      */
    318     public function hasFreeVersion(): bool
    319     {
    320         return $this->freeVersion;
    321     }
    322 
    323     /**
    324      * @return string
    325      */
    326     public function getPrice(): string
    327     {
    328         return $this->price;
    329     }
    330 
    331     /**
    332      * @return array|object|null
    333      */
    334     public function getBacklinkModal()
    335     {
    336         return $this->backlinkModal;
    337     }
    338 
    339     /**
    340      * @return array
    341      */
    342     public function getActions(): array
    343     {
    344         $name = $this->getName();
    345         if (!Manipulate::isInstalled($name)) {
    346 
    347             if ($this->isComingSoon()) {
    348 
    349                 $actions[] = $this->createAction([
    350                     'enable'    => false,
    351                     self::TITLE => __('Coming Soon', PR__PLG__PMPR),
    352                 ]);
    353             } else {
    354 
    355                 if ($backlinkModal = $this->getBacklinkModal() ) {
    356 
    357                     $actions[] = Manipulate::createModal([
    358                         self::ID            => $this->getName() . '_modal',
    359                         self::NAME          => 'install-component',
    360                         self::TOGGLE_TEXT   => __('Install', PR__PLG__PMPR),
    361                         self::MODAL_TITLE   => Manipulate::getArrayItem($backlinkModal, self::TITLE),
    362                         self::TOGGLE_ATTRS  => [
    363                             'class' => 'button button-default',
    364                         ],
    365                         self::MODAL_CONTETN => wpautop(Manipulate::getArrayItem($backlinkModal, self::TEXT)),
    366                         self::MODAL_BUTTONS => [
    367                             'close'       => [
    368                                 self::TITLE => __('Cancel', PR__PLG__PMPR),
    369                             ],
    370                             self::INSTALL => $this->createAction([
    371                                 self::JOB   => self::INSTALL,
    372                                 self::TITLE => __('Accept & Install', PR__PLG__PMPR),
    373                                 self::ATTRS => [
    374                                     'rol'   => 'button',
    375                                     'class' => 'pmpr-modal-button button button-secondary show',
    376                                 ],
    377                             ]),
    378                         ],
    379                     ]);
    380                 } else {
    381 
    382                     $actions[] = $this->createAction([
    383                         self::JOB   => self::INSTALL,
    384                         self::TITLE => __('Install', PR__PLG__PMPR),
    385                     ]);
    386                 }
    387             }
    388         } else {
    389 
    390             if ($this->hasUpdate()) {
    391 
    392                 $actions[] = $this->createAction([
    393                     self::JOB   => 'update',
    394                     self::TITLE => __('Update', PR__PLG__PMPR),
    395                 ]);
    396 
    397             } else if (Manipulate::isActive($name)) {
    398 
    399                 $actions[] = $this->createAction([
    400                     'enable'    => false,
    401                     self::TITLE => __('Active', PR__PLG__PMPR),
    402                 ]);
    403             } else {
    404 
    405                 $actions[] = $this->createAction([
    406                     self::JOB   => 'activate',
    407                     self::TITLE => __('Activate', PR__PLG__PMPR),
    408                 ]);
    409             }
    410         }
    411 
    412         if ($this->isPublished()
    413             && ($permalink = $this->getPermalink())) {
    414 
    415             $actions[] = Manipulate::createElement('a', [
    416                 'href'       => $permalink,
    417                 'aria-label' => sprintf(__('More information about %s', PR__PLG__PMPR), $name),
    418                 'data-title' => $name,
    419                 'target'     => '_blank',
    420             ], __('More Details', PR__PLG__PMPR));
    421 
    422             if ($price = $this->getPrice()) {
    423 
    424                 $actions[] = Manipulate::createStrong($price, [
    425                     'class' => 'font-16' . ($this->hasFreeVersion() ? ' text-danger' : ''),
    426                 ]);
    427             }
    428         }
    429 
    430         return $actions;
    431     }
    432 
    433     /**
    434      * @param array $args
    435      *
    436      * @return string
    437      */
    438     public function createAction(array $args = []): string
    439     {
    440         $job    = Manipulate::getArrayItem($args, self::JOB);
    441         $title  = Manipulate::getArrayItem($args, self::TITLE);
    442         $enable = Manipulate::getArrayItem($args, 'enable', true);
    443 
    444         $args = wp_parse_args($args, [
    445             self::VER   => $this->getVersion(),
    446             self::JOB   => $job,
    447             self::NAME  => $this->getName(),
    448             self::SLUG  => $this->getSlug(),
    449             self::TYPE  => $this->getType(),
    450             self::TITLE => $title,
    451             self::ATTRS => [
    452                 'rol'   => 'button',
    453                 'class' => 'button button-default' . ($enable ? '' : ' disabled'),
    454             ],
    455         ]);
    456 
    457         return Manipulate::createAction($args);
    458     }
    459 
    460     /**
    461      * @return bool
    462      */
    463     public function hasUpdate(): bool
    464     {
    465         $newVersion = $this->getNewVersion();
    466         // TODO: it's can get error, if data cleared by user and new data fetched
    467         return $newVersion && version_compare($newVersion, $this->getVersion(), '>');
    468     }
     14    /**
     15     * @var array|object
     16     */
     17    protected $data = [];
     18
     19    /**
     20     * @var int|string|null
     21     */
     22    protected $rating = 0;
     23
     24    /**
     25     * @var int|mixed|string
     26     */
     27    protected $ratingCount = 0;
     28
     29    /**
     30     * @var int|mixed|string
     31     */
     32    protected $activeInstall = 0;
     33
     34    /**
     35     * @var string|null
     36     */
     37    protected ?string $state = 'coming_soon';
     38
     39    /**
     40     * @var string|null
     41     */
     42    protected ?string $type = null;
     43
     44    /**
     45     * @var string|null
     46     */
     47    protected ?string $name = null;
     48
     49    /**
     50     * @var string|null
     51     */
     52    protected ?string $title = null;
     53
     54    /**
     55     * @var string|null
     56     */
     57    protected ?string $image = null;
     58
     59    /**
     60     * @var string|null
     61     */
     62    protected ?string $version = null;
     63
     64    /**
     65     * @var string
     66     */
     67    protected string $price = '0';
     68
     69    /**
     70     * @var array|object|null
     71     */
     72    protected $backlinkModal = [];
     73
     74    /**
     75     * @var string|null
     76     */
     77    protected ?string $imageDate = null;
     78
     79    /**
     80     * @var string|null
     81     */
     82    protected ?string $lastUpdate = null;
     83
     84    /**
     85     * @var string|null
     86     */
     87    protected ?string $permalink = null;
     88
     89    /**
     90     * @var bool
     91     */
     92    protected bool $freeVersion = false;
     93
     94    /**
     95     * @var string|null
     96     */
     97    protected ?string $newVersion = null;
     98
     99    /**
     100     * @var string|null
     101     */
     102    protected ?string $publishDue = null;
     103
     104    /**
     105     * @var string|null
     106     */
     107    protected ?string $description = null;
     108
     109    /**
     110     * Item constructor.
     111     *
     112     * @param $component
     113     */
     114    public function __construct($component)
     115    {
     116        if (is_array($component)) {
     117
     118            $this->data = $component;
     119            foreach ($component as $key => $value) {
     120
     121                $key = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $key))));
     122                if ($value && property_exists($this, $key)) {
     123
     124                    $this->{$key} = $value;
     125                }
     126            }
     127        }
     128    }
     129
     130    /**
     131     * @return array|object
     132     */
     133    public function getData()
     134    {
     135        return $this->data;
     136    }
     137
     138    /**
     139     * @return int|mixed|string
     140     */
     141    public function getActiveInstall()
     142    {
     143        return $this->activeInstall;
     144    }
     145
     146    /**
     147     * @return string|null
     148     */
     149    public function getState(): ?string
     150    {
     151        return $this->state;
     152    }
     153
     154    /**
     155     * @return int|string|null
     156     */
     157    public function getRating()
     158    {
     159        return $this->rating;
     160    }
     161
     162    /**
     163     * @return string|null
     164     */
     165    public function getVersion(): ?string
     166    {
     167        return $this->version;
     168    }
     169
     170    /**
     171     * @return string|null
     172     */
     173    public function getNewVersion(): ?string
     174    {
     175        return $this->newVersion;
     176    }
     177
     178    /**
     179     * @return string|null
     180     */
     181    public function getPublishDue(): ?string
     182    {
     183        return $this->publishDue;
     184    }
     185
     186    /**
     187     * @return int|mixed|string
     188     */
     189    public function getRatingCount()
     190    {
     191        return $this->ratingCount;
     192    }
     193
     194    /**
     195     * @return string|null
     196     */
     197    public function getLastUpdate(): ?string
     198    {
     199        return $this->lastUpdate;
     200    }
     201
     202    /**
     203     * @return string|null
     204     */
     205    public function getPermalink(): ?string
     206    {
     207        return $this->permalink;
     208    }
     209
     210    /**
     211     * @return string|null
     212     */
     213    public function getImage(): ?string
     214    {
     215        return $this->image;
     216    }
     217
     218    /**
     219     * @return string|null
     220     */
     221    public function getImageDate(): ?string
     222    {
     223        return $this->imageDate;
     224    }
     225
     226    /**
     227     * @return string|null
     228     */
     229    public function getTitle(): ?string
     230    {
     231        return $this->title;
     232    }
     233
     234    /**
     235     * @return string|null
     236     */
     237    public function getType(): ?string
     238    {
     239        return $this->type;
     240    }
     241
     242    /**
     243     * @return string|null
     244     */
     245    public function getSlug(): ?string
     246    {
     247        $type = '';
     248        switch ($this->getType()) {
     249            case self::MODULE:
     250                $type = self::MDL;
     251                break;
     252            case self::CUSTOM:
     253                $type = self::CST;
     254                break;
     255            case self::COVER:
     256                $type = self::CVR;
     257                break;
     258            case self::COMMON:
     259                $type = self::CMN;
     260                break;
     261        }
     262
     263        $name = str_replace("wp-{$type}-", '', $this->getName());
     264        return "pr__{$type}__" . str_replace('-', '_', $name);
     265    }
     266
     267    /**
     268     * @return bool
     269     */
     270    public function isInstalled(): bool
     271    {
     272        return Manipulate::isInstalled($this->getName());
     273    }
     274
     275    /**
     276     * @return bool
     277     */
     278    public function isPublished(): bool
     279    {
     280        return $this->getState() === 'publish';
     281    }
     282
     283    /**
     284     * @return bool
     285     */
     286    public function isComingSoon(): bool
     287    {
     288        return $this->getState() === 'coming_soon';
     289    }
     290
     291    /**
     292     * @return string|null
     293     */
     294    public function getName(): ?string
     295    {
     296        return $this->name;
     297    }
     298
     299    /**
     300     * @return string|null
     301     */
     302    public function getDescription(): ?string
     303    {
     304        return $this->description;
     305    }
     306
     307    /**
     308     * @return string
     309     */
     310    public function getImageURL(): string
     311    {
     312        return Manipulate::maybeSaveAsset($this->getImage(), 'img/component/', $this->getName(), "-{$this->getImageDate()}");
     313    }
     314
     315    /**
     316     * @return bool
     317     */
     318    public function hasFreeVersion(): bool
     319    {
     320        return $this->freeVersion;
     321    }
     322
     323    /**
     324     * @return string
     325     */
     326    public function getPrice(): string
     327    {
     328        return $this->price;
     329    }
     330
     331    /**
     332     * @return array|object|null
     333     */
     334    public function getBacklinkModal()
     335    {
     336        return $this->backlinkModal;
     337    }
     338
     339    /**
     340     * @return array
     341     */
     342    public function getActions(): array
     343    {
     344        $name = $this->getName();
     345        if (!Manipulate::isInstalled($name)) {
     346
     347            if ($this->isComingSoon()) {
     348
     349                $actions[] = $this->createAction([
     350                    'enable'    => false,
     351                    self::TITLE => __('Coming Soon', PR__PLG__PMPR),
     352                ]);
     353            } else {
     354
     355                if ($backlinkModal = $this->getBacklinkModal()) {
     356
     357                    $actions[] = Manipulate::createModal([
     358                        self::ID            => $this->getName() . '_modal',
     359                        self::NAME          => 'install-component',
     360                        self::TOGGLE_TEXT   => __('Install', PR__PLG__PMPR),
     361                        self::MODAL_TITLE   => Manipulate::getArrayItem($backlinkModal, self::TITLE),
     362                        self::TOGGLE_ATTRS  => [
     363                            'class' => 'button button-default',
     364                        ],
     365                        self::MODAL_CONTETN => wpautop(Manipulate::getArrayItem($backlinkModal, self::TEXT)),
     366                        self::MODAL_BUTTONS => [
     367                            'close'       => [
     368                                self::TITLE => __('Cancel', PR__PLG__PMPR),
     369                            ],
     370                            self::INSTALL => $this->createAction([
     371                                self::JOB   => self::INSTALL,
     372                                self::TITLE => __('Accept & Install', PR__PLG__PMPR),
     373                                self::ATTRS => [
     374                                    'rol'   => 'button',
     375                                    'class' => 'pmpr-modal-button button button-secondary show',
     376                                ],
     377                            ]),
     378                        ],
     379                    ]);
     380                } else {
     381
     382                    $actions[] = $this->createAction([
     383                        self::JOB   => self::INSTALL,
     384                        self::TITLE => __('Install', PR__PLG__PMPR),
     385                    ]);
     386                }
     387            }
     388        } else {
     389
     390            if ($this->hasUpdate()) {
     391
     392                $actions[] = $this->createAction([
     393                    self::JOB   => 'update',
     394                    self::TITLE => __('Update', PR__PLG__PMPR),
     395                ]);
     396
     397            } else if (Manipulate::isActive($name)) {
     398
     399                $actions[] = $this->createAction([
     400                    'enable'    => false,
     401                    self::TITLE => __('Active', PR__PLG__PMPR),
     402                ]);
     403            } else {
     404
     405                $actions[] = $this->createAction([
     406                    self::JOB   => 'activate',
     407                    self::TITLE => __('Activate', PR__PLG__PMPR),
     408                ]);
     409            }
     410        }
     411
     412        if ($this->isPublished()
     413            && ($permalink = $this->getPermalink())) {
     414
     415            $actions[] = Manipulate::createElement('a', [
     416                'href'       => $permalink,
     417                'aria-label' => sprintf(__('More information about %s', PR__PLG__PMPR), $name),
     418                'data-title' => $name,
     419                'target'     => '_blank',
     420            ], __('More Details', PR__PLG__PMPR));
     421
     422            if ($price = $this->getPrice()) {
     423
     424                $actions[] = Manipulate::createStrong($price, [
     425                    'class' => 'font-16' . ($this->hasFreeVersion() ? ' text-danger' : ''),
     426                ]);
     427            }
     428        }
     429
     430        return $actions;
     431    }
     432
     433    /**
     434     * @param array $args
     435     *
     436     * @return string
     437     */
     438    public function createAction(array $args = []): string
     439    {
     440        $job    = Manipulate::getArrayItem($args, self::JOB);
     441        $title  = Manipulate::getArrayItem($args, self::TITLE);
     442        $enable = Manipulate::getArrayItem($args, 'enable', true);
     443
     444        $args = wp_parse_args($args, [
     445            self::VER   => $this->getVersion(),
     446            self::JOB   => $job,
     447            self::NAME  => $this->getName(),
     448            self::SLUG  => $this->getSlug(),
     449            self::TYPE  => $this->getType(),
     450            self::TITLE => $title,
     451            self::ATTRS => [
     452                'rol'   => 'button',
     453                'class' => 'button button-default' . ($enable ? '' : ' disabled'),
     454            ],
     455        ]);
     456
     457        return Manipulate::createAction($args);
     458    }
     459
     460    /**
     461     * @return bool
     462     */
     463    public function hasUpdate(): bool
     464    {
     465        $has        = false;
     466        $version    = $this->getVersion();
     467        $newVersion = $this->getNewVersion();
     468        if ($version !== $newVersion) {
     469
     470            // TODO: it's can get error, if data cleared by user and new data fetched
     471            $has = $newVersion && version_compare($newVersion, $version, '>');
     472        }
     473        return $has;
     474    }
    469475}
Note: See TracChangeset for help on using the changeset viewer.