diff --git a/.circleci/config.yml b/.circleci/config.yml index 2d8f38a76e..43f18b010b 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -68,7 +68,7 @@ jobs: name: "Set up environment" command: | source ./.circleci/setup.bash && setup php7 - wget https://github.com/phpstan/phpstan/releases/download/0.11.4/phpstan.phar + wget https://github.com/phpstan/phpstan/releases/download/0.11.5/phpstan.phar - run: name: "Static analysis" command: ./do qa:phpstan diff --git a/lib/API/JSON/v1/CustomFields.php b/lib/API/JSON/v1/CustomFields.php index 3ec3377b82..f660cd4c4b 100644 --- a/lib/API/JSON/v1/CustomFields.php +++ b/lib/API/JSON/v1/CustomFields.php @@ -27,14 +27,14 @@ class CustomFields extends APIEndpoint { function delete($data = array()) { $id = (isset($data['id']) ? (int)$data['id'] : null); $custom_field = CustomField::findOne($id); - if ($custom_field === false) { - return $this->errorResponse(array( - APIError::NOT_FOUND => WPFunctions::get()->__('This custom field does not exist.', 'mailpoet') - )); - } else { + if ($custom_field instanceof CustomField) { $custom_field->delete(); return $this->successResponse($custom_field->asArray()); + } else { + return $this->errorResponse(array( + APIError::NOT_FOUND => WPFunctions::get()->__('This custom field does not exist.', 'mailpoet') + )); } } @@ -44,22 +44,20 @@ class CustomFields extends APIEndpoint { if (!empty($errors)) { return $this->badRequest($errors); - } else { - return $this->successResponse( - CustomField::findOne($custom_field->id)->asArray() - ); } + $custom_field = CustomField::findOne($custom_field->id); + if(!$custom_field instanceof CustomField) return $this->errorResponse(); + return $this->successResponse($custom_field->asArray()); } function get($data = array()) { $id = (isset($data['id']) ? (int)$data['id'] : null); $custom_field = CustomField::findOne($id); - if ($custom_field === false) { - return $this->errorResponse(array( - APIError::NOT_FOUND => WPFunctions::get()->__('This custom field does not exist.', 'mailpoet') - )); - } else { + if ($custom_field instanceof CustomField) { return $this->successResponse($custom_field->asArray()); } + return $this->errorResponse(array( + APIError::NOT_FOUND => WPFunctions::get()->__('This custom field does not exist.', 'mailpoet') + )); } } diff --git a/lib/API/JSON/v1/Forms.php b/lib/API/JSON/v1/Forms.php index daaddb7e51..adacab98c2 100644 --- a/lib/API/JSON/v1/Forms.php +++ b/lib/API/JSON/v1/Forms.php @@ -37,13 +37,12 @@ class Forms extends APIEndpoint { function get($data = array()) { $id = (isset($data['id']) ? (int)$data['id'] : false); $form = Form::findOne($id); - if ($form === false) { - return $this->errorResponse(array( - APIError::NOT_FOUND => WPFunctions::get()->__('This form does not exist.', 'mailpoet') - )); - } else { + if ($form instanceof Form) { return $this->successResponse($form->asArray()); } + return $this->errorResponse(array( + APIError::NOT_FOUND => WPFunctions::get()->__('This form does not exist.', 'mailpoet') + )); } function listing($data = array()) { @@ -111,13 +110,12 @@ class Forms extends APIEndpoint { $form = Form::createOrUpdate($data); $errors = $form->getErrors(); - if (!empty($errors)) { - return $this->badRequest($errors); - } else { - return $this->successResponse( - Form::findOne($form->id)->asArray() - ); + if (empty($errors)) { + $form = Form::findOne($form->id); + if(!$form instanceof Form) return $this->errorResponse(); + return $this->successResponse($form->asArray()); } + return $this->badRequest($errors); } function previewEditor($data = array()) { @@ -139,19 +137,16 @@ class Forms extends APIEndpoint { function exportsEditor($data = array()) { $id = (isset($data['id']) ? (int)$data['id'] : false); $form = Form::findOne($id); - if ($form === false) { - return $this->errorResponse(array( - APIError::NOT_FOUND => WPFunctions::get()->__('This form does not exist.', 'mailpoet') - )); - } else { + if ($form instanceof Form) { $exports = Util\Export::getAll($form->asArray()); return $this->successResponse($exports); } + return $this->errorResponse(array( + APIError::NOT_FOUND => WPFunctions::get()->__('This form does not exist.', 'mailpoet') + )); } function saveEditor($data = array()) { - $id = (isset($data['id']) ? (int)$data['id'] : false); - $form_id = (isset($data['id']) ? (int)$data['id'] : 0); $name = (isset($data['name']) ? $data['name'] : WPFunctions::get()->__('New form', 'mailpoet')); $body = (isset($data['body']) ? $data['body'] : array()); @@ -211,56 +206,62 @@ class Forms extends APIEndpoint { if (!empty($errors)) { return $this->badRequest($errors); - } else { - return $this->successResponse( - Form::findOne($form->id)->asArray(), - array('is_widget' => $is_widget) - ); } + $form = Form::findOne($form->id); + if(!$form instanceof Form) return $this->errorResponse(); + return $this->successResponse( + $form->asArray(), + array('is_widget' => $is_widget) + ); } function restore($data = array()) { $id = (isset($data['id']) ? (int)$data['id'] : false); $form = Form::findOne($id); - if ($form === false) { + if ($form instanceof Form) { + $form->restore(); + $form = Form::findOne($form->id); + if(!$form instanceof Form) return $this->errorResponse(); + return $this->successResponse( + $form->asArray(), + array('count' => 1) + ); + } else { return $this->errorResponse(array( APIError::NOT_FOUND => WPFunctions::get()->__('This form does not exist.', 'mailpoet') )); - } else { - $form->restore(); - return $this->successResponse( - Form::findOne($form->id)->asArray(), - array('count' => 1) - ); } } function trash($data = array()) { $id = (isset($data['id']) ? (int)$data['id'] : false); $form = Form::findOne($id); - if ($form === false) { + if ($form instanceof Form) { + $form->trash(); + $form = Form::findOne($form->id); + if(!$form instanceof Form) return $this->errorResponse(); + return $this->successResponse( + $form->asArray(), + array('count' => 1) + ); + } else { return $this->errorResponse(array( APIError::NOT_FOUND => WPFunctions::get()->__('This form does not exist.', 'mailpoet') )); - } else { - $form->trash(); - return $this->successResponse( - Form::findOne($form->id)->asArray(), - array('count' => 1) - ); } } function delete($data = array()) { $id = (isset($data['id']) ? (int)$data['id'] : false); $form = Form::findOne($id); - if ($form === false) { + if ($form instanceof Form) { + $form->delete(); + + return $this->successResponse(null, array('count' => 1)); + } else { return $this->errorResponse(array( APIError::NOT_FOUND => WPFunctions::get()->__('This form does not exist.', 'mailpoet') )); - } else { - $form->delete(); - return $this->successResponse(null, array('count' => 1)); } } @@ -268,11 +269,7 @@ class Forms extends APIEndpoint { $id = (isset($data['id']) ? (int)$data['id'] : false); $form = Form::findOne($id); - if ($form === false) { - return $this->errorResponse(array( - APIError::NOT_FOUND => WPFunctions::get()->__('This form does not exist.', 'mailpoet') - )); - } else { + if ($form instanceof Form) { $data = array( 'name' => sprintf(__('Copy of %s', 'mailpoet'), $form->name) ); @@ -282,11 +279,17 @@ class Forms extends APIEndpoint { if (!empty($errors)) { return $this->errorResponse($errors); } else { + $duplicate = Form::findOne($duplicate->id); + if(!$duplicate instanceof Form) return $this->errorResponse(); return $this->successResponse( - Form::findOne($duplicate->id)->asArray(), + $duplicate->asArray(), array('count' => 1) ); } + } else { + return $this->errorResponse(array( + APIError::NOT_FOUND => WPFunctions::get()->__('This form does not exist.', 'mailpoet') + )); } } diff --git a/lib/API/JSON/v1/ImportExport.php b/lib/API/JSON/v1/ImportExport.php index 40ee0aeb38..76a5ef16ab 100644 --- a/lib/API/JSON/v1/ImportExport.php +++ b/lib/API/JSON/v1/ImportExport.php @@ -45,9 +45,9 @@ class ImportExport extends APIEndpoint { if (!empty($errors)) { return $this->errorResponse($errors); } else { - return $this->successResponse( - Segment::findOne($segment->id)->asArray() - ); + $segment = Segment::findOne($segment->id); + if(!$segment instanceof Segment) return $this->errorResponse(); + return $this->successResponse($segment->asArray()); } } diff --git a/lib/API/JSON/v1/NewsletterTemplates.php b/lib/API/JSON/v1/NewsletterTemplates.php index 1fd2176744..76afa7f806 100644 --- a/lib/API/JSON/v1/NewsletterTemplates.php +++ b/lib/API/JSON/v1/NewsletterTemplates.php @@ -18,14 +18,14 @@ class NewsletterTemplates extends APIEndpoint { function get($data = array()) { $id = (isset($data['id']) ? (int)$data['id'] : false); $template = NewsletterTemplate::findOne($id); - if ($template === false) { - return $this->errorResponse(array( - APIError::NOT_FOUND => WPFunctions::get()->__('This template does not exist.', 'mailpoet') - )); - } else { + if ($template instanceof NewsletterTemplate) { return $this->successResponse( $template->asArray() ); + } else { + return $this->errorResponse(array( + APIError::NOT_FOUND => WPFunctions::get()->__('This template does not exist.', 'mailpoet') + )); } } @@ -47,7 +47,7 @@ class NewsletterTemplates extends APIEndpoint { ignore_user_abort(true); if (!empty($data['newsletter_id'])) { $template = NewsletterTemplate::whereEqual('newsletter_id', $data['newsletter_id'])->findOne(); - if (!empty($template)) { + if ($template instanceof NewsletterTemplate) { $data['id'] = $template->id; } } @@ -60,8 +60,10 @@ class NewsletterTemplates extends APIEndpoint { if (!empty($errors)) { return $this->errorResponse($errors); } else { + $template = NewsletterTemplate::findOne($template->id); + if(!$template instanceof NewsletterTemplate) return $this->errorResponse(); return $this->successResponse( - NewsletterTemplate::findOne($template->id)->asArray() + $template->asArray() ); } } @@ -69,13 +71,13 @@ class NewsletterTemplates extends APIEndpoint { function delete($data = array()) { $id = (isset($data['id']) ? (int)$data['id'] : false); $template = NewsletterTemplate::findOne($id); - if ($template === false) { + if ($template instanceof NewsletterTemplate) { + $template->delete(); + return $this->successResponse(null, array('count' => 1)); + } else { return $this->errorResponse(array( APIError::NOT_FOUND => WPFunctions::get()->__('This template does not exist.', 'mailpoet') )); - } else { - $template->delete(); - return $this->successResponse(null, array('count' => 1)); } } } diff --git a/lib/API/JSON/v1/Newsletters.php b/lib/API/JSON/v1/Newsletters.php index c7fa71608e..5391f580e4 100644 --- a/lib/API/JSON/v1/Newsletters.php +++ b/lib/API/JSON/v1/Newsletters.php @@ -58,11 +58,7 @@ class Newsletters extends APIEndpoint { function get($data = array()) { $id = (isset($data['id']) ? (int)$data['id'] : false); $newsletter = Newsletter::findOne($id); - if ($newsletter === false) { - return $this->errorResponse(array( - APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet') - )); - } else { + if ($newsletter instanceof Newsletter) { $newsletter = $newsletter ->withSegments() ->withOptions() @@ -76,6 +72,10 @@ class Newsletters extends APIEndpoint { $newsletter = $this->wp->applyFilters('mailpoet_api_newsletters_get_after', $newsletter->asArray()); return $this->successResponse($newsletter, ['preview_url' => $preview_url]); + } else { + return $this->errorResponse(array( + APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet') + )); } } @@ -96,7 +96,7 @@ class Newsletters extends APIEndpoint { if (!empty($data['template_id'])) { $template = NewsletterTemplate::whereEqual('id', $data['template_id'])->findOne(); - if (!empty($template)) { + if ($template instanceof NewsletterTemplate) { $template = $template->asArray(); $data['body'] = $template['body']; } @@ -110,6 +110,7 @@ class Newsletters extends APIEndpoint { // Re-fetch newsletter to sync changes made by DB // updated_at column use CURRENT_TIMESTAMP for update and this change is not updated automatically by ORM $newsletter = Newsletter::findOne($newsletter->id); + if(!$newsletter instanceof Newsletter) return $this->errorResponse(); if (!empty($segments)) { NewsletterSegment::where('newsletter_id', $newsletter->id) @@ -146,6 +147,7 @@ class Newsletters extends APIEndpoint { } // reload newsletter with updated options $newsletter = Newsletter::filter('filterWithOptions', $newsletter->type)->findOne($newsletter->id); + if(!$newsletter instanceof Newsletter) return $this->errorResponse(); // if this is a post notification, process newsletter options and update its schedule if ($newsletter->type === Newsletter::TYPE_NOTIFICATION) { // generate the new schedule from options and get the new "next run" date @@ -226,54 +228,62 @@ class Newsletters extends APIEndpoint { Scheduler::createPostNotificationSendingTask($newsletter); } - + $newsletter = Newsletter::findOne($newsletter->id); + if(!$newsletter instanceof Newsletter) return $this->errorResponse(); return $this->successResponse( - Newsletter::findOne($newsletter->id)->asArray() + $newsletter->asArray() ); } function restore($data = array()) { $id = (isset($data['id']) ? (int)$data['id'] : false); $newsletter = Newsletter::findOne($id); - if ($newsletter === false) { + if ($newsletter instanceof Newsletter) { + $newsletter->restore(); + + $newsletter = Newsletter::findOne($newsletter->id); + if(!$newsletter instanceof Newsletter) return $this->errorResponse(); + + return $this->successResponse( + $newsletter->asArray(), + array('count' => 1) + ); + } else { return $this->errorResponse(array( APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet') )); - } else { - $newsletter->restore(); - return $this->successResponse( - Newsletter::findOne($newsletter->id)->asArray(), - array('count' => 1) - ); } } function trash($data = array()) { $id = (isset($data['id']) ? (int)$data['id'] : false); $newsletter = Newsletter::findOne($id); - if ($newsletter === false) { + if ($newsletter instanceof Newsletter) { + $newsletter->trash(); + + $newsletter = Newsletter::findOne($newsletter->id); + if(!$newsletter instanceof Newsletter) return $this->errorResponse(); + return $this->successResponse( + $newsletter->asArray(), + array('count' => 1) + ); + } else { return $this->errorResponse(array( APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet') )); - } else { - $newsletter->trash(); - return $this->successResponse( - Newsletter::findOne($newsletter->id)->asArray(), - array('count' => 1) - ); } } function delete($data = array()) { $id = (isset($data['id']) ? (int)$data['id'] : false); $newsletter = Newsletter::findOne($id); - if ($newsletter === false) { + if ($newsletter instanceof Newsletter) { + $newsletter->delete(); + return $this->successResponse(null, array('count' => 1)); + } else { return $this->errorResponse(array( APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet') )); - } else { - $newsletter->delete(); - return $this->successResponse(null, array('count' => 1)); } } @@ -281,11 +291,7 @@ class Newsletters extends APIEndpoint { $id = (isset($data['id']) ? (int)$data['id'] : false); $newsletter = Newsletter::findOne($id); - if ($newsletter === false) { - return $this->errorResponse(array( - APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet') - )); - } else { + if ($newsletter instanceof Newsletter) { $data = array( 'subject' => sprintf(__('Copy of %s', 'mailpoet'), $newsletter->subject) ); @@ -296,11 +302,17 @@ class Newsletters extends APIEndpoint { return $this->errorResponse($errors); } else { $this->wp->doAction('mailpoet_api_newsletters_duplicate_after', $newsletter, $duplicate); + $duplicate = Newsletter::findOne($duplicate->id); + if(!$duplicate instanceof Newsletter) return $this->errorResponse(); return $this->successResponse( - Newsletter::findOne($duplicate->id)->asArray(), + $duplicate->asArray(), array('count' => 1) ); } + } else { + return $this->errorResponse(array( + APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet') + )); } } @@ -314,11 +326,7 @@ class Newsletters extends APIEndpoint { $id = (isset($data['id'])) ? (int)$data['id'] : false; $newsletter = Newsletter::findOne($id); - if ($newsletter === false) { - return $this->errorResponse(array( - APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet') - )); - } else { + if ($newsletter instanceof Newsletter) { $newsletter->body = $data['body']; $newsletter->save(); $subscriber = Subscriber::getCurrentWPUser(); @@ -330,10 +338,16 @@ class Newsletters extends APIEndpoint { // strip protocol to avoid mix content error $preview_url = preg_replace('{^https?:}i', '', $preview_url); + $newsletter = Newsletter::findOne($newsletter->id); + if(!$newsletter instanceof Newsletter) return $this->errorResponse(); return $this->successResponse( - Newsletter::findOne($newsletter->id)->asArray(), + $newsletter->asArray(), array('preview_url' => $preview_url) ); + } else { + return $this->errorResponse(array( + APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet') + )); } } @@ -347,11 +361,7 @@ class Newsletters extends APIEndpoint { $id = (isset($data['id'])) ? (int)$data['id'] : false; $newsletter = Newsletter::findOne($id); - if ($newsletter === false) { - return $this->errorResponse(array( - APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet') - )); - } else { + if ($newsletter instanceof Newsletter) { $renderer = new Renderer($newsletter, $preview = true); $rendered_newsletter = $renderer->render(); $divider = '***MailPoet***'; @@ -376,7 +386,7 @@ class Newsletters extends APIEndpoint { $rendered_newsletter['subject'], $rendered_newsletter['body']['html'], $rendered_newsletter['body']['text'] - ) = explode($divider, $shortcodes->replace($body)); + ) = explode($divider, $shortcodes->replace($body)); $rendered_newsletter['id'] = $newsletter->id; try { @@ -386,7 +396,7 @@ class Newsletters extends APIEndpoint { $mailer = false, $sender = false, $reply_to = false - ); + ); $extra_params = array('unsubscribe_url' => WPFunctions::get()->homeUrl()); $result = $mailer->send($rendered_newsletter, $data['subscriber'], $extra_params); @@ -397,8 +407,11 @@ class Newsletters extends APIEndpoint { ); return $this->errorResponse(array(APIError::BAD_REQUEST => $error)); } else { + $newsletter = Newsletter::findOne($newsletter->id); + if(!$newsletter instanceof Newsletter) return $this->errorResponse(); + return $this->successResponse( - Newsletter::findOne($id)->asArray() + $newsletter->asArray() ); } } catch (\Exception $e) { @@ -406,6 +419,10 @@ class Newsletters extends APIEndpoint { $e->getCode() => $e->getMessage() )); } + } else { + return $this->errorResponse(array( + APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet') + )); } } @@ -494,10 +511,10 @@ class Newsletters extends APIEndpoint { // try to load template data $template_id = (isset($data['template']) ? (int)$data['template'] : false); $template = NewsletterTemplate::findOne($template_id); - if ($template === false) { - $newsletter->body = array(); - } else { + if ($template instanceof NewsletterTemplate) { $newsletter->body = $template->body; + } else { + $newsletter->body = array(); } } @@ -533,8 +550,10 @@ class Newsletters extends APIEndpoint { Scheduler::processPostNotificationSchedule($newsletter); } + $newsletter = Newsletter::findOne($newsletter->id); + if(!$newsletter instanceof Newsletter) return $this->errorResponse(); return $this->successResponse( - Newsletter::findOne($newsletter->id)->asArray() + $newsletter->asArray() ); } } diff --git a/lib/API/JSON/v1/Segments.php b/lib/API/JSON/v1/Segments.php index d4350cff84..4bbd63d481 100644 --- a/lib/API/JSON/v1/Segments.php +++ b/lib/API/JSON/v1/Segments.php @@ -40,12 +40,12 @@ class Segments extends APIEndpoint { function get($data = array()) { $id = (isset($data['id']) ? (int)$data['id'] : false); $segment = Segment::findOne($id); - if ($segment === false) { + if ($segment instanceof Segment) { + return $this->successResponse($segment->asArray()); + } else { return $this->errorResponse(array( APIError::NOT_FOUND => WPFunctions::get()->__('This list does not exist.', 'mailpoet') )); - } else { - return $this->successResponse($segment->asArray()); } } @@ -77,8 +77,10 @@ class Segments extends APIEndpoint { if (!empty($errors)) { return $this->badRequest($errors); } else { + $segment = Segment::findOne($segment->id); + if(!$segment instanceof Segment) return $this->errorResponse(); return $this->successResponse( - Segment::findOne($segment->id)->asArray() + $segment->asArray() ); } } @@ -86,45 +88,49 @@ class Segments extends APIEndpoint { function restore($data = array()) { $id = (isset($data['id']) ? (int)$data['id'] : false); $segment = Segment::findOne($id); - if ($segment === false) { + if ($segment instanceof Segment) { + $segment->restore(); + $segment = Segment::findOne($segment->id); + if(!$segment instanceof Segment) return $this->errorResponse(); + return $this->successResponse( + $segment->asArray(), + array('count' => 1) + ); + } else { return $this->errorResponse(array( APIError::NOT_FOUND => WPFunctions::get()->__('This list does not exist.', 'mailpoet') )); - } else { - $segment->restore(); - return $this->successResponse( - Segment::findOne($segment->id)->asArray(), - array('count' => 1) - ); } } function trash($data = array()) { $id = (isset($data['id']) ? (int)$data['id'] : false); $segment = Segment::findOne($id); - if ($segment === false) { + if ($segment instanceof Segment) { + $segment->trash(); + $segment = Segment::findOne($segment->id); + if(!$segment instanceof Segment) return $this->errorResponse(); + return $this->successResponse( + $segment->asArray(), + array('count' => 1) + ); + } else { return $this->errorResponse(array( APIError::NOT_FOUND => WPFunctions::get()->__('This list does not exist.', 'mailpoet') )); - } else { - $segment->trash(); - return $this->successResponse( - Segment::findOne($segment->id)->asArray(), - array('count' => 1) - ); } } function delete($data = array()) { $id = (isset($data['id']) ? (int)$data['id'] : false); $segment = Segment::findOne($id); - if ($segment === false) { + if ($segment instanceof Segment) { + $segment->delete(); + return $this->successResponse(null, array('count' => 1)); + } else { return $this->errorResponse(array( APIError::NOT_FOUND => WPFunctions::get()->__('This list does not exist.', 'mailpoet') )); - } else { - $segment->delete(); - return $this->successResponse(null, array('count' => 1)); } } @@ -132,11 +138,7 @@ class Segments extends APIEndpoint { $id = (isset($data['id']) ? (int)$data['id'] : false); $segment = Segment::findOne($id); - if ($segment === false) { - return $this->errorResponse(array( - APIError::NOT_FOUND => WPFunctions::get()->__('This list does not exist.', 'mailpoet') - )); - } else { + if ($segment instanceof Segment) { $data = array( 'name' => sprintf(__('Copy of %s', 'mailpoet'), $segment->name) ); @@ -146,11 +148,17 @@ class Segments extends APIEndpoint { if (!empty($errors)) { return $this->errorResponse($errors); } else { + $duplicate = Segment::findOne($duplicate->id); + if(!$duplicate instanceof Segment) return $this->errorResponse(); return $this->successResponse( - Segment::findOne($duplicate->id)->asArray(), + $duplicate->asArray(), array('count' => 1) ); } + } else { + return $this->errorResponse(array( + APIError::NOT_FOUND => WPFunctions::get()->__('This list does not exist.', 'mailpoet') + )); } } diff --git a/lib/API/JSON/v1/SendingQueue.php b/lib/API/JSON/v1/SendingQueue.php index 25660e6753..83e49a7448 100644 --- a/lib/API/JSON/v1/SendingQueue.php +++ b/lib/API/JSON/v1/SendingQueue.php @@ -28,7 +28,7 @@ class SendingQueue extends APIEndpoint { // check that the newsletter exists $newsletter = Newsletter::findOneWithOptions($newsletter_id); - if ($newsletter === false) { + if (!$newsletter instanceof Newsletter) { return $this->errorResponse(array( APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet') )); @@ -59,7 +59,7 @@ class SendingQueue extends APIEndpoint { ->where('queues.newsletter_id', $newsletter->id) ->where('tasks.status', SendingQueueModel::STATUS_SCHEDULED) ->findOne(); - if ($scheduled_queue) { + if ($scheduled_queue instanceof SendingQueueModel) { $queue = SendingTask::createFromQueue($scheduled_queue); } else { $queue = SendingTask::create(); @@ -108,11 +108,7 @@ class SendingQueue extends APIEndpoint { ); $newsletter = Newsletter::findOne($newsletter_id); - if ($newsletter === false) { - return $this->errorResponse(array( - APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet') - )); - } else { + if ($newsletter instanceof Newsletter) { $queue = $newsletter->getQueue(); if ($queue === false) { @@ -125,6 +121,10 @@ class SendingQueue extends APIEndpoint { $newsletter->getQueue()->asArray() ); } + } else { + return $this->errorResponse(array( + APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet') + )); } } @@ -134,11 +134,7 @@ class SendingQueue extends APIEndpoint { : false ); $newsletter = Newsletter::findOne($newsletter_id); - if ($newsletter === false) { - return $this->errorResponse(array( - APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet') - )); - } else { + if ($newsletter instanceof Newsletter) { $queue = $newsletter->getQueue(); if ($queue === false) { @@ -151,6 +147,10 @@ class SendingQueue extends APIEndpoint { $newsletter->getQueue()->asArray() ); } + } else { + return $this->errorResponse(array( + APIError::NOT_FOUND => WPFunctions::get()->__('This newsletter does not exist.', 'mailpoet') + )); } } } diff --git a/lib/API/JSON/v1/Subscribers.php b/lib/API/JSON/v1/Subscribers.php index ce2fc71a67..88dad9282f 100644 --- a/lib/API/JSON/v1/Subscribers.php +++ b/lib/API/JSON/v1/Subscribers.php @@ -120,7 +120,7 @@ class Subscribers extends APIEndpoint { $recaptcha = $this->settings->get('re_captcha'); - if (!$form) { + if (!$form instanceof Form) { return $this->badRequest(array( APIError::BAD_REQUEST => WPFunctions::get()->__('Please specify a valid form ID.', 'mailpoet') )); @@ -266,45 +266,49 @@ class Subscribers extends APIEndpoint { function restore($data = array()) { $id = (isset($data['id']) ? (int)$data['id'] : false); $subscriber = Subscriber::findOne($id); - if ($subscriber === false) { + if ($subscriber instanceof Subscriber) { + $subscriber->restore(); + $subscriber = Subscriber::findOne($subscriber->id); + if(!$subscriber instanceof Subscriber) return $this->errorResponse(); + return $this->successResponse( + $subscriber->asArray(), + array('count' => 1) + ); + } else { return $this->errorResponse(array( APIError::NOT_FOUND => WPFunctions::get()->__('This subscriber does not exist.', 'mailpoet') )); - } else { - $subscriber->restore(); - return $this->successResponse( - Subscriber::findOne($subscriber->id)->asArray(), - array('count' => 1) - ); } } function trash($data = array()) { $id = (isset($data['id']) ? (int)$data['id'] : false); $subscriber = Subscriber::findOne($id); - if ($subscriber === false) { + if ($subscriber instanceof Subscriber) { + $subscriber->trash(); + $subscriber = Subscriber::findOne($subscriber->id); + if(!$subscriber instanceof Subscriber) return $this->errorResponse(); + return $this->successResponse( + $subscriber->asArray(), + array('count' => 1) + ); + } else { return $this->errorResponse(array( APIError::NOT_FOUND => WPFunctions::get()->__('This subscriber does not exist.', 'mailpoet') )); - } else { - $subscriber->trash(); - return $this->successResponse( - Subscriber::findOne($subscriber->id)->asArray(), - array('count' => 1) - ); } } function delete($data = array()) { $id = (isset($data['id']) ? (int)$data['id'] : false); $subscriber = Subscriber::findOne($id); - if ($subscriber === false) { + if ($subscriber instanceof Subscriber) { + $subscriber->delete(); + return $this->successResponse(null, array('count' => 1)); + } else { return $this->errorResponse(array( APIError::NOT_FOUND => WPFunctions::get()->__('This subscriber does not exist.', 'mailpoet') )); - } else { - $subscriber->delete(); - return $this->successResponse(null, array('count' => 1)); } } diff --git a/lib/API/MP/v1/API.php b/lib/API/MP/v1/API.php index ed231ddb88..dec16a37b4 100644 --- a/lib/API/MP/v1/API.php +++ b/lib/API/MP/v1/API.php @@ -268,6 +268,9 @@ class API { // reload list to get the saved created|updated|delete dates/other fields $new_list = Segment::findOne($new_list->id); + if (!$new_list instanceof Segment) { + throw new \Exception(WPFunctions::get()->__('Failed to add list', 'mailpoet')); + } return $new_list->asArray(); } diff --git a/lib/Config/Capabilities.php b/lib/Config/Capabilities.php index 7dde2d9623..3fd047bc65 100644 --- a/lib/Config/Capabilities.php +++ b/lib/Config/Capabilities.php @@ -2,6 +2,7 @@ namespace MailPoet\Config; use MailPoet\WP\Functions as WPFunctions; +use WP_Role; class Capabilities { const MEMBERS_CAP_GROUP_NAME = 'mailpoet'; @@ -35,7 +36,7 @@ class Capabilities { if (!isset($role_objects[$role])) { $role_objects[$role] = WPFunctions::get()->getRole($role); } - if (!is_object($role_objects[$role])) continue; + if (!$role_objects[$role] instanceof WP_Role) continue; $role_objects[$role]->add_cap($name); } } @@ -49,7 +50,7 @@ class Capabilities { if (!isset($role_objects[$role])) { $role_objects[$role] = WPFunctions::get()->getRole($role); } - if (!is_object($role_objects[$role])) continue; + if (!$role_objects[$role] instanceof WP_Role) continue; $role_objects[$role]->remove_cap($name); } } diff --git a/lib/Config/Menu.php b/lib/Config/Menu.php index d55a004184..4bd4381c65 100644 --- a/lib/Config/Menu.php +++ b/lib/Config/Menu.php @@ -748,7 +748,7 @@ class Menu { function formEditor() { $id = (isset($_GET['id']) ? (int)$_GET['id'] : 0); $form = Form::findOne($id); - if ($form !== false) { + if ($form instanceof Form) { $form = $form->asArray(); } diff --git a/lib/Config/Renderer.php b/lib/Config/Renderer.php index 6907147775..a7f2f47c29 100644 --- a/lib/Config/Renderer.php +++ b/lib/Config/Renderer.php @@ -121,9 +121,13 @@ class Renderer { } function getAssetManifest($manifest_file) { - return (is_readable($manifest_file)) ? - json_decode(file_get_contents($manifest_file), true) : - false; + if (is_readable($manifest_file)) { + $contents = file_get_contents($manifest_file); + if (is_string($contents)) { + return json_decode($contents, true); + } + } + return false; } function getJsAsset($asset) { diff --git a/lib/Config/RequirementsChecker.php b/lib/Config/RequirementsChecker.php index 80908c58d6..69cd1e621a 100644 --- a/lib/Config/RequirementsChecker.php +++ b/lib/Config/RequirementsChecker.php @@ -47,7 +47,10 @@ class RequirementsChecker { ); $results = array(); foreach ($available_tests as $test) { - $results[$test] = call_user_func(array($this, 'check' . $test)); + $callback = [$this, 'check' . $test]; + if (is_callable($callback)) { + $results[$test] = call_user_func($callback); + } } return $results; } diff --git a/lib/Config/Updater.php b/lib/Config/Updater.php index ccb3b71296..0444830492 100644 --- a/lib/Config/Updater.php +++ b/lib/Config/Updater.php @@ -28,9 +28,9 @@ class Updater { function init() { WPFunctions::get()->addFilter('pre_set_site_transient_update_plugins', array($this, 'checkForUpdate')); } - + function checkForUpdate($update_transient) { - if (!is_object($update_transient)) { + if (!$update_transient instanceof \stdClass) { $update_transient = new \stdClass; } diff --git a/lib/Cron/Workers/SendingQueue/Tasks/Newsletter.php b/lib/Cron/Workers/SendingQueue/Tasks/Newsletter.php index f80c6de797..251926a326 100644 --- a/lib/Cron/Workers/SendingQueue/Tasks/Newsletter.php +++ b/lib/Cron/Workers/SendingQueue/Tasks/Newsletter.php @@ -121,7 +121,9 @@ class Newsletter { if (!$queue_errors) { // verify that the rendered body was successfully saved $queue = SendingQueueModel::findOne($queue->id); - $queue_errors = ($queue->validate() !== true); + if ($queue instanceof SendingQueueModel) { + $queue_errors = ($queue->validate() !== true); + } } if ($queue_errors) { $this->stopNewsletterPreProcessing(sprintf('QUEUE-%d-SAVE', $queue->id)); diff --git a/lib/Cron/Workers/StatsNotifications/Worker.php b/lib/Cron/Workers/StatsNotifications/Worker.php index 2efe00e48c..3ebab65895 100644 --- a/lib/Cron/Workers/StatsNotifications/Worker.php +++ b/lib/Cron/Workers/StatsNotifications/Worker.php @@ -9,6 +9,7 @@ use MailPoet\Mailer\Mailer; use MailPoet\Models\Newsletter; use MailPoet\Models\NewsletterLink; use MailPoet\Models\ScheduledTask; +use MailPoet\Models\StatsNotification; use MailPoet\Settings\SettingsController; use MailPoet\Tasks\Sending; use MailPoet\WP\Functions as WPFunctions; @@ -81,8 +82,11 @@ class Worker { private function getNewsletter(ScheduledTask $task) { $statsNotificationModel = $task->statsNotification()->findOne(); + if (!$statsNotificationModel instanceof StatsNotification) { + throw new \Exception('Newsletter not found'); + } $newsletter = $statsNotificationModel->newsletter()->findOne(); - if (!$newsletter) { + if (!$newsletter instanceof Newsletter) { throw new \Exception('Newsletter not found'); } return $newsletter diff --git a/lib/Listing/BulkActionController.php b/lib/Listing/BulkActionController.php index 6b7166d6d4..1875b9026b 100644 --- a/lib/Listing/BulkActionController.php +++ b/lib/Listing/BulkActionController.php @@ -20,10 +20,13 @@ class BulkActionController { unset($data['action']); $action_class = $this->factory->getActionClass($model_class, $bulk_action_method); + $callback = [$action_class, $bulk_action_method]; - return call_user_func_array( - array($action_class, $bulk_action_method), - array($this->handler->getSelection($model_class, $data['listing']), $data) - ); + if (is_callable($callback)) { + return call_user_func_array( + $callback, + array($this->handler->getSelection($model_class, $data['listing']), $data) + ); + } } } diff --git a/lib/Models/Model.php b/lib/Models/Model.php index c7c7c8e2e8..cc5a588b84 100644 --- a/lib/Models/Model.php +++ b/lib/Models/Model.php @@ -22,8 +22,8 @@ if (!defined('ABSPATH')) exit; * @method $this useIdColumn($id_column) * @method $this|bool findOne($id=null) * @method static static|bool findOne($id=null) - * @method array|\IdiormResultSet findMany() - * @method static array|\IdiormResultSet findMany() + * @method array findMany() + * @method static array findMany() * @method \IdiormResultSet findResultSet() * @method array findArray() * @method static array findArray() @@ -162,7 +162,7 @@ class Model extends \Sudzy\ValidModel { } if ($model === false) { - if (!empty($onCreate)) { + if (!empty($onCreate) && is_callable($onCreate)) { $data = $onCreate($data); } $model = static::create(); diff --git a/lib/Models/ModelValidator.php b/lib/Models/ModelValidator.php index 40c3583e26..bc0f6fcf51 100644 --- a/lib/Models/ModelValidator.php +++ b/lib/Models/ModelValidator.php @@ -26,7 +26,10 @@ class ModelValidator extends \Sudzy\Engine { $_this = $this; foreach ($this->validators as $validator => $action) { $this->addValidator($validator, function($params) use ($action, $_this) { - return call_user_func(array($_this, $action), $params); + $callback = [$_this, $action]; + if (is_callable($callback)) { + return call_user_func($callback, $params); + } }); } } diff --git a/lib/Models/Newsletter.php b/lib/Models/Newsletter.php index a7084df92e..04c3a8481b 100644 --- a/lib/Models/Newsletter.php +++ b/lib/Models/Newsletter.php @@ -29,6 +29,8 @@ if (!defined('ABSPATH')) exit; * @property string $subject * @property string $body * @property string|null $schedule + * @property boolean|null $isScheduled + * @property string|null $scheduledAt */ class Newsletter extends Model { diff --git a/lib/Models/Subscriber.php b/lib/Models/Subscriber.php index ff2aa8fc02..2d7269809f 100644 --- a/lib/Models/Subscriber.php +++ b/lib/Models/Subscriber.php @@ -218,7 +218,7 @@ class Subscriber extends Model { return self::filter('withoutSegments'); } else { $segment = Segment::findOne($value); - if ($segment !== false) { + if ($segment instanceof Segment) { return $segment->subscribers(); } } @@ -459,10 +459,10 @@ class Subscriber extends Model { ->where('subscriber_id', $this->id()) ->findOne(); - if ($custom_field === false) { - return $default; - } else { + if ($custom_field instanceof SubscriberCustomField) { return $custom_field->value; + } else { + return $default; } } @@ -495,7 +495,10 @@ class Subscriber extends Model { function setUnconfirmedData(array $subscriber_data) { $subscriber_data = self::filterOutReservedColumns($subscriber_data); - $this->unconfirmed_data = json_encode($subscriber_data); + $encoded = json_encode($subscriber_data); + if (is_string($encoded)) { + $this->unconfirmed_data = $encoded; + } } function getUnconfirmedData() { @@ -511,7 +514,7 @@ class Subscriber extends Model { $segment_id = (isset($data['segment_id']) ? (int)$data['segment_id'] : 0); $segment = Segment::findOne($segment_id); - if ($segment === false) return false; + if (!$segment instanceof Segment) return false; $count = parent::bulkAction($orm, function($subscriber_ids) use($segment) { @@ -531,7 +534,7 @@ class Subscriber extends Model { $segment_id = (isset($data['segment_id']) ? (int)$data['segment_id'] : 0); $segment = Segment::findOne($segment_id); - if ($segment === false) return false; + if (!$segment instanceof Segment) return false; $count = parent::bulkAction($orm, function($subscriber_ids) use($segment) { @@ -552,7 +555,7 @@ class Subscriber extends Model { $segment_id = (isset($data['segment_id']) ? (int)$data['segment_id'] : 0); $segment = Segment::findOne($segment_id); - if ($segment === false) return false; + if (!$segment instanceof Segment) return false; $count = $orm->count(); diff --git a/lib/Models/SubscriberCustomField.php b/lib/Models/SubscriberCustomField.php index 25cd5dfb00..991fab6432 100644 --- a/lib/Models/SubscriberCustomField.php +++ b/lib/Models/SubscriberCustomField.php @@ -15,10 +15,10 @@ class SubscriberCustomField extends Model { static function createOrUpdate($data = array()) { $custom_field = CustomField::findOne($data['custom_field_id']); - if ($custom_field === false) { - return false; - } else { + if ($custom_field instanceof CustomField) { $custom_field = $custom_field->asArray(); + } else { + return false; } if ($custom_field['type'] === 'date') { diff --git a/lib/Newsletter/Links/Links.php b/lib/Newsletter/Links/Links.php index 17879ad3df..7922ad8ab3 100644 --- a/lib/Newsletter/Links/Links.php +++ b/lib/Newsletter/Links/Links.php @@ -179,7 +179,7 @@ class Links { ->findOne(); // convert either only link shortcodes or all hashes links if "convert all" // option is specified - if ($newsletter_link && + if (($newsletter_link instanceof NewsletterLink) && (preg_match('/\[link:/', $newsletter_link->url) || $convert_all) ) { $content = str_replace($link, $newsletter_link->url, $content); diff --git a/lib/Router/Router.php b/lib/Router/Router.php index 2938ec776b..ede6e355ac 100644 --- a/lib/Router/Router.php +++ b/lib/Router/Router.php @@ -55,13 +55,13 @@ class Router { return $this->terminateRequest(self::RESPONE_FORBIDDEN, WPFunctions::get()->__('You do not have the required permissions.', 'mailpoet')); } WPFunctions::get()->doAction('mailpoet_conflict_resolver_router_url_query_parameters'); - return call_user_func( - [ - $endpoint, - $this->endpoint_action, - ], - $this->data - ); + $callback = [ + $endpoint, + $this->endpoint_action, + ]; + if (is_callable($callback)) { + return call_user_func($callback, $this->data); + } } static function decodeRequestData($data) { diff --git a/lib/Segments/BulkAction.php b/lib/Segments/BulkAction.php index 9a47f69fb0..7e78912c5c 100644 --- a/lib/Segments/BulkAction.php +++ b/lib/Segments/BulkAction.php @@ -26,14 +26,14 @@ class BulkAction { throw new \InvalidArgumentException('Missing segment id'); } $segment = Segment::findOne($this->data['listing']['filter']['segment']); - if ($segment) { + if ($segment instanceof Segment) { $segment = $segment->asArray(); } return $this->applySegment($segment); } /** - * @param array $segment + * @param array|bool $segment * * @return array * @throws \Exception diff --git a/lib/Segments/SubscribersListings.php b/lib/Segments/SubscribersListings.php index ad8daae211..50eea133b5 100644 --- a/lib/Segments/SubscribersListings.php +++ b/lib/Segments/SubscribersListings.php @@ -23,7 +23,7 @@ class SubscribersListings { throw new \InvalidArgumentException('Missing segment id'); } $segment = Segment::findOne($data['filter']['segment']); - return $this->getListings($data, $segment ?: null); + return $this->getListings($data, $segment instanceof Segment ? $segment : null); } diff --git a/lib/Settings/SettingsController.php b/lib/Settings/SettingsController.php index d2704d1741..4ed4701b47 100644 --- a/lib/Settings/SettingsController.php +++ b/lib/Settings/SettingsController.php @@ -129,7 +129,7 @@ class SettingsController { private function fetchValue($key) { $setting = Setting::where('name', $key)->findOne(); - if ($setting === false) { + if (!$setting instanceof Setting) { return null; } if (is_serialized($setting->value)) { diff --git a/lib/Subscribers/ImportExport/Export/Export.php b/lib/Subscribers/ImportExport/Export/Export.php index 976b778102..e32a3c0d9d 100644 --- a/lib/Subscribers/ImportExport/Export/Export.php +++ b/lib/Subscribers/ImportExport/Export/Export.php @@ -59,6 +59,7 @@ class Export { } function process() { + $processed_subscribers = 0; $this->default_subscribers_getter->reset(); try { if (is_writable($this->export_path) === false) { @@ -67,12 +68,13 @@ class Export { if (!extension_loaded('zip')) { throw new \Exception(__('Export requires a ZIP extension to be installed on the host.', 'mailpoet')); } - $processed_subscribers = call_user_func( - array( - $this, - 'generate' . strtoupper($this->export_format_option) - ) - ); + $callback = [ + $this, + 'generate' . strtoupper($this->export_format_option) + ]; + if (is_callable($callback)) { + $processed_subscribers = call_user_func($callback); + } } catch (\Exception $e) { throw new \Exception($e->getMessage()); } diff --git a/lib/Tasks/Sending.php b/lib/Tasks/Sending.php index 4510f8cfda..a1f6864eac 100644 --- a/lib/Tasks/Sending.php +++ b/lib/Tasks/Sending.php @@ -235,7 +235,10 @@ class Sending { public function __call($name, $args) { $obj = method_exists($this->queue, $name) ? $this->queue : $this->task; - return call_user_func_array(array($obj, $name), $args); + $callback = [$obj, $name]; + if (is_callable($callback)) { + return call_user_func_array($callback, $args); + } } private function isQueueProperty($prop) { diff --git a/lib/Tasks/State.php b/lib/Tasks/State.php index 89fda969a4..8bd34fc081 100644 --- a/lib/Tasks/State.php +++ b/lib/Tasks/State.php @@ -73,7 +73,7 @@ class State $queue = $newsletter = null; if ($task->type === Sending::TASK_TYPE) { $queue = SendingQueue::where('task_id', $task->id)->findOne(); - $newsletter = $queue ? $queue->newsletter()->findOne() : null; + $newsletter = $queue instanceof SendingQueue ? $queue->newsletter()->findOne() : null; } return [ 'id' => (int)$task->id, diff --git a/lib/Twig/I18n.php b/lib/Twig/I18n.php index ee19f75a47..e2344ef0ee 100644 --- a/lib/Twig/I18n.php +++ b/lib/Twig/I18n.php @@ -92,7 +92,7 @@ class I18n extends AbstractExtension { $date = strtotime($date); } - return WPFunctions::get()->getDateFromGmt(date('Y-m-d H:i:s', $date), $date_format); + return WPFunctions::get()->getDateFromGmt(date('Y-m-d H:i:s', (int)$date), $date_format); } private function setTextDomain($args = array()) { diff --git a/lib/Util/ProgressBar.php b/lib/Util/ProgressBar.php index a86f6552c0..b5e24d163e 100644 --- a/lib/Util/ProgressBar.php +++ b/lib/Util/ProgressBar.php @@ -51,12 +51,14 @@ if (!class_exists('ProgressBar', false)) { * @return array|false Array of counters */ private function readProgress() { - if (file_exists($this->filename)) { - $json_content = file_get_contents($this->filename); - return json_decode($json_content); - } else { + if (!file_exists($this->filename)) { return false; } + $json_content = file_get_contents($this->filename); + if (is_string($json_content)) { + return json_decode($json_content); + } + return false; } /** diff --git a/lib/Util/Sudzy/Engine.php b/lib/Util/Sudzy/Engine.php index 2ba666faec..f330a64aeb 100644 --- a/lib/Util/Sudzy/Engine.php +++ b/lib/Util/Sudzy/Engine.php @@ -37,7 +37,10 @@ class Engine public function executeOne($check, $val, $params=array()) { - return call_user_func(__NAMESPACE__ .'\Engine::'.$check, $val, $params); + $callback = __NAMESPACE__ .'\Engine::'.$check; + if (is_callable($callback)) { + return call_user_func($callback, $val, $params); + } } /** diff --git a/lib/Util/XLSXWriter.php b/lib/Util/XLSXWriter.php index 1f37ff9592..b1964e0ae2 100644 --- a/lib/Util/XLSXWriter.php +++ b/lib/Util/XLSXWriter.php @@ -201,7 +201,7 @@ class XLSXWriter $max_cell_tag = ''; $padding_length = $sheet->max_cell_tag_end - $sheet->max_cell_tag_start - strlen($max_cell_tag); $sheet->file_writer->fseek($sheet->max_cell_tag_start); - $sheet->file_writer->write($max_cell_tag.str_repeat(" ", $padding_length)); + $sheet->file_writer->write($max_cell_tag . str_repeat(" ", (int)$padding_length)); $sheet->file_writer->close(); $sheet->finalized=true; } diff --git a/lib/WP/Functions.php b/lib/WP/Functions.php index fe0c481bb1..a830725571 100644 --- a/lib/WP/Functions.php +++ b/lib/WP/Functions.php @@ -1,6 +1,8 @@