page_slug = 'settings'; // Add the page in admin menu add_action( 'admin_menu', array( $this, 'register_pages' ), 10 ); // Redirect to download exported file add_action( 'init', array( $this, 'init' ), 10 ); // Add the settings menu item to the Plugins table. add_filter( 'plugin_action_links_' . plugin_basename( SOCIALSNAP_PLUGIN_FILE ), array( $this, 'settings_link' ) ); $this->add_ajax_requests(); } /** * Register the pages to be used for the Settings screen. * * @since 1.0.0 */ public function register_pages() { // Default Social Snap top level menu item add_menu_page( __( 'Social Snap', 'socialsnap' ), __( 'Social Snap', 'socialsnap' ), apply_filters( 'socialsnap_manage_cap', 'manage_options' ), 'socialsnap-settings', array( $this, 'render' ), 'data:image/svg+xml;base64,' . base64_encode( '' ), apply_filters( 'socialsnap_menu_position', '999.1' ) ); // Add Settings submenu page add_submenu_page( 'socialsnap-settings', __( 'Social Snap Settings', 'socialsnap' ), __( 'Settings', 'socialsnap' ), apply_filters( 'socialsnap_welcome_cap', 'manage_options' ), 'socialsnap-settings', array( $this, 'render' ) ); } /** * Determing if the user is viewing the settings page. * * @since 1.0.0 */ public function init() { // Initialize settings $this->initialize_settings(); // Check what page we are on. $page = isset( $_GET['page'] ) ? $_GET['page'] : ''; // Only load if we are actually on the settings page. if ( 'socialsnap-settings' === $page ) { add_action( 'socialsnap_settings_init', array( $this, 'export_settings' ) ); add_action( 'socialsnap_live_preview', array( $this, 'preview_settings' ) ); // Hook for add-ons do_action( 'socialsnap_settings_init' ); } } /** * Add Ajax Requests for the Settings panel * * @since 1.0.0 */ public function add_ajax_requests() { add_action( 'wp_ajax_socialsnap_settings_save', array( $this, 'save_settings', ) ); add_action( 'wp_ajax_socialsnap_settings_restore', array( $this, 'restore_settings', ) ); add_action( 'wp_ajax_socialsnap_settings_import', array( $this, 'import_settings', ) ); } /** * Initialize the settings array to default values if the settings do not exist in the database * * @since 1.0.0 * @param array $settings */ private function initialize_settings() { $this->settings = require_once SOCIALSNAP_PLUGIN_DIR . 'includes/admin/settings/config-settings.php'; // Stop if settings are not defined if ( ! is_array( $this->settings ) ) { return; } // Stop if settings are stored in the database if ( is_array( get_option( SOCIALSNAP_SETTINGS ) ) ) { return; } $default_settings = $this->get_default_settings( new ArrayIterator( $this->settings ) ); $default_settings = apply_filters( 'socialsnap_default_settings', $default_settings ); // Update the settings in WP Database $updated = update_option( SOCIALSNAP_SETTINGS, $default_settings ); // If updated, set the Social Snap settings variable to default settings. if ( $updated ) { socialsnap()->settings = get_option( SOCIALSNAP_SETTINGS ); } return $updated; } /** * Generate array of settings with default values. * * @since 1.0.0 * @param ArrayIterator $iterator */ public function get_default_settings( $iterator ) { $result = array(); while ( $iterator->valid() ) { $current = $iterator->current(); if ( isset( $current['type'] ) ) { // If current setting is a group or subgroup, go through it's fields. if ( 'group' === $current['type'] || 'subgroup' === $current['type'] ) { $result = array_merge( $result, $this->get_default_settings( new ArrayIterator( $current['fields'] ) ) ); } else { $result[ $current['id'] ] = isset( $current['default'] ) ? $current['default'] : ''; } } $iterator->next(); } return $result; } /** * Save the settings * * @since 1.0.0 */ public function save_settings() { // Run a security check check_ajax_referer( 'socialsnap-settings', 'nonce' ); if ( ! current_user_can( apply_filters( 'socialsnap_manage_cap', 'manage_options' ) ) ) { wp_send_json_error( array( 'message' => __( 'Error. Access denied.', 'socialsnap' ), ) ); } // Get form data $data = isset( $_POST['data'] ) ? $_POST['data'] : null; // Parse form data parse_str( $data, $data ); // Sanitize form data if ( is_array( $data ) ) { $data = array_map( 'stripslashes_deep', $data ); array_walk_recursive( $data, function( &$value ) { $value = sanitize_text_field( $value ); } ); } else { $data = stripslashes( $data ); $data = sanitize_text_field( $data ); } if ( ! empty( $data ) ) { // Try to update the options in the database, if ( update_option( SOCIALSNAP_SETTINGS, $data ) ) { wp_send_json_success( array( 'message' => __( 'Saved', 'socialsnap' ), ) ); } else { wp_send_json_success( array( 'message' => __( 'No changes were made', 'socialsnap' ), ) ); } } else { wp_send_json_error( array( 'message' => __( 'Error. Please reload and try again.', 'socialsnap' ), ) ); } } /** * Restore the settings * * @since 1.0.0 */ public function restore_settings() { // Run a security check check_ajax_referer( 'socialsnap-settings', 'nonce' ); if ( ! current_user_can( apply_filters( 'socialsnap_manage_cap', 'manage_options' ) ) ) { wp_send_json_error( array( 'message' => __( 'Error. Access denied.', 'socialsnap' ), ) ); } // Delete option delete_option( SOCIALSNAP_SETTINGS ); // Initialize with default values // $this->initialize_settings(); // Return wp_send_json_success( array( 'message' => esc_html__( 'Settings restored. Reloading...', 'socialsnap' ), ) ); } /** * Import the settings * * @since 1.0.0 */ public function import_settings() { // Run a security check check_ajax_referer( 'socialsnap-settings', 'nonce' ); if ( ! current_user_can( apply_filters( 'socialsnap_manage_cap', 'manage_options' ) ) ) { wp_send_json_error( array( 'message' => __( 'Error. Access denied.', 'socialsnap' ), ) ); } if ( empty( $_FILES['file']['tmp_name'] ) ) { wp_send_json_error( array( 'code' => 'no-file-uploaded', 'message' => esc_html__( 'No file uploaded', 'socialsnap' ), ) ); } $ext = strtolower( pathinfo( $_FILES['file']['name'], PATHINFO_EXTENSION ) ); if ( 'json' !== $ext ) { wp_send_json_error( array( 'code' => 'wrong-format', 'message' => esc_html__( 'Please upload a valid .json form export file', 'socialsnap' ), ) ); } $import_data = json_decode( file_get_contents( $_FILES['file']['tmp_name'] ), true ); if ( ! empty( $import_data ) && is_array( $import_data ) ) { if ( update_option( SOCIALSNAP_SETTINGS, $import_data ) ) { wp_send_json_success( array( 'code' => 'success', 'message' => esc_html__( 'Settings imported successfully. Reloading...', 'socialsnap' ), ) ); } else { wp_send_json_error( array( 'code' => 'already-saved', 'message' => esc_html__( 'Imported settings are identical as current settings.', 'socialsnap' ), ) ); } } wp_send_json_error( array( 'code' => 'could-not-complete', 'message' => esc_html__( 'Import could not be finished', 'socialsnap' ), ) ); } /** * Export the settings * * @since 1.0.0 */ public function export_settings() { if ( ! isset( $_GET['ss_export_settings'] ) || ! current_user_can( apply_filters( 'socialsnap_manage_cap', 'manage_options' ) ) ) { return; } ignore_user_abort( true ); nocache_headers(); header( 'Content-disposition: attachment; filename=socialsnap-export-' . gmdate( 'm-d-Y' ) . '.json' ); header( 'Content-type: text/html' ); header( 'Expires: 0' ); echo wp_json_encode( get_option( SOCIALSNAP_SETTINGS ) ); exit; } /** * Render save button for the settings panel. * * @since 1.0.0 */ public function render_save_action() { ?>
page_slug !== $hook ) { return; } wp_enqueue_style( 'socialsnap-admin-settings', SOCIALSNAP_PLUGIN_URL . 'assets/css/admin-settings.css', null, SOCIALSNAP_VERSION ); wp_enqueue_script( 'socialsnap-settings-js', SOCIALSNAP_PLUGIN_URL . 'assets/js/admin-settings.js', array( 'jquery' ), SOCIALSNAP_VERSION, true ); // Localize variables to be used in plugin JavaScript files. $strings = array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'wait_text' => __( 'Please wait…', 'socialsnap' ), 'icons' => socialsnap()->icons->get_all_svg_icons(), 'nonce' => wp_create_nonce( 'socialsnap-admin' ), ); wp_localize_script( 'socialsnap-settings-js', 'socialsnap_admin', $strings ); // Enqueue WordPress media upload wp_enqueue_media(); // Load common assets from parent class. parent::load_assets( $hook ); } /** * Add Settings page and Go Pro to plugin action links in the Plugins table. * * @since 1.0.0 * * @param array $links Default plugin action links. * @return array $links Amended plugin action links. */ public function settings_link( $links ) { $settings_link = '' . __( 'Settings', 'socialsnap' ) . ''; array_unshift( $links, $settings_link ); $links['go_pro'] = '' . __( 'Addons', 'socialsnap' ) . ''; return $links; } /** * Returns one random line for the bottom of the settings screen. * * @since 1.0.0 * * @param array $links Default plugin action links. * @return array $links Amended plugin action links. */ private function get_random_fact() { $facts = array( __( 'If you like Social Snap please leave us a ★★★★★ rating to help us spread the word. Thank you!', 'socialsnap' ), __( 'Bring your old posts to life by automatically posting them on your social media.', 'socialsnap' ), __( 'Completely automate posting on social media whenever you publish a new article.', 'socialsnap' ), __( 'Allow your users to log into your website through their favorite social networks.', 'socialsnap' ), __( 'Unlock 30+ share providers, share counters for all providers, URL shortening and more awesome features!', 'socialsnap' ), ); $facts = apply_filters( 'socialsnap_facts', $facts ); return $facts[ array_rand( $facts ) ]; } /** * Render settings preview screen * * @since 1.0.0 */ public function preview_settings() { $preview_social_share = socialsnap_get_social_share_positions(); if ( is_array( $preview_social_share ) && ! empty( $preview_social_share ) ) { foreach ( $preview_social_share as $element ) { ?>
$sub_settings ) { $this->render_field( $sub_settings, $level ); } } } } /** * Build the output for the plugin settings page. * * @since 1.0.0 */ public function render() { do_action( 'socialsnap_admin_settings_before', $this->settings ); ?>
settings ) && ! empty( $this->settings ) ) { foreach ( $this->settings as $id => $field ) { $this->render_field( $field ); } } ?> render_save_action(); ?>

get_random_fact(), socialsnap_get_allowed_html_tags( 'post' ) ); ?>

pro ) { ?> icons->get_svg( 'socialsnap-icon' ); // phpcs:ignore ?> icons->get_svg( 'socialsnap-icon' ); // phpcs:ignore ?>