add_setting_section( $section );
}
// since PPF04
$this->maybe_register_setting();
}
/**
* add a single setting section
*
* @since PPF04
* @param array $sections setting section to add
* @access public
* @see add_settings()
*/
public function add_setting_section( $section ) {
// as of PPF04 add sections to _sections array one by one
$this->_sections[] = $section;
if ( array_key_exists( 'fields', $section ) ) {
$this->add_settings( $section );
}
$this->maybe_register_setting();
}
/**
* register the setting
*
* @since PPF04
* @access private
*
* was part of add_setting_sections() before PPF04
*/
public function maybe_register_setting() {
if ( ! $this->_settings_registered ) {
// Register the options
// since PPF03 only if there is a settings class
// so we can use the same function also if we don't need any settings
if ( false !== $this->settings() ) {
register_setting( $this->core()->get_plugin_slug(), $this->settings()->get_option_name(), array( 'sanitize_callback' => array( $this, 'sanitize_callback' ) ) );
}
$this->_settings_registered = true;
}
}
/**
* helper function to add a complete setting section
*
* @since PPF01
* @param array $settings array of settings to add
* string $section => ID of the section
* int $order => sort order
* this was added in PPF04, so we check if it exists for backwards compatibility
* string $title => title for section (used by print_setting_sections())
* string $icon => icon for tab
* since PPF05
* string $html => HTML code to add to this section
* array $fields => multidimensional array of fields to add
* string $key => key of the option array
* string $callback => function to call
* as of PPF04 this can be an array to enable external callbacks
* array $callbackargs => array of arguments to pass to callback function
* introduced in PPF08
* bool $nosubmit => this section should not show the submit button
* @access private
*/
private function add_settings( $settings ) {
$section_id = $this->core()->get_plugin_slug() . '-' . $settings['section'];
add_settings_section( $section_id, '', null, $this->core()->get_plugin_slug() );
foreach ( $settings['fields'] as $field ) {
$field_id = $this->core()->get_plugin_slug() . '-' . $field['key'];
// since PPF04
if ( is_array( $field['callback'] ) ) {
$callback = $field['callback'];
} else {
$callback = array( $this, $field['callback'] );
}
$callbackargs = false;
if ( isset( $field['callbackargs'] ) && is_array( $field['callbackargs'] ) ) {
$callbackargs = $field['callbackargs'];
}
add_settings_field( $field_id, '' , $callback, $this->core()->get_plugin_slug(), $section_id, $callbackargs );
}
return;
}
/**
* helper function to print out a slider styled checkbox
*
* @since PPF01
* @param string $key option key name
* @param string $title title
* @param string $help anchor to link to in manual
* @param string $video YouTube video ID
* @param string $note second line
* @param bool $disabled true/false (optional)
* @access public
*/
public function print_slider_check( $key, $title, $help, $video, $note, $disabled = false ) {
$dis = '';
if ( $disabled ) {
$dis = ' disabled="disabled"';
}
$hlp = '';
if ( ! empty( $help ) ) {
$hlp = $this->add_manual_link( $help );
}
$vid = '';
if ( ! empty( $video ) ) {
$vid = $this->add_video_link( $video );
}
$add = '';
if ( ! empty( $note ) ) {
$add = '
' . $note;
}
echo '
' . $title . $hlp . $vid . $add . '
'; } /** * helper function to add a plugin manual link * * @since PPF01 * @param string $anchor name of the anchor to link to * @return string * @access private */ private function add_manual_link( $anchor = '' ) { return ' '; } /** * helper function to add a video link * * @since PPF01 * @param string $youtubeid ID of the YouTube video * @return string * @access private */ private function add_video_link( $youtubeid = '' ) { return ' '; } /** * print out setting sections * it is not possible to use do_settings_sections() because we are not able to create a tabbed interface * * @since PPF01 * @access public * @see add_settings() */ public function print_setting_sections() { $currentclass = ' current'; foreach( $this->_sections as $section ) { $section_id = $this->core()->get_plugin_slug() . '-' . $section['section']; $extraclass = ''; if ( array_key_exists( 'nosubmit', $section ) && true === $section['nosubmit'] ) { $extraclass = ' nosubmit'; } echo '