content_type = $content_type; $this->trigger = $trigger; $this->heading = $heading; $this->actions = $actions; // Allow callers to provide the callback without any parameters. Assuming the content provided is the callback. if ( 'callback' === $this->content_type && ! isset( $content['parameters'] ) ) { $this->content = array( 'callback' => $content, 'parameters' => array(), ); } else { $this->content = $content; } self::register_scripts_and_styles(); } /** * Prints the modal HTML. * * @since 2.6.0 */ public function print_html() { wc_get_template( 'html-modal.php', array( 'modal' => $this ), '', plugin_dir_path( WC_Subscriptions::$plugin_file ) . 'templates/' ); } /** * Prints the modal inner content. * * @since 2.6.0 */ public function print_content() { switch ( $this->content_type ) { case 'plain-text': echo '
' . wp_kses_post( $this->content ) . '
'; break; case 'html': echo wp_kses_post( $this->content ); break; case 'template': wc_get_template( $this->content['template_name'], $this->content['args'], '', $this->content['template_path'] ); break; case 'callback': call_user_func_array( $this->content['callback'], $this->content['parameters'] ); break; } } /** * Determines if the modal has a heading. * * @since 2.6.0 * * @return bool */ public function has_heading() { return ! empty( $this->heading ); } /** * Determines if the modal has actions. * * @since 2.6.0 * * @return bool */ public function has_actions() { return ! empty( $this->actions ); } /** * Adds a button or link action which will be printed in the modal footer. * * @since 2.6.0 * * @param array $action_args { * Action button or link details. * * @type string $type Optional. The element type. Can be 'button' or 'a'. Default 'a' (link element). * @type array $attributes Optional. An array of HTML attributes in a array( 'attribute' => 'value' ) format. The value can also be an array of attribute values. Default is empty array. * @type string $text Optional. The text should appear inside the button or a tag. Default is empty string. * } */ public function add_action( $action_args ) { $action = wp_parse_args( $action_args, array( 'type' => 'a', 'text' => '', 'attributes' => array( 'class' => 'button', ), ) ); $this->actions[] = $action; } /** * Returns the modal heading. * * @since 2.6.0 * * @return string */ public function get_heading() { return $this->heading; } /** * Returns the array of actions. * * @since 2.6.0 * * @return array The modal actions. */ public function get_actions() { return $this->actions; } /** * Returns the modal's trigger selector. * * @since 2.6.0 * * @return string The trigger element's selector. */ public function get_trigger() { return $this->trigger; } /** * Returns a flattened string of HTML element attributes from an array of attributes and values. * * @since 2.6.0 * * @param array $attributes An array of attributes in a array( 'attribute' => 'value' ) or array( 'attribute' => array( 'value', 'value ) ). * @return string */ public function get_attribute_string( $attributes ) { foreach ( $attributes as $attribute => $values ) { $attributes[ $attribute ] = $attribute . '="' . implode( ' ', array_map( 'esc_attr', (array) $values ) ) . '"'; } return implode( ' ', $attributes ); } }