/**
  * Add Book by URL
  */
 static function formAddByUrl()
 {
     check_admin_referer('bulk-books');
     // Nonce auto-generated by WP_List_Table
     $catalog = new static();
     $user_id = $catalog->getUserId();
     // Set Redirect URL
     if (get_current_user_id() != $user_id) {
         $redirect_url = get_admin_url(get_current_blog_id(), '/index.php?page=pb_catalog&user_id=' . $user_id);
     } else {
         $redirect_url = get_admin_url(get_current_blog_id(), '/index.php?page=pb_catalog');
     }
     $url = parse_url(\Pressbooks\Sanitize\canonicalize_url($_REQUEST['add_book_by_url']));
     $main = parse_url(network_site_url());
     if (strpos($url['host'], $main['host']) === false) {
         $_SESSION['pb_errors'][] = __('Invalid URL.', 'pressbooks');
         \Pressbooks\Redirect\location($redirect_url);
     }
     if ($url['host'] == $main['host']) {
         // Get slug using the path
         $slug = str_replace($main['path'], '', $url['path']);
         $slug = trim($slug, '/');
         $slug = explode('/', $slug);
         $slug = $slug[0];
     } else {
         // Get slug using host
         $slug = str_replace($main['host'], '', $url['host']);
         $slug = trim($slug, '.');
         $slug = explode('.', $slug);
         $slug = $slug[0];
     }
     $book_id = get_id_from_blogname($slug);
     if (!$book_id) {
         $_SESSION['pb_errors'][] = __('No book found.', 'pressbooks');
         \Pressbooks\Redirect\location($redirect_url);
     }
     //      if ( ! get_blog_option( $book_id, 'blog_public' ) ) {
     //          $_SESSION['pb_errors'][] = __( 'Book is not public', 'pressbooks' );
     //          \Pressbooks\Redirect\location( $redirect_url );
     //      }
     $catalog->saveBook($book_id, array());
     $catalog->deleteCache();
     // Ok!
     $_SESSION['pb_notices'][] = __('Settings saved.');
     // Redirect back to form
     \Pressbooks\Redirect\location($redirect_url);
 }
 /**
  * Change hrefs
  *
  * @param \DOMDocument $doc
  * @param string $type front-matter, part, chapter, back-matter, ...
  * @param int $pos (optional) position of content, used when creating filenames like: chapter-001, chapter-002, ...
  *
  * @return \DOMDocument
  */
 protected function kneadHref(\DOMDocument $doc, $type, $pos)
 {
     $urls = $doc->getElementsByTagName('a');
     foreach ($urls as $url) {
         $current_url = '' . $url->getAttribute('href');
         // Stringify
         // Don't touch empty urls
         if (!trim($current_url)) {
             continue;
         }
         // WordPress auto wraps images in a href tags.
         // For example: <a href="some_image-original.png"><img src="some_image-300x200.png" /></a>
         // This causes an EPUB validation error of: hyperlink to non-standard resource ( of type 'image/...' )
         // We fix this by removing the href
         if ($url->childNodes->length) {
             foreach ($url->childNodes as $node) {
                 if ('img' == $node->nodeName && $this->fuzzyImageNameMatch($current_url, $node->getAttribute('src'))) {
                     $url->removeAttribute('href');
                     continue 2;
                 }
             }
         }
         // Determine if we are trying to link to our own internal content
         $internal_url = $this->fuzzyHrefMatch($current_url, $type, $pos);
         if (false !== $internal_url) {
             $url->setAttribute('href', $internal_url);
             continue;
         }
         // Canonicalize, fix typos, remove garbage
         if ('#' != @$current_url[0]) {
             $url->setAttribute('href', \Pressbooks\Sanitize\canonicalize_url($current_url));
         }
     }
     return $doc;
 }
 /**
  * @covers \Pressbooks\Sanitize\canonicalize_url
  */
 public function test_canonicalize_url()
 {
     $url = 'pressbooks.com/';
     $this->assertEquals('http://pressbooks.com', \Pressbooks\Sanitize\canonicalize_url($url));
     $url = 'https://pressbooks.com/';
     $this->assertEquals('https://pressbooks.com', \Pressbooks\Sanitize\canonicalize_url($url));
     $url = 'HTTPS://PRESSBOOKS.COM/FOO/BAR/';
     $this->assertEquals('https://pressbooks.com/FOO/BAR', \Pressbooks\Sanitize\canonicalize_url($url));
     $url = 'ftp://PRESSBOOKS.COM/foo/BAR�/?hello=world&TESTING=��123';
     $this->assertEquals('http://pressbooks.com/foo/BAR/?hello=world&TESTING=123', \Pressbooks\Sanitize\canonicalize_url($url));
     $url = 'MAILTO:^accepts�!mostly,garb@ge.../';
     $this->assertEquals('MAILTO:^accepts!mostly,garb@ge...', \Pressbooks\Sanitize\canonicalize_url($url));
     $url = 'mailto:miranda@yourcompany.com?bcc=eventsteam@yourcompany.com&subject=Excited%20to%20meet%20at%20the%20event!&body=Hi%20Miranda,';
     $this->assertEquals($url, \Pressbooks\Sanitize\canonicalize_url($url));
 }
 /**
  * Sanitize various options (boolean, string, integer, float).
  *
  * @param array $input
  * @return array $options
  */
 function sanitize($input)
 {
     $options = array();
     if (!is_array($input)) {
         $input = array();
     }
     if (property_exists($this, 'booleans')) {
         foreach ($this->booleans as $key) {
             if (!isset($input[$key]) || 1 != @$input[$key]) {
                 $options[$key] = 0;
             } else {
                 $options[$key] = 1;
             }
         }
     }
     if (property_exists($this, 'strings')) {
         foreach ($this->strings as $key) {
             if (empty($input[$key])) {
                 unset($options[$key]);
             } else {
                 $options[$key] = sanitize_text_field($input[$key]);
             }
         }
     }
     if (property_exists($this, 'urls')) {
         foreach ($this->urls as $key) {
             if (empty($input[$key])) {
                 unset($options[$key]);
             } else {
                 $value = trim(strip_tags(stripslashes($input[$key])));
                 if ($value) {
                     $options[$key] = \Pressbooks\Sanitize\canonicalize_url($value);
                 } else {
                     unset($options[$key]);
                 }
             }
         }
     }
     if (property_exists($this, 'integers')) {
         foreach ($this->integers as $key) {
             if (empty($input[$key])) {
                 unset($options[$key]);
             } else {
                 $options[$key] = absint($input[$key]);
             }
         }
     }
     if (property_exists($this, 'floats')) {
         foreach ($this->floats as $key) {
             if (empty($input[$key])) {
                 unset($options[$key]);
             } else {
                 $options[$key] = filter_var($input[$key], FILTER_VALIDATE_FLOAT);
             }
         }
     }
     if (property_exists($this, 'predefined')) {
         foreach ($this->predefined as $key) {
             if (empty($input[$key])) {
                 unset($options[$key]);
             } else {
                 $options[$key] = $input[$key];
             }
         }
     }
     return $options;
 }