maybe_convert_options();
}
/**
* This can be set in child themes to set the GD dummy data default type.
*
* @param $type
* @param $post_type
*
* @return mixed
*/
public function gd_set_default_dummy_data_type( $type, $post_type ) {
return $type;
}
/**
* Removes the "ninjs-forms" plugin from the list of recommended WordPress plugins.
*
* @param array $plugins The array of recommended WordPress plugins.
*
* @return array The updated array of recommended WordPress plugins with "ninjs-forms" plugin removed.
*/
public function gd_recommend_wp_plugins( $plugins ) {
// remove ninja forms as we have our own built-in contact form.
unset( $plugins['ninja-forms'] );
$plugins['blockstrap-page-builder-blocks'] = array(
'url' => 'https://wordpress.org/plugins/blockstrap-page-builder-blocks/',
'slug' => 'blockstrap-page-builder-blocks',
'name' => 'Blockstrap Page Builder',
'file' => 'blockstrap-page-builder-blocks/blockstrap-page-builder-blocks.php',
'desc' => __( 'Required to display page template blocks, the theme will be blank without this.', 'blockstrap' ),
'required' => true,
);
return $plugins;
}
/**
* Add demo pages on theme install.
*
* @return void
*/
public function maybe_add_pages() {
$pages = $this->get_demo_pages();
if ( ! empty( $pages ) ) {
$theme_slug = sanitize_title_with_dashes( $this->get_theme_title() );
if ( ! blockstrap_get_option( 'blockstrap_demo_pages_installed_' . $theme_slug ) ) {
foreach ( $pages as $page ) {
if ( ! $this->demo_page_exists( $page['slug'] ) ) {
$page_id = $this->add_demo_page( $page );
}
}
blockstrap_update_option( 'blockstrap_demo_pages_installed_' . $theme_slug, true );
}
}
}
/**
* Get the array of demo pages.
*
* @return array[]
*/
public function get_demo_pages() {
return array();
}
/**
* Get the theme title.
*
* @return string|null
*/
public function get_theme_title() {
return __( 'BlockStrap', 'blockstrap' );
}
/**
* Check if demo page exists and return the ID if so.
*
* @param $page_slug
*
* @return int|string
*/
public function demo_page_exists( $page_slug ) {
$theme_slug = get_template();
$option_key = 'blockstrap_demo_pages';
$page_status = blockstrap_get_option( $option_key );
return isset( $page_status[ $theme_slug ][ $page_slug ] ) ? absint( $page_status[ $theme_slug ][ $page_slug ] ) : '';
}
/**
* Add a demo page from arguments.
*
* @param $page
*
* @return false|int|WP_Error
*/
public function add_demo_page( $page ) {
$theme_slug = get_template();
$option_key = 'blockstrap_demo_pages';
$page_status = blockstrap_get_option( $option_key );
$page_status = empty( $page_status ) ? array() : $page_status;
$page_slug = esc_attr( $page['slug'] );
$page_id = wp_insert_post(
array(
'post_title' => $page['title'],
'post_name' => $page['slug'],
'post_content' => $page['desc'],
'post_status' => 'publish',
'post_type' => 'page',
)
);
if ( is_wp_error( $page_id ) ) {
return false;
} else {
$page_status[ $theme_slug ][ $page_slug ] = $page_id;
blockstrap_update_option( $option_key, $page_status );
if ( ! empty( $page['is_blog'] ) ) {
update_option( 'page_for_posts', $page_id );
update_option( 'show_on_front', 'page' );
if ( ! get_option( 'page_on_front' ) ) {
update_option( 'page_on_front', 2 ); // if page on front not set then it will show blog page on front.
}
} elseif ( ! empty( $page['is_front'] ) ) {
update_option( 'page_on_front', $page_id ); // this is probably not needed as the theme can set the front page anyway.
update_option( 'show_on_front', 'page' );
}
}
return $page_id;
}
/**
* Load AUI on our settings page.
*
* @param $aui_screens
*
* @return mixed
*/
public function aui_screen_ids( $aui_screens ) {
$aui_screens[] = 'appearance_page_blockstrap';
return $aui_screens;
}
/**
* Returns the count of required plugins that are not activated.
*
* @return int The count of required plugins that are not activated.
*/
public function required_plugins_count() {
$count = 0;
$required_plugins = $this->get_required_plugins();
foreach ( $required_plugins as $slug => $name ) {
$active = is_plugin_active( $slug . '/' . $slug . '.php' );
if ( ! $active ) {
$count++;
}
}
return $count;
}
/**
* Register the menu item.
* @return void
*/
public function register_menu_page() {
$notifications_count = self::required_plugins_count();
add_submenu_page(
'themes.php',
__( 'BlockStrap Settings', 'blockstrap' ),
! empty( $notifications_count ) ? __( 'Theme Setup', 'blockstrap' ) . ' ' . absint( $notifications_count ) . '' : __( 'Theme Setup', 'blockstrap' ),
'manage_options',
'blockstrap',
array( $this, 'output_settings_page' ),
60
);
}
/**
* Output the settings page HTML.
*
* @return void
*/
public function output_settings_page() {
?>
get_theme_title() ); ?>
vget_version() ); ?>
get( 'Version' ) );
}
/**
* Get the required pages HTML output.
*
* @return void
*/
public function get_required_pages_html() {
$pages = $this->get_demo_pages();
?>
get_required_plugins();
$border_class = self::required_plugins_count() ? 'border-danger' : '';
?>
get_setting_js();
}
/**
* Get the required plugins details array.
*
* @return array
*/
public function get_required_plugins() {
return array(
'blockstrap-page-builder-blocks' => __( 'BlockStrap Builder', 'blockstrap' ),
);
}
/**
* Get the Settings JS.
*
* @return void
*/
public function get_setting_js() {
?>
v2 Tickbox keys. (this helps protect the contact form block)', 'blockstrap' ); ?>
$plugin_slug ) );
if ( is_wp_error( $plugin_info ) ) {
wp_send_json_error( 'Plugin not found' );
wp_die();
}
$plugin_file = $plugin_slug . '/' . $plugin_info->slug . '.php';
$plugin_installed = is_plugin_active( $plugin_file );
if ( ! $plugin_installed ) {
$installed_plugins = get_plugins();
$plugin_installed = array_key_exists( $plugin_file, $installed_plugins ) || in_array( $plugin_file, $installed_plugins, true );
}
if ( $deactivate ) {
if ( $plugin_installed ) {
deactivate_plugins( $plugin_file );
wp_send_json_success( 'Plugin deactivated successfully' );
} else {
wp_send_json_error( 'Plugin not active' );
}
} else {
if ( ! $plugin_installed ) {
// Install and activate the plugin.
$skin = new Automatic_Upgrader_Skin();
$upgrader = new Plugin_Upgrader($skin);
$install_result = $upgrader->install($plugin_info->download_link);
if (is_wp_error($install_result)) {
wp_send_json_error('Plugin installation failed: ' . $install_result->get_error_message());
wp_die();
}
}
// Activate the plugin.
$activate_result = activate_plugin( $plugin_file );
if ( is_wp_error( $activate_result ) ) {
wp_send_json_error( 'Plugin activation failed: ' . $activate_result->get_error_message() );
wp_die();
} else {
wp_send_json_success( 'Plugin activated successfully' );
}
}
} else {
wp_send_json_error( 'You do not have permission to activate/deactivate plugins' );
}
wp_die();
}
/**
* The AJAX function that will add or remove the demo pages.
*
* @return void
*/
public function page_management_handler() {
if ( current_user_can( 'activate_plugins' ) ) {
if ( ! check_ajax_referer( 'blockstrap_page_management', 'security', false ) ) {
wp_send_json_error( 'Invalid nonce' );
wp_die();
}
$demo_pages = $this->get_demo_pages();
$page_slug = isset( $_POST['page_slug'] ) ? sanitize_text_field( $_POST['page_slug'] ) : '';
$delete = isset( $_POST['delete'] ) && $_POST['delete'] === 'true';
if ( ! isset( $demo_pages[ $page_slug ] ) ) {
wp_send_json_error( __( 'Page is not set', 'blockstrap' ) );
wp_die();
} else {
$page = $demo_pages[ $page_slug ];
}
$theme_slug = get_template();
$option_key = 'blockstrap_demo_pages';
$page_status = blockstrap_get_option( $option_key );
$page_id = $this->demo_page_exists( $page_slug );
if ( $delete ) {
if ( $page_id ) {
wp_trash_post( $page_id );
unset( $page_status[ $theme_slug ][ $page_slug ] );
blockstrap_update_option( $option_key, $page_status );
}
wp_send_json_success( __( 'Page moved to trash', 'blockstrap' ) );
wp_die();
} else {
if ( ! $page_id ) {
$page_id = $this->add_demo_page( $page );
if ( is_wp_error( $page_id ) ) {
wp_send_json_error( __( 'Page failed to create', 'blockstrap' ) );
wp_die();
}
wp_send_json_success( __( 'Page Added', 'blockstrap' ) );
wp_die();
} else {
wp_send_json_error( __( 'Page already exists', 'blockstrap' ) );
wp_die();
}
}
} else {
wp_send_json_error( 'You do not have permission to add/remove pages.' );
}
wp_die();
}
/**
* The AJAX function that will save the recaptcha keys.
*
* @return void
*/
public function recaptcha_management_handler() {
if ( current_user_can( 'activate_plugins' ) ) {
if ( ! check_ajax_referer( 'blockstrap_save_recaptcha_keys', 'security', false ) ) {
wp_send_json_error( 'Invalid nonce' );
wp_die();
}
parse_str( $_POST['form_data'], $data );
if ( isset( $data['site_key'] ) && isset( $data['site_secret'] ) ) {
blockstrap_update_option(
'blockstrap_recaptcha_keys',
array(
'site_key' => sanitize_html_class( $data['site_key'] ),
'site_secret' => sanitize_html_class( $data['site_secret'] ),
)
);
wp_send_json_success( __( 'Keys Saved', 'blockstrap' ) );
} else {
wp_send_json_error( 'Something went wrong' );
}
} else {
wp_send_json_error( 'You do not have permission for this.' );
}
wp_die();
}
/**
* Maybe update single options to our new one array of options.
*
* @return void
*/
public function maybe_convert_options() {
$options = blockstrap_get_options();
if ( empty( $options ) ) {
$theme_slug = sanitize_title_with_dashes( $this->get_theme_title() );
$keys = array(
'blockstrap_recaptcha_keys',
'blockstrap_demo_pages_installed_' . $theme_slug,
'directory_geodirectory_dismiss',
'directory_theme_v3',
'school_theme_v2',
);
foreach ( $keys as $key ) {
blockstrap_update_option( $key, get_option( $key ) );
}
}
}
}