notice_key = $notice_key; $instance->message = $message; $instance->type = $type; $instance->dismissible = $dismissible; $instance->remind_when = $remind_when; $instance->cta_text = $cta_text; $instance->cta_url = $cta_url; $instance->nonce = wp_create_nonce( 'adplugg_set_notice_pref' ); return $instance; } /** * Static function to recreate a Notice. Call using * AdPlugg_Notice::recreate( $data_array ); * * See the to_array function below for the expected array structure. * * @param array $array An array containing the Notice data. * @return \self Works like a constructor. */ public static function recreate( $array ) { $instance = new self(); $instance->notice_key = $array['notice_key']; $instance->message = $array['message']; $instance->type = $array['type']; $instance->dismissible = $array['dismissible']; if ( isset( $array['remind_when'] ) ) { $instance->remind_when = $array['remind_when']; } if ( isset( $array['cta_text'] ) ) { $instance->cta_text = $array['cta_text']; } if ( isset( $array['cta_url'] ) ) { $instance->cta_url = $array['cta_url']; } $instance->nonce = wp_create_nonce( 'adplugg_set_notice_pref' ); return $instance; } /** * Gets the html for the notice. * * @return string Returns a string of html for rendering the notice. */ public function get_rendered() { $html = ''; if ( ! $this->is_dismissed() ) { $buttons = ''; $html .= '
'; $html .= '

' . 'AdPlugg: ' . $this->message . '

'; if ( $this->dismissible ) { $buttons = '' . ''; } if ( null !== $this->cta_text ) { $buttons .= ''; } if ( ! empty( $buttons ) ) { $html .= '

' . $buttons . '

'; } $html .= '
'; } return $html; } /** * Renders the html for the notice. */ public function render() { echo $this->get_rendered(); // phpcs:ignore } /** * Gets the value of notice_key. * * @return string Returns the notice_key. */ public function get_notice_key() { return $this->notice_key; } /** * Gets the nonce for the Notice. This is used for testing. * * @return string Returns the nonce. */ public function get_nonce() { return $this->nonce; } /** * Gets the value of message. * * @return string Returns the Notice message. */ public function get_message() { return $this->message; } /** * Gets the value of type. * * @return string Returns the Notice type. */ public function get_type() { return $this->type; } /** * Gets the value of dismissible. * * @return string Returns whether or not the Notice is dismissible. */ public function is_dismissible() { return $this->dismissible; } /** * Converts the class into an array for inserting into a WordPress option. * * @returns array Returns an array representation of the object. */ public function to_array() { $data_array = array( 'notice_key' => $this->notice_key, 'message' => $this->message, 'type' => $this->type, 'dismissible' => $this->dismissible, 'remind_when' => $this->remind_when, 'cta_text' => $this->cta_text, 'cta_url' => $this->cta_url, ); return $data_array; } /** * Returns whether or not the notice is dismissed. * * @return boolean Whether or not the notice is dismissed. */ public function is_dismissed() { $ret = false; $dismissals = get_option( ADPLUGG_NOTICES_DISMISSED_NAME, array() ); if ( array_key_exists( $this->notice_key, $dismissals ) ) { $remind_on = $dismissals[ $this->notice_key ]; if ( null !== $remind_on ) { if ( $remind_on > time() ) { $ret = true; } } else { $ret = true; } } return $ret; } }