-
- .htaccess',
- esc_html__( 'file that is writable by the server. Please enable write access for file', 'spiderblocker' )
- );
- ?>
- .
-
- generate_block_rules();
-
- }
-
- /**
- * Check if the .htaccess file is writable.
- *
- * @return bool
- * @codeCoverageIgnore
- */
- private static function is_htaccess_writable() {
- $htaccess_file = self::join_paths( ABSPATH, '.htaccess' );
- return is_writable( $htaccess_file );
- }
-
- /**
- * Function to join the supplied arguments together.
- *
- * @return string
- * @codeCoverageIgnore
- */
- private static function join_paths() {
- $paths = array();
-
- foreach ( func_get_args() as $arg ) {
- if ( '' !== $arg ) {
- $paths[] = $arg;
- }
- }
-
- return preg_replace( '#/+#', '/', join( '/', $paths ) );
- }
-
- /**
- * Change file permission of the .htaccess file.
- *
- * @param int $mod octet value for chmod.
- * @return bool
- * @codeCoverageIgnore
- */
- private static function chmod_htaccess( $mod = 0644 ) {
- $home_path = function_exists( 'get_home_path' ) ? get_home_path() : ABSPATH;
- $htaccess_file = $home_path . '.htaccess';
-
- return chmod( $htaccess_file, $mod );
}
/**
@@ -564,29 +594,34 @@ public function remove_block_rules() {
*/
public function save_list() {
check_ajax_referer( self::NONCE, 'nonce' );
- if ( isset( $_POST['data'] ) && '' !== $_POST['data'] ) {
- $data = json_decode( stripcslashes( $_POST['data'] ), true );
-
- if ( json_last_error() ) {
- if ( function_exists( 'json_last_error_msg' ) ) {
- wp_send_json_error( json_last_error_msg() );
- } else {
- wp_send_json_error( esc_html__( 'Failed parsing JSON', 'spiderblocker' ) );
- }
- }
- if ( get_option( self::OPTIONNAME ) !== false ) {
- update_option( self::OPTIONNAME, maybe_serialize( $data ) );
- } else {
- add_option( self::OPTIONNAME, maybe_serialize( $data ), '', 'no' );
- }
+ if ( ! isset( $_POST['data'] ) ) {
+ wp_send_json_error( esc_html__( 'Unable to process the request as no data has been received.', 'spiderblocker' ) );
+ return;
+ }
- $this->generate_block_rules();
- add_filter( 'robots_txt', array( &$this, 'robots_file' ), ~PHP_INT_MAX, 2 );
- wp_send_json_success( $this->get_bots() );
- } else {
+ if ( empty( $_POST['data'] ) ) {
wp_send_json_error( esc_html__( 'Unable to process the request as no data has been received.', 'spiderblocker' ) );
+ return;
}
+
+ $data = json_decode( stripcslashes( $_POST['data'] ), true );
+
+ if ( json_last_error() ) {
+ wp_send_json_error( esc_html__( 'Failed parsing JSON data.', 'spiderblocker' ) );
+ return;
+ }
+
+ update_option( self::OPTIONNAME, maybe_serialize( $data ), 'no' );
+
+ // Add rule to .htaccess file
+ $this->generate_block_rules();
+
+ // Update robots_txt file
+ add_filter( 'robots_txt', array( &$this, 'robots_file' ), ~PHP_INT_MAX, 2 );
+
+ // Send success response
+ wp_send_json_success( $this->get_bots() );
}
/**
@@ -618,28 +653,101 @@ public function robots_file( $output, $public ) {
* @codeCoverageIgnore
*/
public function view_handler() {
- ?>
-
+ /**
+ * Adds action for admin scripts.
+ */
+ public function view_handler_load() {
+ add_action( 'admin_enqueue_scripts', array( &$this, 'view_handler_scripts' ) );
+ }
+
+ /**
+ * Registers & Enqueues the admin scripts for the view_handler() function.
+ */
+ public function view_handler_scripts() {
+ wp_enqueue_script( 'spiderblocker-admin', 'https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js', array( 'jquery' ), self::PLUGIN_VERSION, false );
+
+ $localize = array(
+ 'nonce' => wp_create_nonce( self::NONCE ),
+ 'save_text' => esc_html__( 'List of bots was saved and new blocklist applied!', 'spiderblocker' ),
+ 'save_reset_text' => esc_html__( 'List of bots was reset to defaults!', 'spiderblocker' ),
+ 'bot_text' => esc_html__( 'Bot', 'spiderblocker' ),
+ 'added_text' => esc_html__( 'was added!', 'spiderblocker' ),
+ 'removed_text' => esc_html__( 'Bot was removed!', 'spiderblocker' ),
+ );
+
+ // Pass data to JS
+ wp_localize_script( 'spiderblocker-admin', 'sb_i18n', $localize );
+
+ wp_enqueue_script( 'thickbox' );
+ wp_enqueue_style( 'thickbox' );
+ }
+
+ /**
+ * CSS for settings panel.
+ *
+ * @codeCoverageIgnore
+ */
+ public function admin_css() {
+ ?>
+
+
- window.sb_nonce = "";
+ /**
+ * JS for settings panel.
+ *
+ * @codeCoverageIgnore
+ */
+ public function admin_js() {
+ ?>
+
-