| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Admin |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Generates the HTML for an inline Help Button and Panel. |
| 10 |
*/ |
| 11 |
class WPSEO_Admin_Help_Panel { |
| 12 |
|
| 13 |
/** |
| 14 |
* Unique identifier of the element the inline help refers to, used as an identifier in the html. |
| 15 |
* |
| 16 |
* @var string |
| 17 |
*/ |
| 18 |
private $id; |
| 19 |
|
| 20 |
/** |
| 21 |
* The Help Button text. Needs a properly escaped string. |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
private $help_button_text; |
| 26 |
|
| 27 |
/** |
| 28 |
* The Help Panel content. Needs a properly escaped string (might contain HTML). |
| 29 |
* |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
private $help_content; |
| 33 |
|
| 34 |
/** |
| 35 |
* Optional Whether to print out a container div element for the Help Panel, used for styling. |
| 36 |
* |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
private $wrapper; |
| 40 |
|
| 41 |
/** |
| 42 |
* Constructor. |
| 43 |
* |
| 44 |
* @param string $id Unique identifier of the element the inline help refers to, used as |
| 45 |
* an identifier in the html. |
| 46 |
* @param string $help_button_text The Help Button text. Needs a properly escaped string. |
| 47 |
* @param string $help_content The Help Panel content. Needs a properly escaped string (might contain HTML). |
| 48 |
* @param string $wrapper Optional Whether to print out a container div element for the Help Panel, |
| 49 |
* used for styling. |
| 50 |
* Pass a `has-wrapper` value to print out the container. Default: no container. |
| 51 |
*/ |
| 52 |
public function __construct( $id, $help_button_text, $help_content, $wrapper = '' ) { |
| 53 |
$this->id = $id; |
| 54 |
$this->help_button_text = $help_button_text; |
| 55 |
$this->help_content = $help_content; |
| 56 |
$this->wrapper = $wrapper; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Returns the html for the Help Button. |
| 61 |
* |
| 62 |
* @return string |
| 63 |
*/ |
| 64 |
public function get_button_html() { |
| 65 |
|
| 66 |
if ( ! $this->id || ! $this->help_button_text || ! $this->help_content ) { |
| 67 |
return ''; |
| 68 |
} |
| 69 |
|
| 70 |
return sprintf( |
| 71 |
' <button type="button" class="yoast_help yoast-help-button dashicons" id="%1$s-help-toggle" aria-expanded="false" aria-controls="%1$s-help"><span class="yoast-help-icon" aria-hidden="true"></span><span class="screen-reader-text">%2$s</span></button>', |
| 72 |
esc_attr( $this->id ), |
| 73 |
$this->help_button_text, |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Returns the html for the Help Panel. |
| 79 |
* |
| 80 |
* @return string |
| 81 |
*/ |
| 82 |
public function get_panel_html() { |
| 83 |
|
| 84 |
if ( ! $this->id || ! $this->help_button_text || ! $this->help_content ) { |
| 85 |
return ''; |
| 86 |
} |
| 87 |
|
| 88 |
$wrapper_start = ''; |
| 89 |
$wrapper_end = ''; |
| 90 |
|
| 91 |
if ( $this->wrapper === 'has-wrapper' ) { |
| 92 |
$wrapper_start = '<div class="yoast-seo-help-container">'; |
| 93 |
$wrapper_end = '</div>'; |
| 94 |
} |
| 95 |
|
| 96 |
return sprintf( |
| 97 |
'%1$s<p id="%2$s-help" class="yoast-help-panel">%3$s</p>%4$s', |
| 98 |
$wrapper_start, |
| 99 |
esc_attr( $this->id ), |
| 100 |
$this->help_content, |
| 101 |
$wrapper_end, |
| 102 |
); |
| 103 |
} |
| 104 |
} |
| 105 |
|