Пример #1
0
 /**
  * @covers \Pressbooks\Sanitize\remove_control_characters
  */
 public function test_remove_control_characters()
 {
     $var = "Hello-World!";
     $test = $this->_generateControlCharacters() . $var;
     $test = \Pressbooks\Sanitize\remove_control_characters($test);
     $this->assertEquals(12, strlen($test));
     $var = "Héllö Wôrld!";
     $test = \Pressbooks\Sanitize\remove_control_characters($var);
     $this->assertEquals(12, mb_strlen($test, 'UTF-8'));
     $var = "こんにちは世界!";
     $test = \Pressbooks\Sanitize\remove_control_characters($var);
     $this->assertEquals(8, mb_strlen($test, 'UTF-8'));
 }
Пример #2
0
 /**
  * Returns book information in a useful, string only, format. Data is converted to HTML.
  *
  * @return array
  */
 static function getBookInformation($id = '')
 {
     // -----------------------------------------------------------------------------
     // Is cached?
     // -----------------------------------------------------------------------------
     if (!empty($id) && is_int($id)) {
         // @codingStandardsIgnoreLine
         $blog_id = $id;
         switch_to_blog($blog_id);
     } else {
         global $blog_id;
     }
     $cache_id = "book-inf-{$blog_id}";
     $book_information = wp_cache_get($cache_id, 'pb');
     if ($book_information) {
         return $book_information;
     }
     // ----------------------------------------------------------------------------
     // Book Information
     // ----------------------------------------------------------------------------
     $expected_array = array('pb_keywords_tags', 'pb_bisac_subject', 'pb_contributing_authors');
     $expected_the_content = array('pb_custom_copyright', 'pb_about_unlimited');
     $expected_url = array('pb_cover_image');
     $book_information = array();
     $meta = new Metadata();
     $data = $meta->getMetaPostMetadata();
     foreach ($data as $key => $val) {
         // Skip anything not prefixed with pb_
         if (!preg_match('/^pb_/', $key)) {
             continue;
         }
         // We only care about strings
         if (is_array($val)) {
             if (false !== in_array($key, $expected_array)) {
                 $val = implode(', ', $val);
             } else {
                 $val = array_values($val);
                 $val = array_pop($val);
             }
         }
         // Skip empty values
         if (!trim($val)) {
             continue;
         }
         if (false !== in_array($key, $expected_the_content)) {
             $val = wptexturize($val);
             $val = wpautop($val);
         } else {
             $val = htmlspecialchars($val, ENT_NOQUOTES | ENT_XHTML, 'UTF-8', false);
         }
         // Normalize URLs
         if (in_array($key, $expected_url)) {
             $val = set_url_scheme($val);
         }
         // Remove invisible control characters that break XML
         $val = \Pressbooks\Sanitize\remove_control_characters($val);
         $book_information[$key] = $val;
     }
     // Return our best guess if no book information has been entered.
     if (empty($book_information)) {
         $book_information['pb_title'] = get_bloginfo('name');
         if (!function_exists('get_user_by')) {
             include ABSPATH . 'wp-includes/pluggable.php';
         }
         $author = get_user_by('email', get_bloginfo('admin_email'));
         $book_information['pb_author'] = isset($author->display_name) ? $author->display_name : '';
         $book_information['pb_cover_image'] = \Pressbooks\Image\default_cover_url();
     }
     // -----------------------------------------------------------------------------
     // Cache & Return
     // -----------------------------------------------------------------------------
     wp_cache_set($cache_id, $book_information, 'pb', 86400);
     if (!empty($id) && is_int($id)) {
         restore_current_blog();
     }
     return $book_information;
 }