| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Presenters\Admin; |
| 4 |
|
| 5 |
use WPSEO_Admin_Asset_Manager; |
| 6 |
use Yoast\WP\SEO\Presenters\Abstract_Presenter; |
| 7 |
|
| 8 |
/** |
| 9 |
* Represents the presenter class for Yoast-styled WordPress admin notices. |
| 10 |
*/ |
| 11 |
class Notice_Presenter extends Abstract_Presenter { |
| 12 |
|
| 13 |
/** |
| 14 |
* The title of the admin notice. |
| 15 |
* |
| 16 |
* @var string |
| 17 |
*/ |
| 18 |
private $title; |
| 19 |
|
| 20 |
/** |
| 21 |
* The content of the admin notice. |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
private $content; |
| 26 |
|
| 27 |
/** |
| 28 |
* The filename of the image for the notice. Should be a file in the 'images' folder. |
| 29 |
* |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
private $image_filename; |
| 33 |
|
| 34 |
/** |
| 35 |
* HTML string to be displayed after the main content, usually a button. |
| 36 |
* |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
private $button; |
| 40 |
|
| 41 |
/** |
| 42 |
* Whether the notice should be dismissible. |
| 43 |
* |
| 44 |
* @var bool |
| 45 |
*/ |
| 46 |
private $is_dismissible; |
| 47 |
|
| 48 |
/** |
| 49 |
* The id for the div of the notice. |
| 50 |
* |
| 51 |
* @var string |
| 52 |
*/ |
| 53 |
private $id; |
| 54 |
|
| 55 |
/** |
| 56 |
* An instance of the WPSEO_Admin_Asset_Manager class. |
| 57 |
* |
| 58 |
* @var WPSEO_Admin_Asset_Manager |
| 59 |
*/ |
| 60 |
protected $asset_manager; |
| 61 |
|
| 62 |
/** |
| 63 |
* Notice_Presenter constructor. |
| 64 |
* |
| 65 |
* @param string $title Title of the admin notice. |
| 66 |
* @param string $content Content of the admin notice. |
| 67 |
* @param string $image_filename Optional. The filename of the image of the admin notice, should be inside the 'images' folder. |
| 68 |
* @param string $button Optional. An HTML string to be displayed after the main content, usually a button. |
| 69 |
* @param bool $is_dismissible Optional. Whether the admin notice should be dismissible. |
| 70 |
* @param string $id Optional. The id of the notice. |
| 71 |
*/ |
| 72 |
public function __construct( $title, $content, $image_filename = null, $button = null, $is_dismissible = false, $id = '' ) { |
| 73 |
$this->title = $title; |
| 74 |
$this->content = $content; |
| 75 |
$this->image_filename = $image_filename; |
| 76 |
$this->button = $button; |
| 77 |
$this->is_dismissible = $is_dismissible; |
| 78 |
$this->id = $id; |
| 79 |
|
| 80 |
if ( ! $this->asset_manager ) { |
| 81 |
$this->asset_manager = new WPSEO_Admin_Asset_Manager(); |
| 82 |
} |
| 83 |
|
| 84 |
$this->asset_manager->enqueue_style( 'notifications' ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Presents the Notice. |
| 89 |
* |
| 90 |
* @return string The styled Notice. |
| 91 |
*/ |
| 92 |
public function present() { |
| 93 |
$dismissible = ( $this->is_dismissible ) ? ' is-dismissible' : ''; |
| 94 |
$id = ( $this->id ) ? ' id="' . $this->id . '"' : ''; |
| 95 |
|
| 96 |
// WordPress admin notice. |
| 97 |
$out = '<div' . $id . ' class="notice notice-yoast yoast' . $dismissible . '">'; |
| 98 |
$out .= '<div class="notice-yoast__container">'; |
| 99 |
|
| 100 |
// Header. |
| 101 |
$out .= '<div>'; |
| 102 |
$out .= '<div class="notice-yoast__header">'; |
| 103 |
$out .= '<span class="yoast-icon"></span>'; |
| 104 |
$out .= \sprintf( |
| 105 |
'<h1 class="notice-yoast__header-heading">%s</h1>', |
| 106 |
\esc_html( $this->title ) |
| 107 |
); |
| 108 |
$out .= '</div>'; |
| 109 |
$out .= '<p>' . $this->content . '</p>'; |
| 110 |
if ( ! \is_null( $this->button ) ) { |
| 111 |
$out .= '<p>' . $this->button . '</p>'; |
| 112 |
} |
| 113 |
$out .= '</div>'; |
| 114 |
|
| 115 |
if ( ! \is_null( $this->image_filename ) ) { |
| 116 |
$out .= '<img src="' . \esc_url( plugin_dir_url( WPSEO_FILE ) . 'images/' . $this->image_filename ) . '" alt="" height="60" width="75"/>'; |
| 117 |
} |
| 118 |
|
| 119 |
$out .= '</div>'; |
| 120 |
$out .= '</div>'; |
| 121 |
|
| 122 |
return $out; |
| 123 |
} |
| 124 |
} |
| 125 |
|