repository_name stringlengths 5 67 | func_path_in_repository stringlengths 4 234 | func_name stringlengths 0 314 | whole_func_string stringlengths 52 3.87M | language stringclasses 6 values | func_code_string stringlengths 52 3.87M | func_code_tokens listlengths 15 672k | func_documentation_string stringlengths 1 47.2k | func_documentation_tokens listlengths 1 3.92k | split_name stringclasses 1 value | func_code_url stringlengths 85 339 |
|---|---|---|---|---|---|---|---|---|---|---|
pressbooks/pressbooks | inc/class-styles.php | Styles.isCurrentThemeCompatible | public function isCurrentThemeCompatible( $version = 1, $theme = null ) {
if ( null === $theme ) {
$theme = wp_get_theme();
}
$basepath = $this->getDir( $theme );
$types = [
'prince',
'epub',
'web',
];
foreach ( $types as $type ) {
$path = '';
if ( 1 === $version && 'web' !== $type ) {
$path = $basepath . "/export/$type/style.scss";
} elseif ( 1 === $version && 'web' === $type ) {
$path = $basepath . '/style.scss';
}
if ( 2 === $version ) {
$path = $basepath . "/assets/styles/$type/style.scss";
}
$fullpath = realpath( $path );
if ( ! is_file( $fullpath ) ) {
return false;
}
}
return true;
} | php | public function isCurrentThemeCompatible( $version = 1, $theme = null ) {
if ( null === $theme ) {
$theme = wp_get_theme();
}
$basepath = $this->getDir( $theme );
$types = [
'prince',
'epub',
'web',
];
foreach ( $types as $type ) {
$path = '';
if ( 1 === $version && 'web' !== $type ) {
$path = $basepath . "/export/$type/style.scss";
} elseif ( 1 === $version && 'web' === $type ) {
$path = $basepath . '/style.scss';
}
if ( 2 === $version ) {
$path = $basepath . "/assets/styles/$type/style.scss";
}
$fullpath = realpath( $path );
if ( ! is_file( $fullpath ) ) {
return false;
}
}
return true;
} | [
"public",
"function",
"isCurrentThemeCompatible",
"(",
"$",
"version",
"=",
"1",
",",
"$",
"theme",
"=",
"null",
")",
"{",
"if",
"(",
"null",
"===",
"$",
"theme",
")",
"{",
"$",
"theme",
"=",
"wp_get_theme",
"(",
")",
";",
"}",
"$",
"basepath",
"=",
"$",
"this",
"->",
"getDir",
"(",
"$",
"theme",
")",
";",
"$",
"types",
"=",
"[",
"'prince'",
",",
"'epub'",
",",
"'web'",
",",
"]",
";",
"foreach",
"(",
"$",
"types",
"as",
"$",
"type",
")",
"{",
"$",
"path",
"=",
"''",
";",
"if",
"(",
"1",
"===",
"$",
"version",
"&&",
"'web'",
"!==",
"$",
"type",
")",
"{",
"$",
"path",
"=",
"$",
"basepath",
".",
"\"/export/$type/style.scss\"",
";",
"}",
"elseif",
"(",
"1",
"===",
"$",
"version",
"&&",
"'web'",
"===",
"$",
"type",
")",
"{",
"$",
"path",
"=",
"$",
"basepath",
".",
"'/style.scss'",
";",
"}",
"if",
"(",
"2",
"===",
"$",
"version",
")",
"{",
"$",
"path",
"=",
"$",
"basepath",
".",
"\"/assets/styles/$type/style.scss\"",
";",
"}",
"$",
"fullpath",
"=",
"realpath",
"(",
"$",
"path",
")",
";",
"if",
"(",
"!",
"is_file",
"(",
"$",
"fullpath",
")",
")",
"{",
"return",
"false",
";",
"}",
"}",
"return",
"true",
";",
"}"
] | Are the current theme's stylesheets SCSS compatible?
@param int $version
@param \WP_Theme $theme (optional)
@return bool | [
"Are",
"the",
"current",
"theme",
"s",
"stylesheets",
"SCSS",
"compatible?"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-styles.php#L324-L357 |
pressbooks/pressbooks | inc/class-styles.php | Styles.getBuckramVersion | public function getBuckramVersion() {
$fullpath = realpath( $this->sass->pathToGlobals() . 'buckram.scss' );
if ( is_file( $fullpath ) ) {
return get_file_data(
$fullpath,
[
'version' => 'Version',
]
)['version'];
}
return false; // No version available.
} | php | public function getBuckramVersion() {
$fullpath = realpath( $this->sass->pathToGlobals() . 'buckram.scss' );
if ( is_file( $fullpath ) ) {
return get_file_data(
$fullpath,
[
'version' => 'Version',
]
)['version'];
}
return false; // No version available.
} | [
"public",
"function",
"getBuckramVersion",
"(",
")",
"{",
"$",
"fullpath",
"=",
"realpath",
"(",
"$",
"this",
"->",
"sass",
"->",
"pathToGlobals",
"(",
")",
".",
"'buckram.scss'",
")",
";",
"if",
"(",
"is_file",
"(",
"$",
"fullpath",
")",
")",
"{",
"return",
"get_file_data",
"(",
"$",
"fullpath",
",",
"[",
"'version'",
"=>",
"'Version'",
",",
"]",
")",
"[",
"'version'",
"]",
";",
"}",
"return",
"false",
";",
"// No version available.",
"}"
] | Get the version of Buckram for the current install or locked theme.
@since 5.0.0
@see https://github.com/pressbooks/buckram/blob/master/styles/buckram.scss
@return string|bool | [
"Get",
"the",
"version",
"of",
"Buckram",
"for",
"the",
"current",
"install",
"or",
"locked",
"theme",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-styles.php#L368-L379 |
pressbooks/pressbooks | inc/class-styles.php | Styles.hasBuckram | public function hasBuckram( $version = 0 ) {
if ( $this->isCurrentThemeCompatible( 2 ) && version_compare( $this->getBuckramVersion(), $version ) >= 0 ) {
return true;
}
return false;
} | php | public function hasBuckram( $version = 0 ) {
if ( $this->isCurrentThemeCompatible( 2 ) && version_compare( $this->getBuckramVersion(), $version ) >= 0 ) {
return true;
}
return false;
} | [
"public",
"function",
"hasBuckram",
"(",
"$",
"version",
"=",
"0",
")",
"{",
"if",
"(",
"$",
"this",
"->",
"isCurrentThemeCompatible",
"(",
"2",
")",
"&&",
"version_compare",
"(",
"$",
"this",
"->",
"getBuckramVersion",
"(",
")",
",",
"$",
"version",
")",
">=",
"0",
")",
"{",
"return",
"true",
";",
"}",
"return",
"false",
";",
"}"
] | Check that the currently active theme uses Buckram (optionally a minimum version of Buckram).
@since 5.3.0
@param int|string $version
@return bool | [
"Check",
"that",
"the",
"currently",
"active",
"theme",
"uses",
"Buckram",
"(",
"optionally",
"a",
"minimum",
"version",
"of",
"Buckram",
")",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-styles.php#L390-L395 |
pressbooks/pressbooks | inc/class-styles.php | Styles.customizeWeb | public function customizeWeb( $overrides = [] ) {
$path = $this->getPathToWebScss();
if ( $path ) {
return $this->customize( 'web', \Pressbooks\Utility\get_contents( $path ), $overrides );
}
return '';
} | php | public function customizeWeb( $overrides = [] ) {
$path = $this->getPathToWebScss();
if ( $path ) {
return $this->customize( 'web', \Pressbooks\Utility\get_contents( $path ), $overrides );
}
return '';
} | [
"public",
"function",
"customizeWeb",
"(",
"$",
"overrides",
"=",
"[",
"]",
")",
"{",
"$",
"path",
"=",
"$",
"this",
"->",
"getPathToWebScss",
"(",
")",
";",
"if",
"(",
"$",
"path",
")",
"{",
"return",
"$",
"this",
"->",
"customize",
"(",
"'web'",
",",
"\\",
"Pressbooks",
"\\",
"Utility",
"\\",
"get_contents",
"(",
"$",
"path",
")",
",",
"$",
"overrides",
")",
";",
"}",
"return",
"''",
";",
"}"
] | @param array|string $overrides (optional)
@return string | [
"@param",
"array|string",
"$overrides",
"(",
"optional",
")"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-styles.php#L402-L408 |
pressbooks/pressbooks | inc/class-styles.php | Styles.customizePrince | public function customizePrince( $overrides = [] ) {
$path = $this->getPathToPrinceScss();
if ( $path ) {
return $this->customize( 'prince', \Pressbooks\Utility\get_contents( $path ), $overrides );
}
return '';
} | php | public function customizePrince( $overrides = [] ) {
$path = $this->getPathToPrinceScss();
if ( $path ) {
return $this->customize( 'prince', \Pressbooks\Utility\get_contents( $path ), $overrides );
}
return '';
} | [
"public",
"function",
"customizePrince",
"(",
"$",
"overrides",
"=",
"[",
"]",
")",
"{",
"$",
"path",
"=",
"$",
"this",
"->",
"getPathToPrinceScss",
"(",
")",
";",
"if",
"(",
"$",
"path",
")",
"{",
"return",
"$",
"this",
"->",
"customize",
"(",
"'prince'",
",",
"\\",
"Pressbooks",
"\\",
"Utility",
"\\",
"get_contents",
"(",
"$",
"path",
")",
",",
"$",
"overrides",
")",
";",
"}",
"return",
"''",
";",
"}"
] | @param array|string $overrides (optional)
@return string | [
"@param",
"array|string",
"$overrides",
"(",
"optional",
")"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-styles.php#L415-L421 |
pressbooks/pressbooks | inc/class-styles.php | Styles.customizeEpub | public function customizeEpub( $overrides = [] ) {
$path = $this->getPathToEpubScss();
if ( $path ) {
return $this->customize( 'epub', \Pressbooks\Utility\get_contents( $path ), $overrides );
}
return '';
} | php | public function customizeEpub( $overrides = [] ) {
$path = $this->getPathToEpubScss();
if ( $path ) {
return $this->customize( 'epub', \Pressbooks\Utility\get_contents( $path ), $overrides );
}
return '';
} | [
"public",
"function",
"customizeEpub",
"(",
"$",
"overrides",
"=",
"[",
"]",
")",
"{",
"$",
"path",
"=",
"$",
"this",
"->",
"getPathToEpubScss",
"(",
")",
";",
"if",
"(",
"$",
"path",
")",
"{",
"return",
"$",
"this",
"->",
"customize",
"(",
"'epub'",
",",
"\\",
"Pressbooks",
"\\",
"Utility",
"\\",
"get_contents",
"(",
"$",
"path",
")",
",",
"$",
"overrides",
")",
";",
"}",
"return",
"''",
";",
"}"
] | @param array|string $overrides (optional)
@return string | [
"@param",
"array|string",
"$overrides",
"(",
"optional",
")"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-styles.php#L428-L434 |
pressbooks/pressbooks | inc/class-styles.php | Styles.customize | public function customize( $type, $scss, $overrides = [] ) {
$scss = $this->applyOverrides( $scss, $overrides );
// Apply Theme Options
if ( $type === 'prince' ) {
$scss = apply_filters( 'pb_pdf_css_override', $scss );
} else {
$scss = apply_filters( "pb_{$type}_css_override", $scss );
}
if ( $this->isCurrentThemeCompatible( 1 ) ) {
$css = $this->sass->compile(
$scss,
[
$this->sass->pathToUserGeneratedSass(),
$this->sass->pathToPartials(),
$this->sass->pathToFonts(),
$this->getDir(),
]
);
} elseif ( $this->isCurrentThemeCompatible( 2 ) ) {
$css = $this->sass->compile(
$scss,
$this->sass->defaultIncludePaths( $type )
);
} elseif ( CustomCss::isCustomCss() ) {
// Compile pressbooks-book web stylesheet when using the *DEPRECATED* Custom CSS theme
$custom_css_includes = $this->sass->defaultIncludePaths( $type, wp_get_theme( 'pressbooks-book' ) );
// Append deprecated partials path last, in case we can't find old stuff in previous paths
$custom_css_includes[] = $this->sass->pathToPartials();
$css = $this->sass->compile(
$scss,
$custom_css_includes
);
} else {
$css = $this->injectHouseStyles( $scss );
}
return $css;
} | php | public function customize( $type, $scss, $overrides = [] ) {
$scss = $this->applyOverrides( $scss, $overrides );
// Apply Theme Options
if ( $type === 'prince' ) {
$scss = apply_filters( 'pb_pdf_css_override', $scss );
} else {
$scss = apply_filters( "pb_{$type}_css_override", $scss );
}
if ( $this->isCurrentThemeCompatible( 1 ) ) {
$css = $this->sass->compile(
$scss,
[
$this->sass->pathToUserGeneratedSass(),
$this->sass->pathToPartials(),
$this->sass->pathToFonts(),
$this->getDir(),
]
);
} elseif ( $this->isCurrentThemeCompatible( 2 ) ) {
$css = $this->sass->compile(
$scss,
$this->sass->defaultIncludePaths( $type )
);
} elseif ( CustomCss::isCustomCss() ) {
// Compile pressbooks-book web stylesheet when using the *DEPRECATED* Custom CSS theme
$custom_css_includes = $this->sass->defaultIncludePaths( $type, wp_get_theme( 'pressbooks-book' ) );
// Append deprecated partials path last, in case we can't find old stuff in previous paths
$custom_css_includes[] = $this->sass->pathToPartials();
$css = $this->sass->compile(
$scss,
$custom_css_includes
);
} else {
$css = $this->injectHouseStyles( $scss );
}
return $css;
} | [
"public",
"function",
"customize",
"(",
"$",
"type",
",",
"$",
"scss",
",",
"$",
"overrides",
"=",
"[",
"]",
")",
"{",
"$",
"scss",
"=",
"$",
"this",
"->",
"applyOverrides",
"(",
"$",
"scss",
",",
"$",
"overrides",
")",
";",
"// Apply Theme Options",
"if",
"(",
"$",
"type",
"===",
"'prince'",
")",
"{",
"$",
"scss",
"=",
"apply_filters",
"(",
"'pb_pdf_css_override'",
",",
"$",
"scss",
")",
";",
"}",
"else",
"{",
"$",
"scss",
"=",
"apply_filters",
"(",
"\"pb_{$type}_css_override\"",
",",
"$",
"scss",
")",
";",
"}",
"if",
"(",
"$",
"this",
"->",
"isCurrentThemeCompatible",
"(",
"1",
")",
")",
"{",
"$",
"css",
"=",
"$",
"this",
"->",
"sass",
"->",
"compile",
"(",
"$",
"scss",
",",
"[",
"$",
"this",
"->",
"sass",
"->",
"pathToUserGeneratedSass",
"(",
")",
",",
"$",
"this",
"->",
"sass",
"->",
"pathToPartials",
"(",
")",
",",
"$",
"this",
"->",
"sass",
"->",
"pathToFonts",
"(",
")",
",",
"$",
"this",
"->",
"getDir",
"(",
")",
",",
"]",
")",
";",
"}",
"elseif",
"(",
"$",
"this",
"->",
"isCurrentThemeCompatible",
"(",
"2",
")",
")",
"{",
"$",
"css",
"=",
"$",
"this",
"->",
"sass",
"->",
"compile",
"(",
"$",
"scss",
",",
"$",
"this",
"->",
"sass",
"->",
"defaultIncludePaths",
"(",
"$",
"type",
")",
")",
";",
"}",
"elseif",
"(",
"CustomCss",
"::",
"isCustomCss",
"(",
")",
")",
"{",
"// Compile pressbooks-book web stylesheet when using the *DEPRECATED* Custom CSS theme",
"$",
"custom_css_includes",
"=",
"$",
"this",
"->",
"sass",
"->",
"defaultIncludePaths",
"(",
"$",
"type",
",",
"wp_get_theme",
"(",
"'pressbooks-book'",
")",
")",
";",
"// Append deprecated partials path last, in case we can't find old stuff in previous paths",
"$",
"custom_css_includes",
"[",
"]",
"=",
"$",
"this",
"->",
"sass",
"->",
"pathToPartials",
"(",
")",
";",
"$",
"css",
"=",
"$",
"this",
"->",
"sass",
"->",
"compile",
"(",
"$",
"scss",
",",
"$",
"custom_css_includes",
")",
";",
"}",
"else",
"{",
"$",
"css",
"=",
"$",
"this",
"->",
"injectHouseStyles",
"(",
"$",
"scss",
")",
";",
"}",
"return",
"$",
"css",
";",
"}"
] | Transpile SCSS based on theme compatibility
@param string $type
@param string $scss
@param array|string $overrides
@return string | [
"Transpile",
"SCSS",
"based",
"on",
"theme",
"compatibility"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-styles.php#L445-L484 |
pressbooks/pressbooks | inc/class-styles.php | Styles.applyOverrides | public function applyOverrides( $scss, $overrides = [] ) {
if ( ! is_array( $overrides ) ) {
$overrides = (array) $overrides;
}
$overrides = implode( "\n", $overrides );
if ( $this->isCurrentThemeCompatible( 2 ) ) {
// Prepend override variables (see: http://sass-lang.com/documentation/file.SASS_REFERENCE.html#variable_defaults_).
$scss = $overrides . "\n" . $scss;
} else {
// Append overrides.
$scss .= "\n" . $overrides;
}
return $scss;
} | php | public function applyOverrides( $scss, $overrides = [] ) {
if ( ! is_array( $overrides ) ) {
$overrides = (array) $overrides;
}
$overrides = implode( "\n", $overrides );
if ( $this->isCurrentThemeCompatible( 2 ) ) {
// Prepend override variables (see: http://sass-lang.com/documentation/file.SASS_REFERENCE.html#variable_defaults_).
$scss = $overrides . "\n" . $scss;
} else {
// Append overrides.
$scss .= "\n" . $overrides;
}
return $scss;
} | [
"public",
"function",
"applyOverrides",
"(",
"$",
"scss",
",",
"$",
"overrides",
"=",
"[",
"]",
")",
"{",
"if",
"(",
"!",
"is_array",
"(",
"$",
"overrides",
")",
")",
"{",
"$",
"overrides",
"=",
"(",
"array",
")",
"$",
"overrides",
";",
"}",
"$",
"overrides",
"=",
"implode",
"(",
"\"\\n\"",
",",
"$",
"overrides",
")",
";",
"if",
"(",
"$",
"this",
"->",
"isCurrentThemeCompatible",
"(",
"2",
")",
")",
"{",
"// Prepend override variables (see: http://sass-lang.com/documentation/file.SASS_REFERENCE.html#variable_defaults_).",
"$",
"scss",
"=",
"$",
"overrides",
".",
"\"\\n\"",
".",
"$",
"scss",
";",
"}",
"else",
"{",
"// Append overrides.",
"$",
"scss",
".=",
"\"\\n\"",
".",
"$",
"overrides",
";",
"}",
"return",
"$",
"scss",
";",
"}"
] | Prepend or append SCSS overrides depending on which version of the theme architecture is in use.
@param string $scss
@param array|string $overrides
@return string | [
"Prepend",
"or",
"append",
"SCSS",
"overrides",
"depending",
"on",
"which",
"version",
"of",
"the",
"theme",
"architecture",
"is",
"in",
"use",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-styles.php#L494-L510 |
pressbooks/pressbooks | inc/class-styles.php | Styles.injectHouseStyles | public function injectHouseStyles( $css ) {
$scan = [
'/*__INSERT_PDF_HOUSE_STYLE__*/' => get_theme_root( 'pressbooks-book' ) . '/pressbooks-book/assets/legacy/styles/_pdf-house-style.scss',
'/*__INSERT_EPUB_HOUSE_STYLE__*/' => get_theme_root( 'pressbooks-book' ) . '/pressbooks-book/assets/legacy/styles/_epub-house-style.scss',
'/*__INSERT_MOBI_HOUSE_STYLE__*/' => get_theme_root( 'pressbooks-book' ) . '/pressbooks-book/assets/legacy/styles/_mobi-house-style.scss',
];
foreach ( $scan as $token => $replace_with ) {
if ( is_file( $replace_with ) ) {
$css = str_replace( $token, \Pressbooks\Utility\get_contents( $replace_with ), $css );
}
}
return $css;
} | php | public function injectHouseStyles( $css ) {
$scan = [
'/*__INSERT_PDF_HOUSE_STYLE__*/' => get_theme_root( 'pressbooks-book' ) . '/pressbooks-book/assets/legacy/styles/_pdf-house-style.scss',
'/*__INSERT_EPUB_HOUSE_STYLE__*/' => get_theme_root( 'pressbooks-book' ) . '/pressbooks-book/assets/legacy/styles/_epub-house-style.scss',
'/*__INSERT_MOBI_HOUSE_STYLE__*/' => get_theme_root( 'pressbooks-book' ) . '/pressbooks-book/assets/legacy/styles/_mobi-house-style.scss',
];
foreach ( $scan as $token => $replace_with ) {
if ( is_file( $replace_with ) ) {
$css = str_replace( $token, \Pressbooks\Utility\get_contents( $replace_with ), $css );
}
}
return $css;
} | [
"public",
"function",
"injectHouseStyles",
"(",
"$",
"css",
")",
"{",
"$",
"scan",
"=",
"[",
"'/*__INSERT_PDF_HOUSE_STYLE__*/'",
"=>",
"get_theme_root",
"(",
"'pressbooks-book'",
")",
".",
"'/pressbooks-book/assets/legacy/styles/_pdf-house-style.scss'",
",",
"'/*__INSERT_EPUB_HOUSE_STYLE__*/'",
"=>",
"get_theme_root",
"(",
"'pressbooks-book'",
")",
".",
"'/pressbooks-book/assets/legacy/styles/_epub-house-style.scss'",
",",
"'/*__INSERT_MOBI_HOUSE_STYLE__*/'",
"=>",
"get_theme_root",
"(",
"'pressbooks-book'",
")",
".",
"'/pressbooks-book/assets/legacy/styles/_mobi-house-style.scss'",
",",
"]",
";",
"foreach",
"(",
"$",
"scan",
"as",
"$",
"token",
"=>",
"$",
"replace_with",
")",
"{",
"if",
"(",
"is_file",
"(",
"$",
"replace_with",
")",
")",
"{",
"$",
"css",
"=",
"str_replace",
"(",
"$",
"token",
",",
"\\",
"Pressbooks",
"\\",
"Utility",
"\\",
"get_contents",
"(",
"$",
"replace_with",
")",
",",
"$",
"css",
")",
";",
"}",
"}",
"return",
"$",
"css",
";",
"}"
] | Inject house styles into CSS
@param string $css
@return string | [
"Inject",
"house",
"styles",
"into",
"CSS"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-styles.php#L519-L534 |
pressbooks/pressbooks | inc/class-styles.php | Styles.updateWebBookStyleSheet | public function updateWebBookStyleSheet( $stylesheet = null ) {
if ( CustomCss::isCustomCss() ) {
// Compile pressbooks-book web stylesheet when using the *DEPRECATED* Custom CSS theme
$theme = wp_get_theme( 'pressbooks-book' );
} else {
$theme = wp_get_theme( $stylesheet );
}
// Populate $url-base variable so that links to images and other assets remain intact
$overrides = [ '$url-base: "' . $theme->get_stylesheet_directory_uri() . '";' ];
if ( $this->isCurrentThemeCompatible( 1 ) ) {
$scss = \Pressbooks\Utility\get_contents( realpath( $this->getDir( $theme ) . '/style.scss' ) );
} elseif ( $this->isCurrentThemeCompatible( 2 ) || CustomCss::isCustomCss() ) {
$scss = \Pressbooks\Utility\get_contents( realpath( $this->getDir( $theme ) . '/assets/styles/web/style.scss' ) );
} else {
return;
}
$custom_styles = $this->getWebPost();
if ( $custom_styles && ! empty( $custom_styles->post_content ) ) {
// append the user's custom styles to the theme stylesheet prior to compilation
$scss .= "\n" . $custom_styles->post_content;
}
$css = $this->customize( 'web', $scss, $overrides );
$css = normalize_css_urls( $css );
$css_file = $this->sass->pathToUserGeneratedCss() . '/style.css';
\Pressbooks\Utility\put_contents( $css_file, $css );
} | php | public function updateWebBookStyleSheet( $stylesheet = null ) {
if ( CustomCss::isCustomCss() ) {
// Compile pressbooks-book web stylesheet when using the *DEPRECATED* Custom CSS theme
$theme = wp_get_theme( 'pressbooks-book' );
} else {
$theme = wp_get_theme( $stylesheet );
}
// Populate $url-base variable so that links to images and other assets remain intact
$overrides = [ '$url-base: "' . $theme->get_stylesheet_directory_uri() . '";' ];
if ( $this->isCurrentThemeCompatible( 1 ) ) {
$scss = \Pressbooks\Utility\get_contents( realpath( $this->getDir( $theme ) . '/style.scss' ) );
} elseif ( $this->isCurrentThemeCompatible( 2 ) || CustomCss::isCustomCss() ) {
$scss = \Pressbooks\Utility\get_contents( realpath( $this->getDir( $theme ) . '/assets/styles/web/style.scss' ) );
} else {
return;
}
$custom_styles = $this->getWebPost();
if ( $custom_styles && ! empty( $custom_styles->post_content ) ) {
// append the user's custom styles to the theme stylesheet prior to compilation
$scss .= "\n" . $custom_styles->post_content;
}
$css = $this->customize( 'web', $scss, $overrides );
$css = normalize_css_urls( $css );
$css_file = $this->sass->pathToUserGeneratedCss() . '/style.css';
\Pressbooks\Utility\put_contents( $css_file, $css );
} | [
"public",
"function",
"updateWebBookStyleSheet",
"(",
"$",
"stylesheet",
"=",
"null",
")",
"{",
"if",
"(",
"CustomCss",
"::",
"isCustomCss",
"(",
")",
")",
"{",
"// Compile pressbooks-book web stylesheet when using the *DEPRECATED* Custom CSS theme",
"$",
"theme",
"=",
"wp_get_theme",
"(",
"'pressbooks-book'",
")",
";",
"}",
"else",
"{",
"$",
"theme",
"=",
"wp_get_theme",
"(",
"$",
"stylesheet",
")",
";",
"}",
"// Populate $url-base variable so that links to images and other assets remain intact",
"$",
"overrides",
"=",
"[",
"'$url-base: \"'",
".",
"$",
"theme",
"->",
"get_stylesheet_directory_uri",
"(",
")",
".",
"'\";'",
"]",
";",
"if",
"(",
"$",
"this",
"->",
"isCurrentThemeCompatible",
"(",
"1",
")",
")",
"{",
"$",
"scss",
"=",
"\\",
"Pressbooks",
"\\",
"Utility",
"\\",
"get_contents",
"(",
"realpath",
"(",
"$",
"this",
"->",
"getDir",
"(",
"$",
"theme",
")",
".",
"'/style.scss'",
")",
")",
";",
"}",
"elseif",
"(",
"$",
"this",
"->",
"isCurrentThemeCompatible",
"(",
"2",
")",
"||",
"CustomCss",
"::",
"isCustomCss",
"(",
")",
")",
"{",
"$",
"scss",
"=",
"\\",
"Pressbooks",
"\\",
"Utility",
"\\",
"get_contents",
"(",
"realpath",
"(",
"$",
"this",
"->",
"getDir",
"(",
"$",
"theme",
")",
".",
"'/assets/styles/web/style.scss'",
")",
")",
";",
"}",
"else",
"{",
"return",
";",
"}",
"$",
"custom_styles",
"=",
"$",
"this",
"->",
"getWebPost",
"(",
")",
";",
"if",
"(",
"$",
"custom_styles",
"&&",
"!",
"empty",
"(",
"$",
"custom_styles",
"->",
"post_content",
")",
")",
"{",
"// append the user's custom styles to the theme stylesheet prior to compilation",
"$",
"scss",
".=",
"\"\\n\"",
".",
"$",
"custom_styles",
"->",
"post_content",
";",
"}",
"$",
"css",
"=",
"$",
"this",
"->",
"customize",
"(",
"'web'",
",",
"$",
"scss",
",",
"$",
"overrides",
")",
";",
"$",
"css",
"=",
"normalize_css_urls",
"(",
"$",
"css",
")",
";",
"$",
"css_file",
"=",
"$",
"this",
"->",
"sass",
"->",
"pathToUserGeneratedCss",
"(",
")",
".",
"'/style.css'",
";",
"\\",
"Pressbooks",
"\\",
"Utility",
"\\",
"put_contents",
"(",
"$",
"css_file",
",",
"$",
"css",
")",
";",
"}"
] | Update and save the supplementary webBook stylesheet which incorporates user options, etc.
@param string $stylesheet Directory name for the theme. Defaults to current theme.
@return void | [
"Update",
"and",
"save",
"the",
"supplementary",
"webBook",
"stylesheet",
"which",
"incorporates",
"user",
"options",
"etc",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-styles.php#L542-L573 |
pressbooks/pressbooks | inc/class-styles.php | Styles.maybeUpdateStylesheets | public function maybeUpdateStylesheets() {
// If this is ajax/cron/404, don't update right now
if ( wp_doing_ajax() || wp_doing_cron() || is_404() ) {
return false;
}
// Compare current and previous Buckram versions
$current_buckram_version = $this->getBuckramVersion();
$last_buckram_version = get_option( 'pressbooks_buckram_version' );
$buckram_updated = version_compare( $current_buckram_version, $last_buckram_version ) > 0;
// Compare current and previous theme versions
$theme = wp_get_theme();
$current_theme_version = $theme->get( 'Version' );
$last_theme_version = get_option( 'pressbooks_theme_version' );
$theme_updated = version_compare( $current_theme_version, $last_theme_version ) > 0;
// If either Buckram or the theme were updated, rebuild the web and editor stylesheets.
if ( $buckram_updated || $theme_updated ) {
// Try to stop a Cache Stampede, Dog-Pile, Cascading Failure...
if ( ! get_transient( 'pressbooks_updating_stylesheet' ) ) {
set_transient( 'pressbooks_updating_stylesheet', 1, 5 * MINUTE_IN_SECONDS );
( new Modules\ThemeOptions\Admin() )->clearCache();
$this->updateWebBookStyleSheet();
update_editor_style();
if ( $buckram_updated ) {
update_option( 'pressbooks_buckram_version', $current_buckram_version );
}
if ( $theme_updated ) {
update_option( 'pressbooks_theme_version', $current_theme_version );
}
delete_transient( 'pressbooks_updating_stylesheet' );
return true;
}
}
return false;
} | php | public function maybeUpdateStylesheets() {
// If this is ajax/cron/404, don't update right now
if ( wp_doing_ajax() || wp_doing_cron() || is_404() ) {
return false;
}
// Compare current and previous Buckram versions
$current_buckram_version = $this->getBuckramVersion();
$last_buckram_version = get_option( 'pressbooks_buckram_version' );
$buckram_updated = version_compare( $current_buckram_version, $last_buckram_version ) > 0;
// Compare current and previous theme versions
$theme = wp_get_theme();
$current_theme_version = $theme->get( 'Version' );
$last_theme_version = get_option( 'pressbooks_theme_version' );
$theme_updated = version_compare( $current_theme_version, $last_theme_version ) > 0;
// If either Buckram or the theme were updated, rebuild the web and editor stylesheets.
if ( $buckram_updated || $theme_updated ) {
// Try to stop a Cache Stampede, Dog-Pile, Cascading Failure...
if ( ! get_transient( 'pressbooks_updating_stylesheet' ) ) {
set_transient( 'pressbooks_updating_stylesheet', 1, 5 * MINUTE_IN_SECONDS );
( new Modules\ThemeOptions\Admin() )->clearCache();
$this->updateWebBookStyleSheet();
update_editor_style();
if ( $buckram_updated ) {
update_option( 'pressbooks_buckram_version', $current_buckram_version );
}
if ( $theme_updated ) {
update_option( 'pressbooks_theme_version', $current_theme_version );
}
delete_transient( 'pressbooks_updating_stylesheet' );
return true;
}
}
return false;
} | [
"public",
"function",
"maybeUpdateStylesheets",
"(",
")",
"{",
"// If this is ajax/cron/404, don't update right now",
"if",
"(",
"wp_doing_ajax",
"(",
")",
"||",
"wp_doing_cron",
"(",
")",
"||",
"is_404",
"(",
")",
")",
"{",
"return",
"false",
";",
"}",
"// Compare current and previous Buckram versions",
"$",
"current_buckram_version",
"=",
"$",
"this",
"->",
"getBuckramVersion",
"(",
")",
";",
"$",
"last_buckram_version",
"=",
"get_option",
"(",
"'pressbooks_buckram_version'",
")",
";",
"$",
"buckram_updated",
"=",
"version_compare",
"(",
"$",
"current_buckram_version",
",",
"$",
"last_buckram_version",
")",
">",
"0",
";",
"// Compare current and previous theme versions",
"$",
"theme",
"=",
"wp_get_theme",
"(",
")",
";",
"$",
"current_theme_version",
"=",
"$",
"theme",
"->",
"get",
"(",
"'Version'",
")",
";",
"$",
"last_theme_version",
"=",
"get_option",
"(",
"'pressbooks_theme_version'",
")",
";",
"$",
"theme_updated",
"=",
"version_compare",
"(",
"$",
"current_theme_version",
",",
"$",
"last_theme_version",
")",
">",
"0",
";",
"// If either Buckram or the theme were updated, rebuild the web and editor stylesheets.",
"if",
"(",
"$",
"buckram_updated",
"||",
"$",
"theme_updated",
")",
"{",
"// Try to stop a Cache Stampede, Dog-Pile, Cascading Failure...",
"if",
"(",
"!",
"get_transient",
"(",
"'pressbooks_updating_stylesheet'",
")",
")",
"{",
"set_transient",
"(",
"'pressbooks_updating_stylesheet'",
",",
"1",
",",
"5",
"*",
"MINUTE_IN_SECONDS",
")",
";",
"(",
"new",
"Modules",
"\\",
"ThemeOptions",
"\\",
"Admin",
"(",
")",
")",
"->",
"clearCache",
"(",
")",
";",
"$",
"this",
"->",
"updateWebBookStyleSheet",
"(",
")",
";",
"update_editor_style",
"(",
")",
";",
"if",
"(",
"$",
"buckram_updated",
")",
"{",
"update_option",
"(",
"'pressbooks_buckram_version'",
",",
"$",
"current_buckram_version",
")",
";",
"}",
"if",
"(",
"$",
"theme_updated",
")",
"{",
"update_option",
"(",
"'pressbooks_theme_version'",
",",
"$",
"current_theme_version",
")",
";",
"}",
"delete_transient",
"(",
"'pressbooks_updating_stylesheet'",
")",
";",
"return",
"true",
";",
"}",
"}",
"return",
"false",
";",
"}"
] | If the current theme's version or Buckram's version has increased, do SCSS stuff
@return bool | [
"If",
"the",
"current",
"theme",
"s",
"version",
"or",
"Buckram",
"s",
"version",
"has",
"increased",
"do",
"SCSS",
"stuff"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-styles.php#L580-L617 |
pressbooks/pressbooks | inc/class-styles.php | Styles.renderDropdownForSlugs | public function renderDropdownForSlugs( $slug ) {
$select_name = 'slug';
$select_id = $select_name;
$redirect_url = get_admin_url( get_current_blog_id(), '/themes.php?page=' . $this::PAGE . '&slug=' );
$html = '';
$html .= "
<script type='text/javascript'>
// <![CDATA[
jQuery.noConflict();
jQuery(function ($) {
$('#" . $select_id . "').change(function() {
window.location = '" . $redirect_url . "' + $(this).val();
});
});
// ]]>
</script>";
$html .= '<select id="' . $select_id . '" name="' . $select_name . '">';
foreach ( $this->supported as $key => $val ) {
$html .= '<option value="' . $key . '"';
if ( $key === $slug ) {
$html .= ' selected="selected"';
}
if ( 'Web' === $val ) {
$val = __( 'Web', 'pressbooks' );
}
$html .= '>' . $val . '</option>';
}
$html .= '</select>';
return $html;
} | php | public function renderDropdownForSlugs( $slug ) {
$select_name = 'slug';
$select_id = $select_name;
$redirect_url = get_admin_url( get_current_blog_id(), '/themes.php?page=' . $this::PAGE . '&slug=' );
$html = '';
$html .= "
<script type='text/javascript'>
// <![CDATA[
jQuery.noConflict();
jQuery(function ($) {
$('#" . $select_id . "').change(function() {
window.location = '" . $redirect_url . "' + $(this).val();
});
});
// ]]>
</script>";
$html .= '<select id="' . $select_id . '" name="' . $select_name . '">';
foreach ( $this->supported as $key => $val ) {
$html .= '<option value="' . $key . '"';
if ( $key === $slug ) {
$html .= ' selected="selected"';
}
if ( 'Web' === $val ) {
$val = __( 'Web', 'pressbooks' );
}
$html .= '>' . $val . '</option>';
}
$html .= '</select>';
return $html;
} | [
"public",
"function",
"renderDropdownForSlugs",
"(",
"$",
"slug",
")",
"{",
"$",
"select_name",
"=",
"'slug'",
";",
"$",
"select_id",
"=",
"$",
"select_name",
";",
"$",
"redirect_url",
"=",
"get_admin_url",
"(",
"get_current_blog_id",
"(",
")",
",",
"'/themes.php?page='",
".",
"$",
"this",
"::",
"PAGE",
".",
"'&slug='",
")",
";",
"$",
"html",
"=",
"''",
";",
"$",
"html",
".=",
"\"\n\t<script type='text/javascript'>\n // <![CDATA[\n\tjQuery.noConflict();\n\tjQuery(function ($) {\n\t\t$('#\"",
".",
"$",
"select_id",
".",
"\"').change(function() {\n\t\t window.location = '\"",
".",
"$",
"redirect_url",
".",
"\"' + $(this).val();\n\t\t});\n\t});\n\t// ]]>\n </script>\"",
";",
"$",
"html",
".=",
"'<select id=\"'",
".",
"$",
"select_id",
".",
"'\" name=\"'",
".",
"$",
"select_name",
".",
"'\">'",
";",
"foreach",
"(",
"$",
"this",
"->",
"supported",
"as",
"$",
"key",
"=>",
"$",
"val",
")",
"{",
"$",
"html",
".=",
"'<option value=\"'",
".",
"$",
"key",
".",
"'\"'",
";",
"if",
"(",
"$",
"key",
"===",
"$",
"slug",
")",
"{",
"$",
"html",
".=",
"' selected=\"selected\"'",
";",
"}",
"if",
"(",
"'Web'",
"===",
"$",
"val",
")",
"{",
"$",
"val",
"=",
"__",
"(",
"'Web'",
",",
"'pressbooks'",
")",
";",
"}",
"$",
"html",
".=",
"'>'",
".",
"$",
"val",
".",
"'</option>'",
";",
"}",
"$",
"html",
".=",
"'</select>'",
";",
"return",
"$",
"html",
";",
"}"
] | Render dropdown and JavaScript for slugs.
@param string $slug
@return string | [
"Render",
"dropdown",
"and",
"JavaScript",
"for",
"slugs",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-styles.php#L651-L683 |
pressbooks/pressbooks | inc/class-styles.php | Styles.renderRevisionsTable | public function renderRevisionsTable( $slug, $post_id ) {
$args = [
'posts_per_page' => 10,
'post_type' => 'revision',
'post_status' => 'inherit',
'post_parent' => $post_id,
'orderby' => 'date',
'order' => 'DESC',
];
$q = new \WP_Query();
$results = $q->query( $args );
$html = '<table class="widefat fixed">';
$html .= '<thead><tr><th>' . __( 'Last 10 Revisions', 'pressbooks' ) . ' <em>(' . $this->supported[ $slug ] . ')</em> </th></tr></thead><tbody>';
foreach ( $results as $post ) {
$html .= '<tr><td>' . wp_post_revision_title( $post ) . ' ';
$html .= __( 'by', 'pressbooks' ) . ' ' . get_userdata( $post->post_author )->user_login . '</td></tr>';
}
$html .= '</tbody></table>';
return $html;
} | php | public function renderRevisionsTable( $slug, $post_id ) {
$args = [
'posts_per_page' => 10,
'post_type' => 'revision',
'post_status' => 'inherit',
'post_parent' => $post_id,
'orderby' => 'date',
'order' => 'DESC',
];
$q = new \WP_Query();
$results = $q->query( $args );
$html = '<table class="widefat fixed">';
$html .= '<thead><tr><th>' . __( 'Last 10 Revisions', 'pressbooks' ) . ' <em>(' . $this->supported[ $slug ] . ')</em> </th></tr></thead><tbody>';
foreach ( $results as $post ) {
$html .= '<tr><td>' . wp_post_revision_title( $post ) . ' ';
$html .= __( 'by', 'pressbooks' ) . ' ' . get_userdata( $post->post_author )->user_login . '</td></tr>';
}
$html .= '</tbody></table>';
return $html;
} | [
"public",
"function",
"renderRevisionsTable",
"(",
"$",
"slug",
",",
"$",
"post_id",
")",
"{",
"$",
"args",
"=",
"[",
"'posts_per_page'",
"=>",
"10",
",",
"'post_type'",
"=>",
"'revision'",
",",
"'post_status'",
"=>",
"'inherit'",
",",
"'post_parent'",
"=>",
"$",
"post_id",
",",
"'orderby'",
"=>",
"'date'",
",",
"'order'",
"=>",
"'DESC'",
",",
"]",
";",
"$",
"q",
"=",
"new",
"\\",
"WP_Query",
"(",
")",
";",
"$",
"results",
"=",
"$",
"q",
"->",
"query",
"(",
"$",
"args",
")",
";",
"$",
"html",
"=",
"'<table class=\"widefat fixed\">'",
";",
"$",
"html",
".=",
"'<thead><tr><th>'",
".",
"__",
"(",
"'Last 10 Revisions'",
",",
"'pressbooks'",
")",
".",
"' <em>('",
".",
"$",
"this",
"->",
"supported",
"[",
"$",
"slug",
"]",
".",
"')</em> </th></tr></thead><tbody>'",
";",
"foreach",
"(",
"$",
"results",
"as",
"$",
"post",
")",
"{",
"$",
"html",
".=",
"'<tr><td>'",
".",
"wp_post_revision_title",
"(",
"$",
"post",
")",
".",
"' '",
";",
"$",
"html",
".=",
"__",
"(",
"'by'",
",",
"'pressbooks'",
")",
".",
"' '",
".",
"get_userdata",
"(",
"$",
"post",
"->",
"post_author",
")",
"->",
"user_login",
".",
"'</td></tr>'",
";",
"}",
"$",
"html",
".=",
"'</tbody></table>'",
";",
"return",
"$",
"html",
";",
"}"
] | Render table for revisions.
@param string $slug
@param int $post_id
@return string | [
"Render",
"table",
"for",
"revisions",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-styles.php#L693-L716 |
pressbooks/pressbooks | inc/class-styles.php | Styles.formSubmit | public function formSubmit() {
if ( empty( $this->isFormSubmission() ) || empty( current_user_can( 'edit_others_posts' ) ) ) {
// Don't do anything in this function, bail.
return;
}
// Process form
if ( isset( $_GET['custom_styles'] ) && $_GET['custom_styles'] === 'yes' && isset( $_POST['your_styles'] ) && check_admin_referer( 'pb-custom-styles' ) ) {
$slug = isset( $_POST['slug'] ) ? $_POST['slug'] : 'web';
$redirect_url = get_admin_url( get_current_blog_id(), '/themes.php?page=' . $this::PAGE . '&slug=' . $slug );
if ( ! isset( $_POST['post_id'], $_POST['post_id_integrity'] ) ) {
debug_error_log( __METHOD__ . ' error: Missing post ID' );
\Pressbooks\Redirect\location( $redirect_url . '&custom_styles_error=true' );
}
if ( md5( NONCE_KEY . $_POST['post_id'] ) !== $_POST['post_id_integrity'] ) {
// A hacker trying to overwrite posts?.
debug_error_log( __METHOD__ . ' error: unexpected value for post_id_integrity' );
\Pressbooks\Redirect\location( $redirect_url . '&custom_styles_error=true' );
}
// Remove wp_filter_post_kses, this causes CSS escaping issues
remove_filter( 'content_save_pre', 'wp_filter_post_kses' );
remove_filter( 'content_filtered_save_pre', 'wp_filter_post_kses' );
remove_all_filters( 'content_save_pre' );
// Write to database
$my_post = [
'ID' => absint( $_POST['post_id'] ),
'post_content' => \Pressbooks\Sanitize\cleanup_css( $_POST['your_styles'] ),
];
$response = wp_update_post( $my_post, true );
if ( is_wp_error( $response ) ) {
// Something went wrong?
debug_error_log( __METHOD__ . ' error, wp_update_post(): ' . $response->get_error_message() );
\Pressbooks\Redirect\location( $redirect_url . '&custom_styles_error=true' );
}
if ( $slug === 'web' ) {
// a recompile will be triggered whenever the user saves custom styles targeting web
$this->updateWebBookStyleSheet();
}
// Ok!
\Pressbooks\Redirect\location( $redirect_url );
}
} | php | public function formSubmit() {
if ( empty( $this->isFormSubmission() ) || empty( current_user_can( 'edit_others_posts' ) ) ) {
// Don't do anything in this function, bail.
return;
}
// Process form
if ( isset( $_GET['custom_styles'] ) && $_GET['custom_styles'] === 'yes' && isset( $_POST['your_styles'] ) && check_admin_referer( 'pb-custom-styles' ) ) {
$slug = isset( $_POST['slug'] ) ? $_POST['slug'] : 'web';
$redirect_url = get_admin_url( get_current_blog_id(), '/themes.php?page=' . $this::PAGE . '&slug=' . $slug );
if ( ! isset( $_POST['post_id'], $_POST['post_id_integrity'] ) ) {
debug_error_log( __METHOD__ . ' error: Missing post ID' );
\Pressbooks\Redirect\location( $redirect_url . '&custom_styles_error=true' );
}
if ( md5( NONCE_KEY . $_POST['post_id'] ) !== $_POST['post_id_integrity'] ) {
// A hacker trying to overwrite posts?.
debug_error_log( __METHOD__ . ' error: unexpected value for post_id_integrity' );
\Pressbooks\Redirect\location( $redirect_url . '&custom_styles_error=true' );
}
// Remove wp_filter_post_kses, this causes CSS escaping issues
remove_filter( 'content_save_pre', 'wp_filter_post_kses' );
remove_filter( 'content_filtered_save_pre', 'wp_filter_post_kses' );
remove_all_filters( 'content_save_pre' );
// Write to database
$my_post = [
'ID' => absint( $_POST['post_id'] ),
'post_content' => \Pressbooks\Sanitize\cleanup_css( $_POST['your_styles'] ),
];
$response = wp_update_post( $my_post, true );
if ( is_wp_error( $response ) ) {
// Something went wrong?
debug_error_log( __METHOD__ . ' error, wp_update_post(): ' . $response->get_error_message() );
\Pressbooks\Redirect\location( $redirect_url . '&custom_styles_error=true' );
}
if ( $slug === 'web' ) {
// a recompile will be triggered whenever the user saves custom styles targeting web
$this->updateWebBookStyleSheet();
}
// Ok!
\Pressbooks\Redirect\location( $redirect_url );
}
} | [
"public",
"function",
"formSubmit",
"(",
")",
"{",
"if",
"(",
"empty",
"(",
"$",
"this",
"->",
"isFormSubmission",
"(",
")",
")",
"||",
"empty",
"(",
"current_user_can",
"(",
"'edit_others_posts'",
")",
")",
")",
"{",
"// Don't do anything in this function, bail.",
"return",
";",
"}",
"// Process form",
"if",
"(",
"isset",
"(",
"$",
"_GET",
"[",
"'custom_styles'",
"]",
")",
"&&",
"$",
"_GET",
"[",
"'custom_styles'",
"]",
"===",
"'yes'",
"&&",
"isset",
"(",
"$",
"_POST",
"[",
"'your_styles'",
"]",
")",
"&&",
"check_admin_referer",
"(",
"'pb-custom-styles'",
")",
")",
"{",
"$",
"slug",
"=",
"isset",
"(",
"$",
"_POST",
"[",
"'slug'",
"]",
")",
"?",
"$",
"_POST",
"[",
"'slug'",
"]",
":",
"'web'",
";",
"$",
"redirect_url",
"=",
"get_admin_url",
"(",
"get_current_blog_id",
"(",
")",
",",
"'/themes.php?page='",
".",
"$",
"this",
"::",
"PAGE",
".",
"'&slug='",
".",
"$",
"slug",
")",
";",
"if",
"(",
"!",
"isset",
"(",
"$",
"_POST",
"[",
"'post_id'",
"]",
",",
"$",
"_POST",
"[",
"'post_id_integrity'",
"]",
")",
")",
"{",
"debug_error_log",
"(",
"__METHOD__",
".",
"' error: Missing post ID'",
")",
";",
"\\",
"Pressbooks",
"\\",
"Redirect",
"\\",
"location",
"(",
"$",
"redirect_url",
".",
"'&custom_styles_error=true'",
")",
";",
"}",
"if",
"(",
"md5",
"(",
"NONCE_KEY",
".",
"$",
"_POST",
"[",
"'post_id'",
"]",
")",
"!==",
"$",
"_POST",
"[",
"'post_id_integrity'",
"]",
")",
"{",
"// A hacker trying to overwrite posts?.",
"debug_error_log",
"(",
"__METHOD__",
".",
"' error: unexpected value for post_id_integrity'",
")",
";",
"\\",
"Pressbooks",
"\\",
"Redirect",
"\\",
"location",
"(",
"$",
"redirect_url",
".",
"'&custom_styles_error=true'",
")",
";",
"}",
"// Remove wp_filter_post_kses, this causes CSS escaping issues",
"remove_filter",
"(",
"'content_save_pre'",
",",
"'wp_filter_post_kses'",
")",
";",
"remove_filter",
"(",
"'content_filtered_save_pre'",
",",
"'wp_filter_post_kses'",
")",
";",
"remove_all_filters",
"(",
"'content_save_pre'",
")",
";",
"// Write to database",
"$",
"my_post",
"=",
"[",
"'ID'",
"=>",
"absint",
"(",
"$",
"_POST",
"[",
"'post_id'",
"]",
")",
",",
"'post_content'",
"=>",
"\\",
"Pressbooks",
"\\",
"Sanitize",
"\\",
"cleanup_css",
"(",
"$",
"_POST",
"[",
"'your_styles'",
"]",
")",
",",
"]",
";",
"$",
"response",
"=",
"wp_update_post",
"(",
"$",
"my_post",
",",
"true",
")",
";",
"if",
"(",
"is_wp_error",
"(",
"$",
"response",
")",
")",
"{",
"// Something went wrong?",
"debug_error_log",
"(",
"__METHOD__",
".",
"' error, wp_update_post(): '",
".",
"$",
"response",
"->",
"get_error_message",
"(",
")",
")",
";",
"\\",
"Pressbooks",
"\\",
"Redirect",
"\\",
"location",
"(",
"$",
"redirect_url",
".",
"'&custom_styles_error=true'",
")",
";",
"}",
"if",
"(",
"$",
"slug",
"===",
"'web'",
")",
"{",
"// a recompile will be triggered whenever the user saves custom styles targeting web",
"$",
"this",
"->",
"updateWebBookStyleSheet",
"(",
")",
";",
"}",
"// Ok!",
"\\",
"Pressbooks",
"\\",
"Redirect",
"\\",
"location",
"(",
"$",
"redirect_url",
")",
";",
"}",
"}"
] | Save custom styles to database | [
"Save",
"custom",
"styles",
"to",
"database"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-styles.php#L721-L770 |
pressbooks/pressbooks | inc/shortcodes/class-tablepress.php | TablePress.loadShortcodes | public function loadShortcodes() {
add_action(
'tablepress_run', function () {
if ( \Pressbooks\Modules\Export\Export::isFormSubmission() ) {
\TablePress::$model_options = \TablePress::load_model( 'options' );
\TablePress::$model_table = \TablePress::load_model( 'table' );
$GLOBALS['tablepress_frontend_controller'] = \TablePress::load_controller( 'frontend' );
}
}
);
add_filter(
'tablepress_edit_link_below_table', function ( $show ) {
if ( \Pressbooks\Modules\Export\Export::isFormSubmission() ) {
return false;
}
return $show;
}
);
} | php | public function loadShortcodes() {
add_action(
'tablepress_run', function () {
if ( \Pressbooks\Modules\Export\Export::isFormSubmission() ) {
\TablePress::$model_options = \TablePress::load_model( 'options' );
\TablePress::$model_table = \TablePress::load_model( 'table' );
$GLOBALS['tablepress_frontend_controller'] = \TablePress::load_controller( 'frontend' );
}
}
);
add_filter(
'tablepress_edit_link_below_table', function ( $show ) {
if ( \Pressbooks\Modules\Export\Export::isFormSubmission() ) {
return false;
}
return $show;
}
);
} | [
"public",
"function",
"loadShortcodes",
"(",
")",
"{",
"add_action",
"(",
"'tablepress_run'",
",",
"function",
"(",
")",
"{",
"if",
"(",
"\\",
"Pressbooks",
"\\",
"Modules",
"\\",
"Export",
"\\",
"Export",
"::",
"isFormSubmission",
"(",
")",
")",
"{",
"\\",
"TablePress",
"::",
"$",
"model_options",
"=",
"\\",
"TablePress",
"::",
"load_model",
"(",
"'options'",
")",
";",
"\\",
"TablePress",
"::",
"$",
"model_table",
"=",
"\\",
"TablePress",
"::",
"load_model",
"(",
"'table'",
")",
";",
"$",
"GLOBALS",
"[",
"'tablepress_frontend_controller'",
"]",
"=",
"\\",
"TablePress",
"::",
"load_controller",
"(",
"'frontend'",
")",
";",
"}",
"}",
")",
";",
"add_filter",
"(",
"'tablepress_edit_link_below_table'",
",",
"function",
"(",
"$",
"show",
")",
"{",
"if",
"(",
"\\",
"Pressbooks",
"\\",
"Modules",
"\\",
"Export",
"\\",
"Export",
"::",
"isFormSubmission",
"(",
")",
")",
"{",
"return",
"false",
";",
"}",
"return",
"$",
"show",
";",
"}",
")",
";",
"}"
] | Add actions and filters to TablePress to load shortcodes in the admin context. | [
"Add",
"actions",
"and",
"filters",
"to",
"TablePress",
"to",
"load",
"shortcodes",
"in",
"the",
"admin",
"context",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/shortcodes/class-tablepress.php#L47-L65 |
pressbooks/pressbooks | inc/modules/export/prince/class-docraptor.php | Docraptor.convert | public function convert() {
// Sanity check
if ( empty( $this->exportStylePath ) || ! is_file( $this->exportStylePath ) ) {
$this->logError( '$this->exportStylePath must be set before calling convert().' );
return false;
}
// Set logfile
$this->logfile = $this->createTmpFile();
// Set filename
$filename = $this->generateFileName();
$this->outputPath = $filename;
// Fonts
Container::get( 'GlobalTypography' )->getFonts();
// CSS
$this->truncateExportStylesheets( 'prince' );
$timestamp = time();
$css = $this->kneadCss();
$css_file = \Pressbooks\Container::get( 'Sass' )->pathToUserGeneratedCss() . "/prince-$timestamp.css";
\Pressbooks\Utility\put_contents( $css_file, $css );
// --------------------------------------------------------------------
// Save PDF as file in exports folder
$configuration = \DocRaptor\Configuration::getDefaultConfiguration();
$configuration->setUsername( DOCRAPTOR_API_KEY );
$docraptor = new \DocRaptor\DocApi();
$prince_options = new \DocRaptor\PrinceOptions();
$prince_options->setNoCompress( false );
$prince_options->setHttpTimeout( max( ini_get( 'max_execution_time' ), 30 ) );
$prince_options->setProfile( $this->pdfProfile );
$retval = false;
try {
$doc = new \DocRaptor\Doc();
if ( defined( 'WP_TESTS_MULTISITE' ) ) {
// Unit tests
$document_content = str_replace( '</head>', "<style>$css</style></head>", \Pressbooks\Utility\get_contents( $this->url ) );
$doc->setTest( true );
$doc->setDocumentContent( $document_content );
} elseif ( defined( 'WP_ENV' ) && ( WP_ENV === 'development' ) ) {
// Instead of a localhost URL that DocRaptor can't see, send a document
$response = wp_remote_get( $this->url );
if ( is_wp_error( $response ) ) {
$this->logError( $response->get_error_message() );
return false;
}
$document_content = str_replace( '</head>', "<style>$css</style></head>", $response['body'] );
$doc->setTest( true );
$doc->setDocumentContent( $document_content );
} else {
// The real thing
$doc->setTest( false );
$doc->setDocumentUrl( $this->url );
}
$doc->setName( get_bloginfo( 'name' ) );
$doc->setPrinceOptions( $prince_options );
$create_response = $docraptor->createAsyncDoc( $doc );
$done = false;
while ( ! $done ) {
$status_response = $docraptor->getAsyncDocStatus( $create_response->getStatusId() );
switch ( $status_response->getStatus() ) {
case 'completed':
if ( ! function_exists( 'download_url' ) ) {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
}
$result = \download_url( $status_response->getDownloadUrl() );
if ( is_wp_error( $result ) ) {
$_SESSION['pb_errors'][] = __( 'Your PDF could not be retrieved.', 'pressbooks-docraptor' );
} else {
copy( $result, $this->outputPath );
unlink( $result );
$retval = true;
}
$done = true;
$exportoptions = get_option( 'pressbooks_export_options' );
if ( isset( $exportoptions['email_validation_logs'] ) && 1 === absint( $exportoptions['email_validation_logs'] ) ) {
$msg = $this->getDetailedLog( $create_response->getStatusId() );
\Pressbooks\Utility\put_contents( $this->logfile, $msg );
}
break;
case 'failed':
$msg = $status_response;
\Pressbooks\Utility\put_contents( $this->logfile, $msg );
$done = true;
break;
default:
sleep( 1 );
}
}
} catch ( \DocRaptor\ApiException $exception ) {
$msg = $exception->getResponseBody();
\Pressbooks\Utility\put_contents( $this->logfile, $msg );
}
if ( ! empty( $msg ) ) {
$this->logError( \Pressbooks\Utility\get_contents( $this->logfile ) );
}
return $retval;
} | php | public function convert() {
// Sanity check
if ( empty( $this->exportStylePath ) || ! is_file( $this->exportStylePath ) ) {
$this->logError( '$this->exportStylePath must be set before calling convert().' );
return false;
}
// Set logfile
$this->logfile = $this->createTmpFile();
// Set filename
$filename = $this->generateFileName();
$this->outputPath = $filename;
// Fonts
Container::get( 'GlobalTypography' )->getFonts();
// CSS
$this->truncateExportStylesheets( 'prince' );
$timestamp = time();
$css = $this->kneadCss();
$css_file = \Pressbooks\Container::get( 'Sass' )->pathToUserGeneratedCss() . "/prince-$timestamp.css";
\Pressbooks\Utility\put_contents( $css_file, $css );
// --------------------------------------------------------------------
// Save PDF as file in exports folder
$configuration = \DocRaptor\Configuration::getDefaultConfiguration();
$configuration->setUsername( DOCRAPTOR_API_KEY );
$docraptor = new \DocRaptor\DocApi();
$prince_options = new \DocRaptor\PrinceOptions();
$prince_options->setNoCompress( false );
$prince_options->setHttpTimeout( max( ini_get( 'max_execution_time' ), 30 ) );
$prince_options->setProfile( $this->pdfProfile );
$retval = false;
try {
$doc = new \DocRaptor\Doc();
if ( defined( 'WP_TESTS_MULTISITE' ) ) {
// Unit tests
$document_content = str_replace( '</head>', "<style>$css</style></head>", \Pressbooks\Utility\get_contents( $this->url ) );
$doc->setTest( true );
$doc->setDocumentContent( $document_content );
} elseif ( defined( 'WP_ENV' ) && ( WP_ENV === 'development' ) ) {
// Instead of a localhost URL that DocRaptor can't see, send a document
$response = wp_remote_get( $this->url );
if ( is_wp_error( $response ) ) {
$this->logError( $response->get_error_message() );
return false;
}
$document_content = str_replace( '</head>', "<style>$css</style></head>", $response['body'] );
$doc->setTest( true );
$doc->setDocumentContent( $document_content );
} else {
// The real thing
$doc->setTest( false );
$doc->setDocumentUrl( $this->url );
}
$doc->setName( get_bloginfo( 'name' ) );
$doc->setPrinceOptions( $prince_options );
$create_response = $docraptor->createAsyncDoc( $doc );
$done = false;
while ( ! $done ) {
$status_response = $docraptor->getAsyncDocStatus( $create_response->getStatusId() );
switch ( $status_response->getStatus() ) {
case 'completed':
if ( ! function_exists( 'download_url' ) ) {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
}
$result = \download_url( $status_response->getDownloadUrl() );
if ( is_wp_error( $result ) ) {
$_SESSION['pb_errors'][] = __( 'Your PDF could not be retrieved.', 'pressbooks-docraptor' );
} else {
copy( $result, $this->outputPath );
unlink( $result );
$retval = true;
}
$done = true;
$exportoptions = get_option( 'pressbooks_export_options' );
if ( isset( $exportoptions['email_validation_logs'] ) && 1 === absint( $exportoptions['email_validation_logs'] ) ) {
$msg = $this->getDetailedLog( $create_response->getStatusId() );
\Pressbooks\Utility\put_contents( $this->logfile, $msg );
}
break;
case 'failed':
$msg = $status_response;
\Pressbooks\Utility\put_contents( $this->logfile, $msg );
$done = true;
break;
default:
sleep( 1 );
}
}
} catch ( \DocRaptor\ApiException $exception ) {
$msg = $exception->getResponseBody();
\Pressbooks\Utility\put_contents( $this->logfile, $msg );
}
if ( ! empty( $msg ) ) {
$this->logError( \Pressbooks\Utility\get_contents( $this->logfile ) );
}
return $retval;
} | [
"public",
"function",
"convert",
"(",
")",
"{",
"// Sanity check",
"if",
"(",
"empty",
"(",
"$",
"this",
"->",
"exportStylePath",
")",
"||",
"!",
"is_file",
"(",
"$",
"this",
"->",
"exportStylePath",
")",
")",
"{",
"$",
"this",
"->",
"logError",
"(",
"'$this->exportStylePath must be set before calling convert().'",
")",
";",
"return",
"false",
";",
"}",
"// Set logfile",
"$",
"this",
"->",
"logfile",
"=",
"$",
"this",
"->",
"createTmpFile",
"(",
")",
";",
"// Set filename",
"$",
"filename",
"=",
"$",
"this",
"->",
"generateFileName",
"(",
")",
";",
"$",
"this",
"->",
"outputPath",
"=",
"$",
"filename",
";",
"// Fonts",
"Container",
"::",
"get",
"(",
"'GlobalTypography'",
")",
"->",
"getFonts",
"(",
")",
";",
"// CSS",
"$",
"this",
"->",
"truncateExportStylesheets",
"(",
"'prince'",
")",
";",
"$",
"timestamp",
"=",
"time",
"(",
")",
";",
"$",
"css",
"=",
"$",
"this",
"->",
"kneadCss",
"(",
")",
";",
"$",
"css_file",
"=",
"\\",
"Pressbooks",
"\\",
"Container",
"::",
"get",
"(",
"'Sass'",
")",
"->",
"pathToUserGeneratedCss",
"(",
")",
".",
"\"/prince-$timestamp.css\"",
";",
"\\",
"Pressbooks",
"\\",
"Utility",
"\\",
"put_contents",
"(",
"$",
"css_file",
",",
"$",
"css",
")",
";",
"// --------------------------------------------------------------------",
"// Save PDF as file in exports folder",
"$",
"configuration",
"=",
"\\",
"DocRaptor",
"\\",
"Configuration",
"::",
"getDefaultConfiguration",
"(",
")",
";",
"$",
"configuration",
"->",
"setUsername",
"(",
"DOCRAPTOR_API_KEY",
")",
";",
"$",
"docraptor",
"=",
"new",
"\\",
"DocRaptor",
"\\",
"DocApi",
"(",
")",
";",
"$",
"prince_options",
"=",
"new",
"\\",
"DocRaptor",
"\\",
"PrinceOptions",
"(",
")",
";",
"$",
"prince_options",
"->",
"setNoCompress",
"(",
"false",
")",
";",
"$",
"prince_options",
"->",
"setHttpTimeout",
"(",
"max",
"(",
"ini_get",
"(",
"'max_execution_time'",
")",
",",
"30",
")",
")",
";",
"$",
"prince_options",
"->",
"setProfile",
"(",
"$",
"this",
"->",
"pdfProfile",
")",
";",
"$",
"retval",
"=",
"false",
";",
"try",
"{",
"$",
"doc",
"=",
"new",
"\\",
"DocRaptor",
"\\",
"Doc",
"(",
")",
";",
"if",
"(",
"defined",
"(",
"'WP_TESTS_MULTISITE'",
")",
")",
"{",
"// Unit tests",
"$",
"document_content",
"=",
"str_replace",
"(",
"'</head>'",
",",
"\"<style>$css</style></head>\"",
",",
"\\",
"Pressbooks",
"\\",
"Utility",
"\\",
"get_contents",
"(",
"$",
"this",
"->",
"url",
")",
")",
";",
"$",
"doc",
"->",
"setTest",
"(",
"true",
")",
";",
"$",
"doc",
"->",
"setDocumentContent",
"(",
"$",
"document_content",
")",
";",
"}",
"elseif",
"(",
"defined",
"(",
"'WP_ENV'",
")",
"&&",
"(",
"WP_ENV",
"===",
"'development'",
")",
")",
"{",
"// Instead of a localhost URL that DocRaptor can't see, send a document",
"$",
"response",
"=",
"wp_remote_get",
"(",
"$",
"this",
"->",
"url",
")",
";",
"if",
"(",
"is_wp_error",
"(",
"$",
"response",
")",
")",
"{",
"$",
"this",
"->",
"logError",
"(",
"$",
"response",
"->",
"get_error_message",
"(",
")",
")",
";",
"return",
"false",
";",
"}",
"$",
"document_content",
"=",
"str_replace",
"(",
"'</head>'",
",",
"\"<style>$css</style></head>\"",
",",
"$",
"response",
"[",
"'body'",
"]",
")",
";",
"$",
"doc",
"->",
"setTest",
"(",
"true",
")",
";",
"$",
"doc",
"->",
"setDocumentContent",
"(",
"$",
"document_content",
")",
";",
"}",
"else",
"{",
"// The real thing",
"$",
"doc",
"->",
"setTest",
"(",
"false",
")",
";",
"$",
"doc",
"->",
"setDocumentUrl",
"(",
"$",
"this",
"->",
"url",
")",
";",
"}",
"$",
"doc",
"->",
"setName",
"(",
"get_bloginfo",
"(",
"'name'",
")",
")",
";",
"$",
"doc",
"->",
"setPrinceOptions",
"(",
"$",
"prince_options",
")",
";",
"$",
"create_response",
"=",
"$",
"docraptor",
"->",
"createAsyncDoc",
"(",
"$",
"doc",
")",
";",
"$",
"done",
"=",
"false",
";",
"while",
"(",
"!",
"$",
"done",
")",
"{",
"$",
"status_response",
"=",
"$",
"docraptor",
"->",
"getAsyncDocStatus",
"(",
"$",
"create_response",
"->",
"getStatusId",
"(",
")",
")",
";",
"switch",
"(",
"$",
"status_response",
"->",
"getStatus",
"(",
")",
")",
"{",
"case",
"'completed'",
":",
"if",
"(",
"!",
"function_exists",
"(",
"'download_url'",
")",
")",
"{",
"require_once",
"(",
"ABSPATH",
".",
"'wp-admin/includes/file.php'",
")",
";",
"}",
"$",
"result",
"=",
"\\",
"download_url",
"(",
"$",
"status_response",
"->",
"getDownloadUrl",
"(",
")",
")",
";",
"if",
"(",
"is_wp_error",
"(",
"$",
"result",
")",
")",
"{",
"$",
"_SESSION",
"[",
"'pb_errors'",
"]",
"[",
"]",
"=",
"__",
"(",
"'Your PDF could not be retrieved.'",
",",
"'pressbooks-docraptor'",
")",
";",
"}",
"else",
"{",
"copy",
"(",
"$",
"result",
",",
"$",
"this",
"->",
"outputPath",
")",
";",
"unlink",
"(",
"$",
"result",
")",
";",
"$",
"retval",
"=",
"true",
";",
"}",
"$",
"done",
"=",
"true",
";",
"$",
"exportoptions",
"=",
"get_option",
"(",
"'pressbooks_export_options'",
")",
";",
"if",
"(",
"isset",
"(",
"$",
"exportoptions",
"[",
"'email_validation_logs'",
"]",
")",
"&&",
"1",
"===",
"absint",
"(",
"$",
"exportoptions",
"[",
"'email_validation_logs'",
"]",
")",
")",
"{",
"$",
"msg",
"=",
"$",
"this",
"->",
"getDetailedLog",
"(",
"$",
"create_response",
"->",
"getStatusId",
"(",
")",
")",
";",
"\\",
"Pressbooks",
"\\",
"Utility",
"\\",
"put_contents",
"(",
"$",
"this",
"->",
"logfile",
",",
"$",
"msg",
")",
";",
"}",
"break",
";",
"case",
"'failed'",
":",
"$",
"msg",
"=",
"$",
"status_response",
";",
"\\",
"Pressbooks",
"\\",
"Utility",
"\\",
"put_contents",
"(",
"$",
"this",
"->",
"logfile",
",",
"$",
"msg",
")",
";",
"$",
"done",
"=",
"true",
";",
"break",
";",
"default",
":",
"sleep",
"(",
"1",
")",
";",
"}",
"}",
"}",
"catch",
"(",
"\\",
"DocRaptor",
"\\",
"ApiException",
"$",
"exception",
")",
"{",
"$",
"msg",
"=",
"$",
"exception",
"->",
"getResponseBody",
"(",
")",
";",
"\\",
"Pressbooks",
"\\",
"Utility",
"\\",
"put_contents",
"(",
"$",
"this",
"->",
"logfile",
",",
"$",
"msg",
")",
";",
"}",
"if",
"(",
"!",
"empty",
"(",
"$",
"msg",
")",
")",
"{",
"$",
"this",
"->",
"logError",
"(",
"\\",
"Pressbooks",
"\\",
"Utility",
"\\",
"get_contents",
"(",
"$",
"this",
"->",
"logfile",
")",
")",
";",
"}",
"return",
"$",
"retval",
";",
"}"
] | @since 5.4.0
Create $this->outputPath.
@return bool|string | [
"@since",
"5",
".",
"4",
".",
"0"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/export/prince/class-docraptor.php#L34-L140 |
pressbooks/pressbooks | inc/modules/export/prince/class-docraptor.php | Docraptor.getDetailedLog | protected function getDetailedLog( $id ) {
// @see: https://docraptor.com/documentation/api#doc_log_listing
$response = wp_remote_get( esc_url( 'https://docraptor.com/doc_logs.json?per_page=25&user_credentials=' . DOCRAPTOR_API_KEY ) );
if ( is_wp_error( $response ) ) {
return $response->get_error_message();
}
$logs = json_decode( $response['body'] );
if ( $logs ) {
foreach ( $logs as $log ) {
if ( $log->status_id == $id ) { // @codingStandardsIgnoreLine
return $log->generation_log;
}
}
}
return '';
} | php | protected function getDetailedLog( $id ) {
// @see: https://docraptor.com/documentation/api#doc_log_listing
$response = wp_remote_get( esc_url( 'https://docraptor.com/doc_logs.json?per_page=25&user_credentials=' . DOCRAPTOR_API_KEY ) );
if ( is_wp_error( $response ) ) {
return $response->get_error_message();
}
$logs = json_decode( $response['body'] );
if ( $logs ) {
foreach ( $logs as $log ) {
if ( $log->status_id == $id ) { // @codingStandardsIgnoreLine
return $log->generation_log;
}
}
}
return '';
} | [
"protected",
"function",
"getDetailedLog",
"(",
"$",
"id",
")",
"{",
"// @see: https://docraptor.com/documentation/api#doc_log_listing",
"$",
"response",
"=",
"wp_remote_get",
"(",
"esc_url",
"(",
"'https://docraptor.com/doc_logs.json?per_page=25&user_credentials='",
".",
"DOCRAPTOR_API_KEY",
")",
")",
";",
"if",
"(",
"is_wp_error",
"(",
"$",
"response",
")",
")",
"{",
"return",
"$",
"response",
"->",
"get_error_message",
"(",
")",
";",
"}",
"$",
"logs",
"=",
"json_decode",
"(",
"$",
"response",
"[",
"'body'",
"]",
")",
";",
"if",
"(",
"$",
"logs",
")",
"{",
"foreach",
"(",
"$",
"logs",
"as",
"$",
"log",
")",
"{",
"if",
"(",
"$",
"log",
"->",
"status_id",
"==",
"$",
"id",
")",
"{",
"// @codingStandardsIgnoreLine",
"return",
"$",
"log",
"->",
"generation_log",
";",
"}",
"}",
"}",
"return",
"''",
";",
"}"
] | When given a DocRaptor async status ID, return the document generation log for the relevant job.
@param string $id
@return string | [
"When",
"given",
"a",
"DocRaptor",
"async",
"status",
"ID",
"return",
"the",
"document",
"generation",
"log",
"for",
"the",
"relevant",
"job",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/export/prince/class-docraptor.php#L149-L164 |
pressbooks/pressbooks | inc/class-sass.php | Sass.defaultIncludePaths | public function defaultIncludePaths( $type, $theme = null ) {
if ( null === $theme ) {
$theme = wp_get_theme();
}
return [
$this->pathToUserGeneratedSass(),
$this->pathToGlobals(),
$this->pathToFonts(),
Container::get( 'Styles' )->getDir( $theme ) . "/assets/styles/$type/",
];
} | php | public function defaultIncludePaths( $type, $theme = null ) {
if ( null === $theme ) {
$theme = wp_get_theme();
}
return [
$this->pathToUserGeneratedSass(),
$this->pathToGlobals(),
$this->pathToFonts(),
Container::get( 'Styles' )->getDir( $theme ) . "/assets/styles/$type/",
];
} | [
"public",
"function",
"defaultIncludePaths",
"(",
"$",
"type",
",",
"$",
"theme",
"=",
"null",
")",
"{",
"if",
"(",
"null",
"===",
"$",
"theme",
")",
"{",
"$",
"theme",
"=",
"wp_get_theme",
"(",
")",
";",
"}",
"return",
"[",
"$",
"this",
"->",
"pathToUserGeneratedSass",
"(",
")",
",",
"$",
"this",
"->",
"pathToGlobals",
"(",
")",
",",
"$",
"this",
"->",
"pathToFonts",
"(",
")",
",",
"Container",
"::",
"get",
"(",
"'Styles'",
")",
"->",
"getDir",
"(",
"$",
"theme",
")",
".",
"\"/assets/styles/$type/\"",
",",
"]",
";",
"}"
] | Get default include paths
@param string $type
@param string $theme
@return array | [
"Get",
"default",
"include",
"paths"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-sass.php#L43-L55 |
pressbooks/pressbooks | inc/class-sass.php | Sass.getStringsToLocalize | public function getStringsToLocalize() {
$switched_locale = switch_to_locale( Modules\Export\Export::locale() );
$localized = [
/**
* Filter the label used for post types (front matter/parts/chapters/back matter) in the TOC and section headings.
*
* @since 5.6.0
*
* @param string $label
* @param array $args
*
* @return string Filtered label
*/
'chapter' => apply_filters( 'pb_post_type_label', __( 'Chapter', 'pressbooks' ), [ 'post_type' => 'chapter' ] ),
/**
* Filter the label used for post types (front matter/parts/chapters/back matter) in the TOC and section headings.
*
* @since 5.6.0
*
* @param string $label
* @param array $args
*
* @return string Filtered label
*/
'part' => apply_filters( 'pb_post_type_label', __( 'Part', 'pressbooks' ), [ 'post_type' => 'part' ] ),
];
if ( $switched_locale ) {
restore_previous_locale();
}
return $localized;
} | php | public function getStringsToLocalize() {
$switched_locale = switch_to_locale( Modules\Export\Export::locale() );
$localized = [
/**
* Filter the label used for post types (front matter/parts/chapters/back matter) in the TOC and section headings.
*
* @since 5.6.0
*
* @param string $label
* @param array $args
*
* @return string Filtered label
*/
'chapter' => apply_filters( 'pb_post_type_label', __( 'Chapter', 'pressbooks' ), [ 'post_type' => 'chapter' ] ),
/**
* Filter the label used for post types (front matter/parts/chapters/back matter) in the TOC and section headings.
*
* @since 5.6.0
*
* @param string $label
* @param array $args
*
* @return string Filtered label
*/
'part' => apply_filters( 'pb_post_type_label', __( 'Part', 'pressbooks' ), [ 'post_type' => 'part' ] ),
];
if ( $switched_locale ) {
restore_previous_locale();
}
return $localized;
} | [
"public",
"function",
"getStringsToLocalize",
"(",
")",
"{",
"$",
"switched_locale",
"=",
"switch_to_locale",
"(",
"Modules",
"\\",
"Export",
"\\",
"Export",
"::",
"locale",
"(",
")",
")",
";",
"$",
"localized",
"=",
"[",
"/**\n\t\t\t * Filter the label used for post types (front matter/parts/chapters/back matter) in the TOC and section headings.\n\t\t\t *\n\t\t\t * @since 5.6.0\n\t\t\t *\n\t\t\t * @param string $label\n\t\t\t * @param array $args\n\t\t\t *\n\t\t\t * @return string Filtered label\n\t\t\t */",
"'chapter'",
"=>",
"apply_filters",
"(",
"'pb_post_type_label'",
",",
"__",
"(",
"'Chapter'",
",",
"'pressbooks'",
")",
",",
"[",
"'post_type'",
"=>",
"'chapter'",
"]",
")",
",",
"/**\n\t\t\t * Filter the label used for post types (front matter/parts/chapters/back matter) in the TOC and section headings.\n\t\t\t *\n\t\t\t * @since 5.6.0\n\t\t\t *\n\t\t\t * @param string $label\n\t\t\t * @param array $args\n\t\t\t *\n\t\t\t * @return string Filtered label\n\t\t\t */",
"'part'",
"=>",
"apply_filters",
"(",
"'pb_post_type_label'",
",",
"__",
"(",
"'Part'",
",",
"'pressbooks'",
")",
",",
"[",
"'post_type'",
"=>",
"'part'",
"]",
")",
",",
"]",
";",
"if",
"(",
"$",
"switched_locale",
")",
"{",
"restore_previous_locale",
"(",
")",
";",
"}",
"return",
"$",
"localized",
";",
"}"
] | Fetch an array of strings in (S)CSS which need to be localized
@return array | [
"Fetch",
"an",
"array",
"of",
"strings",
"in",
"(",
"S",
")",
"CSS",
"which",
"need",
"to",
"be",
"localized"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-sass.php#L62-L96 |
pressbooks/pressbooks | inc/class-sass.php | Sass.compile | public function compile( $scss, $includes = [] ) {
$scss = $this->prependLocalizedVars( $scss );
try {
$css = '/* Silence is golden. */'; // If no SCSS input was passed, prevent file write errors by putting a comment in the CSS output.
$scssphp = new \Leafo\ScssPhp\Compiler;
if ( ! empty( $scss ) || ! empty( $this->vars ) ) {
$scssphp->setVariables( $this->vars );
$scssphp->setImportPaths( $includes );
$css = $scssphp->compile( $scss );
$this->vars = []; // Reset
}
} catch ( \Exception $e ) {
$error_message = print_r( $scssphp->getParsedFiles(), true ); // @codingStandardsIgnoreLine
$_SESSION['pb_errors'][] = sprintf(
__( 'There was a problem with SASS. Contact your site administrator. Error: %1$s %2$s', 'pressbooks' ),
$e->getMessage(),
"<pre>{$error_message}</pre>"
);
$this->logException( $e );
if ( WP_DEBUG ) {
$this->debug( "/* {$e->getMessage()} */", $scss, 'last-thrown-exception' );
}
return ''; // Return empty string on error
}
return $css;
} | php | public function compile( $scss, $includes = [] ) {
$scss = $this->prependLocalizedVars( $scss );
try {
$css = '/* Silence is golden. */'; // If no SCSS input was passed, prevent file write errors by putting a comment in the CSS output.
$scssphp = new \Leafo\ScssPhp\Compiler;
if ( ! empty( $scss ) || ! empty( $this->vars ) ) {
$scssphp->setVariables( $this->vars );
$scssphp->setImportPaths( $includes );
$css = $scssphp->compile( $scss );
$this->vars = []; // Reset
}
} catch ( \Exception $e ) {
$error_message = print_r( $scssphp->getParsedFiles(), true ); // @codingStandardsIgnoreLine
$_SESSION['pb_errors'][] = sprintf(
__( 'There was a problem with SASS. Contact your site administrator. Error: %1$s %2$s', 'pressbooks' ),
$e->getMessage(),
"<pre>{$error_message}</pre>"
);
$this->logException( $e );
if ( WP_DEBUG ) {
$this->debug( "/* {$e->getMessage()} */", $scss, 'last-thrown-exception' );
}
return ''; // Return empty string on error
}
return $css;
} | [
"public",
"function",
"compile",
"(",
"$",
"scss",
",",
"$",
"includes",
"=",
"[",
"]",
")",
"{",
"$",
"scss",
"=",
"$",
"this",
"->",
"prependLocalizedVars",
"(",
"$",
"scss",
")",
";",
"try",
"{",
"$",
"css",
"=",
"'/* Silence is golden. */'",
";",
"// If no SCSS input was passed, prevent file write errors by putting a comment in the CSS output.",
"$",
"scssphp",
"=",
"new",
"\\",
"Leafo",
"\\",
"ScssPhp",
"\\",
"Compiler",
";",
"if",
"(",
"!",
"empty",
"(",
"$",
"scss",
")",
"||",
"!",
"empty",
"(",
"$",
"this",
"->",
"vars",
")",
")",
"{",
"$",
"scssphp",
"->",
"setVariables",
"(",
"$",
"this",
"->",
"vars",
")",
";",
"$",
"scssphp",
"->",
"setImportPaths",
"(",
"$",
"includes",
")",
";",
"$",
"css",
"=",
"$",
"scssphp",
"->",
"compile",
"(",
"$",
"scss",
")",
";",
"$",
"this",
"->",
"vars",
"=",
"[",
"]",
";",
"// Reset",
"}",
"}",
"catch",
"(",
"\\",
"Exception",
"$",
"e",
")",
"{",
"$",
"error_message",
"=",
"print_r",
"(",
"$",
"scssphp",
"->",
"getParsedFiles",
"(",
")",
",",
"true",
")",
";",
"// @codingStandardsIgnoreLine",
"$",
"_SESSION",
"[",
"'pb_errors'",
"]",
"[",
"]",
"=",
"sprintf",
"(",
"__",
"(",
"'There was a problem with SASS. Contact your site administrator. Error: %1$s %2$s'",
",",
"'pressbooks'",
")",
",",
"$",
"e",
"->",
"getMessage",
"(",
")",
",",
"\"<pre>{$error_message}</pre>\"",
")",
";",
"$",
"this",
"->",
"logException",
"(",
"$",
"e",
")",
";",
"if",
"(",
"WP_DEBUG",
")",
"{",
"$",
"this",
"->",
"debug",
"(",
"\"/* {$e->getMessage()} */\"",
",",
"$",
"scss",
",",
"'last-thrown-exception'",
")",
";",
"}",
"return",
"''",
";",
"// Return empty string on error",
"}",
"return",
"$",
"css",
";",
"}"
] | Returns the compiled CSS from SCSS input
@param string $scss
@param array $includes (optional)
@return string | [
"Returns",
"the",
"compiled",
"CSS",
"from",
"SCSS",
"input"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-sass.php#L182-L214 |
pressbooks/pressbooks | inc/class-sass.php | Sass.prependLocalizedVars | public function prependLocalizedVars( $scss ) {
$strings = $this->getStringsToLocalize();
$localizations = '';
foreach ( $strings as $var => $string ) {
$localizations .= "\$$var: '$string';\n";
}
if ( WP_DEBUG ) {
$this->debug( '/* Silence is golden. */', $localizations, 'localizations' );
}
return $localizations . $scss;
} | php | public function prependLocalizedVars( $scss ) {
$strings = $this->getStringsToLocalize();
$localizations = '';
foreach ( $strings as $var => $string ) {
$localizations .= "\$$var: '$string';\n";
}
if ( WP_DEBUG ) {
$this->debug( '/* Silence is golden. */', $localizations, 'localizations' );
}
return $localizations . $scss;
} | [
"public",
"function",
"prependLocalizedVars",
"(",
"$",
"scss",
")",
"{",
"$",
"strings",
"=",
"$",
"this",
"->",
"getStringsToLocalize",
"(",
")",
";",
"$",
"localizations",
"=",
"''",
";",
"foreach",
"(",
"$",
"strings",
"as",
"$",
"var",
"=>",
"$",
"string",
")",
"{",
"$",
"localizations",
".=",
"\"\\$$var: '$string';\\n\"",
";",
"}",
"if",
"(",
"WP_DEBUG",
")",
"{",
"$",
"this",
"->",
"debug",
"(",
"'/* Silence is golden. */'",
",",
"$",
"localizations",
",",
"'localizations'",
")",
";",
"}",
"return",
"$",
"localizations",
".",
"$",
"scss",
";",
"}"
] | Prepend localized version of content string variables.
@param string $scss
@return string | [
"Prepend",
"localized",
"version",
"of",
"content",
"string",
"variables",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-sass.php#L223-L236 |
pressbooks/pressbooks | inc/class-sass.php | Sass.parseVariables | public function parseVariables( $scss ) {
$output = [];
$parser = new \Leafo\ScssPhp\Parser( null );
$tree = $parser->parse( $scss );
foreach ( $tree->children as $item ) {
if ( $item[0] === \Leafo\ScssPhp\Type::T_ASSIGN && $item[1][0] === \Leafo\ScssPhp\Type::T_VARIABLE && ! str_starts_with( $item[1][1], '_' ) ) {
$key = $item[1][1];
switch ( $item[2][0] ) {
case \Leafo\ScssPhp\Type::T_VARIABLE:
$val = '$' . $item[2][1];
break;
case \Leafo\ScssPhp\Type::T_FUNCTION_CALL:
$fncall = $item[2][1];
$fncall_params = '';
foreach ( $item[2][2] as $param ) {
$fncall_params .= $param[1][1] . ', ';
}
$fncall_params = rtrim( $fncall_params, ', ' );
$val = "{$fncall}({$fncall_params})";
break;
default:
$val = @( new \Leafo\ScssPhp\Compiler() )->compileValue( $item[2] ); // @codingStandardsIgnoreLine
}
$output[ $key ] = $val;
}
}
return $output;
} | php | public function parseVariables( $scss ) {
$output = [];
$parser = new \Leafo\ScssPhp\Parser( null );
$tree = $parser->parse( $scss );
foreach ( $tree->children as $item ) {
if ( $item[0] === \Leafo\ScssPhp\Type::T_ASSIGN && $item[1][0] === \Leafo\ScssPhp\Type::T_VARIABLE && ! str_starts_with( $item[1][1], '_' ) ) {
$key = $item[1][1];
switch ( $item[2][0] ) {
case \Leafo\ScssPhp\Type::T_VARIABLE:
$val = '$' . $item[2][1];
break;
case \Leafo\ScssPhp\Type::T_FUNCTION_CALL:
$fncall = $item[2][1];
$fncall_params = '';
foreach ( $item[2][2] as $param ) {
$fncall_params .= $param[1][1] . ', ';
}
$fncall_params = rtrim( $fncall_params, ', ' );
$val = "{$fncall}({$fncall_params})";
break;
default:
$val = @( new \Leafo\ScssPhp\Compiler() )->compileValue( $item[2] ); // @codingStandardsIgnoreLine
}
$output[ $key ] = $val;
}
}
return $output;
} | [
"public",
"function",
"parseVariables",
"(",
"$",
"scss",
")",
"{",
"$",
"output",
"=",
"[",
"]",
";",
"$",
"parser",
"=",
"new",
"\\",
"Leafo",
"\\",
"ScssPhp",
"\\",
"Parser",
"(",
"null",
")",
";",
"$",
"tree",
"=",
"$",
"parser",
"->",
"parse",
"(",
"$",
"scss",
")",
";",
"foreach",
"(",
"$",
"tree",
"->",
"children",
"as",
"$",
"item",
")",
"{",
"if",
"(",
"$",
"item",
"[",
"0",
"]",
"===",
"\\",
"Leafo",
"\\",
"ScssPhp",
"\\",
"Type",
"::",
"T_ASSIGN",
"&&",
"$",
"item",
"[",
"1",
"]",
"[",
"0",
"]",
"===",
"\\",
"Leafo",
"\\",
"ScssPhp",
"\\",
"Type",
"::",
"T_VARIABLE",
"&&",
"!",
"str_starts_with",
"(",
"$",
"item",
"[",
"1",
"]",
"[",
"1",
"]",
",",
"'_'",
")",
")",
"{",
"$",
"key",
"=",
"$",
"item",
"[",
"1",
"]",
"[",
"1",
"]",
";",
"switch",
"(",
"$",
"item",
"[",
"2",
"]",
"[",
"0",
"]",
")",
"{",
"case",
"\\",
"Leafo",
"\\",
"ScssPhp",
"\\",
"Type",
"::",
"T_VARIABLE",
":",
"$",
"val",
"=",
"'$'",
".",
"$",
"item",
"[",
"2",
"]",
"[",
"1",
"]",
";",
"break",
";",
"case",
"\\",
"Leafo",
"\\",
"ScssPhp",
"\\",
"Type",
"::",
"T_FUNCTION_CALL",
":",
"$",
"fncall",
"=",
"$",
"item",
"[",
"2",
"]",
"[",
"1",
"]",
";",
"$",
"fncall_params",
"=",
"''",
";",
"foreach",
"(",
"$",
"item",
"[",
"2",
"]",
"[",
"2",
"]",
"as",
"$",
"param",
")",
"{",
"$",
"fncall_params",
".=",
"$",
"param",
"[",
"1",
"]",
"[",
"1",
"]",
".",
"', '",
";",
"}",
"$",
"fncall_params",
"=",
"rtrim",
"(",
"$",
"fncall_params",
",",
"', '",
")",
";",
"$",
"val",
"=",
"\"{$fncall}({$fncall_params})\"",
";",
"break",
";",
"default",
":",
"$",
"val",
"=",
"@",
"(",
"new",
"\\",
"Leafo",
"\\",
"ScssPhp",
"\\",
"Compiler",
"(",
")",
")",
"->",
"compileValue",
"(",
"$",
"item",
"[",
"2",
"]",
")",
";",
"// @codingStandardsIgnoreLine",
"}",
"$",
"output",
"[",
"$",
"key",
"]",
"=",
"$",
"val",
";",
"}",
"}",
"return",
"$",
"output",
";",
"}"
] | Parse an SCSS file into an array of variables.
@param string $scss
@return array | [
"Parse",
"an",
"SCSS",
"file",
"into",
"an",
"array",
"of",
"variables",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-sass.php#L245-L272 |
pressbooks/pressbooks | inc/class-sass.php | Sass.logException | protected function logException( \Exception $e ) {
$subject = __( 'SASS Error' );
/** $var \WP_User $current_user */
global $current_user;
$info = [
'time' => strftime( '%c' ),
'user' => ( isset( $current_user ) ? $current_user->user_login : '__UNKNOWN__' ),
'site_url' => site_url(),
'blog_id' => get_current_blog_id(),
'theme' => '' . wp_get_theme(), // Stringify by appending to empty string
'Exception' => [
'code' => $e->getCode(),
'error' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => $e->getTraceAsString(),
],
];
$message = print_r( array_merge( $info ), true ); // @codingStandardsIgnoreLine
\Pressbooks\Utility\email_error_log(
$this->errorsEmail,
$subject,
$message
);
} | php | protected function logException( \Exception $e ) {
$subject = __( 'SASS Error' );
/** $var \WP_User $current_user */
global $current_user;
$info = [
'time' => strftime( '%c' ),
'user' => ( isset( $current_user ) ? $current_user->user_login : '__UNKNOWN__' ),
'site_url' => site_url(),
'blog_id' => get_current_blog_id(),
'theme' => '' . wp_get_theme(), // Stringify by appending to empty string
'Exception' => [
'code' => $e->getCode(),
'error' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => $e->getTraceAsString(),
],
];
$message = print_r( array_merge( $info ), true ); // @codingStandardsIgnoreLine
\Pressbooks\Utility\email_error_log(
$this->errorsEmail,
$subject,
$message
);
} | [
"protected",
"function",
"logException",
"(",
"\\",
"Exception",
"$",
"e",
")",
"{",
"$",
"subject",
"=",
"__",
"(",
"'SASS Error'",
")",
";",
"/** $var \\WP_User $current_user */",
"global",
"$",
"current_user",
";",
"$",
"info",
"=",
"[",
"'time'",
"=>",
"strftime",
"(",
"'%c'",
")",
",",
"'user'",
"=>",
"(",
"isset",
"(",
"$",
"current_user",
")",
"?",
"$",
"current_user",
"->",
"user_login",
":",
"'__UNKNOWN__'",
")",
",",
"'site_url'",
"=>",
"site_url",
"(",
")",
",",
"'blog_id'",
"=>",
"get_current_blog_id",
"(",
")",
",",
"'theme'",
"=>",
"''",
".",
"wp_get_theme",
"(",
")",
",",
"// Stringify by appending to empty string",
"'Exception'",
"=>",
"[",
"'code'",
"=>",
"$",
"e",
"->",
"getCode",
"(",
")",
",",
"'error'",
"=>",
"$",
"e",
"->",
"getMessage",
"(",
")",
",",
"'file'",
"=>",
"$",
"e",
"->",
"getFile",
"(",
")",
",",
"'line'",
"=>",
"$",
"e",
"->",
"getLine",
"(",
")",
",",
"'trace'",
"=>",
"$",
"e",
"->",
"getTraceAsString",
"(",
")",
",",
"]",
",",
"]",
";",
"$",
"message",
"=",
"print_r",
"(",
"array_merge",
"(",
"$",
"info",
")",
",",
"true",
")",
";",
"// @codingStandardsIgnoreLine",
"\\",
"Pressbooks",
"\\",
"Utility",
"\\",
"email_error_log",
"(",
"$",
"this",
"->",
"errorsEmail",
",",
"$",
"subject",
",",
"$",
"message",
")",
";",
"}"
] | Log Exceptions
@param \Exception $e | [
"Log",
"Exceptions"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-sass.php#L279-L308 |
pressbooks/pressbooks | inc/class-sass.php | Sass.debug | public function debug( $css, $scss, $filename ) {
$debug_dir = $this->pathToDebugDir();
$css_debug_file = $debug_dir . "/{$filename}.css";
\Pressbooks\Utility\put_contents( $css_debug_file, $css );
$scss_debug_file = $debug_dir . "/{$filename}.scss";
\Pressbooks\Utility\put_contents( $scss_debug_file, $scss );
} | php | public function debug( $css, $scss, $filename ) {
$debug_dir = $this->pathToDebugDir();
$css_debug_file = $debug_dir . "/{$filename}.css";
\Pressbooks\Utility\put_contents( $css_debug_file, $css );
$scss_debug_file = $debug_dir . "/{$filename}.scss";
\Pressbooks\Utility\put_contents( $scss_debug_file, $scss );
} | [
"public",
"function",
"debug",
"(",
"$",
"css",
",",
"$",
"scss",
",",
"$",
"filename",
")",
"{",
"$",
"debug_dir",
"=",
"$",
"this",
"->",
"pathToDebugDir",
"(",
")",
";",
"$",
"css_debug_file",
"=",
"$",
"debug_dir",
".",
"\"/{$filename}.css\"",
";",
"\\",
"Pressbooks",
"\\",
"Utility",
"\\",
"put_contents",
"(",
"$",
"css_debug_file",
",",
"$",
"css",
")",
";",
"$",
"scss_debug_file",
"=",
"$",
"debug_dir",
".",
"\"/{$filename}.scss\"",
";",
"\\",
"Pressbooks",
"\\",
"Utility",
"\\",
"put_contents",
"(",
"$",
"scss_debug_file",
",",
"$",
"scss",
")",
";",
"}"
] | Write CSS to a a debug dir
@param string $css
@param string $scss
@param string $filename | [
"Write",
"CSS",
"to",
"a",
"a",
"debug",
"dir"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-sass.php#L318-L327 |
pressbooks/pressbooks | inc/modules/export/wordpress/class-vanillawxr.php | VanillaWxr.deleteNode | private function deleteNode( $node ) {
$this->deleteChildren( $node );
$parent = $node->parentNode;
$oldnode = $parent->removeChild( $node );
} | php | private function deleteNode( $node ) {
$this->deleteChildren( $node );
$parent = $node->parentNode;
$oldnode = $parent->removeChild( $node );
} | [
"private",
"function",
"deleteNode",
"(",
"$",
"node",
")",
"{",
"$",
"this",
"->",
"deleteChildren",
"(",
"$",
"node",
")",
";",
"$",
"parent",
"=",
"$",
"node",
"->",
"parentNode",
";",
"$",
"oldnode",
"=",
"$",
"parent",
"->",
"removeChild",
"(",
"$",
"node",
")",
";",
"}"
] | deletes a node and all of its children
@param \DOMNode $node | [
"deletes",
"a",
"node",
"and",
"all",
"of",
"its",
"children"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/export/wordpress/class-vanillawxr.php#L123-L127 |
pressbooks/pressbooks | inc/modules/export/wordpress/class-vanillawxr.php | VanillaWxr.deleteChildren | private function deleteChildren( $node ) {
while ( isset( $node->firstChild ) ) {
$this->deleteChildren( $node->firstChild );
$node->removeChild( $node->firstChild );
}
} | php | private function deleteChildren( $node ) {
while ( isset( $node->firstChild ) ) {
$this->deleteChildren( $node->firstChild );
$node->removeChild( $node->firstChild );
}
} | [
"private",
"function",
"deleteChildren",
"(",
"$",
"node",
")",
"{",
"while",
"(",
"isset",
"(",
"$",
"node",
"->",
"firstChild",
")",
")",
"{",
"$",
"this",
"->",
"deleteChildren",
"(",
"$",
"node",
"->",
"firstChild",
")",
";",
"$",
"node",
"->",
"removeChild",
"(",
"$",
"node",
"->",
"firstChild",
")",
";",
"}",
"}"
] | recursive function to delete all children of a node
@param \DOMNode $node | [
"recursive",
"function",
"to",
"delete",
"all",
"children",
"of",
"a",
"node"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/export/wordpress/class-vanillawxr.php#L134-L139 |
pressbooks/pressbooks | inc/covergenerator/class-princejpg.php | PrinceJpg.generate | public function generate() {
$tmp_pdf_path = create_tmp_file();
$success = $this->generateWithPrince( null, null, $this->generateHtml(), $tmp_pdf_path );
if ( true !== $success ) {
throw new \Exception( 'Failed to create PDF file' );
}
$output_path = $this->timestampedFileName( 'jpg' );
$this->convert( $tmp_pdf_path, $output_path );
delete_transient( 'dirsize_cache' ); /** @see get_dirsize() */
return $output_path;
} | php | public function generate() {
$tmp_pdf_path = create_tmp_file();
$success = $this->generateWithPrince( null, null, $this->generateHtml(), $tmp_pdf_path );
if ( true !== $success ) {
throw new \Exception( 'Failed to create PDF file' );
}
$output_path = $this->timestampedFileName( 'jpg' );
$this->convert( $tmp_pdf_path, $output_path );
delete_transient( 'dirsize_cache' ); /** @see get_dirsize() */
return $output_path;
} | [
"public",
"function",
"generate",
"(",
")",
"{",
"$",
"tmp_pdf_path",
"=",
"create_tmp_file",
"(",
")",
";",
"$",
"success",
"=",
"$",
"this",
"->",
"generateWithPrince",
"(",
"null",
",",
"null",
",",
"$",
"this",
"->",
"generateHtml",
"(",
")",
",",
"$",
"tmp_pdf_path",
")",
";",
"if",
"(",
"true",
"!==",
"$",
"success",
")",
"{",
"throw",
"new",
"\\",
"Exception",
"(",
"'Failed to create PDF file'",
")",
";",
"}",
"$",
"output_path",
"=",
"$",
"this",
"->",
"timestampedFileName",
"(",
"'jpg'",
")",
";",
"$",
"this",
"->",
"convert",
"(",
"$",
"tmp_pdf_path",
",",
"$",
"output_path",
")",
";",
"delete_transient",
"(",
"'dirsize_cache'",
")",
";",
"/** @see get_dirsize() */",
"return",
"$",
"output_path",
";",
"}"
] | Generate Ebook JPG cover
@throws \Exception
@return string Output path | [
"Generate",
"Ebook",
"JPG",
"cover"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/covergenerator/class-princejpg.php#L74-L84 |
pressbooks/pressbooks | inc/modules/import/wordpress/class-wxr.php | Wxr.dependencies | public function dependencies( $downloads = null, $contributors = null ) {
$this->downloads = $downloads ? $downloads : new Downloads( $this );
$this->contributors = $contributors ? $contributors : new Contributors();
} | php | public function dependencies( $downloads = null, $contributors = null ) {
$this->downloads = $downloads ? $downloads : new Downloads( $this );
$this->contributors = $contributors ? $contributors : new Contributors();
} | [
"public",
"function",
"dependencies",
"(",
"$",
"downloads",
"=",
"null",
",",
"$",
"contributors",
"=",
"null",
")",
"{",
"$",
"this",
"->",
"downloads",
"=",
"$",
"downloads",
"?",
"$",
"downloads",
":",
"new",
"Downloads",
"(",
"$",
"this",
")",
";",
"$",
"this",
"->",
"contributors",
"=",
"$",
"contributors",
"?",
"$",
"contributors",
":",
"new",
"Contributors",
"(",
")",
";",
"}"
] | For testing, ability to mock objects
@param null Downloads $downloads
@param null \Pressbooks\Contributors $contributors | [
"For",
"testing",
"ability",
"to",
"mock",
"objects"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/import/wordpress/class-wxr.php#L86-L89 |
pressbooks/pressbooks | inc/modules/import/wordpress/class-wxr.php | Wxr.setCurrentImportOption | function setCurrentImportOption( array $upload ) {
try {
$parser = new Parser();
$xml = $parser->parse( $upload['file'] );
} catch ( \Exception $e ) {
return false;
}
$this->pbCheck( $xml );
$this->sourceBookUrl = $xml['base_url'];
$option = [
'file' => $upload['file'],
'url' => $upload['url'] ?? null,
'file_type' => $upload['type'],
'type_of' => self::TYPE_OF,
'chapters' => [],
'post_types' => [],
'allow_parts' => true,
];
/**
* Allow custom post types to be imported.
*
* @since 3.6.0
*
* @param array $value
*/
$supported_post_types = apply_filters( 'pb_import_custom_post_types', [ 'post', 'page', 'front-matter', 'chapter', 'part', 'back-matter', 'metadata', 'glossary' ] );
if ( $this->isPbWxr ) {
//put the posts in correct part / menu_order order
$xml['posts'] = $this->customNestedSort( $xml['posts'] );
}
foreach ( $xml['posts'] as $p ) {
// Skip unsupported post types.
if ( ! in_array( $p['post_type'], $supported_post_types, true ) ) {
continue;
}
// Skip deleted post types.
if ( $p['status'] === 'trash' ) {
continue;
}
// Skip webbook required pages.
if ( '<!-- Here be dragons.-->' === $p['post_content'] || '<!-- Here be dragons. -->' === $p['post_content'] ) {
continue;
}
// Set
$option['chapters'][ $p['post_id'] ] = $p['post_title'];
$option['post_types'][ $p['post_id'] ] = $p['post_type'];
}
return update_option( 'pressbooks_current_import', $option );
} | php | function setCurrentImportOption( array $upload ) {
try {
$parser = new Parser();
$xml = $parser->parse( $upload['file'] );
} catch ( \Exception $e ) {
return false;
}
$this->pbCheck( $xml );
$this->sourceBookUrl = $xml['base_url'];
$option = [
'file' => $upload['file'],
'url' => $upload['url'] ?? null,
'file_type' => $upload['type'],
'type_of' => self::TYPE_OF,
'chapters' => [],
'post_types' => [],
'allow_parts' => true,
];
/**
* Allow custom post types to be imported.
*
* @since 3.6.0
*
* @param array $value
*/
$supported_post_types = apply_filters( 'pb_import_custom_post_types', [ 'post', 'page', 'front-matter', 'chapter', 'part', 'back-matter', 'metadata', 'glossary' ] );
if ( $this->isPbWxr ) {
//put the posts in correct part / menu_order order
$xml['posts'] = $this->customNestedSort( $xml['posts'] );
}
foreach ( $xml['posts'] as $p ) {
// Skip unsupported post types.
if ( ! in_array( $p['post_type'], $supported_post_types, true ) ) {
continue;
}
// Skip deleted post types.
if ( $p['status'] === 'trash' ) {
continue;
}
// Skip webbook required pages.
if ( '<!-- Here be dragons.-->' === $p['post_content'] || '<!-- Here be dragons. -->' === $p['post_content'] ) {
continue;
}
// Set
$option['chapters'][ $p['post_id'] ] = $p['post_title'];
$option['post_types'][ $p['post_id'] ] = $p['post_type'];
}
return update_option( 'pressbooks_current_import', $option );
} | [
"function",
"setCurrentImportOption",
"(",
"array",
"$",
"upload",
")",
"{",
"try",
"{",
"$",
"parser",
"=",
"new",
"Parser",
"(",
")",
";",
"$",
"xml",
"=",
"$",
"parser",
"->",
"parse",
"(",
"$",
"upload",
"[",
"'file'",
"]",
")",
";",
"}",
"catch",
"(",
"\\",
"Exception",
"$",
"e",
")",
"{",
"return",
"false",
";",
"}",
"$",
"this",
"->",
"pbCheck",
"(",
"$",
"xml",
")",
";",
"$",
"this",
"->",
"sourceBookUrl",
"=",
"$",
"xml",
"[",
"'base_url'",
"]",
";",
"$",
"option",
"=",
"[",
"'file'",
"=>",
"$",
"upload",
"[",
"'file'",
"]",
",",
"'url'",
"=>",
"$",
"upload",
"[",
"'url'",
"]",
"??",
"null",
",",
"'file_type'",
"=>",
"$",
"upload",
"[",
"'type'",
"]",
",",
"'type_of'",
"=>",
"self",
"::",
"TYPE_OF",
",",
"'chapters'",
"=>",
"[",
"]",
",",
"'post_types'",
"=>",
"[",
"]",
",",
"'allow_parts'",
"=>",
"true",
",",
"]",
";",
"/**\n\t\t * Allow custom post types to be imported.\n\t\t *\n\t\t * @since 3.6.0\n\t\t *\n\t\t * @param array $value\n\t\t */",
"$",
"supported_post_types",
"=",
"apply_filters",
"(",
"'pb_import_custom_post_types'",
",",
"[",
"'post'",
",",
"'page'",
",",
"'front-matter'",
",",
"'chapter'",
",",
"'part'",
",",
"'back-matter'",
",",
"'metadata'",
",",
"'glossary'",
"]",
")",
";",
"if",
"(",
"$",
"this",
"->",
"isPbWxr",
")",
"{",
"//put the posts in correct part / menu_order order",
"$",
"xml",
"[",
"'posts'",
"]",
"=",
"$",
"this",
"->",
"customNestedSort",
"(",
"$",
"xml",
"[",
"'posts'",
"]",
")",
";",
"}",
"foreach",
"(",
"$",
"xml",
"[",
"'posts'",
"]",
"as",
"$",
"p",
")",
"{",
"// Skip unsupported post types.",
"if",
"(",
"!",
"in_array",
"(",
"$",
"p",
"[",
"'post_type'",
"]",
",",
"$",
"supported_post_types",
",",
"true",
")",
")",
"{",
"continue",
";",
"}",
"// Skip deleted post types.",
"if",
"(",
"$",
"p",
"[",
"'status'",
"]",
"===",
"'trash'",
")",
"{",
"continue",
";",
"}",
"// Skip webbook required pages.",
"if",
"(",
"'<!-- Here be dragons.-->'",
"===",
"$",
"p",
"[",
"'post_content'",
"]",
"||",
"'<!-- Here be dragons. -->'",
"===",
"$",
"p",
"[",
"'post_content'",
"]",
")",
"{",
"continue",
";",
"}",
"// Set",
"$",
"option",
"[",
"'chapters'",
"]",
"[",
"$",
"p",
"[",
"'post_id'",
"]",
"]",
"=",
"$",
"p",
"[",
"'post_title'",
"]",
";",
"$",
"option",
"[",
"'post_types'",
"]",
"[",
"$",
"p",
"[",
"'post_id'",
"]",
"]",
"=",
"$",
"p",
"[",
"'post_type'",
"]",
";",
"}",
"return",
"update_option",
"(",
"'pressbooks_current_import'",
",",
"$",
"option",
")",
";",
"}"
] | @param array $upload
@return bool | [
"@param",
"array",
"$upload"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/import/wordpress/class-wxr.php#L110-L170 |
pressbooks/pressbooks | inc/modules/import/wordpress/class-wxr.php | Wxr.import | function import( array $current_import ) {
try {
$parser = new Parser();
$xml = $parser->parse( $current_import['file'] );
} catch ( \Exception $e ) {
return false;
}
wp_defer_term_counting( true );
$this->pbCheck( $xml );
$this->sourceBookUrl = $xml['base_url'];
$this->knownMedia = $this->buildListOfKnownMedia( $xml );
// Sort by the length of sourceUrls for better search and replace
$known_media_sorted = $this->knownMedia;
uasort(
$known_media_sorted, function ( $a, $b ) {
return strlen( $b->sourceUrl ) <=> strlen( $a->sourceUrl );
}
);
$this->knownMedia = $known_media_sorted;
if ( $this->isPbWxr ) {
$xml['posts'] = $this->customNestedSort( $xml['posts'] );
}
$match_ids = array_flip( array_keys( $current_import['chapters'] ) );
$chapter_parent = $this->getChapterParent();
$totals = [
'front-matter' => 0,
'chapter' => 0,
'part' => 0,
'back-matter' => 0,
'glossary' => 0,
'media' => 0,
];
/**
* Allow custom post taxonomies to be imported.
*
* @since 3.6.0
*
* @param array $value
*/
$taxonomies = apply_filters( 'pb_import_custom_taxonomies', [ 'front-matter-type', 'chapter-type', 'back-matter-type', 'glossary-type' ] );
$custom_post_types = apply_filters( 'pb_import_custom_post_types', [ 'post', 'page', 'front-matter', 'chapter', 'part', 'back-matter', 'metadata', 'glossary' ] );
// set custom terms...
$terms = apply_filters( 'pb_import_custom_terms', $xml['terms'] );
// and import them if they don't already exist.
foreach ( $terms as $t ) {
$term = term_exists( $t['term_name'], $t['term_taxonomy'] );
if ( null === $term || 0 === $term ) {
$results = wp_insert_term(
$t['term_name'],
$t['term_taxonomy'],
[
'description' => $t['term_description'],
'slug' => $t['slug'],
]
);
if ( ! empty( $t['termmeta'] ) && is_array( $results ) ) {
foreach ( $t['termmeta'] as $termmeta ) {
add_term_meta( $results['term_id'], $termmeta['key'], $termmeta['value'], true );
}
}
}
}
// -----------------------------------------------------------------------------
// Import posts, start!
// -----------------------------------------------------------------------------
foreach ( $xml['posts'] as $p ) {
// Skip
if ( ! $this->flaggedForImport( $p['post_id'] ) ) {
continue;
}
if ( ! isset( $match_ids[ $p['post_id'] ] ) ) {
continue;
}
// Insert
$post_type = $this->determinePostType( $p['post_id'] );
$html5 = new HtmlParser();
$dom = $html5->loadHtml( $p['post_content'] );
// Download images, change image paths
$media = $this->downloads->scrapeAndKneadImages( $dom );
$dom = $media['dom'];
$attachments = $media['attachments'];
// Download media, change media paths
$media = $this->downloads->scrapeAndKneadMedia( $dom, $html5->parser );
$dom = $media['dom'];
$attachments = array_merge( $attachments, $media['attachments'] );
// TODO? We should probably do the same thing as seen in Cloner::fixInternalLinks( $dom )
$html = $html5->saveHTML( $dom );
if ( 'metadata' === $post_type ) {
$pid = $this->bookInfoPid();
} else {
$pid = $this->insertNewPost( $post_type, $p, $html, $chapter_parent, $current_import['default_post_status'] );
if ( 'part' === $post_type ) {
$chapter_parent = $pid;
}
}
// if this is a custom post type,
// and it has terms associated with it...
if ( ( in_array( $post_type, $custom_post_types, true ) && isset( $p['terms'] ) ) ) {
// associate post with terms.
foreach ( $p['terms'] as $t ) {
if ( in_array( $t['domain'], $taxonomies, true ) ) {
wp_set_object_terms(
$pid,
$t['slug'],
$t['domain'],
true
);
}
}
}
if ( isset( $p['postmeta'] ) && is_array( $p['postmeta'] ) ) {
if ( 'metadata' === $post_type ) {
$this->importMetaBoxes( $pid, $p );
} else {
$this->importPbPostMeta( $pid, $p );
}
}
Book::consolidatePost( $pid, get_post( $pid ) ); // Reorder
if ( 'metadata' !== $post_type ) {
++$totals[ $post_type ];
}
// Attach attachments to post
foreach ( $attachments as $attachment ) {
wp_update_post(
[
'ID' => $attachment,
'post_parent' => $pid,
]
);
}
$totals['media'] = $totals['media'] + count( $attachments );
// Shortcode hacker, no ease up tonight.
$this->checkInternalShortcodes( $pid, $html );
// Store a transitional state
$this->createTransition( $post_type, $p['post_id'], $pid );
}
$this->fixInternalShortcodes();
// -----------------------------------------------------------------------------
// Import posts, done!
// -----------------------------------------------------------------------------
wp_defer_term_counting( false ); // Flush
// Done
$_SESSION['pb_notices'][] = sprintf(
_x( 'Imported %1$s, %2$s, %3$s, %4$s, %5$s, and %6$s.', 'String which tells user how many front matter, parts, chapters, back matter, media attachments, and glossary terms were imported.', 'pressbooks' ),
sprintf( _n( '%s front matter', '%s front matter', $totals['front-matter'], 'pressbooks' ), $totals['front-matter'] ),
sprintf( _n( '%s part', '%s parts', $totals['part'], 'pressbooks' ), $totals['part'] ),
sprintf( _n( '%s chapter', '%s chapters', $totals['chapter'], 'pressbooks' ), $totals['chapter'] ),
sprintf( _n( '%s back matter', '%s back matter', $totals['back-matter'], 'pressbooks' ), $totals['back-matter'] ),
sprintf( _n( '%s media attachment', '%s media attachments', $totals['media'], 'pressbooks' ), $totals['media'] ),
sprintf( _n( '%s glossary term', '%s glossary terms', $totals['glossary'], 'pressbooks' ), $totals['glossary'] )
);
return $this->revokeCurrentImport();
} | php | function import( array $current_import ) {
try {
$parser = new Parser();
$xml = $parser->parse( $current_import['file'] );
} catch ( \Exception $e ) {
return false;
}
wp_defer_term_counting( true );
$this->pbCheck( $xml );
$this->sourceBookUrl = $xml['base_url'];
$this->knownMedia = $this->buildListOfKnownMedia( $xml );
// Sort by the length of sourceUrls for better search and replace
$known_media_sorted = $this->knownMedia;
uasort(
$known_media_sorted, function ( $a, $b ) {
return strlen( $b->sourceUrl ) <=> strlen( $a->sourceUrl );
}
);
$this->knownMedia = $known_media_sorted;
if ( $this->isPbWxr ) {
$xml['posts'] = $this->customNestedSort( $xml['posts'] );
}
$match_ids = array_flip( array_keys( $current_import['chapters'] ) );
$chapter_parent = $this->getChapterParent();
$totals = [
'front-matter' => 0,
'chapter' => 0,
'part' => 0,
'back-matter' => 0,
'glossary' => 0,
'media' => 0,
];
/**
* Allow custom post taxonomies to be imported.
*
* @since 3.6.0
*
* @param array $value
*/
$taxonomies = apply_filters( 'pb_import_custom_taxonomies', [ 'front-matter-type', 'chapter-type', 'back-matter-type', 'glossary-type' ] );
$custom_post_types = apply_filters( 'pb_import_custom_post_types', [ 'post', 'page', 'front-matter', 'chapter', 'part', 'back-matter', 'metadata', 'glossary' ] );
// set custom terms...
$terms = apply_filters( 'pb_import_custom_terms', $xml['terms'] );
// and import them if they don't already exist.
foreach ( $terms as $t ) {
$term = term_exists( $t['term_name'], $t['term_taxonomy'] );
if ( null === $term || 0 === $term ) {
$results = wp_insert_term(
$t['term_name'],
$t['term_taxonomy'],
[
'description' => $t['term_description'],
'slug' => $t['slug'],
]
);
if ( ! empty( $t['termmeta'] ) && is_array( $results ) ) {
foreach ( $t['termmeta'] as $termmeta ) {
add_term_meta( $results['term_id'], $termmeta['key'], $termmeta['value'], true );
}
}
}
}
// -----------------------------------------------------------------------------
// Import posts, start!
// -----------------------------------------------------------------------------
foreach ( $xml['posts'] as $p ) {
// Skip
if ( ! $this->flaggedForImport( $p['post_id'] ) ) {
continue;
}
if ( ! isset( $match_ids[ $p['post_id'] ] ) ) {
continue;
}
// Insert
$post_type = $this->determinePostType( $p['post_id'] );
$html5 = new HtmlParser();
$dom = $html5->loadHtml( $p['post_content'] );
// Download images, change image paths
$media = $this->downloads->scrapeAndKneadImages( $dom );
$dom = $media['dom'];
$attachments = $media['attachments'];
// Download media, change media paths
$media = $this->downloads->scrapeAndKneadMedia( $dom, $html5->parser );
$dom = $media['dom'];
$attachments = array_merge( $attachments, $media['attachments'] );
// TODO? We should probably do the same thing as seen in Cloner::fixInternalLinks( $dom )
$html = $html5->saveHTML( $dom );
if ( 'metadata' === $post_type ) {
$pid = $this->bookInfoPid();
} else {
$pid = $this->insertNewPost( $post_type, $p, $html, $chapter_parent, $current_import['default_post_status'] );
if ( 'part' === $post_type ) {
$chapter_parent = $pid;
}
}
// if this is a custom post type,
// and it has terms associated with it...
if ( ( in_array( $post_type, $custom_post_types, true ) && isset( $p['terms'] ) ) ) {
// associate post with terms.
foreach ( $p['terms'] as $t ) {
if ( in_array( $t['domain'], $taxonomies, true ) ) {
wp_set_object_terms(
$pid,
$t['slug'],
$t['domain'],
true
);
}
}
}
if ( isset( $p['postmeta'] ) && is_array( $p['postmeta'] ) ) {
if ( 'metadata' === $post_type ) {
$this->importMetaBoxes( $pid, $p );
} else {
$this->importPbPostMeta( $pid, $p );
}
}
Book::consolidatePost( $pid, get_post( $pid ) ); // Reorder
if ( 'metadata' !== $post_type ) {
++$totals[ $post_type ];
}
// Attach attachments to post
foreach ( $attachments as $attachment ) {
wp_update_post(
[
'ID' => $attachment,
'post_parent' => $pid,
]
);
}
$totals['media'] = $totals['media'] + count( $attachments );
// Shortcode hacker, no ease up tonight.
$this->checkInternalShortcodes( $pid, $html );
// Store a transitional state
$this->createTransition( $post_type, $p['post_id'], $pid );
}
$this->fixInternalShortcodes();
// -----------------------------------------------------------------------------
// Import posts, done!
// -----------------------------------------------------------------------------
wp_defer_term_counting( false ); // Flush
// Done
$_SESSION['pb_notices'][] = sprintf(
_x( 'Imported %1$s, %2$s, %3$s, %4$s, %5$s, and %6$s.', 'String which tells user how many front matter, parts, chapters, back matter, media attachments, and glossary terms were imported.', 'pressbooks' ),
sprintf( _n( '%s front matter', '%s front matter', $totals['front-matter'], 'pressbooks' ), $totals['front-matter'] ),
sprintf( _n( '%s part', '%s parts', $totals['part'], 'pressbooks' ), $totals['part'] ),
sprintf( _n( '%s chapter', '%s chapters', $totals['chapter'], 'pressbooks' ), $totals['chapter'] ),
sprintf( _n( '%s back matter', '%s back matter', $totals['back-matter'], 'pressbooks' ), $totals['back-matter'] ),
sprintf( _n( '%s media attachment', '%s media attachments', $totals['media'], 'pressbooks' ), $totals['media'] ),
sprintf( _n( '%s glossary term', '%s glossary terms', $totals['glossary'], 'pressbooks' ), $totals['glossary'] )
);
return $this->revokeCurrentImport();
} | [
"function",
"import",
"(",
"array",
"$",
"current_import",
")",
"{",
"try",
"{",
"$",
"parser",
"=",
"new",
"Parser",
"(",
")",
";",
"$",
"xml",
"=",
"$",
"parser",
"->",
"parse",
"(",
"$",
"current_import",
"[",
"'file'",
"]",
")",
";",
"}",
"catch",
"(",
"\\",
"Exception",
"$",
"e",
")",
"{",
"return",
"false",
";",
"}",
"wp_defer_term_counting",
"(",
"true",
")",
";",
"$",
"this",
"->",
"pbCheck",
"(",
"$",
"xml",
")",
";",
"$",
"this",
"->",
"sourceBookUrl",
"=",
"$",
"xml",
"[",
"'base_url'",
"]",
";",
"$",
"this",
"->",
"knownMedia",
"=",
"$",
"this",
"->",
"buildListOfKnownMedia",
"(",
"$",
"xml",
")",
";",
"// Sort by the length of sourceUrls for better search and replace",
"$",
"known_media_sorted",
"=",
"$",
"this",
"->",
"knownMedia",
";",
"uasort",
"(",
"$",
"known_media_sorted",
",",
"function",
"(",
"$",
"a",
",",
"$",
"b",
")",
"{",
"return",
"strlen",
"(",
"$",
"b",
"->",
"sourceUrl",
")",
"<=>",
"strlen",
"(",
"$",
"a",
"->",
"sourceUrl",
")",
";",
"}",
")",
";",
"$",
"this",
"->",
"knownMedia",
"=",
"$",
"known_media_sorted",
";",
"if",
"(",
"$",
"this",
"->",
"isPbWxr",
")",
"{",
"$",
"xml",
"[",
"'posts'",
"]",
"=",
"$",
"this",
"->",
"customNestedSort",
"(",
"$",
"xml",
"[",
"'posts'",
"]",
")",
";",
"}",
"$",
"match_ids",
"=",
"array_flip",
"(",
"array_keys",
"(",
"$",
"current_import",
"[",
"'chapters'",
"]",
")",
")",
";",
"$",
"chapter_parent",
"=",
"$",
"this",
"->",
"getChapterParent",
"(",
")",
";",
"$",
"totals",
"=",
"[",
"'front-matter'",
"=>",
"0",
",",
"'chapter'",
"=>",
"0",
",",
"'part'",
"=>",
"0",
",",
"'back-matter'",
"=>",
"0",
",",
"'glossary'",
"=>",
"0",
",",
"'media'",
"=>",
"0",
",",
"]",
";",
"/**\n\t\t * Allow custom post taxonomies to be imported.\n\t\t *\n\t\t * @since 3.6.0\n\t\t *\n\t\t * @param array $value\n\t\t */",
"$",
"taxonomies",
"=",
"apply_filters",
"(",
"'pb_import_custom_taxonomies'",
",",
"[",
"'front-matter-type'",
",",
"'chapter-type'",
",",
"'back-matter-type'",
",",
"'glossary-type'",
"]",
")",
";",
"$",
"custom_post_types",
"=",
"apply_filters",
"(",
"'pb_import_custom_post_types'",
",",
"[",
"'post'",
",",
"'page'",
",",
"'front-matter'",
",",
"'chapter'",
",",
"'part'",
",",
"'back-matter'",
",",
"'metadata'",
",",
"'glossary'",
"]",
")",
";",
"// set custom terms...",
"$",
"terms",
"=",
"apply_filters",
"(",
"'pb_import_custom_terms'",
",",
"$",
"xml",
"[",
"'terms'",
"]",
")",
";",
"// and import them if they don't already exist.",
"foreach",
"(",
"$",
"terms",
"as",
"$",
"t",
")",
"{",
"$",
"term",
"=",
"term_exists",
"(",
"$",
"t",
"[",
"'term_name'",
"]",
",",
"$",
"t",
"[",
"'term_taxonomy'",
"]",
")",
";",
"if",
"(",
"null",
"===",
"$",
"term",
"||",
"0",
"===",
"$",
"term",
")",
"{",
"$",
"results",
"=",
"wp_insert_term",
"(",
"$",
"t",
"[",
"'term_name'",
"]",
",",
"$",
"t",
"[",
"'term_taxonomy'",
"]",
",",
"[",
"'description'",
"=>",
"$",
"t",
"[",
"'term_description'",
"]",
",",
"'slug'",
"=>",
"$",
"t",
"[",
"'slug'",
"]",
",",
"]",
")",
";",
"if",
"(",
"!",
"empty",
"(",
"$",
"t",
"[",
"'termmeta'",
"]",
")",
"&&",
"is_array",
"(",
"$",
"results",
")",
")",
"{",
"foreach",
"(",
"$",
"t",
"[",
"'termmeta'",
"]",
"as",
"$",
"termmeta",
")",
"{",
"add_term_meta",
"(",
"$",
"results",
"[",
"'term_id'",
"]",
",",
"$",
"termmeta",
"[",
"'key'",
"]",
",",
"$",
"termmeta",
"[",
"'value'",
"]",
",",
"true",
")",
";",
"}",
"}",
"}",
"}",
"// -----------------------------------------------------------------------------",
"// Import posts, start!",
"// -----------------------------------------------------------------------------",
"foreach",
"(",
"$",
"xml",
"[",
"'posts'",
"]",
"as",
"$",
"p",
")",
"{",
"// Skip",
"if",
"(",
"!",
"$",
"this",
"->",
"flaggedForImport",
"(",
"$",
"p",
"[",
"'post_id'",
"]",
")",
")",
"{",
"continue",
";",
"}",
"if",
"(",
"!",
"isset",
"(",
"$",
"match_ids",
"[",
"$",
"p",
"[",
"'post_id'",
"]",
"]",
")",
")",
"{",
"continue",
";",
"}",
"// Insert",
"$",
"post_type",
"=",
"$",
"this",
"->",
"determinePostType",
"(",
"$",
"p",
"[",
"'post_id'",
"]",
")",
";",
"$",
"html5",
"=",
"new",
"HtmlParser",
"(",
")",
";",
"$",
"dom",
"=",
"$",
"html5",
"->",
"loadHtml",
"(",
"$",
"p",
"[",
"'post_content'",
"]",
")",
";",
"// Download images, change image paths",
"$",
"media",
"=",
"$",
"this",
"->",
"downloads",
"->",
"scrapeAndKneadImages",
"(",
"$",
"dom",
")",
";",
"$",
"dom",
"=",
"$",
"media",
"[",
"'dom'",
"]",
";",
"$",
"attachments",
"=",
"$",
"media",
"[",
"'attachments'",
"]",
";",
"// Download media, change media paths",
"$",
"media",
"=",
"$",
"this",
"->",
"downloads",
"->",
"scrapeAndKneadMedia",
"(",
"$",
"dom",
",",
"$",
"html5",
"->",
"parser",
")",
";",
"$",
"dom",
"=",
"$",
"media",
"[",
"'dom'",
"]",
";",
"$",
"attachments",
"=",
"array_merge",
"(",
"$",
"attachments",
",",
"$",
"media",
"[",
"'attachments'",
"]",
")",
";",
"// TODO? We should probably do the same thing as seen in Cloner::fixInternalLinks( $dom )",
"$",
"html",
"=",
"$",
"html5",
"->",
"saveHTML",
"(",
"$",
"dom",
")",
";",
"if",
"(",
"'metadata'",
"===",
"$",
"post_type",
")",
"{",
"$",
"pid",
"=",
"$",
"this",
"->",
"bookInfoPid",
"(",
")",
";",
"}",
"else",
"{",
"$",
"pid",
"=",
"$",
"this",
"->",
"insertNewPost",
"(",
"$",
"post_type",
",",
"$",
"p",
",",
"$",
"html",
",",
"$",
"chapter_parent",
",",
"$",
"current_import",
"[",
"'default_post_status'",
"]",
")",
";",
"if",
"(",
"'part'",
"===",
"$",
"post_type",
")",
"{",
"$",
"chapter_parent",
"=",
"$",
"pid",
";",
"}",
"}",
"// if this is a custom post type,",
"// and it has terms associated with it...",
"if",
"(",
"(",
"in_array",
"(",
"$",
"post_type",
",",
"$",
"custom_post_types",
",",
"true",
")",
"&&",
"isset",
"(",
"$",
"p",
"[",
"'terms'",
"]",
")",
")",
")",
"{",
"// associate post with terms.",
"foreach",
"(",
"$",
"p",
"[",
"'terms'",
"]",
"as",
"$",
"t",
")",
"{",
"if",
"(",
"in_array",
"(",
"$",
"t",
"[",
"'domain'",
"]",
",",
"$",
"taxonomies",
",",
"true",
")",
")",
"{",
"wp_set_object_terms",
"(",
"$",
"pid",
",",
"$",
"t",
"[",
"'slug'",
"]",
",",
"$",
"t",
"[",
"'domain'",
"]",
",",
"true",
")",
";",
"}",
"}",
"}",
"if",
"(",
"isset",
"(",
"$",
"p",
"[",
"'postmeta'",
"]",
")",
"&&",
"is_array",
"(",
"$",
"p",
"[",
"'postmeta'",
"]",
")",
")",
"{",
"if",
"(",
"'metadata'",
"===",
"$",
"post_type",
")",
"{",
"$",
"this",
"->",
"importMetaBoxes",
"(",
"$",
"pid",
",",
"$",
"p",
")",
";",
"}",
"else",
"{",
"$",
"this",
"->",
"importPbPostMeta",
"(",
"$",
"pid",
",",
"$",
"p",
")",
";",
"}",
"}",
"Book",
"::",
"consolidatePost",
"(",
"$",
"pid",
",",
"get_post",
"(",
"$",
"pid",
")",
")",
";",
"// Reorder",
"if",
"(",
"'metadata'",
"!==",
"$",
"post_type",
")",
"{",
"++",
"$",
"totals",
"[",
"$",
"post_type",
"]",
";",
"}",
"// Attach attachments to post",
"foreach",
"(",
"$",
"attachments",
"as",
"$",
"attachment",
")",
"{",
"wp_update_post",
"(",
"[",
"'ID'",
"=>",
"$",
"attachment",
",",
"'post_parent'",
"=>",
"$",
"pid",
",",
"]",
")",
";",
"}",
"$",
"totals",
"[",
"'media'",
"]",
"=",
"$",
"totals",
"[",
"'media'",
"]",
"+",
"count",
"(",
"$",
"attachments",
")",
";",
"// Shortcode hacker, no ease up tonight.",
"$",
"this",
"->",
"checkInternalShortcodes",
"(",
"$",
"pid",
",",
"$",
"html",
")",
";",
"// Store a transitional state",
"$",
"this",
"->",
"createTransition",
"(",
"$",
"post_type",
",",
"$",
"p",
"[",
"'post_id'",
"]",
",",
"$",
"pid",
")",
";",
"}",
"$",
"this",
"->",
"fixInternalShortcodes",
"(",
")",
";",
"// -----------------------------------------------------------------------------",
"// Import posts, done!",
"// -----------------------------------------------------------------------------",
"wp_defer_term_counting",
"(",
"false",
")",
";",
"// Flush",
"// Done",
"$",
"_SESSION",
"[",
"'pb_notices'",
"]",
"[",
"]",
"=",
"sprintf",
"(",
"_x",
"(",
"'Imported %1$s, %2$s, %3$s, %4$s, %5$s, and %6$s.'",
",",
"'String which tells user how many front matter, parts, chapters, back matter, media attachments, and glossary terms were imported.'",
",",
"'pressbooks'",
")",
",",
"sprintf",
"(",
"_n",
"(",
"'%s front matter'",
",",
"'%s front matter'",
",",
"$",
"totals",
"[",
"'front-matter'",
"]",
",",
"'pressbooks'",
")",
",",
"$",
"totals",
"[",
"'front-matter'",
"]",
")",
",",
"sprintf",
"(",
"_n",
"(",
"'%s part'",
",",
"'%s parts'",
",",
"$",
"totals",
"[",
"'part'",
"]",
",",
"'pressbooks'",
")",
",",
"$",
"totals",
"[",
"'part'",
"]",
")",
",",
"sprintf",
"(",
"_n",
"(",
"'%s chapter'",
",",
"'%s chapters'",
",",
"$",
"totals",
"[",
"'chapter'",
"]",
",",
"'pressbooks'",
")",
",",
"$",
"totals",
"[",
"'chapter'",
"]",
")",
",",
"sprintf",
"(",
"_n",
"(",
"'%s back matter'",
",",
"'%s back matter'",
",",
"$",
"totals",
"[",
"'back-matter'",
"]",
",",
"'pressbooks'",
")",
",",
"$",
"totals",
"[",
"'back-matter'",
"]",
")",
",",
"sprintf",
"(",
"_n",
"(",
"'%s media attachment'",
",",
"'%s media attachments'",
",",
"$",
"totals",
"[",
"'media'",
"]",
",",
"'pressbooks'",
")",
",",
"$",
"totals",
"[",
"'media'",
"]",
")",
",",
"sprintf",
"(",
"_n",
"(",
"'%s glossary term'",
",",
"'%s glossary terms'",
",",
"$",
"totals",
"[",
"'glossary'",
"]",
",",
"'pressbooks'",
")",
",",
"$",
"totals",
"[",
"'glossary'",
"]",
")",
")",
";",
"return",
"$",
"this",
"->",
"revokeCurrentImport",
"(",
")",
";",
"}"
] | @param array $current_import
@return bool | [
"@param",
"array",
"$current_import"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/import/wordpress/class-wxr.php#L178-L361 |
pressbooks/pressbooks | inc/modules/import/wordpress/class-wxr.php | Wxr.pbCheck | protected function pbCheck( array $xml ) {
$pt = 0;
$ch = 0;
$fm = 0;
$bm = 0;
$meta = 0;
foreach ( $xml['posts'] as $p ) {
if ( 'part' === $p['post_type'] ) {
$pt = 1;
} elseif ( 'chapter' === $p['post_type'] ) {
$ch = 1;
} elseif ( 'front-matter' === $p['post_type'] ) {
$fm = 1;
} elseif ( 'back-matter' === $p['post_type'] ) {
$bm = 1;
} elseif ( 'metadata' === $p['post_type'] ) {
$meta = 1;
}
if ( $pt + $ch + $fm + $bm + $meta >= 2 ) {
$this->isPbWxr = true;
break;
}
}
} | php | protected function pbCheck( array $xml ) {
$pt = 0;
$ch = 0;
$fm = 0;
$bm = 0;
$meta = 0;
foreach ( $xml['posts'] as $p ) {
if ( 'part' === $p['post_type'] ) {
$pt = 1;
} elseif ( 'chapter' === $p['post_type'] ) {
$ch = 1;
} elseif ( 'front-matter' === $p['post_type'] ) {
$fm = 1;
} elseif ( 'back-matter' === $p['post_type'] ) {
$bm = 1;
} elseif ( 'metadata' === $p['post_type'] ) {
$meta = 1;
}
if ( $pt + $ch + $fm + $bm + $meta >= 2 ) {
$this->isPbWxr = true;
break;
}
}
} | [
"protected",
"function",
"pbCheck",
"(",
"array",
"$",
"xml",
")",
"{",
"$",
"pt",
"=",
"0",
";",
"$",
"ch",
"=",
"0",
";",
"$",
"fm",
"=",
"0",
";",
"$",
"bm",
"=",
"0",
";",
"$",
"meta",
"=",
"0",
";",
"foreach",
"(",
"$",
"xml",
"[",
"'posts'",
"]",
"as",
"$",
"p",
")",
"{",
"if",
"(",
"'part'",
"===",
"$",
"p",
"[",
"'post_type'",
"]",
")",
"{",
"$",
"pt",
"=",
"1",
";",
"}",
"elseif",
"(",
"'chapter'",
"===",
"$",
"p",
"[",
"'post_type'",
"]",
")",
"{",
"$",
"ch",
"=",
"1",
";",
"}",
"elseif",
"(",
"'front-matter'",
"===",
"$",
"p",
"[",
"'post_type'",
"]",
")",
"{",
"$",
"fm",
"=",
"1",
";",
"}",
"elseif",
"(",
"'back-matter'",
"===",
"$",
"p",
"[",
"'post_type'",
"]",
")",
"{",
"$",
"bm",
"=",
"1",
";",
"}",
"elseif",
"(",
"'metadata'",
"===",
"$",
"p",
"[",
"'post_type'",
"]",
")",
"{",
"$",
"meta",
"=",
"1",
";",
"}",
"if",
"(",
"$",
"pt",
"+",
"$",
"ch",
"+",
"$",
"fm",
"+",
"$",
"bm",
"+",
"$",
"meta",
">=",
"2",
")",
"{",
"$",
"this",
"->",
"isPbWxr",
"=",
"true",
";",
"break",
";",
"}",
"}",
"}"
] | Is it a WXR generated by PB?
@param array $xml | [
"Is",
"it",
"a",
"WXR",
"generated",
"by",
"PB?"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/import/wordpress/class-wxr.php#L368-L396 |
pressbooks/pressbooks | inc/modules/import/wordpress/class-wxr.php | Wxr.customNestedSort | protected function customNestedSort( $xml ) {
$array = [];
//first, put them in ascending menu_order
usort(
$xml, function ( $a, $b ) {
return ( $a['menu_order'] - $b['menu_order'] );
}
);
// Start with book info
foreach ( $xml as $p ) {
if ( 'metadata' === $p['post_type'] ) {
$array[] = $p;
break;
}
}
//now, list all front matter
foreach ( $xml as $p ) {
if ( 'front-matter' === $p['post_type'] ) {
$array[] = $p;
}
}
//now, list all parts, then their associated chapters
foreach ( $xml as $p ) {
if ( 'part' === $p['post_type'] ) {
$array[] = $p;
foreach ( $xml as $psub ) {
if ( 'chapter' === $psub['post_type'] && $psub['post_parent'] === $p['post_id'] ) {
$array[] = $psub;
}
}
}
}
//now, list all back matter
foreach ( $xml as $p ) {
if ( 'back-matter' === $p['post_type'] ) {
$array[] = $p;
}
}
// Glossary
foreach ( $xml as $p ) {
if ( 'glossary' === $p['post_type'] ) {
$array[] = $p;
}
}
// Remaining custom post types
$custom_post_types = apply_filters( 'pb_import_custom_post_types', [] );
foreach ( $xml as $p ) {
if ( in_array( $p['post_type'], $custom_post_types, true ) ) {
$array[] = $p;
}
}
return $array;
} | php | protected function customNestedSort( $xml ) {
$array = [];
//first, put them in ascending menu_order
usort(
$xml, function ( $a, $b ) {
return ( $a['menu_order'] - $b['menu_order'] );
}
);
// Start with book info
foreach ( $xml as $p ) {
if ( 'metadata' === $p['post_type'] ) {
$array[] = $p;
break;
}
}
//now, list all front matter
foreach ( $xml as $p ) {
if ( 'front-matter' === $p['post_type'] ) {
$array[] = $p;
}
}
//now, list all parts, then their associated chapters
foreach ( $xml as $p ) {
if ( 'part' === $p['post_type'] ) {
$array[] = $p;
foreach ( $xml as $psub ) {
if ( 'chapter' === $psub['post_type'] && $psub['post_parent'] === $p['post_id'] ) {
$array[] = $psub;
}
}
}
}
//now, list all back matter
foreach ( $xml as $p ) {
if ( 'back-matter' === $p['post_type'] ) {
$array[] = $p;
}
}
// Glossary
foreach ( $xml as $p ) {
if ( 'glossary' === $p['post_type'] ) {
$array[] = $p;
}
}
// Remaining custom post types
$custom_post_types = apply_filters( 'pb_import_custom_post_types', [] );
foreach ( $xml as $p ) {
if ( in_array( $p['post_type'], $custom_post_types, true ) ) {
$array[] = $p;
}
}
return $array;
} | [
"protected",
"function",
"customNestedSort",
"(",
"$",
"xml",
")",
"{",
"$",
"array",
"=",
"[",
"]",
";",
"//first, put them in ascending menu_order",
"usort",
"(",
"$",
"xml",
",",
"function",
"(",
"$",
"a",
",",
"$",
"b",
")",
"{",
"return",
"(",
"$",
"a",
"[",
"'menu_order'",
"]",
"-",
"$",
"b",
"[",
"'menu_order'",
"]",
")",
";",
"}",
")",
";",
"// Start with book info",
"foreach",
"(",
"$",
"xml",
"as",
"$",
"p",
")",
"{",
"if",
"(",
"'metadata'",
"===",
"$",
"p",
"[",
"'post_type'",
"]",
")",
"{",
"$",
"array",
"[",
"]",
"=",
"$",
"p",
";",
"break",
";",
"}",
"}",
"//now, list all front matter",
"foreach",
"(",
"$",
"xml",
"as",
"$",
"p",
")",
"{",
"if",
"(",
"'front-matter'",
"===",
"$",
"p",
"[",
"'post_type'",
"]",
")",
"{",
"$",
"array",
"[",
"]",
"=",
"$",
"p",
";",
"}",
"}",
"//now, list all parts, then their associated chapters",
"foreach",
"(",
"$",
"xml",
"as",
"$",
"p",
")",
"{",
"if",
"(",
"'part'",
"===",
"$",
"p",
"[",
"'post_type'",
"]",
")",
"{",
"$",
"array",
"[",
"]",
"=",
"$",
"p",
";",
"foreach",
"(",
"$",
"xml",
"as",
"$",
"psub",
")",
"{",
"if",
"(",
"'chapter'",
"===",
"$",
"psub",
"[",
"'post_type'",
"]",
"&&",
"$",
"psub",
"[",
"'post_parent'",
"]",
"===",
"$",
"p",
"[",
"'post_id'",
"]",
")",
"{",
"$",
"array",
"[",
"]",
"=",
"$",
"psub",
";",
"}",
"}",
"}",
"}",
"//now, list all back matter",
"foreach",
"(",
"$",
"xml",
"as",
"$",
"p",
")",
"{",
"if",
"(",
"'back-matter'",
"===",
"$",
"p",
"[",
"'post_type'",
"]",
")",
"{",
"$",
"array",
"[",
"]",
"=",
"$",
"p",
";",
"}",
"}",
"// Glossary",
"foreach",
"(",
"$",
"xml",
"as",
"$",
"p",
")",
"{",
"if",
"(",
"'glossary'",
"===",
"$",
"p",
"[",
"'post_type'",
"]",
")",
"{",
"$",
"array",
"[",
"]",
"=",
"$",
"p",
";",
"}",
"}",
"// Remaining custom post types",
"$",
"custom_post_types",
"=",
"apply_filters",
"(",
"'pb_import_custom_post_types'",
",",
"[",
"]",
")",
";",
"foreach",
"(",
"$",
"xml",
"as",
"$",
"p",
")",
"{",
"if",
"(",
"in_array",
"(",
"$",
"p",
"[",
"'post_type'",
"]",
",",
"$",
"custom_post_types",
",",
"true",
")",
")",
"{",
"$",
"array",
"[",
"]",
"=",
"$",
"p",
";",
"}",
"}",
"return",
"$",
"array",
";",
"}"
] | Custom sort for the xml posts to put them in correct nested order
@param array $xml
@return array sorted $xml | [
"Custom",
"sort",
"for",
"the",
"xml",
"posts",
"to",
"put",
"them",
"in",
"correct",
"nested",
"order"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/import/wordpress/class-wxr.php#L405-L466 |
pressbooks/pressbooks | inc/modules/import/wordpress/class-wxr.php | Wxr.bookInfoPid | protected function bookInfoPid() {
$post = ( new Metadata() )->getMetaPost();
if ( empty( $post->ID ) ) {
$new_post = [
'post_title' => __( 'Book Info', 'pressbooks' ),
'post_type' => 'metadata',
'post_status' => 'publish',
];
$pid = wp_insert_post( add_magic_quotes( $new_post ) );
} else {
$pid = $post->ID;
}
return $pid;
} | php | protected function bookInfoPid() {
$post = ( new Metadata() )->getMetaPost();
if ( empty( $post->ID ) ) {
$new_post = [
'post_title' => __( 'Book Info', 'pressbooks' ),
'post_type' => 'metadata',
'post_status' => 'publish',
];
$pid = wp_insert_post( add_magic_quotes( $new_post ) );
} else {
$pid = $post->ID;
}
return $pid;
} | [
"protected",
"function",
"bookInfoPid",
"(",
")",
"{",
"$",
"post",
"=",
"(",
"new",
"Metadata",
"(",
")",
")",
"->",
"getMetaPost",
"(",
")",
";",
"if",
"(",
"empty",
"(",
"$",
"post",
"->",
"ID",
")",
")",
"{",
"$",
"new_post",
"=",
"[",
"'post_title'",
"=>",
"__",
"(",
"'Book Info'",
",",
"'pressbooks'",
")",
",",
"'post_type'",
"=>",
"'metadata'",
",",
"'post_status'",
"=>",
"'publish'",
",",
"]",
";",
"$",
"pid",
"=",
"wp_insert_post",
"(",
"add_magic_quotes",
"(",
"$",
"new_post",
")",
")",
";",
"}",
"else",
"{",
"$",
"pid",
"=",
"$",
"post",
"->",
"ID",
";",
"}",
"return",
"$",
"pid",
";",
"}"
] | Get existing Meta Post, if none exists create one
@return int Post ID | [
"Get",
"existing",
"Meta",
"Post",
"if",
"none",
"exists",
"create",
"one"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/import/wordpress/class-wxr.php#L474-L489 |
pressbooks/pressbooks | inc/modules/import/wordpress/class-wxr.php | Wxr.insertNewPost | protected function insertNewPost( $post_type, $p, $html, $chapter_parent, $post_status ) {
$new_post = [
'post_title' => wp_strip_all_tags( $p['post_title'] ),
'post_name' => $p['post_name'],
'post_type' => $post_type,
'post_status' => ( 'part' === $post_type ) ? 'publish' : $post_status,
'post_content' => $html,
];
if ( 'chapter' === $post_type ) {
$new_post['post_parent'] = $chapter_parent;
}
$pid = wp_insert_post( add_magic_quotes( $new_post ) );
return $pid;
} | php | protected function insertNewPost( $post_type, $p, $html, $chapter_parent, $post_status ) {
$new_post = [
'post_title' => wp_strip_all_tags( $p['post_title'] ),
'post_name' => $p['post_name'],
'post_type' => $post_type,
'post_status' => ( 'part' === $post_type ) ? 'publish' : $post_status,
'post_content' => $html,
];
if ( 'chapter' === $post_type ) {
$new_post['post_parent'] = $chapter_parent;
}
$pid = wp_insert_post( add_magic_quotes( $new_post ) );
return $pid;
} | [
"protected",
"function",
"insertNewPost",
"(",
"$",
"post_type",
",",
"$",
"p",
",",
"$",
"html",
",",
"$",
"chapter_parent",
",",
"$",
"post_status",
")",
"{",
"$",
"new_post",
"=",
"[",
"'post_title'",
"=>",
"wp_strip_all_tags",
"(",
"$",
"p",
"[",
"'post_title'",
"]",
")",
",",
"'post_name'",
"=>",
"$",
"p",
"[",
"'post_name'",
"]",
",",
"'post_type'",
"=>",
"$",
"post_type",
",",
"'post_status'",
"=>",
"(",
"'part'",
"===",
"$",
"post_type",
")",
"?",
"'publish'",
":",
"$",
"post_status",
",",
"'post_content'",
"=>",
"$",
"html",
",",
"]",
";",
"if",
"(",
"'chapter'",
"===",
"$",
"post_type",
")",
"{",
"$",
"new_post",
"[",
"'post_parent'",
"]",
"=",
"$",
"chapter_parent",
";",
"}",
"$",
"pid",
"=",
"wp_insert_post",
"(",
"add_magic_quotes",
"(",
"$",
"new_post",
")",
")",
";",
"return",
"$",
"pid",
";",
"}"
] | Insert a new post
@param string $post_type Post Type
@param array $p Single Item Returned From \Pressbooks\Modules\Import\WordPress\Parser::parse
@param string $html
@param int $chapter_parent
@param string $post_status
@return int Post ID | [
"Insert",
"a",
"new",
"post"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/import/wordpress/class-wxr.php#L502-L519 |
pressbooks/pressbooks | inc/modules/import/wordpress/class-wxr.php | Wxr.importPbPostMeta | protected function importPbPostMeta( $pid, $p ) {
$data_model = $this->figureOutDataModel( $p['postmeta'] );
$meta_to_update = apply_filters( 'pb_import_metakeys', [ 'pb_section_license', 'pb_short_title', 'pb_subtitle', 'pb_show_title' ] );
foreach ( $meta_to_update as $meta_key ) {
$meta_val = $this->searchForMetaValue( $meta_key, $p['postmeta'] );
if ( $meta_val ) {
update_post_meta( $pid, $meta_key, $meta_val );
if ( $meta_key === 'pb_section_license' ) {
wp_set_object_terms( $pid, $meta_val, Licensing::TAXONOMY ); // Link
}
}
}
if ( $data_model === 5 ) {
$meta_val = $this->searchForMetaValue( 'pb_authors', $p['postmeta'] );
if ( $meta_val ) {
// PB5 contributors (slugs)
add_post_meta( $pid, 'pb_authors', $meta_val );
wp_set_object_terms( $pid, $meta_val, Contributors::TAXONOMY );
}
} else {
$meta_val = $this->searchForMetaValue( 'pb_section_author', $p['postmeta'] );
if ( $meta_val ) {
// PB4 contributors (full names)
$this->contributors->convert( 'pb_section_author', $meta_val, $pid );
}
}
} | php | protected function importPbPostMeta( $pid, $p ) {
$data_model = $this->figureOutDataModel( $p['postmeta'] );
$meta_to_update = apply_filters( 'pb_import_metakeys', [ 'pb_section_license', 'pb_short_title', 'pb_subtitle', 'pb_show_title' ] );
foreach ( $meta_to_update as $meta_key ) {
$meta_val = $this->searchForMetaValue( $meta_key, $p['postmeta'] );
if ( $meta_val ) {
update_post_meta( $pid, $meta_key, $meta_val );
if ( $meta_key === 'pb_section_license' ) {
wp_set_object_terms( $pid, $meta_val, Licensing::TAXONOMY ); // Link
}
}
}
if ( $data_model === 5 ) {
$meta_val = $this->searchForMetaValue( 'pb_authors', $p['postmeta'] );
if ( $meta_val ) {
// PB5 contributors (slugs)
add_post_meta( $pid, 'pb_authors', $meta_val );
wp_set_object_terms( $pid, $meta_val, Contributors::TAXONOMY );
}
} else {
$meta_val = $this->searchForMetaValue( 'pb_section_author', $p['postmeta'] );
if ( $meta_val ) {
// PB4 contributors (full names)
$this->contributors->convert( 'pb_section_author', $meta_val, $pid );
}
}
} | [
"protected",
"function",
"importPbPostMeta",
"(",
"$",
"pid",
",",
"$",
"p",
")",
"{",
"$",
"data_model",
"=",
"$",
"this",
"->",
"figureOutDataModel",
"(",
"$",
"p",
"[",
"'postmeta'",
"]",
")",
";",
"$",
"meta_to_update",
"=",
"apply_filters",
"(",
"'pb_import_metakeys'",
",",
"[",
"'pb_section_license'",
",",
"'pb_short_title'",
",",
"'pb_subtitle'",
",",
"'pb_show_title'",
"]",
")",
";",
"foreach",
"(",
"$",
"meta_to_update",
"as",
"$",
"meta_key",
")",
"{",
"$",
"meta_val",
"=",
"$",
"this",
"->",
"searchForMetaValue",
"(",
"$",
"meta_key",
",",
"$",
"p",
"[",
"'postmeta'",
"]",
")",
";",
"if",
"(",
"$",
"meta_val",
")",
"{",
"update_post_meta",
"(",
"$",
"pid",
",",
"$",
"meta_key",
",",
"$",
"meta_val",
")",
";",
"if",
"(",
"$",
"meta_key",
"===",
"'pb_section_license'",
")",
"{",
"wp_set_object_terms",
"(",
"$",
"pid",
",",
"$",
"meta_val",
",",
"Licensing",
"::",
"TAXONOMY",
")",
";",
"// Link",
"}",
"}",
"}",
"if",
"(",
"$",
"data_model",
"===",
"5",
")",
"{",
"$",
"meta_val",
"=",
"$",
"this",
"->",
"searchForMetaValue",
"(",
"'pb_authors'",
",",
"$",
"p",
"[",
"'postmeta'",
"]",
")",
";",
"if",
"(",
"$",
"meta_val",
")",
"{",
"// PB5 contributors (slugs)",
"add_post_meta",
"(",
"$",
"pid",
",",
"'pb_authors'",
",",
"$",
"meta_val",
")",
";",
"wp_set_object_terms",
"(",
"$",
"pid",
",",
"$",
"meta_val",
",",
"Contributors",
"::",
"TAXONOMY",
")",
";",
"}",
"}",
"else",
"{",
"$",
"meta_val",
"=",
"$",
"this",
"->",
"searchForMetaValue",
"(",
"'pb_section_author'",
",",
"$",
"p",
"[",
"'postmeta'",
"]",
")",
";",
"if",
"(",
"$",
"meta_val",
")",
"{",
"// PB4 contributors (full names)",
"$",
"this",
"->",
"contributors",
"->",
"convert",
"(",
"'pb_section_author'",
",",
"$",
"meta_val",
",",
"$",
"pid",
")",
";",
"}",
"}",
"}"
] | Import Pressbooks specific post meta
@param int $pid Post ID
@param array $p Single Item Returned From \Pressbooks\Modules\Import\WordPress\Parser::parse | [
"Import",
"Pressbooks",
"specific",
"post",
"meta"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/import/wordpress/class-wxr.php#L527-L556 |
pressbooks/pressbooks | inc/modules/import/wordpress/class-wxr.php | Wxr.importMetaBoxes | protected function importMetaBoxes( $pid, $p ) {
$data_model = $this->figureOutDataModel( $p['postmeta'] );
// List of meta data keys that can support multiple values:
$metadata_array_values = [
'pb_keywords_tags',
'pb_bisac_subject',
'pb_additional_subjects',
];
// Clear old meta boxes
$metadata = get_post_meta( $pid );
foreach ( $metadata as $key => $val ) {
// Does key start with pb_ prefix?
if ( 0 === strpos( $key, 'pb_' ) ) {
delete_post_meta( $pid, $key );
}
}
// Import contributors
foreach ( $p['postmeta'] as $meta ) {
if ( $data_model === 5 && $this->contributors->isValid( $meta['key'] ) ) {
// PB5 contributors (slugs)
add_post_meta( $pid, $meta['key'], $meta['value'] );
wp_set_object_terms( $pid, $meta['value'], Contributors::TAXONOMY );
} elseif ( $data_model === 4 && $this->contributors->isDeprecated( $meta['key'] ) ) {
// PB4 contributors (full names)
$this->contributors->convert( $meta['key'], $meta['value'], $pid );
}
}
// Import post meta
foreach ( $p['postmeta'] as $meta ) {
if ( 0 !== strpos( $meta['key'], 'pb_' ) ) {
continue; // Skip
}
// Skip contributor meta (already done, look up)
if ( $this->contributors->isValid( $meta['key'] ) || $this->contributors->isDeprecated( $meta['key'] ) ) {
continue;
}
if ( isset( $metadata_array_values[ $meta['key'] ] ) ) {
// Multi value
add_post_meta( $pid, $meta['key'], $meta['value'] );
} else {
// Single value
if ( ! add_post_meta( $pid, $meta['key'], $meta['value'], true ) ) {
update_post_meta( $pid, $meta['key'], $meta['value'] );
}
if ( $meta['key'] === 'pb_book_license' ) {
wp_set_object_terms( $pid, $meta['value'], Licensing::TAXONOMY ); // Link
}
}
}
} | php | protected function importMetaBoxes( $pid, $p ) {
$data_model = $this->figureOutDataModel( $p['postmeta'] );
// List of meta data keys that can support multiple values:
$metadata_array_values = [
'pb_keywords_tags',
'pb_bisac_subject',
'pb_additional_subjects',
];
// Clear old meta boxes
$metadata = get_post_meta( $pid );
foreach ( $metadata as $key => $val ) {
// Does key start with pb_ prefix?
if ( 0 === strpos( $key, 'pb_' ) ) {
delete_post_meta( $pid, $key );
}
}
// Import contributors
foreach ( $p['postmeta'] as $meta ) {
if ( $data_model === 5 && $this->contributors->isValid( $meta['key'] ) ) {
// PB5 contributors (slugs)
add_post_meta( $pid, $meta['key'], $meta['value'] );
wp_set_object_terms( $pid, $meta['value'], Contributors::TAXONOMY );
} elseif ( $data_model === 4 && $this->contributors->isDeprecated( $meta['key'] ) ) {
// PB4 contributors (full names)
$this->contributors->convert( $meta['key'], $meta['value'], $pid );
}
}
// Import post meta
foreach ( $p['postmeta'] as $meta ) {
if ( 0 !== strpos( $meta['key'], 'pb_' ) ) {
continue; // Skip
}
// Skip contributor meta (already done, look up)
if ( $this->contributors->isValid( $meta['key'] ) || $this->contributors->isDeprecated( $meta['key'] ) ) {
continue;
}
if ( isset( $metadata_array_values[ $meta['key'] ] ) ) {
// Multi value
add_post_meta( $pid, $meta['key'], $meta['value'] );
} else {
// Single value
if ( ! add_post_meta( $pid, $meta['key'], $meta['value'], true ) ) {
update_post_meta( $pid, $meta['key'], $meta['value'] );
}
if ( $meta['key'] === 'pb_book_license' ) {
wp_set_object_terms( $pid, $meta['value'], Licensing::TAXONOMY ); // Link
}
}
}
} | [
"protected",
"function",
"importMetaBoxes",
"(",
"$",
"pid",
",",
"$",
"p",
")",
"{",
"$",
"data_model",
"=",
"$",
"this",
"->",
"figureOutDataModel",
"(",
"$",
"p",
"[",
"'postmeta'",
"]",
")",
";",
"// List of meta data keys that can support multiple values:",
"$",
"metadata_array_values",
"=",
"[",
"'pb_keywords_tags'",
",",
"'pb_bisac_subject'",
",",
"'pb_additional_subjects'",
",",
"]",
";",
"// Clear old meta boxes",
"$",
"metadata",
"=",
"get_post_meta",
"(",
"$",
"pid",
")",
";",
"foreach",
"(",
"$",
"metadata",
"as",
"$",
"key",
"=>",
"$",
"val",
")",
"{",
"// Does key start with pb_ prefix?",
"if",
"(",
"0",
"===",
"strpos",
"(",
"$",
"key",
",",
"'pb_'",
")",
")",
"{",
"delete_post_meta",
"(",
"$",
"pid",
",",
"$",
"key",
")",
";",
"}",
"}",
"// Import contributors",
"foreach",
"(",
"$",
"p",
"[",
"'postmeta'",
"]",
"as",
"$",
"meta",
")",
"{",
"if",
"(",
"$",
"data_model",
"===",
"5",
"&&",
"$",
"this",
"->",
"contributors",
"->",
"isValid",
"(",
"$",
"meta",
"[",
"'key'",
"]",
")",
")",
"{",
"// PB5 contributors (slugs)",
"add_post_meta",
"(",
"$",
"pid",
",",
"$",
"meta",
"[",
"'key'",
"]",
",",
"$",
"meta",
"[",
"'value'",
"]",
")",
";",
"wp_set_object_terms",
"(",
"$",
"pid",
",",
"$",
"meta",
"[",
"'value'",
"]",
",",
"Contributors",
"::",
"TAXONOMY",
")",
";",
"}",
"elseif",
"(",
"$",
"data_model",
"===",
"4",
"&&",
"$",
"this",
"->",
"contributors",
"->",
"isDeprecated",
"(",
"$",
"meta",
"[",
"'key'",
"]",
")",
")",
"{",
"// PB4 contributors (full names)",
"$",
"this",
"->",
"contributors",
"->",
"convert",
"(",
"$",
"meta",
"[",
"'key'",
"]",
",",
"$",
"meta",
"[",
"'value'",
"]",
",",
"$",
"pid",
")",
";",
"}",
"}",
"// Import post meta",
"foreach",
"(",
"$",
"p",
"[",
"'postmeta'",
"]",
"as",
"$",
"meta",
")",
"{",
"if",
"(",
"0",
"!==",
"strpos",
"(",
"$",
"meta",
"[",
"'key'",
"]",
",",
"'pb_'",
")",
")",
"{",
"continue",
";",
"// Skip",
"}",
"// Skip contributor meta (already done, look up)",
"if",
"(",
"$",
"this",
"->",
"contributors",
"->",
"isValid",
"(",
"$",
"meta",
"[",
"'key'",
"]",
")",
"||",
"$",
"this",
"->",
"contributors",
"->",
"isDeprecated",
"(",
"$",
"meta",
"[",
"'key'",
"]",
")",
")",
"{",
"continue",
";",
"}",
"if",
"(",
"isset",
"(",
"$",
"metadata_array_values",
"[",
"$",
"meta",
"[",
"'key'",
"]",
"]",
")",
")",
"{",
"// Multi value",
"add_post_meta",
"(",
"$",
"pid",
",",
"$",
"meta",
"[",
"'key'",
"]",
",",
"$",
"meta",
"[",
"'value'",
"]",
")",
";",
"}",
"else",
"{",
"// Single value",
"if",
"(",
"!",
"add_post_meta",
"(",
"$",
"pid",
",",
"$",
"meta",
"[",
"'key'",
"]",
",",
"$",
"meta",
"[",
"'value'",
"]",
",",
"true",
")",
")",
"{",
"update_post_meta",
"(",
"$",
"pid",
",",
"$",
"meta",
"[",
"'key'",
"]",
",",
"$",
"meta",
"[",
"'value'",
"]",
")",
";",
"}",
"if",
"(",
"$",
"meta",
"[",
"'key'",
"]",
"===",
"'pb_book_license'",
")",
"{",
"wp_set_object_terms",
"(",
"$",
"pid",
",",
"$",
"meta",
"[",
"'value'",
"]",
",",
"Licensing",
"::",
"TAXONOMY",
")",
";",
"// Link",
"}",
"}",
"}",
"}"
] | @see \Pressbooks\Admin\Metaboxes\add_meta_boxes
@param int $pid Post ID
@param array $p Single Item Returned From \Pressbooks\Modules\Import\WordPress\Parser::parse | [
"@see",
"\\",
"Pressbooks",
"\\",
"Admin",
"\\",
"Metaboxes",
"\\",
"add_meta_boxes"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/import/wordpress/class-wxr.php#L564-L619 |
pressbooks/pressbooks | inc/modules/import/wordpress/class-wxr.php | Wxr.searchForMetaValue | protected function searchForMetaValue( $meta_key, array $postmeta ) {
if ( empty( $postmeta ) ) {
return '';
}
foreach ( $postmeta as $meta ) {
// prefer this value, if it's set
if ( $meta_key === $meta['key'] ) {
$meta_val = $meta['value'];
if ( is_serialized( $meta_val ) ) {
$meta_val = unserialize( $meta_val ); // @codingStandardsIgnoreLine
if ( is_object( $meta_val ) ) {
$meta_val = ''; // Hack attempt?
}
}
return $meta_val;
}
}
return '';
} | php | protected function searchForMetaValue( $meta_key, array $postmeta ) {
if ( empty( $postmeta ) ) {
return '';
}
foreach ( $postmeta as $meta ) {
// prefer this value, if it's set
if ( $meta_key === $meta['key'] ) {
$meta_val = $meta['value'];
if ( is_serialized( $meta_val ) ) {
$meta_val = unserialize( $meta_val ); // @codingStandardsIgnoreLine
if ( is_object( $meta_val ) ) {
$meta_val = ''; // Hack attempt?
}
}
return $meta_val;
}
}
return '';
} | [
"protected",
"function",
"searchForMetaValue",
"(",
"$",
"meta_key",
",",
"array",
"$",
"postmeta",
")",
"{",
"if",
"(",
"empty",
"(",
"$",
"postmeta",
")",
")",
"{",
"return",
"''",
";",
"}",
"foreach",
"(",
"$",
"postmeta",
"as",
"$",
"meta",
")",
"{",
"// prefer this value, if it's set",
"if",
"(",
"$",
"meta_key",
"===",
"$",
"meta",
"[",
"'key'",
"]",
")",
"{",
"$",
"meta_val",
"=",
"$",
"meta",
"[",
"'value'",
"]",
";",
"if",
"(",
"is_serialized",
"(",
"$",
"meta_val",
")",
")",
"{",
"$",
"meta_val",
"=",
"unserialize",
"(",
"$",
"meta_val",
")",
";",
"// @codingStandardsIgnoreLine",
"if",
"(",
"is_object",
"(",
"$",
"meta_val",
")",
")",
"{",
"$",
"meta_val",
"=",
"''",
";",
"// Hack attempt?",
"}",
"}",
"return",
"$",
"meta_val",
";",
"}",
"}",
"return",
"''",
";",
"}"
] | Check for PB specific metadata, returns empty string if not found.
@param string $meta_key
@param array $postmeta
@return string meta field value | [
"Check",
"for",
"PB",
"specific",
"metadata",
"returns",
"empty",
"string",
"if",
"not",
"found",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/import/wordpress/class-wxr.php#L629-L650 |
pressbooks/pressbooks | inc/modules/import/wordpress/class-wxr.php | Wxr.buildListOfKnownMedia | public function buildListOfKnownMedia( $xml ) {
$known_media = [];
foreach ( $xml['posts'] as $item ) {
if ( $item['post_type'] !== 'attachment' ) {
continue; // Not an attachment, skip
}
$x = [];
foreach ( $item['postmeta'] as $meta ) {
if ( $meta['key'] === '_wp_attachment_metadata' ) {
$x = maybe_unserialize( $meta['value'] );
break;
}
}
if ( ! is_array( $x ) || empty( $x ) ) {
continue; // Something went wrong, skip
}
$m = $this->createMediaEntity( $item );
if ( preg_match( $this->downloads->getPregSupportedImageExtensions(), $m->sourceUrl ) ) {
$prefix = str_replace( $this->downloads->basename( $m->sourceUrl ), '', $x['file'] ); // 2017/08
foreach ( $x['sizes'] as $size => $info ) {
$attached_file = $prefix . $info['file']; // 2017/08/foo-bar-300x225.png
$known_media[ $attached_file ] = $m;
}
} else {
$attached_file = media_strip_baseurl( $m->sourceUrl ); // 2017/08/foo-bar.ext
$known_media[ $attached_file ] = $m;
}
}
return $known_media;
} | php | public function buildListOfKnownMedia( $xml ) {
$known_media = [];
foreach ( $xml['posts'] as $item ) {
if ( $item['post_type'] !== 'attachment' ) {
continue; // Not an attachment, skip
}
$x = [];
foreach ( $item['postmeta'] as $meta ) {
if ( $meta['key'] === '_wp_attachment_metadata' ) {
$x = maybe_unserialize( $meta['value'] );
break;
}
}
if ( ! is_array( $x ) || empty( $x ) ) {
continue; // Something went wrong, skip
}
$m = $this->createMediaEntity( $item );
if ( preg_match( $this->downloads->getPregSupportedImageExtensions(), $m->sourceUrl ) ) {
$prefix = str_replace( $this->downloads->basename( $m->sourceUrl ), '', $x['file'] ); // 2017/08
foreach ( $x['sizes'] as $size => $info ) {
$attached_file = $prefix . $info['file']; // 2017/08/foo-bar-300x225.png
$known_media[ $attached_file ] = $m;
}
} else {
$attached_file = media_strip_baseurl( $m->sourceUrl ); // 2017/08/foo-bar.ext
$known_media[ $attached_file ] = $m;
}
}
return $known_media;
} | [
"public",
"function",
"buildListOfKnownMedia",
"(",
"$",
"xml",
")",
"{",
"$",
"known_media",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"xml",
"[",
"'posts'",
"]",
"as",
"$",
"item",
")",
"{",
"if",
"(",
"$",
"item",
"[",
"'post_type'",
"]",
"!==",
"'attachment'",
")",
"{",
"continue",
";",
"// Not an attachment, skip",
"}",
"$",
"x",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"item",
"[",
"'postmeta'",
"]",
"as",
"$",
"meta",
")",
"{",
"if",
"(",
"$",
"meta",
"[",
"'key'",
"]",
"===",
"'_wp_attachment_metadata'",
")",
"{",
"$",
"x",
"=",
"maybe_unserialize",
"(",
"$",
"meta",
"[",
"'value'",
"]",
")",
";",
"break",
";",
"}",
"}",
"if",
"(",
"!",
"is_array",
"(",
"$",
"x",
")",
"||",
"empty",
"(",
"$",
"x",
")",
")",
"{",
"continue",
";",
"// Something went wrong, skip",
"}",
"$",
"m",
"=",
"$",
"this",
"->",
"createMediaEntity",
"(",
"$",
"item",
")",
";",
"if",
"(",
"preg_match",
"(",
"$",
"this",
"->",
"downloads",
"->",
"getPregSupportedImageExtensions",
"(",
")",
",",
"$",
"m",
"->",
"sourceUrl",
")",
")",
"{",
"$",
"prefix",
"=",
"str_replace",
"(",
"$",
"this",
"->",
"downloads",
"->",
"basename",
"(",
"$",
"m",
"->",
"sourceUrl",
")",
",",
"''",
",",
"$",
"x",
"[",
"'file'",
"]",
")",
";",
"// 2017/08",
"foreach",
"(",
"$",
"x",
"[",
"'sizes'",
"]",
"as",
"$",
"size",
"=>",
"$",
"info",
")",
"{",
"$",
"attached_file",
"=",
"$",
"prefix",
".",
"$",
"info",
"[",
"'file'",
"]",
";",
"// 2017/08/foo-bar-300x225.png",
"$",
"known_media",
"[",
"$",
"attached_file",
"]",
"=",
"$",
"m",
";",
"}",
"}",
"else",
"{",
"$",
"attached_file",
"=",
"media_strip_baseurl",
"(",
"$",
"m",
"->",
"sourceUrl",
")",
";",
"// 2017/08/foo-bar.ext",
"$",
"known_media",
"[",
"$",
"attached_file",
"]",
"=",
"$",
"m",
";",
"}",
"}",
"return",
"$",
"known_media",
";",
"}"
] | Parse XML to build an array of known images
@param array $xml
@return \Pressbooks\Entities\Cloner\Media[] | [
"Parse",
"XML",
"to",
"build",
"an",
"array",
"of",
"known",
"images"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/import/wordpress/class-wxr.php#L659-L692 |
pressbooks/pressbooks | inc/modules/import/wordpress/class-wxr.php | Wxr.createMediaEntity | protected function createMediaEntity( $item ) {
$m = new \Pressbooks\Entities\Cloner\Media();
$m->id = $item['post_id'];
$m->title = $item['post_title'];
$m->description = $item['post_content'];
$m->caption = $item['post_excerpt'];
if ( isset( $item['postmeta'] ) && is_array( $item['postmeta'] ) ) {
foreach ( $item['postmeta'] as $meta ) {
if ( str_starts_with( $meta['key'], '_' ) === false ) {
$m->meta[ $meta['key'] ] = $meta['value'];
}
if ( $meta['key'] === '_wp_attachment_image_alt' ) {
$m->altText = $meta['value'];
}
}
}
$m->sourceUrl = $item['attachment_url'];
return $m;
} | php | protected function createMediaEntity( $item ) {
$m = new \Pressbooks\Entities\Cloner\Media();
$m->id = $item['post_id'];
$m->title = $item['post_title'];
$m->description = $item['post_content'];
$m->caption = $item['post_excerpt'];
if ( isset( $item['postmeta'] ) && is_array( $item['postmeta'] ) ) {
foreach ( $item['postmeta'] as $meta ) {
if ( str_starts_with( $meta['key'], '_' ) === false ) {
$m->meta[ $meta['key'] ] = $meta['value'];
}
if ( $meta['key'] === '_wp_attachment_image_alt' ) {
$m->altText = $meta['value'];
}
}
}
$m->sourceUrl = $item['attachment_url'];
return $m;
} | [
"protected",
"function",
"createMediaEntity",
"(",
"$",
"item",
")",
"{",
"$",
"m",
"=",
"new",
"\\",
"Pressbooks",
"\\",
"Entities",
"\\",
"Cloner",
"\\",
"Media",
"(",
")",
";",
"$",
"m",
"->",
"id",
"=",
"$",
"item",
"[",
"'post_id'",
"]",
";",
"$",
"m",
"->",
"title",
"=",
"$",
"item",
"[",
"'post_title'",
"]",
";",
"$",
"m",
"->",
"description",
"=",
"$",
"item",
"[",
"'post_content'",
"]",
";",
"$",
"m",
"->",
"caption",
"=",
"$",
"item",
"[",
"'post_excerpt'",
"]",
";",
"if",
"(",
"isset",
"(",
"$",
"item",
"[",
"'postmeta'",
"]",
")",
"&&",
"is_array",
"(",
"$",
"item",
"[",
"'postmeta'",
"]",
")",
")",
"{",
"foreach",
"(",
"$",
"item",
"[",
"'postmeta'",
"]",
"as",
"$",
"meta",
")",
"{",
"if",
"(",
"str_starts_with",
"(",
"$",
"meta",
"[",
"'key'",
"]",
",",
"'_'",
")",
"===",
"false",
")",
"{",
"$",
"m",
"->",
"meta",
"[",
"$",
"meta",
"[",
"'key'",
"]",
"]",
"=",
"$",
"meta",
"[",
"'value'",
"]",
";",
"}",
"if",
"(",
"$",
"meta",
"[",
"'key'",
"]",
"===",
"'_wp_attachment_image_alt'",
")",
"{",
"$",
"m",
"->",
"altText",
"=",
"$",
"meta",
"[",
"'value'",
"]",
";",
"}",
"}",
"}",
"$",
"m",
"->",
"sourceUrl",
"=",
"$",
"item",
"[",
"'attachment_url'",
"]",
";",
"return",
"$",
"m",
";",
"}"
] | @param array $item
@return \Pressbooks\Entities\Cloner\Media | [
"@param",
"array",
"$item"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/import/wordpress/class-wxr.php#L699-L721 |
pressbooks/pressbooks | inc/modules/import/wordpress/class-wxr.php | Wxr.checkInternalShortcodes | protected function checkInternalShortcodes( $post_id, $html ) {
// Glossary
if ( has_shortcode( $html, \Pressbooks\Shortcodes\Glossary\Glossary::SHORTCODE ) ) {
$this->postsWithGlossaryShortcodesToFix[] = $post_id;
}
// Attachments
if ( has_shortcode( $html, \Pressbooks\Shortcodes\Attributions\Attachments::SHORTCODE ) ) {
$this->postsWithAttachmentsShortcodesToFix[] = $post_id;
}
// TODO
// H5P not supported in WXR imports
} | php | protected function checkInternalShortcodes( $post_id, $html ) {
// Glossary
if ( has_shortcode( $html, \Pressbooks\Shortcodes\Glossary\Glossary::SHORTCODE ) ) {
$this->postsWithGlossaryShortcodesToFix[] = $post_id;
}
// Attachments
if ( has_shortcode( $html, \Pressbooks\Shortcodes\Attributions\Attachments::SHORTCODE ) ) {
$this->postsWithAttachmentsShortcodesToFix[] = $post_id;
}
// TODO
// H5P not supported in WXR imports
} | [
"protected",
"function",
"checkInternalShortcodes",
"(",
"$",
"post_id",
",",
"$",
"html",
")",
"{",
"// Glossary",
"if",
"(",
"has_shortcode",
"(",
"$",
"html",
",",
"\\",
"Pressbooks",
"\\",
"Shortcodes",
"\\",
"Glossary",
"\\",
"Glossary",
"::",
"SHORTCODE",
")",
")",
"{",
"$",
"this",
"->",
"postsWithGlossaryShortcodesToFix",
"[",
"]",
"=",
"$",
"post_id",
";",
"}",
"// Attachments",
"if",
"(",
"has_shortcode",
"(",
"$",
"html",
",",
"\\",
"Pressbooks",
"\\",
"Shortcodes",
"\\",
"Attributions",
"\\",
"Attachments",
"::",
"SHORTCODE",
")",
")",
"{",
"$",
"this",
"->",
"postsWithAttachmentsShortcodesToFix",
"[",
"]",
"=",
"$",
"post_id",
";",
"}",
"// TODO",
"// H5P not supported in WXR imports",
"}"
] | Check if post content contains shortcodes with references to internal IDs that we will need to fix
@param int $post_id
@param string $html | [
"Check",
"if",
"post",
"content",
"contains",
"shortcodes",
"with",
"references",
"to",
"internal",
"IDs",
"that",
"we",
"will",
"need",
"to",
"fix"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/import/wordpress/class-wxr.php#L745-L756 |
pressbooks/pressbooks | inc/modules/import/wordpress/class-wxr.php | Wxr.fixInternalShortcodes | protected function fixInternalShortcodes() {
// Because $fix replaces left to right, it might replace a previously inserted value when doing multiple replacements.
// Solved by creating a placeholder that can't possibly fall into the replacement order gotcha (famous last words)
$fix = function ( $post_id, $transition_type, $shortcode ) {
$replace_pairs = [];
$post = get_post( $post_id );
foreach ( $this->transitions as $transition ) {
if ( $transition->type === $transition_type ) {
$md5 = md5( $transition->oldId . $transition->newId . rand() );
$to = "<!-- pb_fixme_{$md5} -->";
$replace_pairs[ $to ] = $transition->newId;
$post->post_content = \Pressbooks\Utility\shortcode_att_replace(
$post->post_content,
$shortcode,
'id',
$transition->oldId,
$to
);
}
}
if ( ! empty( $replace_pairs ) ) {
$post->post_content = strtr( $post->post_content, $replace_pairs );
wp_update_post( $post );
}
};
// Glossary
foreach ( $this->postsWithGlossaryShortcodesToFix as $post_id ) {
$fix( $post_id, 'glossary', \Pressbooks\Shortcodes\Glossary\Glossary::SHORTCODE );
}
// Attachments
foreach ( $this->postsWithAttachmentsShortcodesToFix as $post_id ) {
$fix( $post_id, 'attachment', \Pressbooks\Shortcodes\Attributions\Attachments::SHORTCODE );
}
// TODO
// H5P not supported in WXR imports
} | php | protected function fixInternalShortcodes() {
// Because $fix replaces left to right, it might replace a previously inserted value when doing multiple replacements.
// Solved by creating a placeholder that can't possibly fall into the replacement order gotcha (famous last words)
$fix = function ( $post_id, $transition_type, $shortcode ) {
$replace_pairs = [];
$post = get_post( $post_id );
foreach ( $this->transitions as $transition ) {
if ( $transition->type === $transition_type ) {
$md5 = md5( $transition->oldId . $transition->newId . rand() );
$to = "<!-- pb_fixme_{$md5} -->";
$replace_pairs[ $to ] = $transition->newId;
$post->post_content = \Pressbooks\Utility\shortcode_att_replace(
$post->post_content,
$shortcode,
'id',
$transition->oldId,
$to
);
}
}
if ( ! empty( $replace_pairs ) ) {
$post->post_content = strtr( $post->post_content, $replace_pairs );
wp_update_post( $post );
}
};
// Glossary
foreach ( $this->postsWithGlossaryShortcodesToFix as $post_id ) {
$fix( $post_id, 'glossary', \Pressbooks\Shortcodes\Glossary\Glossary::SHORTCODE );
}
// Attachments
foreach ( $this->postsWithAttachmentsShortcodesToFix as $post_id ) {
$fix( $post_id, 'attachment', \Pressbooks\Shortcodes\Attributions\Attachments::SHORTCODE );
}
// TODO
// H5P not supported in WXR imports
} | [
"protected",
"function",
"fixInternalShortcodes",
"(",
")",
"{",
"// Because $fix replaces left to right, it might replace a previously inserted value when doing multiple replacements.",
"// Solved by creating a placeholder that can't possibly fall into the replacement order gotcha (famous last words)",
"$",
"fix",
"=",
"function",
"(",
"$",
"post_id",
",",
"$",
"transition_type",
",",
"$",
"shortcode",
")",
"{",
"$",
"replace_pairs",
"=",
"[",
"]",
";",
"$",
"post",
"=",
"get_post",
"(",
"$",
"post_id",
")",
";",
"foreach",
"(",
"$",
"this",
"->",
"transitions",
"as",
"$",
"transition",
")",
"{",
"if",
"(",
"$",
"transition",
"->",
"type",
"===",
"$",
"transition_type",
")",
"{",
"$",
"md5",
"=",
"md5",
"(",
"$",
"transition",
"->",
"oldId",
".",
"$",
"transition",
"->",
"newId",
".",
"rand",
"(",
")",
")",
";",
"$",
"to",
"=",
"\"<!-- pb_fixme_{$md5} -->\"",
";",
"$",
"replace_pairs",
"[",
"$",
"to",
"]",
"=",
"$",
"transition",
"->",
"newId",
";",
"$",
"post",
"->",
"post_content",
"=",
"\\",
"Pressbooks",
"\\",
"Utility",
"\\",
"shortcode_att_replace",
"(",
"$",
"post",
"->",
"post_content",
",",
"$",
"shortcode",
",",
"'id'",
",",
"$",
"transition",
"->",
"oldId",
",",
"$",
"to",
")",
";",
"}",
"}",
"if",
"(",
"!",
"empty",
"(",
"$",
"replace_pairs",
")",
")",
"{",
"$",
"post",
"->",
"post_content",
"=",
"strtr",
"(",
"$",
"post",
"->",
"post_content",
",",
"$",
"replace_pairs",
")",
";",
"wp_update_post",
"(",
"$",
"post",
")",
";",
"}",
"}",
";",
"// Glossary",
"foreach",
"(",
"$",
"this",
"->",
"postsWithGlossaryShortcodesToFix",
"as",
"$",
"post_id",
")",
"{",
"$",
"fix",
"(",
"$",
"post_id",
",",
"'glossary'",
",",
"\\",
"Pressbooks",
"\\",
"Shortcodes",
"\\",
"Glossary",
"\\",
"Glossary",
"::",
"SHORTCODE",
")",
";",
"}",
"// Attachments",
"foreach",
"(",
"$",
"this",
"->",
"postsWithAttachmentsShortcodesToFix",
"as",
"$",
"post_id",
")",
"{",
"$",
"fix",
"(",
"$",
"post_id",
",",
"'attachment'",
",",
"\\",
"Pressbooks",
"\\",
"Shortcodes",
"\\",
"Attributions",
"\\",
"Attachments",
"::",
"SHORTCODE",
")",
";",
"}",
"// TODO",
"// H5P not supported in WXR imports",
"}"
] | Fix shortcodes with references to internal IDs | [
"Fix",
"shortcodes",
"with",
"references",
"to",
"internal",
"IDs"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/import/wordpress/class-wxr.php#L761-L797 |
pressbooks/pressbooks | inc/modules/import/wordpress/class-wxr.php | Wxr.figureOutDataModel | protected function figureOutDataModel( $postmeta ) {
foreach ( $this->contributors->valid as $contributor_type ) {
if ( $this->searchForMetaValue( $contributor_type, $postmeta ) ) {
return 5;
};
}
foreach ( $this->contributors->deprecated as $contributor_type ) {
if ( $this->searchForMetaValue( $contributor_type, $postmeta ) ) {
return 4;
};
}
// We found nothing? May as well use most recent version then...
return 5;
} | php | protected function figureOutDataModel( $postmeta ) {
foreach ( $this->contributors->valid as $contributor_type ) {
if ( $this->searchForMetaValue( $contributor_type, $postmeta ) ) {
return 5;
};
}
foreach ( $this->contributors->deprecated as $contributor_type ) {
if ( $this->searchForMetaValue( $contributor_type, $postmeta ) ) {
return 4;
};
}
// We found nothing? May as well use most recent version then...
return 5;
} | [
"protected",
"function",
"figureOutDataModel",
"(",
"$",
"postmeta",
")",
"{",
"foreach",
"(",
"$",
"this",
"->",
"contributors",
"->",
"valid",
"as",
"$",
"contributor_type",
")",
"{",
"if",
"(",
"$",
"this",
"->",
"searchForMetaValue",
"(",
"$",
"contributor_type",
",",
"$",
"postmeta",
")",
")",
"{",
"return",
"5",
";",
"}",
";",
"}",
"foreach",
"(",
"$",
"this",
"->",
"contributors",
"->",
"deprecated",
"as",
"$",
"contributor_type",
")",
"{",
"if",
"(",
"$",
"this",
"->",
"searchForMetaValue",
"(",
"$",
"contributor_type",
",",
"$",
"postmeta",
")",
")",
"{",
"return",
"4",
";",
"}",
";",
"}",
"// We found nothing? May as well use most recent version then...",
"return",
"5",
";",
"}"
] | For backwards-compatibility, some PB5 field names are pluralized so that any third-party code that looks for the old fields will still be able to retrieve them.
That means both the old and new fields could still be in the XML. If we try to import both it causes buggy behaviour.
This function helps us pick either/or.
@param array $postmeta
@return int | [
"For",
"backwards",
"-",
"compatibility",
"some",
"PB5",
"field",
"names",
"are",
"pluralized",
"so",
"that",
"any",
"third",
"-",
"party",
"code",
"that",
"looks",
"for",
"the",
"old",
"fields",
"will",
"still",
"be",
"able",
"to",
"retrieve",
"them",
".",
"That",
"means",
"both",
"the",
"old",
"and",
"new",
"fields",
"could",
"still",
"be",
"in",
"the",
"XML",
".",
"If",
"we",
"try",
"to",
"import",
"both",
"it",
"causes",
"buggy",
"behaviour",
".",
"This",
"function",
"helps",
"us",
"pick",
"either",
"/",
"or",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/import/wordpress/class-wxr.php#L808-L824 |
pressbooks/pressbooks | inc/api/endpoints/controller/class-search.php | Search.register_routes | public function register_routes() {
register_rest_route(
$this->namespace, '/' . $this->rest_base, [
[
'methods' => \WP_REST_Server::READABLE,
'callback' => [ $this, 'get_items' ],
'permission_callback' => [ $this, 'get_items_permissions_check' ],
'args' => $this->get_collection_params(),
],
'schema' => [ $this, 'get_public_item_schema' ],
]
);
} | php | public function register_routes() {
register_rest_route(
$this->namespace, '/' . $this->rest_base, [
[
'methods' => \WP_REST_Server::READABLE,
'callback' => [ $this, 'get_items' ],
'permission_callback' => [ $this, 'get_items_permissions_check' ],
'args' => $this->get_collection_params(),
],
'schema' => [ $this, 'get_public_item_schema' ],
]
);
} | [
"public",
"function",
"register_routes",
"(",
")",
"{",
"register_rest_route",
"(",
"$",
"this",
"->",
"namespace",
",",
"'/'",
".",
"$",
"this",
"->",
"rest_base",
",",
"[",
"[",
"'methods'",
"=>",
"\\",
"WP_REST_Server",
"::",
"READABLE",
",",
"'callback'",
"=>",
"[",
"$",
"this",
",",
"'get_items'",
"]",
",",
"'permission_callback'",
"=>",
"[",
"$",
"this",
",",
"'get_items_permissions_check'",
"]",
",",
"'args'",
"=>",
"$",
"this",
"->",
"get_collection_params",
"(",
")",
",",
"]",
",",
"'schema'",
"=>",
"[",
"$",
"this",
",",
"'get_public_item_schema'",
"]",
",",
"]",
")",
";",
"}"
] | Registers routes for Books | [
"Registers",
"routes",
"for",
"Books"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/api/endpoints/controller/class-search.php#L18-L31 |
pressbooks/pressbooks | inc/api/endpoints/controller/class-search.php | Search.get_items | public function get_items( $request ) {
// Register missing routes
$this->registerRouteDependencies();
// Override search request
$search = $request->get_query_params();
unset( $search['per_page'], $search['next'] );
$request['search'] = ! empty( $search ) ? $search : [
null => null,
]; // Set some weird value that means abort
$response = rest_ensure_response( $this->searchBooks( $request ) );
unset( $request['search'] );
$this->addNextSearchLinks( $request, $response );
return $response;
} | php | public function get_items( $request ) {
// Register missing routes
$this->registerRouteDependencies();
// Override search request
$search = $request->get_query_params();
unset( $search['per_page'], $search['next'] );
$request['search'] = ! empty( $search ) ? $search : [
null => null,
]; // Set some weird value that means abort
$response = rest_ensure_response( $this->searchBooks( $request ) );
unset( $request['search'] );
$this->addNextSearchLinks( $request, $response );
return $response;
} | [
"public",
"function",
"get_items",
"(",
"$",
"request",
")",
"{",
"// Register missing routes",
"$",
"this",
"->",
"registerRouteDependencies",
"(",
")",
";",
"// Override search request",
"$",
"search",
"=",
"$",
"request",
"->",
"get_query_params",
"(",
")",
";",
"unset",
"(",
"$",
"search",
"[",
"'per_page'",
"]",
",",
"$",
"search",
"[",
"'next'",
"]",
")",
";",
"$",
"request",
"[",
"'search'",
"]",
"=",
"!",
"empty",
"(",
"$",
"search",
")",
"?",
"$",
"search",
":",
"[",
"null",
"=>",
"null",
",",
"]",
";",
"// Set some weird value that means abort",
"$",
"response",
"=",
"rest_ensure_response",
"(",
"$",
"this",
"->",
"searchBooks",
"(",
"$",
"request",
")",
")",
";",
"unset",
"(",
"$",
"request",
"[",
"'search'",
"]",
")",
";",
"$",
"this",
"->",
"addNextSearchLinks",
"(",
"$",
"request",
",",
"$",
"response",
")",
";",
"return",
"$",
"response",
";",
"}"
] | @param \WP_REST_Request $request
@return \WP_REST_Response | [
"@param",
"\\",
"WP_REST_Request",
"$request"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/api/endpoints/controller/class-search.php#L57-L75 |
pressbooks/pressbooks | inc/api/endpoints/controller/class-search.php | Search.find | public function find( $search ) {
if ( ! is_array( $search ) ) {
wp_die( 'LogicException: $search should be a [meta_key => val] array' );
}
if ( $search === [
null => null,
] ) {
return false; // Abort search
}
if ( $this->paramSearchMeta( $search ) !== false ) {
return true;
}
return false;
} | php | public function find( $search ) {
if ( ! is_array( $search ) ) {
wp_die( 'LogicException: $search should be a [meta_key => val] array' );
}
if ( $search === [
null => null,
] ) {
return false; // Abort search
}
if ( $this->paramSearchMeta( $search ) !== false ) {
return true;
}
return false;
} | [
"public",
"function",
"find",
"(",
"$",
"search",
")",
"{",
"if",
"(",
"!",
"is_array",
"(",
"$",
"search",
")",
")",
"{",
"wp_die",
"(",
"'LogicException: $search should be a [meta_key => val] array'",
")",
";",
"}",
"if",
"(",
"$",
"search",
"===",
"[",
"null",
"=>",
"null",
",",
"]",
")",
"{",
"return",
"false",
";",
"// Abort search",
"}",
"if",
"(",
"$",
"this",
"->",
"paramSearchMeta",
"(",
"$",
"search",
")",
"!==",
"false",
")",
"{",
"return",
"true",
";",
"}",
"return",
"false",
";",
"}"
] | Overridable find method for how to search a book
@param mixed $search
@return bool | [
"Overridable",
"find",
"method",
"for",
"how",
"to",
"search",
"a",
"book"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/api/endpoints/controller/class-search.php#L84-L101 |
pressbooks/pressbooks | inc/api/endpoints/controller/class-search.php | Search.paramSearchMeta | protected function paramSearchMeta( array $search ) {
// Book Metadata
$request_metadata = new \WP_REST_Request( 'GET', '/pressbooks/v2/metadata' );
$response_metadata = rest_do_request( $request_metadata );
$book_metadata = $response_metadata->get_data();
if ( $this->keyValueSearchInMeta( $search, $book_metadata ) ) {
return true;
}
// Chapter Metadata
$request_metadata = new \WP_REST_Request( 'GET', '/pressbooks/v2/toc' );
$response_metadata = rest_do_request( $request_metadata );
$toc = $response_metadata->get_data();
foreach ( $toc['front-matter'] as $fm ) {
if ( $this->keyValueSearchInMeta( $search, $fm['metadata'] ) ) {
return true;
}
}
foreach ( $toc['parts'] as $p ) {
foreach ( $p['chapters'] as $ch ) {
if ( $this->keyValueSearchInMeta( $search, $ch['metadata'] ) ) {
return true;
}
}
}
foreach ( $toc['back-matter'] as $bm ) {
if ( $this->keyValueSearchInMeta( $search, $bm['metadata'] ) ) {
return true;
}
}
return false;
} | php | protected function paramSearchMeta( array $search ) {
// Book Metadata
$request_metadata = new \WP_REST_Request( 'GET', '/pressbooks/v2/metadata' );
$response_metadata = rest_do_request( $request_metadata );
$book_metadata = $response_metadata->get_data();
if ( $this->keyValueSearchInMeta( $search, $book_metadata ) ) {
return true;
}
// Chapter Metadata
$request_metadata = new \WP_REST_Request( 'GET', '/pressbooks/v2/toc' );
$response_metadata = rest_do_request( $request_metadata );
$toc = $response_metadata->get_data();
foreach ( $toc['front-matter'] as $fm ) {
if ( $this->keyValueSearchInMeta( $search, $fm['metadata'] ) ) {
return true;
}
}
foreach ( $toc['parts'] as $p ) {
foreach ( $p['chapters'] as $ch ) {
if ( $this->keyValueSearchInMeta( $search, $ch['metadata'] ) ) {
return true;
}
}
}
foreach ( $toc['back-matter'] as $bm ) {
if ( $this->keyValueSearchInMeta( $search, $bm['metadata'] ) ) {
return true;
}
}
return false;
} | [
"protected",
"function",
"paramSearchMeta",
"(",
"array",
"$",
"search",
")",
"{",
"// Book Metadata",
"$",
"request_metadata",
"=",
"new",
"\\",
"WP_REST_Request",
"(",
"'GET'",
",",
"'/pressbooks/v2/metadata'",
")",
";",
"$",
"response_metadata",
"=",
"rest_do_request",
"(",
"$",
"request_metadata",
")",
";",
"$",
"book_metadata",
"=",
"$",
"response_metadata",
"->",
"get_data",
"(",
")",
";",
"if",
"(",
"$",
"this",
"->",
"keyValueSearchInMeta",
"(",
"$",
"search",
",",
"$",
"book_metadata",
")",
")",
"{",
"return",
"true",
";",
"}",
"// Chapter Metadata",
"$",
"request_metadata",
"=",
"new",
"\\",
"WP_REST_Request",
"(",
"'GET'",
",",
"'/pressbooks/v2/toc'",
")",
";",
"$",
"response_metadata",
"=",
"rest_do_request",
"(",
"$",
"request_metadata",
")",
";",
"$",
"toc",
"=",
"$",
"response_metadata",
"->",
"get_data",
"(",
")",
";",
"foreach",
"(",
"$",
"toc",
"[",
"'front-matter'",
"]",
"as",
"$",
"fm",
")",
"{",
"if",
"(",
"$",
"this",
"->",
"keyValueSearchInMeta",
"(",
"$",
"search",
",",
"$",
"fm",
"[",
"'metadata'",
"]",
")",
")",
"{",
"return",
"true",
";",
"}",
"}",
"foreach",
"(",
"$",
"toc",
"[",
"'parts'",
"]",
"as",
"$",
"p",
")",
"{",
"foreach",
"(",
"$",
"p",
"[",
"'chapters'",
"]",
"as",
"$",
"ch",
")",
"{",
"if",
"(",
"$",
"this",
"->",
"keyValueSearchInMeta",
"(",
"$",
"search",
",",
"$",
"ch",
"[",
"'metadata'",
"]",
")",
")",
"{",
"return",
"true",
";",
"}",
"}",
"}",
"foreach",
"(",
"$",
"toc",
"[",
"'back-matter'",
"]",
"as",
"$",
"bm",
")",
"{",
"if",
"(",
"$",
"this",
"->",
"keyValueSearchInMeta",
"(",
"$",
"search",
",",
"$",
"bm",
"[",
"'metadata'",
"]",
")",
")",
"{",
"return",
"true",
";",
"}",
"}",
"return",
"false",
";",
"}"
] | Parameter based search
@param array $search
@return bool | [
"Parameter",
"based",
"search"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/api/endpoints/controller/class-search.php#L110-L143 |
pressbooks/pressbooks | inc/api/endpoints/controller/class-search.php | Search.keyValueSearchInMeta | protected function keyValueSearchInMeta( array $search, array $metadata ) {
$found = [];
foreach ( $search as $search_key => $needle ) {
$found[ $search_key ] = false;
if ( isset( $metadata[ $search_key ] ) ) {
$haystack = is_array( $metadata[ $search_key ] ) ? $metadata[ $search_key ] : (array) $metadata[ $search_key ];
foreach ( $haystack as $hay ) {
if ( false !== strpos( $needle, ',' ) ) { // look for more than one search word
$needles = array_slice( explode( ',', $needle ), 0, 5 ); // prevent excessive requests
} else {
$needles = (array) $needle;
}
foreach ( $needles as $pin ) {
if ( stripos( $hay, trim( $pin ) ) !== false ) {
$found[ $search_key ] = true;
break 2;
}
}
}
}
}
foreach ( $found as $key => $val ) {
if ( $val === false ) {
return false;
}
}
return true;
} | php | protected function keyValueSearchInMeta( array $search, array $metadata ) {
$found = [];
foreach ( $search as $search_key => $needle ) {
$found[ $search_key ] = false;
if ( isset( $metadata[ $search_key ] ) ) {
$haystack = is_array( $metadata[ $search_key ] ) ? $metadata[ $search_key ] : (array) $metadata[ $search_key ];
foreach ( $haystack as $hay ) {
if ( false !== strpos( $needle, ',' ) ) { // look for more than one search word
$needles = array_slice( explode( ',', $needle ), 0, 5 ); // prevent excessive requests
} else {
$needles = (array) $needle;
}
foreach ( $needles as $pin ) {
if ( stripos( $hay, trim( $pin ) ) !== false ) {
$found[ $search_key ] = true;
break 2;
}
}
}
}
}
foreach ( $found as $key => $val ) {
if ( $val === false ) {
return false;
}
}
return true;
} | [
"protected",
"function",
"keyValueSearchInMeta",
"(",
"array",
"$",
"search",
",",
"array",
"$",
"metadata",
")",
"{",
"$",
"found",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"search",
"as",
"$",
"search_key",
"=>",
"$",
"needle",
")",
"{",
"$",
"found",
"[",
"$",
"search_key",
"]",
"=",
"false",
";",
"if",
"(",
"isset",
"(",
"$",
"metadata",
"[",
"$",
"search_key",
"]",
")",
")",
"{",
"$",
"haystack",
"=",
"is_array",
"(",
"$",
"metadata",
"[",
"$",
"search_key",
"]",
")",
"?",
"$",
"metadata",
"[",
"$",
"search_key",
"]",
":",
"(",
"array",
")",
"$",
"metadata",
"[",
"$",
"search_key",
"]",
";",
"foreach",
"(",
"$",
"haystack",
"as",
"$",
"hay",
")",
"{",
"if",
"(",
"false",
"!==",
"strpos",
"(",
"$",
"needle",
",",
"','",
")",
")",
"{",
"// look for more than one search word",
"$",
"needles",
"=",
"array_slice",
"(",
"explode",
"(",
"','",
",",
"$",
"needle",
")",
",",
"0",
",",
"5",
")",
";",
"// prevent excessive requests",
"}",
"else",
"{",
"$",
"needles",
"=",
"(",
"array",
")",
"$",
"needle",
";",
"}",
"foreach",
"(",
"$",
"needles",
"as",
"$",
"pin",
")",
"{",
"if",
"(",
"stripos",
"(",
"$",
"hay",
",",
"trim",
"(",
"$",
"pin",
")",
")",
"!==",
"false",
")",
"{",
"$",
"found",
"[",
"$",
"search_key",
"]",
"=",
"true",
";",
"break",
"2",
";",
"}",
"}",
"}",
"}",
"}",
"foreach",
"(",
"$",
"found",
"as",
"$",
"key",
"=>",
"$",
"val",
")",
"{",
"if",
"(",
"$",
"val",
"===",
"false",
")",
"{",
"return",
"false",
";",
"}",
"}",
"return",
"true",
";",
"}"
] | subjects=biology,technology&keywords=education
returns all books in a collection with either subject 'biology' OR 'technology' AND the keyword 'education',
(where 'subjects' and 'keywords' are keys in $metadata )
@param array $search
@param array $metadata
@return bool | [
"subjects",
"=",
"biology",
"technology&keywords",
"=",
"education",
"returns",
"all",
"books",
"in",
"a",
"collection",
"with",
"either",
"subject",
"biology",
"OR",
"technology",
"AND",
"the",
"keyword",
"education",
"(",
"where",
"subjects",
"and",
"keywords",
"are",
"keys",
"in",
"$metadata",
")"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/api/endpoints/controller/class-search.php#L155-L185 |
pressbooks/pressbooks | symbionts/pressbooks-latex/pb-latex-admin.php | PBLatexAdmin.addOptions | function addOptions() {
if ( is_array( $this->options ) )
extract( $this->options, EXTR_SKIP );
global $themecolors;
if ( empty($bg) )
$bg = isset( $themecolors['bg'] ) ? $themecolors['bg'] : 'transparent';
if ( empty($fg) )
$fg = isset( $themecolors['text'] ) ? $themecolors['text'] : '000000';
if ( empty( $method ) )
$method = 'Automattic_Latex_WPCOM';
if ( empty( $css ) )
$css = 'img.latex { vertical-align: middle; border: none; background: none; }';
if ( empty( $wrapper ) )
$wrapper = false;
$this->options = compact( 'bg', 'fg', 'method', 'css', 'wrapper' );
update_option( 'pb_latex', $this->options );
} | php | function addOptions() {
if ( is_array( $this->options ) )
extract( $this->options, EXTR_SKIP );
global $themecolors;
if ( empty($bg) )
$bg = isset( $themecolors['bg'] ) ? $themecolors['bg'] : 'transparent';
if ( empty($fg) )
$fg = isset( $themecolors['text'] ) ? $themecolors['text'] : '000000';
if ( empty( $method ) )
$method = 'Automattic_Latex_WPCOM';
if ( empty( $css ) )
$css = 'img.latex { vertical-align: middle; border: none; background: none; }';
if ( empty( $wrapper ) )
$wrapper = false;
$this->options = compact( 'bg', 'fg', 'method', 'css', 'wrapper' );
update_option( 'pb_latex', $this->options );
} | [
"function",
"addOptions",
"(",
")",
"{",
"if",
"(",
"is_array",
"(",
"$",
"this",
"->",
"options",
")",
")",
"extract",
"(",
"$",
"this",
"->",
"options",
",",
"EXTR_SKIP",
")",
";",
"global",
"$",
"themecolors",
";",
"if",
"(",
"empty",
"(",
"$",
"bg",
")",
")",
"$",
"bg",
"=",
"isset",
"(",
"$",
"themecolors",
"[",
"'bg'",
"]",
")",
"?",
"$",
"themecolors",
"[",
"'bg'",
"]",
":",
"'transparent'",
";",
"if",
"(",
"empty",
"(",
"$",
"fg",
")",
")",
"$",
"fg",
"=",
"isset",
"(",
"$",
"themecolors",
"[",
"'text'",
"]",
")",
"?",
"$",
"themecolors",
"[",
"'text'",
"]",
":",
"'000000'",
";",
"if",
"(",
"empty",
"(",
"$",
"method",
")",
")",
"$",
"method",
"=",
"'Automattic_Latex_WPCOM'",
";",
"if",
"(",
"empty",
"(",
"$",
"css",
")",
")",
"$",
"css",
"=",
"'img.latex { vertical-align: middle; border: none; background: none; }'",
";",
"if",
"(",
"empty",
"(",
"$",
"wrapper",
")",
")",
"$",
"wrapper",
"=",
"false",
";",
"$",
"this",
"->",
"options",
"=",
"compact",
"(",
"'bg'",
",",
"'fg'",
",",
"'method'",
",",
"'css'",
",",
"'wrapper'",
")",
";",
"update_option",
"(",
"'pb_latex'",
",",
"$",
"this",
"->",
"options",
")",
";",
"}"
] | Sets up default options | [
"Sets",
"up",
"default",
"options"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/symbionts/pressbooks-latex/pb-latex-admin.php#L298-L320 |
pressbooks/pressbooks | inc/covergenerator/class-sku.php | Sku.invocation | public function invocation( $sku, $not_used_1, $not_used_2, $text_font, $text_size ) {
$ps[] = "50 50 moveto ({$sku}) (includetext textfont={$text_font} textsize={$text_size} height=0.5)";
$ps[] = '/code128 /uk.co.terryburton.bwipp findresource exec';
return implode( "\n", $ps ) . "\n";
} | php | public function invocation( $sku, $not_used_1, $not_used_2, $text_font, $text_size ) {
$ps[] = "50 50 moveto ({$sku}) (includetext textfont={$text_font} textsize={$text_size} height=0.5)";
$ps[] = '/code128 /uk.co.terryburton.bwipp findresource exec';
return implode( "\n", $ps ) . "\n";
} | [
"public",
"function",
"invocation",
"(",
"$",
"sku",
",",
"$",
"not_used_1",
",",
"$",
"not_used_2",
",",
"$",
"text_font",
",",
"$",
"text_size",
")",
"{",
"$",
"ps",
"[",
"]",
"=",
"\"50 50 moveto ({$sku}) (includetext textfont={$text_font} textsize={$text_size} height=0.5)\"",
";",
"$",
"ps",
"[",
"]",
"=",
"'/code128 /uk.co.terryburton.bwipp findresource exec'",
";",
"return",
"implode",
"(",
"\"\\n\"",
",",
"$",
"ps",
")",
".",
"\"\\n\"",
";",
"}"
] | SKU Invocation Code.
@see https://github.com/bwipp/postscriptbarcode/wiki/Code-128
@see https://github.com/bwipp/postscriptbarcode/wiki/Symbol-Dimensions
@see https://github.com/bwipp/postscriptbarcode/wiki/Text-Properties
@param string $sku
@param string $not_used_1
@param float $not_used_2
@param string $text_font
@param float $text_size
@return string | [
"SKU",
"Invocation",
"Code",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/covergenerator/class-sku.php#L105-L111 |
pressbooks/pressbooks | inc/covergenerator/class-sku.php | Sku.compile | public function compile( $path_to_ps ) {
if ( empty( $this->sku ) ) {
throw new \LogicException( '$this->sku is not set' );
}
$sku = \Pressbooks\Utility\get_contents( PB_PLUGIN_DIR . 'symbionts/postscriptbarcode/code128.ps' );
$invocation = $this->invocation(
$this->sku,
null,
null,
$this->textFont,
$this->textSize
);
// @codingStandardsIgnoreStart
file_put_contents( $path_to_ps, $sku );
file_put_contents( $path_to_ps, $invocation, FILE_APPEND | LOCK_EX );
file_put_contents( $path_to_ps, ( "\n" . 'showpage' ), FILE_APPEND | LOCK_EX );
// @codingStandardsIgnoreEnd
} | php | public function compile( $path_to_ps ) {
if ( empty( $this->sku ) ) {
throw new \LogicException( '$this->sku is not set' );
}
$sku = \Pressbooks\Utility\get_contents( PB_PLUGIN_DIR . 'symbionts/postscriptbarcode/code128.ps' );
$invocation = $this->invocation(
$this->sku,
null,
null,
$this->textFont,
$this->textSize
);
// @codingStandardsIgnoreStart
file_put_contents( $path_to_ps, $sku );
file_put_contents( $path_to_ps, $invocation, FILE_APPEND | LOCK_EX );
file_put_contents( $path_to_ps, ( "\n" . 'showpage' ), FILE_APPEND | LOCK_EX );
// @codingStandardsIgnoreEnd
} | [
"public",
"function",
"compile",
"(",
"$",
"path_to_ps",
")",
"{",
"if",
"(",
"empty",
"(",
"$",
"this",
"->",
"sku",
")",
")",
"{",
"throw",
"new",
"\\",
"LogicException",
"(",
"'$this->sku is not set'",
")",
";",
"}",
"$",
"sku",
"=",
"\\",
"Pressbooks",
"\\",
"Utility",
"\\",
"get_contents",
"(",
"PB_PLUGIN_DIR",
".",
"'symbionts/postscriptbarcode/code128.ps'",
")",
";",
"$",
"invocation",
"=",
"$",
"this",
"->",
"invocation",
"(",
"$",
"this",
"->",
"sku",
",",
"null",
",",
"null",
",",
"$",
"this",
"->",
"textFont",
",",
"$",
"this",
"->",
"textSize",
")",
";",
"// @codingStandardsIgnoreStart",
"file_put_contents",
"(",
"$",
"path_to_ps",
",",
"$",
"sku",
")",
";",
"file_put_contents",
"(",
"$",
"path_to_ps",
",",
"$",
"invocation",
",",
"FILE_APPEND",
"|",
"LOCK_EX",
")",
";",
"file_put_contents",
"(",
"$",
"path_to_ps",
",",
"(",
"\"\\n\"",
".",
"'showpage'",
")",
",",
"FILE_APPEND",
"|",
"LOCK_EX",
")",
";",
"// @codingStandardsIgnoreEnd",
"}"
] | Compile a SKU Postscript file
@param string $path_to_ps
@throws \LogicException | [
"Compile",
"a",
"SKU",
"Postscript",
"file"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/covergenerator/class-sku.php#L121-L142 |
pressbooks/pressbooks | inc/admin/class-network-managers-list-table.php | Network_Managers_List_Table.column_user_login | function column_user_login( $item ) {
// Build row actions
$current_user = wp_get_current_user();
if ( absint( $item['ID'] ) !== absint( $current_user->ID ) ) { // Don't let users restrict themselves
if ( ! empty( $item['restricted'] ) ) {
$actions = [
'unrestrict' => '<a data-restrict="0" data-restrict-text="' . __( 'Restrict Access', 'pressbooks' ) . '" data-unrestrict-text="' . __( 'Unrestrict Access', 'pressbooks' ) . '">' . __( 'Unrestrict Access', 'pressbooks' ) . '</a>',
];
} else {
$actions = [
'restrict' => '<a data-restrict="1" data-restrict-text="' . __( 'Restrict Access', 'pressbooks' ) . '" data-unrestrict-text="' . __( 'Unrestrict Access', 'pressbooks' ) . '">' . __( 'Restrict Access', 'pressbooks' ) . '</a>',
];
}
} else {
$actions = [];
}
// Return the title contents
return sprintf(
'%1$s <span class="user_login">%2$s</span> %3$s',
get_avatar( $item['ID'], 32 ),
$item['user_login'],
$this->row_actions( $actions )
);
} | php | function column_user_login( $item ) {
// Build row actions
$current_user = wp_get_current_user();
if ( absint( $item['ID'] ) !== absint( $current_user->ID ) ) { // Don't let users restrict themselves
if ( ! empty( $item['restricted'] ) ) {
$actions = [
'unrestrict' => '<a data-restrict="0" data-restrict-text="' . __( 'Restrict Access', 'pressbooks' ) . '" data-unrestrict-text="' . __( 'Unrestrict Access', 'pressbooks' ) . '">' . __( 'Unrestrict Access', 'pressbooks' ) . '</a>',
];
} else {
$actions = [
'restrict' => '<a data-restrict="1" data-restrict-text="' . __( 'Restrict Access', 'pressbooks' ) . '" data-unrestrict-text="' . __( 'Unrestrict Access', 'pressbooks' ) . '">' . __( 'Restrict Access', 'pressbooks' ) . '</a>',
];
}
} else {
$actions = [];
}
// Return the title contents
return sprintf(
'%1$s <span class="user_login">%2$s</span> %3$s',
get_avatar( $item['ID'], 32 ),
$item['user_login'],
$this->row_actions( $actions )
);
} | [
"function",
"column_user_login",
"(",
"$",
"item",
")",
"{",
"// Build row actions",
"$",
"current_user",
"=",
"wp_get_current_user",
"(",
")",
";",
"if",
"(",
"absint",
"(",
"$",
"item",
"[",
"'ID'",
"]",
")",
"!==",
"absint",
"(",
"$",
"current_user",
"->",
"ID",
")",
")",
"{",
"// Don't let users restrict themselves",
"if",
"(",
"!",
"empty",
"(",
"$",
"item",
"[",
"'restricted'",
"]",
")",
")",
"{",
"$",
"actions",
"=",
"[",
"'unrestrict'",
"=>",
"'<a data-restrict=\"0\" data-restrict-text=\"'",
".",
"__",
"(",
"'Restrict Access'",
",",
"'pressbooks'",
")",
".",
"'\" data-unrestrict-text=\"'",
".",
"__",
"(",
"'Unrestrict Access'",
",",
"'pressbooks'",
")",
".",
"'\">'",
".",
"__",
"(",
"'Unrestrict Access'",
",",
"'pressbooks'",
")",
".",
"'</a>'",
",",
"]",
";",
"}",
"else",
"{",
"$",
"actions",
"=",
"[",
"'restrict'",
"=>",
"'<a data-restrict=\"1\" data-restrict-text=\"'",
".",
"__",
"(",
"'Restrict Access'",
",",
"'pressbooks'",
")",
".",
"'\" data-unrestrict-text=\"'",
".",
"__",
"(",
"'Unrestrict Access'",
",",
"'pressbooks'",
")",
".",
"'\">'",
".",
"__",
"(",
"'Restrict Access'",
",",
"'pressbooks'",
")",
".",
"'</a>'",
",",
"]",
";",
"}",
"}",
"else",
"{",
"$",
"actions",
"=",
"[",
"]",
";",
"}",
"// Return the title contents",
"return",
"sprintf",
"(",
"'%1$s <span class=\"user_login\">%2$s</span> %3$s'",
",",
"get_avatar",
"(",
"$",
"item",
"[",
"'ID'",
"]",
",",
"32",
")",
",",
"$",
"item",
"[",
"'user_login'",
"]",
",",
"$",
"this",
"->",
"row_actions",
"(",
"$",
"actions",
")",
")",
";",
"}"
] | ************************************************************************
Custom column method for the user_login column.
@see WP_List_Table::::single_row_columns()
@param array $item A singular item (one full row's worth of data)
@return string HTML to be placed inside the column <td>
************************************************************************ | [
"************************************************************************",
"Custom",
"column",
"method",
"for",
"the",
"user_login",
"column",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/admin/class-network-managers-list-table.php#L68-L94 |
pressbooks/pressbooks | inc/admin/class-network-managers-list-table.php | Network_Managers_List_Table.prepare_items | function prepare_items() {
/**
* Define column headers.
*/
$columns = $this->get_columns();
$hidden = [];
$sortable = $this->get_sortable_columns();
/**
* Build column header array.
*/
$this->_column_headers = [ $columns, $hidden, $sortable ];
/**
* Check for sorting input and sort the data in our array accordingly.
*/
$network_admins = get_site_option( 'site_admins' );
$network_managers = get_network_option( null, 'pressbooks_network_managers', [] );
$tmp = [];
foreach ( $network_admins as $id => $username ) {
$user = get_user_by( 'login', $username );
$user = $user->data;
$is_restricted = ( in_array( absint( $user->ID ), $network_managers, true ) ) ? true : false; // Determine admin's restricted status
$tmp[ $id ] = [
'ID' => $user->ID,
'user_login' => $user->user_login,
'display_name' => $user->display_name,
'user_email' => $user->user_email,
'restricted' => $is_restricted,
];
}
$network_admins = $tmp;
usort(
$network_admins, function ( $a, $b ) {
$orderby = ( ! empty( $_REQUEST['orderby'] ) ) ? $_REQUEST['orderby'] : 'user_login';
$order = ( ! empty( $_REQUEST['order'] ) ) ? $_REQUEST['order'] : 'asc';
$result = strcmp( $a[ $orderby ], $b[ $orderby ] );
return ( 'asc' === $order ) ? $result : -$result;
}
);
$this->items = $network_admins; // Return our data
} | php | function prepare_items() {
/**
* Define column headers.
*/
$columns = $this->get_columns();
$hidden = [];
$sortable = $this->get_sortable_columns();
/**
* Build column header array.
*/
$this->_column_headers = [ $columns, $hidden, $sortable ];
/**
* Check for sorting input and sort the data in our array accordingly.
*/
$network_admins = get_site_option( 'site_admins' );
$network_managers = get_network_option( null, 'pressbooks_network_managers', [] );
$tmp = [];
foreach ( $network_admins as $id => $username ) {
$user = get_user_by( 'login', $username );
$user = $user->data;
$is_restricted = ( in_array( absint( $user->ID ), $network_managers, true ) ) ? true : false; // Determine admin's restricted status
$tmp[ $id ] = [
'ID' => $user->ID,
'user_login' => $user->user_login,
'display_name' => $user->display_name,
'user_email' => $user->user_email,
'restricted' => $is_restricted,
];
}
$network_admins = $tmp;
usort(
$network_admins, function ( $a, $b ) {
$orderby = ( ! empty( $_REQUEST['orderby'] ) ) ? $_REQUEST['orderby'] : 'user_login';
$order = ( ! empty( $_REQUEST['order'] ) ) ? $_REQUEST['order'] : 'asc';
$result = strcmp( $a[ $orderby ], $b[ $orderby ] );
return ( 'asc' === $order ) ? $result : -$result;
}
);
$this->items = $network_admins; // Return our data
} | [
"function",
"prepare_items",
"(",
")",
"{",
"/**\n\t\t * Define column headers.\n\t\t */",
"$",
"columns",
"=",
"$",
"this",
"->",
"get_columns",
"(",
")",
";",
"$",
"hidden",
"=",
"[",
"]",
";",
"$",
"sortable",
"=",
"$",
"this",
"->",
"get_sortable_columns",
"(",
")",
";",
"/**\n\t\t * Build column header array.\n\t\t */",
"$",
"this",
"->",
"_column_headers",
"=",
"[",
"$",
"columns",
",",
"$",
"hidden",
",",
"$",
"sortable",
"]",
";",
"/**\n\t\t * Check for sorting input and sort the data in our array accordingly.\n\t\t */",
"$",
"network_admins",
"=",
"get_site_option",
"(",
"'site_admins'",
")",
";",
"$",
"network_managers",
"=",
"get_network_option",
"(",
"null",
",",
"'pressbooks_network_managers'",
",",
"[",
"]",
")",
";",
"$",
"tmp",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"network_admins",
"as",
"$",
"id",
"=>",
"$",
"username",
")",
"{",
"$",
"user",
"=",
"get_user_by",
"(",
"'login'",
",",
"$",
"username",
")",
";",
"$",
"user",
"=",
"$",
"user",
"->",
"data",
";",
"$",
"is_restricted",
"=",
"(",
"in_array",
"(",
"absint",
"(",
"$",
"user",
"->",
"ID",
")",
",",
"$",
"network_managers",
",",
"true",
")",
")",
"?",
"true",
":",
"false",
";",
"// Determine admin's restricted status",
"$",
"tmp",
"[",
"$",
"id",
"]",
"=",
"[",
"'ID'",
"=>",
"$",
"user",
"->",
"ID",
",",
"'user_login'",
"=>",
"$",
"user",
"->",
"user_login",
",",
"'display_name'",
"=>",
"$",
"user",
"->",
"display_name",
",",
"'user_email'",
"=>",
"$",
"user",
"->",
"user_email",
",",
"'restricted'",
"=>",
"$",
"is_restricted",
",",
"]",
";",
"}",
"$",
"network_admins",
"=",
"$",
"tmp",
";",
"usort",
"(",
"$",
"network_admins",
",",
"function",
"(",
"$",
"a",
",",
"$",
"b",
")",
"{",
"$",
"orderby",
"=",
"(",
"!",
"empty",
"(",
"$",
"_REQUEST",
"[",
"'orderby'",
"]",
")",
")",
"?",
"$",
"_REQUEST",
"[",
"'orderby'",
"]",
":",
"'user_login'",
";",
"$",
"order",
"=",
"(",
"!",
"empty",
"(",
"$",
"_REQUEST",
"[",
"'order'",
"]",
")",
")",
"?",
"$",
"_REQUEST",
"[",
"'order'",
"]",
":",
"'asc'",
";",
"$",
"result",
"=",
"strcmp",
"(",
"$",
"a",
"[",
"$",
"orderby",
"]",
",",
"$",
"b",
"[",
"$",
"orderby",
"]",
")",
";",
"return",
"(",
"'asc'",
"===",
"$",
"order",
")",
"?",
"$",
"result",
":",
"-",
"$",
"result",
";",
"}",
")",
";",
"$",
"this",
"->",
"items",
"=",
"$",
"network_admins",
";",
"// Return our data",
"}"
] | ************************************************************************
Prepare data for display
@global \wpdb $wpdb
@uses $this->_column_headers
@uses $this->items
@uses $this->get_columns()
@uses $this->get_sortable_columns()
@uses $this->get_pagenum()
@uses $this->set_pagination_args()
************************************************************************ | [
"************************************************************************",
"Prepare",
"data",
"for",
"display"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/admin/class-network-managers-list-table.php#L153-L197 |
pressbooks/pressbooks | inc/theme/class-lock.php | Lock.toggleThemeLock | public function toggleThemeLock( $old_value, $value, $option ) {
if ( isset( $value['theme_lock'] ) && 1 === absint( $value['theme_lock'] ) ) {
return $this->lockTheme();
} elseif ( 1 === absint( $old_value['theme_lock'] ) && empty( $value['theme_lock'] ) ) {
return $this->unlockTheme();
}
return false;
} | php | public function toggleThemeLock( $old_value, $value, $option ) {
if ( isset( $value['theme_lock'] ) && 1 === absint( $value['theme_lock'] ) ) {
return $this->lockTheme();
} elseif ( 1 === absint( $old_value['theme_lock'] ) && empty( $value['theme_lock'] ) ) {
return $this->unlockTheme();
}
return false;
} | [
"public",
"function",
"toggleThemeLock",
"(",
"$",
"old_value",
",",
"$",
"value",
",",
"$",
"option",
")",
"{",
"if",
"(",
"isset",
"(",
"$",
"value",
"[",
"'theme_lock'",
"]",
")",
"&&",
"1",
"===",
"absint",
"(",
"$",
"value",
"[",
"'theme_lock'",
"]",
")",
")",
"{",
"return",
"$",
"this",
"->",
"lockTheme",
"(",
")",
";",
"}",
"elseif",
"(",
"1",
"===",
"absint",
"(",
"$",
"old_value",
"[",
"'theme_lock'",
"]",
")",
"&&",
"empty",
"(",
"$",
"value",
"[",
"'theme_lock'",
"]",
")",
")",
"{",
"return",
"$",
"this",
"->",
"unlockTheme",
"(",
")",
";",
"}",
"return",
"false",
";",
"}"
] | @param array $old_value
@param array $value
@param $option
@return mixed | [
"@param",
"array",
"$old_value",
"@param",
"array",
"$value",
"@param",
"$option"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/theme/class-lock.php#L76-L83 |
pressbooks/pressbooks | inc/theme/class-lock.php | Lock.lockTheme | public function lockTheme() {
if ( true === $this->copyAssets() ) {
$time = time();
$data = $this->generateLock( $time );
$_SESSION['pb_notices'][] = sprintf(
'<strong>%s</strong>',
sprintf(
__( 'Your book’s theme, %1$s, has been locked in its current state as of %2$s at %3$s.', 'pressbooks' ),
$data['name'],
strftime( '%x', $data['timestamp'] ),
strftime( '%X', $data['timestamp'] )
)
);
return $data;
} else {
$option = get_option( 'pressbooks_export_options' );
unset( $option['theme_lock'] );
update_option( 'pressbooks_export_options', $option );
$_SESSION['pb_errors'][] = sprintf(
'<strong>%s</strong>',
__( 'Your book’s theme could not be locked. Please ensure that you have write access to the uploads directory.', 'pressbooks' )
);
}
return false;
} | php | public function lockTheme() {
if ( true === $this->copyAssets() ) {
$time = time();
$data = $this->generateLock( $time );
$_SESSION['pb_notices'][] = sprintf(
'<strong>%s</strong>',
sprintf(
__( 'Your book’s theme, %1$s, has been locked in its current state as of %2$s at %3$s.', 'pressbooks' ),
$data['name'],
strftime( '%x', $data['timestamp'] ),
strftime( '%X', $data['timestamp'] )
)
);
return $data;
} else {
$option = get_option( 'pressbooks_export_options' );
unset( $option['theme_lock'] );
update_option( 'pressbooks_export_options', $option );
$_SESSION['pb_errors'][] = sprintf(
'<strong>%s</strong>',
__( 'Your book’s theme could not be locked. Please ensure that you have write access to the uploads directory.', 'pressbooks' )
);
}
return false;
} | [
"public",
"function",
"lockTheme",
"(",
")",
"{",
"if",
"(",
"true",
"===",
"$",
"this",
"->",
"copyAssets",
"(",
")",
")",
"{",
"$",
"time",
"=",
"time",
"(",
")",
";",
"$",
"data",
"=",
"$",
"this",
"->",
"generateLock",
"(",
"$",
"time",
")",
";",
"$",
"_SESSION",
"[",
"'pb_notices'",
"]",
"[",
"]",
"=",
"sprintf",
"(",
"'<strong>%s</strong>'",
",",
"sprintf",
"(",
"__",
"(",
"'Your book’s theme, %1$s, has been locked in its current state as of %2$s at %3$s.'",
",",
"'pressbooks'",
")",
",",
"$",
"data",
"[",
"'name'",
"]",
",",
"strftime",
"(",
"'%x'",
",",
"$",
"data",
"[",
"'timestamp'",
"]",
")",
",",
"strftime",
"(",
"'%X'",
",",
"$",
"data",
"[",
"'timestamp'",
"]",
")",
")",
")",
";",
"return",
"$",
"data",
";",
"}",
"else",
"{",
"$",
"option",
"=",
"get_option",
"(",
"'pressbooks_export_options'",
")",
";",
"unset",
"(",
"$",
"option",
"[",
"'theme_lock'",
"]",
")",
";",
"update_option",
"(",
"'pressbooks_export_options'",
",",
"$",
"option",
")",
";",
"$",
"_SESSION",
"[",
"'pb_errors'",
"]",
"[",
"]",
"=",
"sprintf",
"(",
"'<strong>%s</strong>'",
",",
"__",
"(",
"'Your book’s theme could not be locked. Please ensure that you have write access to the uploads directory.'",
",",
"'pressbooks'",
")",
")",
";",
"}",
"return",
"false",
";",
"}"
] | Lock the current theme by copying assets to the lock directory and generating a timestamped lockfile.
@return mixed | [
"Lock",
"the",
"current",
"theme",
"by",
"copying",
"assets",
"to",
"the",
"lock",
"directory",
"and",
"generating",
"a",
"timestamped",
"lockfile",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/theme/class-lock.php#L90-L114 |
pressbooks/pressbooks | inc/theme/class-lock.php | Lock.generateLock | public function generateLock( $time ) {
$theme = wp_get_theme();
global $_wp_theme_features;
$theme_features = is_array( $_wp_theme_features ) ? array_keys( $_wp_theme_features ) : [];
$data = [
'stylesheet' => get_stylesheet(),
'name' => $theme->get( 'Name' ),
'version' => $theme->get( 'Version' ),
'timestamp' => $time,
'features' => $theme_features,
];
$json = wp_json_encode( $data );
$lockfile = $this->getLockDir() . '/lock.json';
\Pressbooks\Utility\put_contents( $lockfile, $json );
return $data;
} | php | public function generateLock( $time ) {
$theme = wp_get_theme();
global $_wp_theme_features;
$theme_features = is_array( $_wp_theme_features ) ? array_keys( $_wp_theme_features ) : [];
$data = [
'stylesheet' => get_stylesheet(),
'name' => $theme->get( 'Name' ),
'version' => $theme->get( 'Version' ),
'timestamp' => $time,
'features' => $theme_features,
];
$json = wp_json_encode( $data );
$lockfile = $this->getLockDir() . '/lock.json';
\Pressbooks\Utility\put_contents( $lockfile, $json );
return $data;
} | [
"public",
"function",
"generateLock",
"(",
"$",
"time",
")",
"{",
"$",
"theme",
"=",
"wp_get_theme",
"(",
")",
";",
"global",
"$",
"_wp_theme_features",
";",
"$",
"theme_features",
"=",
"is_array",
"(",
"$",
"_wp_theme_features",
")",
"?",
"array_keys",
"(",
"$",
"_wp_theme_features",
")",
":",
"[",
"]",
";",
"$",
"data",
"=",
"[",
"'stylesheet'",
"=>",
"get_stylesheet",
"(",
")",
",",
"'name'",
"=>",
"$",
"theme",
"->",
"get",
"(",
"'Name'",
")",
",",
"'version'",
"=>",
"$",
"theme",
"->",
"get",
"(",
"'Version'",
")",
",",
"'timestamp'",
"=>",
"$",
"time",
",",
"'features'",
"=>",
"$",
"theme_features",
",",
"]",
";",
"$",
"json",
"=",
"wp_json_encode",
"(",
"$",
"data",
")",
";",
"$",
"lockfile",
"=",
"$",
"this",
"->",
"getLockDir",
"(",
")",
".",
"'/lock.json'",
";",
"\\",
"Pressbooks",
"\\",
"Utility",
"\\",
"put_contents",
"(",
"$",
"lockfile",
",",
"$",
"json",
")",
";",
"return",
"$",
"data",
";",
"}"
] | @param $time
@return array | [
"@param",
"$time"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/theme/class-lock.php#L179-L196 |
pressbooks/pressbooks | inc/theme/class-lock.php | Lock.isLocked | public function isLocked() {
$options = get_option( 'pressbooks_export_options' );
if ( realpath( $this->getLockDir( false ) . '/lock.json' ) && isset( $options['theme_lock'] ) && 1 === absint( $options['theme_lock'] ) ) {
return true;
}
return false;
} | php | public function isLocked() {
$options = get_option( 'pressbooks_export_options' );
if ( realpath( $this->getLockDir( false ) . '/lock.json' ) && isset( $options['theme_lock'] ) && 1 === absint( $options['theme_lock'] ) ) {
return true;
}
return false;
} | [
"public",
"function",
"isLocked",
"(",
")",
"{",
"$",
"options",
"=",
"get_option",
"(",
"'pressbooks_export_options'",
")",
";",
"if",
"(",
"realpath",
"(",
"$",
"this",
"->",
"getLockDir",
"(",
"false",
")",
".",
"'/lock.json'",
")",
"&&",
"isset",
"(",
"$",
"options",
"[",
"'theme_lock'",
"]",
")",
"&&",
"1",
"===",
"absint",
"(",
"$",
"options",
"[",
"'theme_lock'",
"]",
")",
")",
"{",
"return",
"true",
";",
"}",
"return",
"false",
";",
"}"
] | Check for a lockfile.
@return bool | [
"Check",
"for",
"a",
"lockfile",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/theme/class-lock.php#L214-L220 |
pressbooks/pressbooks | inc/theme/class-lock.php | Lock.getLockData | public function getLockData() {
$json = \Pressbooks\Utility\get_contents( $this->getLockDir( false ) . '/lock.json' );
$output = json_decode( $json, true );
return $output;
} | php | public function getLockData() {
$json = \Pressbooks\Utility\get_contents( $this->getLockDir( false ) . '/lock.json' );
$output = json_decode( $json, true );
return $output;
} | [
"public",
"function",
"getLockData",
"(",
")",
"{",
"$",
"json",
"=",
"\\",
"Pressbooks",
"\\",
"Utility",
"\\",
"get_contents",
"(",
"$",
"this",
"->",
"getLockDir",
"(",
"false",
")",
".",
"'/lock.json'",
")",
";",
"$",
"output",
"=",
"json_decode",
"(",
"$",
"json",
",",
"true",
")",
";",
"return",
"$",
"output",
";",
"}"
] | Load data from the lockfile.
@return array | [
"Load",
"data",
"from",
"the",
"lockfile",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/theme/class-lock.php#L227-L231 |
pressbooks/pressbooks | inc/theme/class-lock.php | Lock.globalComponentsPath | public function globalComponentsPath( $path ) {
$dir = $this->getLockDir( false ) . '/global-components/';
if ( file_exists( $dir ) ) {
return $dir;
} else {
return $path;
}
} | php | public function globalComponentsPath( $path ) {
$dir = $this->getLockDir( false ) . '/global-components/';
if ( file_exists( $dir ) ) {
return $dir;
} else {
return $path;
}
} | [
"public",
"function",
"globalComponentsPath",
"(",
"$",
"path",
")",
"{",
"$",
"dir",
"=",
"$",
"this",
"->",
"getLockDir",
"(",
"false",
")",
".",
"'/global-components/'",
";",
"if",
"(",
"file_exists",
"(",
"$",
"dir",
")",
")",
"{",
"return",
"$",
"dir",
";",
"}",
"else",
"{",
"return",
"$",
"path",
";",
"}",
"}"
] | @param string $path
@return string | [
"@param",
"string",
"$path"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/theme/class-lock.php#L238-L245 |
pressbooks/pressbooks | inc/theme/class-lock.php | Lock.restrictThemeManagement | public function restrictThemeManagement() {
$locked = $this->isLocked();
if ( $locked ) {
$data = $this->getLockData();
// Redirect and notify users of theme lock status.
$check_against_url = wp_parse_url( ( is_ssl() ? 'http://' : 'https://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], PHP_URL_PATH );
$redirect_url = get_site_url( get_current_blog_id(), '/wp-admin/' );
// ---------------------------------------------------------------------------------------------------------------
// Don't let user go to theme (options) page, under any circumstance
$restricted = [
'themes',
];
$expr = '~/wp-admin/(' . implode( '|', $restricted ) . ')\.php$~';
if ( preg_match( $expr, $check_against_url ) ) {
$_SESSION['pb_errors'][] = sprintf(
__( 'Your book’s theme, %1$s, was locked in its current state as of %2$s at %3$s. To select a new theme or change your theme options, please %4$s.', 'pressbooks' ),
$data['name'],
strftime( '%x', $data['timestamp'] ),
strftime( '%X', $data['timestamp'] ),
sprintf(
'<a href="%s">%s</a>',
admin_url( 'options-general.php?page=pressbooks_export_options' ),
'unlock your theme'
)
);
\Pressbooks\Redirect\location( $redirect_url );
}
}
} | php | public function restrictThemeManagement() {
$locked = $this->isLocked();
if ( $locked ) {
$data = $this->getLockData();
// Redirect and notify users of theme lock status.
$check_against_url = wp_parse_url( ( is_ssl() ? 'http://' : 'https://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], PHP_URL_PATH );
$redirect_url = get_site_url( get_current_blog_id(), '/wp-admin/' );
// ---------------------------------------------------------------------------------------------------------------
// Don't let user go to theme (options) page, under any circumstance
$restricted = [
'themes',
];
$expr = '~/wp-admin/(' . implode( '|', $restricted ) . ')\.php$~';
if ( preg_match( $expr, $check_against_url ) ) {
$_SESSION['pb_errors'][] = sprintf(
__( 'Your book’s theme, %1$s, was locked in its current state as of %2$s at %3$s. To select a new theme or change your theme options, please %4$s.', 'pressbooks' ),
$data['name'],
strftime( '%x', $data['timestamp'] ),
strftime( '%X', $data['timestamp'] ),
sprintf(
'<a href="%s">%s</a>',
admin_url( 'options-general.php?page=pressbooks_export_options' ),
'unlock your theme'
)
);
\Pressbooks\Redirect\location( $redirect_url );
}
}
} | [
"public",
"function",
"restrictThemeManagement",
"(",
")",
"{",
"$",
"locked",
"=",
"$",
"this",
"->",
"isLocked",
"(",
")",
";",
"if",
"(",
"$",
"locked",
")",
"{",
"$",
"data",
"=",
"$",
"this",
"->",
"getLockData",
"(",
")",
";",
"// Redirect and notify users of theme lock status.",
"$",
"check_against_url",
"=",
"wp_parse_url",
"(",
"(",
"is_ssl",
"(",
")",
"?",
"'http://'",
":",
"'https://'",
")",
".",
"$",
"_SERVER",
"[",
"'HTTP_HOST'",
"]",
".",
"$",
"_SERVER",
"[",
"'REQUEST_URI'",
"]",
",",
"PHP_URL_PATH",
")",
";",
"$",
"redirect_url",
"=",
"get_site_url",
"(",
"get_current_blog_id",
"(",
")",
",",
"'/wp-admin/'",
")",
";",
"// ---------------------------------------------------------------------------------------------------------------",
"// Don't let user go to theme (options) page, under any circumstance",
"$",
"restricted",
"=",
"[",
"'themes'",
",",
"]",
";",
"$",
"expr",
"=",
"'~/wp-admin/('",
".",
"implode",
"(",
"'|'",
",",
"$",
"restricted",
")",
".",
"')\\.php$~'",
";",
"if",
"(",
"preg_match",
"(",
"$",
"expr",
",",
"$",
"check_against_url",
")",
")",
"{",
"$",
"_SESSION",
"[",
"'pb_errors'",
"]",
"[",
"]",
"=",
"sprintf",
"(",
"__",
"(",
"'Your book’s theme, %1$s, was locked in its current state as of %2$s at %3$s. To select a new theme or change your theme options, please %4$s.'",
",",
"'pressbooks'",
")",
",",
"$",
"data",
"[",
"'name'",
"]",
",",
"strftime",
"(",
"'%x'",
",",
"$",
"data",
"[",
"'timestamp'",
"]",
")",
",",
"strftime",
"(",
"'%X'",
",",
"$",
"data",
"[",
"'timestamp'",
"]",
")",
",",
"sprintf",
"(",
"'<a href=\"%s\">%s</a>'",
",",
"admin_url",
"(",
"'options-general.php?page=pressbooks_export_options'",
")",
",",
"'unlock your theme'",
")",
")",
";",
"\\",
"Pressbooks",
"\\",
"Redirect",
"\\",
"location",
"(",
"$",
"redirect_url",
")",
";",
"}",
"}",
"}"
] | Restrict access to Themes and Theme Options. | [
"Restrict",
"access",
"to",
"Themes",
"and",
"Theme",
"Options",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/theme/class-lock.php#L250-L283 |
pressbooks/pressbooks | inc/modules/themeoptions/class-admin.php | Admin.getTabs | public function getTabs() {
$tabs = [
'global' => '\Pressbooks\Modules\ThemeOptions\GlobalOptions',
'web' => '\Pressbooks\Modules\ThemeOptions\WebOptions',
'pdf' => '\Pressbooks\Modules\ThemeOptions\PDFOptions',
'ebook' => '\Pressbooks\Modules\ThemeOptions\EbookOptions',
];
if ( false === get_site_transient( 'pb_pdf_compatible' ) && false === \Pressbooks\Modules\Export\Prince\Filters::hasDependencies() ) {
unset( $tabs['pdf'] );
} else {
set_site_transient( 'pb_pdf_compatible', true );
}
if ( false === get_site_transient( 'pb_epub_compatible' ) && false === \Pressbooks\Modules\Export\Epub\Epub201::hasDependencies() ) {
unset( $tabs['ebook'] );
} else {
set_site_transient( 'pb_epub_compatible', true );
}
/**
* Add a custom tab to the theme options page.
*
* @since 3.9.7
*
* @param array $tabs
*/
return apply_filters( 'pb_theme_options_tabs', $tabs );
} | php | public function getTabs() {
$tabs = [
'global' => '\Pressbooks\Modules\ThemeOptions\GlobalOptions',
'web' => '\Pressbooks\Modules\ThemeOptions\WebOptions',
'pdf' => '\Pressbooks\Modules\ThemeOptions\PDFOptions',
'ebook' => '\Pressbooks\Modules\ThemeOptions\EbookOptions',
];
if ( false === get_site_transient( 'pb_pdf_compatible' ) && false === \Pressbooks\Modules\Export\Prince\Filters::hasDependencies() ) {
unset( $tabs['pdf'] );
} else {
set_site_transient( 'pb_pdf_compatible', true );
}
if ( false === get_site_transient( 'pb_epub_compatible' ) && false === \Pressbooks\Modules\Export\Epub\Epub201::hasDependencies() ) {
unset( $tabs['ebook'] );
} else {
set_site_transient( 'pb_epub_compatible', true );
}
/**
* Add a custom tab to the theme options page.
*
* @since 3.9.7
*
* @param array $tabs
*/
return apply_filters( 'pb_theme_options_tabs', $tabs );
} | [
"public",
"function",
"getTabs",
"(",
")",
"{",
"$",
"tabs",
"=",
"[",
"'global'",
"=>",
"'\\Pressbooks\\Modules\\ThemeOptions\\GlobalOptions'",
",",
"'web'",
"=>",
"'\\Pressbooks\\Modules\\ThemeOptions\\WebOptions'",
",",
"'pdf'",
"=>",
"'\\Pressbooks\\Modules\\ThemeOptions\\PDFOptions'",
",",
"'ebook'",
"=>",
"'\\Pressbooks\\Modules\\ThemeOptions\\EbookOptions'",
",",
"]",
";",
"if",
"(",
"false",
"===",
"get_site_transient",
"(",
"'pb_pdf_compatible'",
")",
"&&",
"false",
"===",
"\\",
"Pressbooks",
"\\",
"Modules",
"\\",
"Export",
"\\",
"Prince",
"\\",
"Filters",
"::",
"hasDependencies",
"(",
")",
")",
"{",
"unset",
"(",
"$",
"tabs",
"[",
"'pdf'",
"]",
")",
";",
"}",
"else",
"{",
"set_site_transient",
"(",
"'pb_pdf_compatible'",
",",
"true",
")",
";",
"}",
"if",
"(",
"false",
"===",
"get_site_transient",
"(",
"'pb_epub_compatible'",
")",
"&&",
"false",
"===",
"\\",
"Pressbooks",
"\\",
"Modules",
"\\",
"Export",
"\\",
"Epub",
"\\",
"Epub201",
"::",
"hasDependencies",
"(",
")",
")",
"{",
"unset",
"(",
"$",
"tabs",
"[",
"'ebook'",
"]",
")",
";",
"}",
"else",
"{",
"set_site_transient",
"(",
"'pb_epub_compatible'",
",",
"true",
")",
";",
"}",
"/**\n\t\t * Add a custom tab to the theme options page.\n\t\t *\n\t\t * @since 3.9.7\n\t\t *\n\t\t * @param array $tabs\n\t\t */",
"return",
"apply_filters",
"(",
"'pb_theme_options_tabs'",
",",
"$",
"tabs",
")",
";",
"}"
] | Returns a filtered array of tabs that we should be loading.
@returns array | [
"Returns",
"a",
"filtered",
"array",
"of",
"tabs",
"that",
"we",
"should",
"be",
"loading",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/themeoptions/class-admin.php#L53-L81 |
pressbooks/pressbooks | inc/modules/themeoptions/class-admin.php | Admin.loadTabs | public function loadTabs() {
foreach ( $this->getTabs() as $slug => $subclass ) {
/** @var \Pressbooks\Options $subclass (not instantiated, just a string) */
add_filter( "option_page_capability_pressbooks_theme_options_$slug", [ $this, 'setPermissions' ], 10, 1 );
add_filter( "pb_theme_options_{$slug}_defaults", [ $subclass, 'filterDefaults' ], 10, 1 );
$options = get_option( "pressbooks_theme_options_{$slug}", [] );
/** @var \Pressbooks\Options $tab */
$tab = new $subclass( $options );
$tab->init();
wp_cache_delete( "pressbooks_theme_options_{$slug}_version", 'options' ); // WordPress Core caches this key in the "options" group
$version = get_option( "pressbooks_theme_options_{$slug}_version", 0 );
if ( $tab::VERSION !== null && $version < $tab::VERSION ) {
$tab->upgrade( $version );
update_option( "pressbooks_theme_options_{$slug}_version", $tab::VERSION, false );
debug_error_log( 'Upgraded ' . $slug . ' options from version ' . $version . ' --> ' . $tab::VERSION );
}
}
} | php | public function loadTabs() {
foreach ( $this->getTabs() as $slug => $subclass ) {
/** @var \Pressbooks\Options $subclass (not instantiated, just a string) */
add_filter( "option_page_capability_pressbooks_theme_options_$slug", [ $this, 'setPermissions' ], 10, 1 );
add_filter( "pb_theme_options_{$slug}_defaults", [ $subclass, 'filterDefaults' ], 10, 1 );
$options = get_option( "pressbooks_theme_options_{$slug}", [] );
/** @var \Pressbooks\Options $tab */
$tab = new $subclass( $options );
$tab->init();
wp_cache_delete( "pressbooks_theme_options_{$slug}_version", 'options' ); // WordPress Core caches this key in the "options" group
$version = get_option( "pressbooks_theme_options_{$slug}_version", 0 );
if ( $tab::VERSION !== null && $version < $tab::VERSION ) {
$tab->upgrade( $version );
update_option( "pressbooks_theme_options_{$slug}_version", $tab::VERSION, false );
debug_error_log( 'Upgraded ' . $slug . ' options from version ' . $version . ' --> ' . $tab::VERSION );
}
}
} | [
"public",
"function",
"loadTabs",
"(",
")",
"{",
"foreach",
"(",
"$",
"this",
"->",
"getTabs",
"(",
")",
"as",
"$",
"slug",
"=>",
"$",
"subclass",
")",
"{",
"/** @var \\Pressbooks\\Options $subclass (not instantiated, just a string) */",
"add_filter",
"(",
"\"option_page_capability_pressbooks_theme_options_$slug\"",
",",
"[",
"$",
"this",
",",
"'setPermissions'",
"]",
",",
"10",
",",
"1",
")",
";",
"add_filter",
"(",
"\"pb_theme_options_{$slug}_defaults\"",
",",
"[",
"$",
"subclass",
",",
"'filterDefaults'",
"]",
",",
"10",
",",
"1",
")",
";",
"$",
"options",
"=",
"get_option",
"(",
"\"pressbooks_theme_options_{$slug}\"",
",",
"[",
"]",
")",
";",
"/** @var \\Pressbooks\\Options $tab */",
"$",
"tab",
"=",
"new",
"$",
"subclass",
"(",
"$",
"options",
")",
";",
"$",
"tab",
"->",
"init",
"(",
")",
";",
"wp_cache_delete",
"(",
"\"pressbooks_theme_options_{$slug}_version\"",
",",
"'options'",
")",
";",
"// WordPress Core caches this key in the \"options\" group",
"$",
"version",
"=",
"get_option",
"(",
"\"pressbooks_theme_options_{$slug}_version\"",
",",
"0",
")",
";",
"if",
"(",
"$",
"tab",
"::",
"VERSION",
"!==",
"null",
"&&",
"$",
"version",
"<",
"$",
"tab",
"::",
"VERSION",
")",
"{",
"$",
"tab",
"->",
"upgrade",
"(",
"$",
"version",
")",
";",
"update_option",
"(",
"\"pressbooks_theme_options_{$slug}_version\"",
",",
"$",
"tab",
"::",
"VERSION",
",",
"false",
")",
";",
"debug_error_log",
"(",
"'Upgraded '",
".",
"$",
"slug",
".",
"' options from version '",
".",
"$",
"version",
".",
"' --> '",
".",
"$",
"tab",
"::",
"VERSION",
")",
";",
"}",
"}",
"}"
] | Register the settings on each tab, run upgrade() if needed. | [
"Register",
"the",
"settings",
"on",
"each",
"tab",
"run",
"upgrade",
"()",
"if",
"needed",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/themeoptions/class-admin.php#L99-L116 |
pressbooks/pressbooks | inc/modules/themeoptions/class-admin.php | Admin.render | public function render() {
?>
<div class="wrap">
<h1><?php echo wp_get_theme(); ?> <?php _e( 'Theme Options', 'pressbooks' ); ?></h1>
<?php settings_errors(); ?>
<?php $active_tab = isset( $_GET['tab'] ) ? $_GET['tab'] : 'global'; ?>
<h2 class="nav-tab-wrapper">
<?php foreach ( $this->getTabs() as $slug => $subclass ) { ?>
<a href="<?php echo admin_url( '/themes.php' ); ?>?page=pressbooks_theme_options&tab=<?php echo $slug; ?>"
class="nav-tab <?php echo $active_tab === $slug ? 'nav-tab-active' : ''; ?>"><?php echo $subclass::getTitle(); ?></a>
<?php } ?>
</h2>
<form method="post" action="options.php">
<?php
do_action( 'pb_before_themeoptions_settings_fields' );
settings_fields( 'pressbooks_theme_options_' . $active_tab );
do_settings_sections( 'pressbooks_theme_options_' . $active_tab );
submit_button();
?>
</form>
</div>
<?php
} | php | public function render() {
?>
<div class="wrap">
<h1><?php echo wp_get_theme(); ?> <?php _e( 'Theme Options', 'pressbooks' ); ?></h1>
<?php settings_errors(); ?>
<?php $active_tab = isset( $_GET['tab'] ) ? $_GET['tab'] : 'global'; ?>
<h2 class="nav-tab-wrapper">
<?php foreach ( $this->getTabs() as $slug => $subclass ) { ?>
<a href="<?php echo admin_url( '/themes.php' ); ?>?page=pressbooks_theme_options&tab=<?php echo $slug; ?>"
class="nav-tab <?php echo $active_tab === $slug ? 'nav-tab-active' : ''; ?>"><?php echo $subclass::getTitle(); ?></a>
<?php } ?>
</h2>
<form method="post" action="options.php">
<?php
do_action( 'pb_before_themeoptions_settings_fields' );
settings_fields( 'pressbooks_theme_options_' . $active_tab );
do_settings_sections( 'pressbooks_theme_options_' . $active_tab );
submit_button();
?>
</form>
</div>
<?php
} | [
"public",
"function",
"render",
"(",
")",
"{",
"?>\n\t\t<div class=\"wrap\">\n\t\t\t<h1><?php",
"echo",
"wp_get_theme",
"(",
")",
";",
"?> <?php",
"_e",
"(",
"'Theme Options'",
",",
"'pressbooks'",
")",
";",
"?></h1>\n\t\t\t<?php",
"settings_errors",
"(",
")",
";",
"?>\n\t\t\t<?php",
"$",
"active_tab",
"=",
"isset",
"(",
"$",
"_GET",
"[",
"'tab'",
"]",
")",
"?",
"$",
"_GET",
"[",
"'tab'",
"]",
":",
"'global'",
";",
"?>\n\t\t\t<h2 class=\"nav-tab-wrapper\">\n\t\t\t\t<?php",
"foreach",
"(",
"$",
"this",
"->",
"getTabs",
"(",
")",
"as",
"$",
"slug",
"=>",
"$",
"subclass",
")",
"{",
"?>\n\t\t\t\t\t<a href=\"<?php",
"echo",
"admin_url",
"(",
"'/themes.php'",
")",
";",
"?>?page=pressbooks_theme_options&tab=<?php",
"echo",
"$",
"slug",
";",
"?>\"\n\t\t\t\t\t\tclass=\"nav-tab <?php",
"echo",
"$",
"active_tab",
"===",
"$",
"slug",
"?",
"'nav-tab-active'",
":",
"''",
";",
"?>\"><?php",
"echo",
"$",
"subclass",
"::",
"getTitle",
"(",
")",
";",
"?></a>\n\t\t\t\t<?php",
"}",
"?>\n\t\t\t</h2>\n\t\t\t<form method=\"post\" action=\"options.php\">\n\t\t\t\t<?php",
"do_action",
"(",
"'pb_before_themeoptions_settings_fields'",
")",
";",
"settings_fields",
"(",
"'pressbooks_theme_options_'",
".",
"$",
"active_tab",
")",
";",
"do_settings_sections",
"(",
"'pressbooks_theme_options_'",
".",
"$",
"active_tab",
")",
";",
"submit_button",
"(",
")",
";",
"?>\n\t\t\t</form>\n\t\t</div>\n\t\t<?php",
"}"
] | Render the theme options page and load the appropriate tab. | [
"Render",
"the",
"theme",
"options",
"page",
"and",
"load",
"the",
"appropriate",
"tab",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/themeoptions/class-admin.php#L135-L157 |
pressbooks/pressbooks | inc/modules/themeoptions/class-admin.php | Admin.afterSwitchTheme | public function afterSwitchTheme() {
$this->clearCache();
foreach ( $this->getTabs() as $slug => $subclass ) {
/** @var \Pressbooks\Options $subclass (not instantiated, just a string) */
$current_options = get_option( "pressbooks_theme_options_{$slug}", [] );
if ( ! empty( $current_options ) ) {
update_option( "pressbooks_theme_options_{$slug}", $subclass::filterDefaults( $current_options ) );
}
}
} | php | public function afterSwitchTheme() {
$this->clearCache();
foreach ( $this->getTabs() as $slug => $subclass ) {
/** @var \Pressbooks\Options $subclass (not instantiated, just a string) */
$current_options = get_option( "pressbooks_theme_options_{$slug}", [] );
if ( ! empty( $current_options ) ) {
update_option( "pressbooks_theme_options_{$slug}", $subclass::filterDefaults( $current_options ) );
}
}
} | [
"public",
"function",
"afterSwitchTheme",
"(",
")",
"{",
"$",
"this",
"->",
"clearCache",
"(",
")",
";",
"foreach",
"(",
"$",
"this",
"->",
"getTabs",
"(",
")",
"as",
"$",
"slug",
"=>",
"$",
"subclass",
")",
"{",
"/** @var \\Pressbooks\\Options $subclass (not instantiated, just a string) */",
"$",
"current_options",
"=",
"get_option",
"(",
"\"pressbooks_theme_options_{$slug}\"",
",",
"[",
"]",
")",
";",
"if",
"(",
"!",
"empty",
"(",
"$",
"current_options",
")",
")",
"{",
"update_option",
"(",
"\"pressbooks_theme_options_{$slug}\"",
",",
"$",
"subclass",
"::",
"filterDefaults",
"(",
"$",
"current_options",
")",
")",
";",
"}",
"}",
"}"
] | Override saved options with filtered defaults when switching theme | [
"Override",
"saved",
"options",
"with",
"filtered",
"defaults",
"when",
"switching",
"theme"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/themeoptions/class-admin.php#L162-L171 |
pressbooks/pressbooks | inc/api/endpoints/controller/class-toc.php | Toc.get_item_schema | public function get_item_schema() {
$item = [
'id' => [
'description' => __( 'Unique identifier for the object.' ),
'type' => 'integer',
'context' => [ 'view' ],
'readonly' => true,
],
'title' => [
'description' => __( 'The title for the object.' ),
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
'slug' => [
'description' => __( 'An alphanumeric identifier for the object unique to its type.' ),
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
'author' => [
'description' => __( 'The ID for the author of the object.' ),
'type' => 'integer',
'context' => [ 'view' ],
'readonly' => true,
],
'comment_count' => [
'description' => __( 'Comment count', 'pressbooks' ),
'type' => 'integer',
'context' => [ 'view' ],
'readonly' => true,
],
'menu_order' => [
'description' => __( 'The order of the object in relation to other object of its type.' ),
'type' => 'integer',
'context' => [ 'view' ],
'readonly' => true,
],
'status' => [
'description' => __( 'A named status for the object.' ),
'type' => 'string',
'enum' => array_keys(
get_post_stati(
[
'internal' => false,
]
)
),
'context' => [ 'view' ],
'readonly' => true,
],
'export' => [
'description' => __( 'Include in exports.', 'pressbooks' ),
'type' => 'boolean',
'context' => [ 'view' ],
'readonly' => true,
],
'has_post_content' => [
'description' => __( 'Has post content, the content field is not empty.', 'pressbooks' ),
'type' => 'boolean',
'readonly' => true,
],
'word_count' => [
'description' => __( 'Word count.', 'pressbooks' ),
'type' => 'integer',
'readonly' => true,
],
'link' => [
'description' => __( 'URL to the object.' ),
'type' => 'string',
'format' => 'uri',
'context' => [ 'view' ],
'readonly' => true,
],
];
$fm_meta_data = $this->frontMatterMetadata->get_item_schema();
$fm_item = array_merge(
$item, [
'metadata' => [
'description' => __( 'Metadata', 'pressbooks' ),
'type' => 'object',
'properties' => $fm_meta_data['properties'],
'context' => [ 'view' ],
'readonly' => true,
],
]
);
$bm_meta_data = $this->backMatterMetadata->get_item_schema();
$bm_item = array_merge(
$item, [
'metadata' => [
'description' => __( 'Metadata', 'pressbooks' ),
'type' => 'object',
'properties' => $bm_meta_data['properties'],
'context' => [ 'view' ],
'readonly' => true,
],
]
);
$ch_meta_data = $this->chapterMetadata->get_item_schema();
$ch_item = array_merge(
$item, [
'metadata' => [
'description' => __( 'Metadata', 'pressbooks' ),
'type' => 'object',
'properties' => $ch_meta_data['properties'],
'context' => [ 'view' ],
'readonly' => true,
],
]
);
$schema = [
'$schema' => 'http://json-schema.org/schema#',
'title' => 'toc',
'type' => 'object',
'properties' => [
'front-matter' => [
'description' => __( 'Front Matter', 'pressbooks' ),
'type' => 'array',
'items' => [
'type' => 'object',
'properties' => $fm_item,
],
'context' => [ 'view' ],
'readonly' => true,
],
'parts' => [
'description' => __( 'Part', 'pressbooks' ),
'type' => 'array',
'items' => [
'type' => 'object',
'properties' => array_merge(
$item, [
'chapters' => [
'description' => __( 'Chapter', 'pressbooks' ),
'type' => 'array',
'items' => [
'type' => 'object',
'properties' => $ch_item,
],
],
'context' => [ 'view' ],
'readonly' => true,
]
),
],
'context' => [ 'view' ],
'readonly' => true,
],
'back-matter' => [
'description' => __( 'Back Matter', 'pressbooks' ),
'type' => 'array',
'items' => [
'type' => 'object',
'properties' => $bm_item,
],
],
'context' => [ 'view' ],
'readonly' => true,
],
];
return $this->add_additional_fields_schema( $schema );
} | php | public function get_item_schema() {
$item = [
'id' => [
'description' => __( 'Unique identifier for the object.' ),
'type' => 'integer',
'context' => [ 'view' ],
'readonly' => true,
],
'title' => [
'description' => __( 'The title for the object.' ),
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
'slug' => [
'description' => __( 'An alphanumeric identifier for the object unique to its type.' ),
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
'author' => [
'description' => __( 'The ID for the author of the object.' ),
'type' => 'integer',
'context' => [ 'view' ],
'readonly' => true,
],
'comment_count' => [
'description' => __( 'Comment count', 'pressbooks' ),
'type' => 'integer',
'context' => [ 'view' ],
'readonly' => true,
],
'menu_order' => [
'description' => __( 'The order of the object in relation to other object of its type.' ),
'type' => 'integer',
'context' => [ 'view' ],
'readonly' => true,
],
'status' => [
'description' => __( 'A named status for the object.' ),
'type' => 'string',
'enum' => array_keys(
get_post_stati(
[
'internal' => false,
]
)
),
'context' => [ 'view' ],
'readonly' => true,
],
'export' => [
'description' => __( 'Include in exports.', 'pressbooks' ),
'type' => 'boolean',
'context' => [ 'view' ],
'readonly' => true,
],
'has_post_content' => [
'description' => __( 'Has post content, the content field is not empty.', 'pressbooks' ),
'type' => 'boolean',
'readonly' => true,
],
'word_count' => [
'description' => __( 'Word count.', 'pressbooks' ),
'type' => 'integer',
'readonly' => true,
],
'link' => [
'description' => __( 'URL to the object.' ),
'type' => 'string',
'format' => 'uri',
'context' => [ 'view' ],
'readonly' => true,
],
];
$fm_meta_data = $this->frontMatterMetadata->get_item_schema();
$fm_item = array_merge(
$item, [
'metadata' => [
'description' => __( 'Metadata', 'pressbooks' ),
'type' => 'object',
'properties' => $fm_meta_data['properties'],
'context' => [ 'view' ],
'readonly' => true,
],
]
);
$bm_meta_data = $this->backMatterMetadata->get_item_schema();
$bm_item = array_merge(
$item, [
'metadata' => [
'description' => __( 'Metadata', 'pressbooks' ),
'type' => 'object',
'properties' => $bm_meta_data['properties'],
'context' => [ 'view' ],
'readonly' => true,
],
]
);
$ch_meta_data = $this->chapterMetadata->get_item_schema();
$ch_item = array_merge(
$item, [
'metadata' => [
'description' => __( 'Metadata', 'pressbooks' ),
'type' => 'object',
'properties' => $ch_meta_data['properties'],
'context' => [ 'view' ],
'readonly' => true,
],
]
);
$schema = [
'$schema' => 'http://json-schema.org/schema#',
'title' => 'toc',
'type' => 'object',
'properties' => [
'front-matter' => [
'description' => __( 'Front Matter', 'pressbooks' ),
'type' => 'array',
'items' => [
'type' => 'object',
'properties' => $fm_item,
],
'context' => [ 'view' ],
'readonly' => true,
],
'parts' => [
'description' => __( 'Part', 'pressbooks' ),
'type' => 'array',
'items' => [
'type' => 'object',
'properties' => array_merge(
$item, [
'chapters' => [
'description' => __( 'Chapter', 'pressbooks' ),
'type' => 'array',
'items' => [
'type' => 'object',
'properties' => $ch_item,
],
],
'context' => [ 'view' ],
'readonly' => true,
]
),
],
'context' => [ 'view' ],
'readonly' => true,
],
'back-matter' => [
'description' => __( 'Back Matter', 'pressbooks' ),
'type' => 'array',
'items' => [
'type' => 'object',
'properties' => $bm_item,
],
],
'context' => [ 'view' ],
'readonly' => true,
],
];
return $this->add_additional_fields_schema( $schema );
} | [
"public",
"function",
"get_item_schema",
"(",
")",
"{",
"$",
"item",
"=",
"[",
"'id'",
"=>",
"[",
"'description'",
"=>",
"__",
"(",
"'Unique identifier for the object.'",
")",
",",
"'type'",
"=>",
"'integer'",
",",
"'context'",
"=>",
"[",
"'view'",
"]",
",",
"'readonly'",
"=>",
"true",
",",
"]",
",",
"'title'",
"=>",
"[",
"'description'",
"=>",
"__",
"(",
"'The title for the object.'",
")",
",",
"'type'",
"=>",
"'string'",
",",
"'context'",
"=>",
"[",
"'view'",
"]",
",",
"'readonly'",
"=>",
"true",
",",
"]",
",",
"'slug'",
"=>",
"[",
"'description'",
"=>",
"__",
"(",
"'An alphanumeric identifier for the object unique to its type.'",
")",
",",
"'type'",
"=>",
"'string'",
",",
"'context'",
"=>",
"[",
"'view'",
"]",
",",
"'readonly'",
"=>",
"true",
",",
"]",
",",
"'author'",
"=>",
"[",
"'description'",
"=>",
"__",
"(",
"'The ID for the author of the object.'",
")",
",",
"'type'",
"=>",
"'integer'",
",",
"'context'",
"=>",
"[",
"'view'",
"]",
",",
"'readonly'",
"=>",
"true",
",",
"]",
",",
"'comment_count'",
"=>",
"[",
"'description'",
"=>",
"__",
"(",
"'Comment count'",
",",
"'pressbooks'",
")",
",",
"'type'",
"=>",
"'integer'",
",",
"'context'",
"=>",
"[",
"'view'",
"]",
",",
"'readonly'",
"=>",
"true",
",",
"]",
",",
"'menu_order'",
"=>",
"[",
"'description'",
"=>",
"__",
"(",
"'The order of the object in relation to other object of its type.'",
")",
",",
"'type'",
"=>",
"'integer'",
",",
"'context'",
"=>",
"[",
"'view'",
"]",
",",
"'readonly'",
"=>",
"true",
",",
"]",
",",
"'status'",
"=>",
"[",
"'description'",
"=>",
"__",
"(",
"'A named status for the object.'",
")",
",",
"'type'",
"=>",
"'string'",
",",
"'enum'",
"=>",
"array_keys",
"(",
"get_post_stati",
"(",
"[",
"'internal'",
"=>",
"false",
",",
"]",
")",
")",
",",
"'context'",
"=>",
"[",
"'view'",
"]",
",",
"'readonly'",
"=>",
"true",
",",
"]",
",",
"'export'",
"=>",
"[",
"'description'",
"=>",
"__",
"(",
"'Include in exports.'",
",",
"'pressbooks'",
")",
",",
"'type'",
"=>",
"'boolean'",
",",
"'context'",
"=>",
"[",
"'view'",
"]",
",",
"'readonly'",
"=>",
"true",
",",
"]",
",",
"'has_post_content'",
"=>",
"[",
"'description'",
"=>",
"__",
"(",
"'Has post content, the content field is not empty.'",
",",
"'pressbooks'",
")",
",",
"'type'",
"=>",
"'boolean'",
",",
"'readonly'",
"=>",
"true",
",",
"]",
",",
"'word_count'",
"=>",
"[",
"'description'",
"=>",
"__",
"(",
"'Word count.'",
",",
"'pressbooks'",
")",
",",
"'type'",
"=>",
"'integer'",
",",
"'readonly'",
"=>",
"true",
",",
"]",
",",
"'link'",
"=>",
"[",
"'description'",
"=>",
"__",
"(",
"'URL to the object.'",
")",
",",
"'type'",
"=>",
"'string'",
",",
"'format'",
"=>",
"'uri'",
",",
"'context'",
"=>",
"[",
"'view'",
"]",
",",
"'readonly'",
"=>",
"true",
",",
"]",
",",
"]",
";",
"$",
"fm_meta_data",
"=",
"$",
"this",
"->",
"frontMatterMetadata",
"->",
"get_item_schema",
"(",
")",
";",
"$",
"fm_item",
"=",
"array_merge",
"(",
"$",
"item",
",",
"[",
"'metadata'",
"=>",
"[",
"'description'",
"=>",
"__",
"(",
"'Metadata'",
",",
"'pressbooks'",
")",
",",
"'type'",
"=>",
"'object'",
",",
"'properties'",
"=>",
"$",
"fm_meta_data",
"[",
"'properties'",
"]",
",",
"'context'",
"=>",
"[",
"'view'",
"]",
",",
"'readonly'",
"=>",
"true",
",",
"]",
",",
"]",
")",
";",
"$",
"bm_meta_data",
"=",
"$",
"this",
"->",
"backMatterMetadata",
"->",
"get_item_schema",
"(",
")",
";",
"$",
"bm_item",
"=",
"array_merge",
"(",
"$",
"item",
",",
"[",
"'metadata'",
"=>",
"[",
"'description'",
"=>",
"__",
"(",
"'Metadata'",
",",
"'pressbooks'",
")",
",",
"'type'",
"=>",
"'object'",
",",
"'properties'",
"=>",
"$",
"bm_meta_data",
"[",
"'properties'",
"]",
",",
"'context'",
"=>",
"[",
"'view'",
"]",
",",
"'readonly'",
"=>",
"true",
",",
"]",
",",
"]",
")",
";",
"$",
"ch_meta_data",
"=",
"$",
"this",
"->",
"chapterMetadata",
"->",
"get_item_schema",
"(",
")",
";",
"$",
"ch_item",
"=",
"array_merge",
"(",
"$",
"item",
",",
"[",
"'metadata'",
"=>",
"[",
"'description'",
"=>",
"__",
"(",
"'Metadata'",
",",
"'pressbooks'",
")",
",",
"'type'",
"=>",
"'object'",
",",
"'properties'",
"=>",
"$",
"ch_meta_data",
"[",
"'properties'",
"]",
",",
"'context'",
"=>",
"[",
"'view'",
"]",
",",
"'readonly'",
"=>",
"true",
",",
"]",
",",
"]",
")",
";",
"$",
"schema",
"=",
"[",
"'$schema'",
"=>",
"'http://json-schema.org/schema#'",
",",
"'title'",
"=>",
"'toc'",
",",
"'type'",
"=>",
"'object'",
",",
"'properties'",
"=>",
"[",
"'front-matter'",
"=>",
"[",
"'description'",
"=>",
"__",
"(",
"'Front Matter'",
",",
"'pressbooks'",
")",
",",
"'type'",
"=>",
"'array'",
",",
"'items'",
"=>",
"[",
"'type'",
"=>",
"'object'",
",",
"'properties'",
"=>",
"$",
"fm_item",
",",
"]",
",",
"'context'",
"=>",
"[",
"'view'",
"]",
",",
"'readonly'",
"=>",
"true",
",",
"]",
",",
"'parts'",
"=>",
"[",
"'description'",
"=>",
"__",
"(",
"'Part'",
",",
"'pressbooks'",
")",
",",
"'type'",
"=>",
"'array'",
",",
"'items'",
"=>",
"[",
"'type'",
"=>",
"'object'",
",",
"'properties'",
"=>",
"array_merge",
"(",
"$",
"item",
",",
"[",
"'chapters'",
"=>",
"[",
"'description'",
"=>",
"__",
"(",
"'Chapter'",
",",
"'pressbooks'",
")",
",",
"'type'",
"=>",
"'array'",
",",
"'items'",
"=>",
"[",
"'type'",
"=>",
"'object'",
",",
"'properties'",
"=>",
"$",
"ch_item",
",",
"]",
",",
"]",
",",
"'context'",
"=>",
"[",
"'view'",
"]",
",",
"'readonly'",
"=>",
"true",
",",
"]",
")",
",",
"]",
",",
"'context'",
"=>",
"[",
"'view'",
"]",
",",
"'readonly'",
"=>",
"true",
",",
"]",
",",
"'back-matter'",
"=>",
"[",
"'description'",
"=>",
"__",
"(",
"'Back Matter'",
",",
"'pressbooks'",
")",
",",
"'type'",
"=>",
"'array'",
",",
"'items'",
"=>",
"[",
"'type'",
"=>",
"'object'",
",",
"'properties'",
"=>",
"$",
"bm_item",
",",
"]",
",",
"]",
",",
"'context'",
"=>",
"[",
"'view'",
"]",
",",
"'readonly'",
"=>",
"true",
",",
"]",
",",
"]",
";",
"return",
"$",
"this",
"->",
"add_additional_fields_schema",
"(",
"$",
"schema",
")",
";",
"}"
] | Retrieves TOC schema, conforming to JSON Schema
@return array | [
"Retrieves",
"TOC",
"schema",
"conforming",
"to",
"JSON",
"Schema"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/api/endpoints/controller/class-toc.php#L70-L238 |
pressbooks/pressbooks | inc/api/endpoints/controller/class-toc.php | Toc.get_item | public function get_item( $request ) {
// Register missing routes
$this->registerRouteDependencies();
$struct = Book::getBookStructure();
unset( $struct['__order'] );
$struct = $this->fixBookStructure( $struct, current_user_can( 'edit_posts' ) );
$response = rest_ensure_response( $struct );
$this->linkCollector['self'] = [
'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
];
$response->add_links( $this->linkCollector );
return $response;
} | php | public function get_item( $request ) {
// Register missing routes
$this->registerRouteDependencies();
$struct = Book::getBookStructure();
unset( $struct['__order'] );
$struct = $this->fixBookStructure( $struct, current_user_can( 'edit_posts' ) );
$response = rest_ensure_response( $struct );
$this->linkCollector['self'] = [
'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
];
$response->add_links( $this->linkCollector );
return $response;
} | [
"public",
"function",
"get_item",
"(",
"$",
"request",
")",
"{",
"// Register missing routes",
"$",
"this",
"->",
"registerRouteDependencies",
"(",
")",
";",
"$",
"struct",
"=",
"Book",
"::",
"getBookStructure",
"(",
")",
";",
"unset",
"(",
"$",
"struct",
"[",
"'__order'",
"]",
")",
";",
"$",
"struct",
"=",
"$",
"this",
"->",
"fixBookStructure",
"(",
"$",
"struct",
",",
"current_user_can",
"(",
"'edit_posts'",
")",
")",
";",
"$",
"response",
"=",
"rest_ensure_response",
"(",
"$",
"struct",
")",
";",
"$",
"this",
"->",
"linkCollector",
"[",
"'self'",
"]",
"=",
"[",
"'href'",
"=>",
"rest_url",
"(",
"sprintf",
"(",
"'%s/%s'",
",",
"$",
"this",
"->",
"namespace",
",",
"$",
"this",
"->",
"rest_base",
")",
")",
",",
"]",
";",
"$",
"response",
"->",
"add_links",
"(",
"$",
"this",
"->",
"linkCollector",
")",
";",
"return",
"$",
"response",
";",
"}"
] | @param \WP_REST_Request $request Full data about the request.
@return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure. | [
"@param",
"\\",
"WP_REST_Request",
"$request",
"Full",
"data",
"about",
"the",
"request",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/api/endpoints/controller/class-toc.php#L263-L279 |
pressbooks/pressbooks | inc/api/endpoints/controller/class-toc.php | Toc.registerRouteDependencies | protected function registerRouteDependencies() {
$this->frontMatterMetadata->register_routes();
$this->backMatterMetadata->register_routes();
$this->chapterMetadata->register_routes();
} | php | protected function registerRouteDependencies() {
$this->frontMatterMetadata->register_routes();
$this->backMatterMetadata->register_routes();
$this->chapterMetadata->register_routes();
} | [
"protected",
"function",
"registerRouteDependencies",
"(",
")",
"{",
"$",
"this",
"->",
"frontMatterMetadata",
"->",
"register_routes",
"(",
")",
";",
"$",
"this",
"->",
"backMatterMetadata",
"->",
"register_routes",
"(",
")",
";",
"$",
"this",
"->",
"chapterMetadata",
"->",
"register_routes",
"(",
")",
";",
"}"
] | Define route dependencies.
Toc content is built by querying section metadata, but those API routes may not exist at the root level. | [
"Define",
"route",
"dependencies",
".",
"Toc",
"content",
"is",
"built",
"by",
"querying",
"section",
"metadata",
"but",
"those",
"API",
"routes",
"may",
"not",
"exist",
"at",
"the",
"root",
"level",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/api/endpoints/controller/class-toc.php#L285-L289 |
pressbooks/pressbooks | inc/api/endpoints/controller/class-toc.php | Toc.fixBookStructure | protected function fixBookStructure( array $book_structure, $has_permission ) {
$toc = [];
$replacement_keys = [
'ID' => 'id',
'post_title' => 'title',
'post_name' => 'slug',
'post_author' => 'author',
'post_status' => 'status',
];
$toc['front-matter'] = $this->fixFrontMatterStructure( $book_structure, $has_permission, $replacement_keys );
$toc['parts'] = $this->fixPartChapterStructure( $book_structure, $has_permission, $replacement_keys );
$toc['back-matter'] = $this->fixBackMatterStructure( $book_structure, $has_permission, $replacement_keys );
return $toc;
} | php | protected function fixBookStructure( array $book_structure, $has_permission ) {
$toc = [];
$replacement_keys = [
'ID' => 'id',
'post_title' => 'title',
'post_name' => 'slug',
'post_author' => 'author',
'post_status' => 'status',
];
$toc['front-matter'] = $this->fixFrontMatterStructure( $book_structure, $has_permission, $replacement_keys );
$toc['parts'] = $this->fixPartChapterStructure( $book_structure, $has_permission, $replacement_keys );
$toc['back-matter'] = $this->fixBackMatterStructure( $book_structure, $has_permission, $replacement_keys );
return $toc;
} | [
"protected",
"function",
"fixBookStructure",
"(",
"array",
"$",
"book_structure",
",",
"$",
"has_permission",
")",
"{",
"$",
"toc",
"=",
"[",
"]",
";",
"$",
"replacement_keys",
"=",
"[",
"'ID'",
"=>",
"'id'",
",",
"'post_title'",
"=>",
"'title'",
",",
"'post_name'",
"=>",
"'slug'",
",",
"'post_author'",
"=>",
"'author'",
",",
"'post_status'",
"=>",
"'status'",
",",
"]",
";",
"$",
"toc",
"[",
"'front-matter'",
"]",
"=",
"$",
"this",
"->",
"fixFrontMatterStructure",
"(",
"$",
"book_structure",
",",
"$",
"has_permission",
",",
"$",
"replacement_keys",
")",
";",
"$",
"toc",
"[",
"'parts'",
"]",
"=",
"$",
"this",
"->",
"fixPartChapterStructure",
"(",
"$",
"book_structure",
",",
"$",
"has_permission",
",",
"$",
"replacement_keys",
")",
";",
"$",
"toc",
"[",
"'back-matter'",
"]",
"=",
"$",
"this",
"->",
"fixBackMatterStructure",
"(",
"$",
"book_structure",
",",
"$",
"has_permission",
",",
"$",
"replacement_keys",
")",
";",
"return",
"$",
"toc",
";",
"}"
] | @param array $book_structure
@param bool $has_permission
@return array | [
"@param",
"array",
"$book_structure",
"@param",
"bool",
"$has_permission"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/api/endpoints/controller/class-toc.php#L297-L314 |
pressbooks/pressbooks | inc/api/endpoints/controller/class-toc.php | Toc.fixFrontMatterStructure | protected function fixFrontMatterStructure( array $book_structure, $has_permission, array $replacement_keys ) {
$base = 'front-matter';
$rest_url = rest_url( sprintf( '%s/%s', $this->namespace, $base ) );
// Front-matter
$front_matter = [];
foreach ( $book_structure['front-matter'] as $old_fm ) {
if ( $has_permission || in_array( $old_fm['post_status'], [ 'web-only', 'publish' ], true ) ) {
$new_fm = [];
foreach ( $old_fm as $old_key => $val ) {
$new_key = strtr( $old_key, $replacement_keys );
$new_fm[ $new_key ] = $val;
}
$new_fm['link'] = get_permalink( $new_fm['id'] );
$new_fm['front-matter-type'] = wp_get_object_terms(
$new_fm['id'], 'front-matter-type', [
'fields' => 'ids',
]
);
$this->linkCollector['front-matter'][] = [
'href' => trailingslashit( $rest_url ) . $new_fm['id'],
'embeddable' => true,
];
// Metadata
$request_metadata = new \WP_REST_Request( 'GET', "/pressbooks/v2/{$base}/{$new_fm['id']}/metadata" );
$response_metadata = rest_do_request( $request_metadata );
$new_fm['metadata'] = $this->prepare_response_for_collection( $response_metadata );
$this->linkCollector['metadata'][] = [
'href' => trailingslashit( $rest_url ) . "{$new_fm['id']}/metadata",
];
unset( $new_fm['metadata']['_links'] );
$front_matter[] = $new_fm;
}
}
return $front_matter;
} | php | protected function fixFrontMatterStructure( array $book_structure, $has_permission, array $replacement_keys ) {
$base = 'front-matter';
$rest_url = rest_url( sprintf( '%s/%s', $this->namespace, $base ) );
// Front-matter
$front_matter = [];
foreach ( $book_structure['front-matter'] as $old_fm ) {
if ( $has_permission || in_array( $old_fm['post_status'], [ 'web-only', 'publish' ], true ) ) {
$new_fm = [];
foreach ( $old_fm as $old_key => $val ) {
$new_key = strtr( $old_key, $replacement_keys );
$new_fm[ $new_key ] = $val;
}
$new_fm['link'] = get_permalink( $new_fm['id'] );
$new_fm['front-matter-type'] = wp_get_object_terms(
$new_fm['id'], 'front-matter-type', [
'fields' => 'ids',
]
);
$this->linkCollector['front-matter'][] = [
'href' => trailingslashit( $rest_url ) . $new_fm['id'],
'embeddable' => true,
];
// Metadata
$request_metadata = new \WP_REST_Request( 'GET', "/pressbooks/v2/{$base}/{$new_fm['id']}/metadata" );
$response_metadata = rest_do_request( $request_metadata );
$new_fm['metadata'] = $this->prepare_response_for_collection( $response_metadata );
$this->linkCollector['metadata'][] = [
'href' => trailingslashit( $rest_url ) . "{$new_fm['id']}/metadata",
];
unset( $new_fm['metadata']['_links'] );
$front_matter[] = $new_fm;
}
}
return $front_matter;
} | [
"protected",
"function",
"fixFrontMatterStructure",
"(",
"array",
"$",
"book_structure",
",",
"$",
"has_permission",
",",
"array",
"$",
"replacement_keys",
")",
"{",
"$",
"base",
"=",
"'front-matter'",
";",
"$",
"rest_url",
"=",
"rest_url",
"(",
"sprintf",
"(",
"'%s/%s'",
",",
"$",
"this",
"->",
"namespace",
",",
"$",
"base",
")",
")",
";",
"// Front-matter",
"$",
"front_matter",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"book_structure",
"[",
"'front-matter'",
"]",
"as",
"$",
"old_fm",
")",
"{",
"if",
"(",
"$",
"has_permission",
"||",
"in_array",
"(",
"$",
"old_fm",
"[",
"'post_status'",
"]",
",",
"[",
"'web-only'",
",",
"'publish'",
"]",
",",
"true",
")",
")",
"{",
"$",
"new_fm",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"old_fm",
"as",
"$",
"old_key",
"=>",
"$",
"val",
")",
"{",
"$",
"new_key",
"=",
"strtr",
"(",
"$",
"old_key",
",",
"$",
"replacement_keys",
")",
";",
"$",
"new_fm",
"[",
"$",
"new_key",
"]",
"=",
"$",
"val",
";",
"}",
"$",
"new_fm",
"[",
"'link'",
"]",
"=",
"get_permalink",
"(",
"$",
"new_fm",
"[",
"'id'",
"]",
")",
";",
"$",
"new_fm",
"[",
"'front-matter-type'",
"]",
"=",
"wp_get_object_terms",
"(",
"$",
"new_fm",
"[",
"'id'",
"]",
",",
"'front-matter-type'",
",",
"[",
"'fields'",
"=>",
"'ids'",
",",
"]",
")",
";",
"$",
"this",
"->",
"linkCollector",
"[",
"'front-matter'",
"]",
"[",
"]",
"=",
"[",
"'href'",
"=>",
"trailingslashit",
"(",
"$",
"rest_url",
")",
".",
"$",
"new_fm",
"[",
"'id'",
"]",
",",
"'embeddable'",
"=>",
"true",
",",
"]",
";",
"// Metadata",
"$",
"request_metadata",
"=",
"new",
"\\",
"WP_REST_Request",
"(",
"'GET'",
",",
"\"/pressbooks/v2/{$base}/{$new_fm['id']}/metadata\"",
")",
";",
"$",
"response_metadata",
"=",
"rest_do_request",
"(",
"$",
"request_metadata",
")",
";",
"$",
"new_fm",
"[",
"'metadata'",
"]",
"=",
"$",
"this",
"->",
"prepare_response_for_collection",
"(",
"$",
"response_metadata",
")",
";",
"$",
"this",
"->",
"linkCollector",
"[",
"'metadata'",
"]",
"[",
"]",
"=",
"[",
"'href'",
"=>",
"trailingslashit",
"(",
"$",
"rest_url",
")",
".",
"\"{$new_fm['id']}/metadata\"",
",",
"]",
";",
"unset",
"(",
"$",
"new_fm",
"[",
"'metadata'",
"]",
"[",
"'_links'",
"]",
")",
";",
"$",
"front_matter",
"[",
"]",
"=",
"$",
"new_fm",
";",
"}",
"}",
"return",
"$",
"front_matter",
";",
"}"
] | @param array $book_structure
@param $has_permission
@param array $replacement_keys
@return array | [
"@param",
"array",
"$book_structure",
"@param",
"$has_permission",
"@param",
"array",
"$replacement_keys"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/api/endpoints/controller/class-toc.php#L323-L362 |
pressbooks/pressbooks | inc/api/endpoints/controller/class-toc.php | Toc.fixPartChapterStructure | protected function fixPartChapterStructure( array $book_structure, $has_permission, array $replacement_keys ) {
$part_base = 'parts';
$part_rest_url = rest_url( sprintf( '%s/%s', $this->namespace, $part_base ) );
$chapter_base = 'chapters';
$chapter_rest_url = rest_url( sprintf( '%s/%s', $this->namespace, $chapter_base ) );
$part = [];
foreach ( $book_structure['part'] as $old_p ) {
$new_p = [];
foreach ( $old_p as $old_key => $val ) {
$new_key = strtr( $old_key, $replacement_keys );
$new_p[ $new_key ] = $val;
}
$chapters = [];
foreach ( $new_p['chapters'] as $old_ch ) {
if ( $has_permission || in_array( $old_ch['post_status'], [ 'web-only', 'publish' ], true ) ) {
$new_ch = [];
foreach ( $old_ch as $old_key => $val ) {
$new_key = strtr( $old_key, $replacement_keys );
$new_ch[ $new_key ] = $val;
}
$new_ch['link'] = get_permalink( $new_ch['id'] );
$new_ch['chapter-type'] = wp_get_object_terms(
$new_ch['id'], 'chapter-type', [
'fields' => 'ids',
]
);
$this->linkCollector['chapter'][] = [
'href' => trailingslashit( $chapter_rest_url ) . $new_ch['id'],
'embeddable' => true,
];
// Metadata
$request_metadata = new \WP_REST_Request( 'GET', "/pressbooks/v2/{$chapter_base}/{$new_ch['id']}/metadata" );
$response_metadata = rest_do_request( $request_metadata );
$new_ch['metadata'] = $this->prepare_response_for_collection( $response_metadata );
$this->linkCollector['metadata'][] = [
'href' => trailingslashit( $chapter_rest_url ) . "{$new_ch['id']}/metadata",
];
unset( $new_ch['metadata']['_links'] );
$chapters[] = $new_ch;
}
}
$new_p['chapters'] = $chapters;
$new_p['link'] = get_permalink( $new_p['id'] );
$this->linkCollector['part'][] = [
'href' => trailingslashit( $part_rest_url ) . $new_p['id'],
'embeddable' => true,
];
$part[] = $new_p;
}
return $part;
} | php | protected function fixPartChapterStructure( array $book_structure, $has_permission, array $replacement_keys ) {
$part_base = 'parts';
$part_rest_url = rest_url( sprintf( '%s/%s', $this->namespace, $part_base ) );
$chapter_base = 'chapters';
$chapter_rest_url = rest_url( sprintf( '%s/%s', $this->namespace, $chapter_base ) );
$part = [];
foreach ( $book_structure['part'] as $old_p ) {
$new_p = [];
foreach ( $old_p as $old_key => $val ) {
$new_key = strtr( $old_key, $replacement_keys );
$new_p[ $new_key ] = $val;
}
$chapters = [];
foreach ( $new_p['chapters'] as $old_ch ) {
if ( $has_permission || in_array( $old_ch['post_status'], [ 'web-only', 'publish' ], true ) ) {
$new_ch = [];
foreach ( $old_ch as $old_key => $val ) {
$new_key = strtr( $old_key, $replacement_keys );
$new_ch[ $new_key ] = $val;
}
$new_ch['link'] = get_permalink( $new_ch['id'] );
$new_ch['chapter-type'] = wp_get_object_terms(
$new_ch['id'], 'chapter-type', [
'fields' => 'ids',
]
);
$this->linkCollector['chapter'][] = [
'href' => trailingslashit( $chapter_rest_url ) . $new_ch['id'],
'embeddable' => true,
];
// Metadata
$request_metadata = new \WP_REST_Request( 'GET', "/pressbooks/v2/{$chapter_base}/{$new_ch['id']}/metadata" );
$response_metadata = rest_do_request( $request_metadata );
$new_ch['metadata'] = $this->prepare_response_for_collection( $response_metadata );
$this->linkCollector['metadata'][] = [
'href' => trailingslashit( $chapter_rest_url ) . "{$new_ch['id']}/metadata",
];
unset( $new_ch['metadata']['_links'] );
$chapters[] = $new_ch;
}
}
$new_p['chapters'] = $chapters;
$new_p['link'] = get_permalink( $new_p['id'] );
$this->linkCollector['part'][] = [
'href' => trailingslashit( $part_rest_url ) . $new_p['id'],
'embeddable' => true,
];
$part[] = $new_p;
}
return $part;
} | [
"protected",
"function",
"fixPartChapterStructure",
"(",
"array",
"$",
"book_structure",
",",
"$",
"has_permission",
",",
"array",
"$",
"replacement_keys",
")",
"{",
"$",
"part_base",
"=",
"'parts'",
";",
"$",
"part_rest_url",
"=",
"rest_url",
"(",
"sprintf",
"(",
"'%s/%s'",
",",
"$",
"this",
"->",
"namespace",
",",
"$",
"part_base",
")",
")",
";",
"$",
"chapter_base",
"=",
"'chapters'",
";",
"$",
"chapter_rest_url",
"=",
"rest_url",
"(",
"sprintf",
"(",
"'%s/%s'",
",",
"$",
"this",
"->",
"namespace",
",",
"$",
"chapter_base",
")",
")",
";",
"$",
"part",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"book_structure",
"[",
"'part'",
"]",
"as",
"$",
"old_p",
")",
"{",
"$",
"new_p",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"old_p",
"as",
"$",
"old_key",
"=>",
"$",
"val",
")",
"{",
"$",
"new_key",
"=",
"strtr",
"(",
"$",
"old_key",
",",
"$",
"replacement_keys",
")",
";",
"$",
"new_p",
"[",
"$",
"new_key",
"]",
"=",
"$",
"val",
";",
"}",
"$",
"chapters",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"new_p",
"[",
"'chapters'",
"]",
"as",
"$",
"old_ch",
")",
"{",
"if",
"(",
"$",
"has_permission",
"||",
"in_array",
"(",
"$",
"old_ch",
"[",
"'post_status'",
"]",
",",
"[",
"'web-only'",
",",
"'publish'",
"]",
",",
"true",
")",
")",
"{",
"$",
"new_ch",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"old_ch",
"as",
"$",
"old_key",
"=>",
"$",
"val",
")",
"{",
"$",
"new_key",
"=",
"strtr",
"(",
"$",
"old_key",
",",
"$",
"replacement_keys",
")",
";",
"$",
"new_ch",
"[",
"$",
"new_key",
"]",
"=",
"$",
"val",
";",
"}",
"$",
"new_ch",
"[",
"'link'",
"]",
"=",
"get_permalink",
"(",
"$",
"new_ch",
"[",
"'id'",
"]",
")",
";",
"$",
"new_ch",
"[",
"'chapter-type'",
"]",
"=",
"wp_get_object_terms",
"(",
"$",
"new_ch",
"[",
"'id'",
"]",
",",
"'chapter-type'",
",",
"[",
"'fields'",
"=>",
"'ids'",
",",
"]",
")",
";",
"$",
"this",
"->",
"linkCollector",
"[",
"'chapter'",
"]",
"[",
"]",
"=",
"[",
"'href'",
"=>",
"trailingslashit",
"(",
"$",
"chapter_rest_url",
")",
".",
"$",
"new_ch",
"[",
"'id'",
"]",
",",
"'embeddable'",
"=>",
"true",
",",
"]",
";",
"// Metadata",
"$",
"request_metadata",
"=",
"new",
"\\",
"WP_REST_Request",
"(",
"'GET'",
",",
"\"/pressbooks/v2/{$chapter_base}/{$new_ch['id']}/metadata\"",
")",
";",
"$",
"response_metadata",
"=",
"rest_do_request",
"(",
"$",
"request_metadata",
")",
";",
"$",
"new_ch",
"[",
"'metadata'",
"]",
"=",
"$",
"this",
"->",
"prepare_response_for_collection",
"(",
"$",
"response_metadata",
")",
";",
"$",
"this",
"->",
"linkCollector",
"[",
"'metadata'",
"]",
"[",
"]",
"=",
"[",
"'href'",
"=>",
"trailingslashit",
"(",
"$",
"chapter_rest_url",
")",
".",
"\"{$new_ch['id']}/metadata\"",
",",
"]",
";",
"unset",
"(",
"$",
"new_ch",
"[",
"'metadata'",
"]",
"[",
"'_links'",
"]",
")",
";",
"$",
"chapters",
"[",
"]",
"=",
"$",
"new_ch",
";",
"}",
"}",
"$",
"new_p",
"[",
"'chapters'",
"]",
"=",
"$",
"chapters",
";",
"$",
"new_p",
"[",
"'link'",
"]",
"=",
"get_permalink",
"(",
"$",
"new_p",
"[",
"'id'",
"]",
")",
";",
"$",
"this",
"->",
"linkCollector",
"[",
"'part'",
"]",
"[",
"]",
"=",
"[",
"'href'",
"=>",
"trailingslashit",
"(",
"$",
"part_rest_url",
")",
".",
"$",
"new_p",
"[",
"'id'",
"]",
",",
"'embeddable'",
"=>",
"true",
",",
"]",
";",
"$",
"part",
"[",
"]",
"=",
"$",
"new_p",
";",
"}",
"return",
"$",
"part",
";",
"}"
] | @param array $book_structure
@param $has_permission
@param array $replacement_keys
@return array | [
"@param",
"array",
"$book_structure",
"@param",
"$has_permission",
"@param",
"array",
"$replacement_keys"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/api/endpoints/controller/class-toc.php#L371-L426 |
pressbooks/pressbooks | inc/api/endpoints/controller/class-toc.php | Toc.fixBackMatterStructure | protected function fixBackMatterStructure( array $book_structure, $has_permission, array $replacement_keys ) {
$base = 'back-matter';
$rest_url = rest_url( sprintf( '%s/%s', $this->namespace, $base ) );
$back_matter = [];
foreach ( $book_structure['back-matter'] as $old_bm ) {
if ( $has_permission || in_array( $old_bm['post_status'], [ 'web-only', 'publish' ], true ) ) {
$new_bm = [];
foreach ( $old_bm as $old_key => $val ) {
$new_key = strtr( $old_key, $replacement_keys );
$new_bm[ $new_key ] = $val;
}
$new_bm['link'] = get_permalink( $new_bm['id'] );
$new_bm['back-matter-type'] = wp_get_object_terms(
$new_bm['id'], 'back-matter-type', [
'fields' => 'ids',
]
);
$this->linkCollector['back-matter'][] = [
'href' => trailingslashit( $rest_url ) . $new_bm['id'],
'embeddable' => true,
];
// Metadata
$request_metadata = new \WP_REST_Request( 'GET', "/pressbooks/v2/{$base}/{$new_bm['id']}/metadata" );
$response_metadata = rest_do_request( $request_metadata );
$new_bm['metadata'] = $this->prepare_response_for_collection( $response_metadata );
$this->linkCollector['metadata'][] = [
'href' => trailingslashit( $rest_url ) . "{$new_bm['id']}/metadata",
];
unset( $new_bm['metadata']['_links'] );
$back_matter[] = $new_bm;
}
}
return $back_matter;
} | php | protected function fixBackMatterStructure( array $book_structure, $has_permission, array $replacement_keys ) {
$base = 'back-matter';
$rest_url = rest_url( sprintf( '%s/%s', $this->namespace, $base ) );
$back_matter = [];
foreach ( $book_structure['back-matter'] as $old_bm ) {
if ( $has_permission || in_array( $old_bm['post_status'], [ 'web-only', 'publish' ], true ) ) {
$new_bm = [];
foreach ( $old_bm as $old_key => $val ) {
$new_key = strtr( $old_key, $replacement_keys );
$new_bm[ $new_key ] = $val;
}
$new_bm['link'] = get_permalink( $new_bm['id'] );
$new_bm['back-matter-type'] = wp_get_object_terms(
$new_bm['id'], 'back-matter-type', [
'fields' => 'ids',
]
);
$this->linkCollector['back-matter'][] = [
'href' => trailingslashit( $rest_url ) . $new_bm['id'],
'embeddable' => true,
];
// Metadata
$request_metadata = new \WP_REST_Request( 'GET', "/pressbooks/v2/{$base}/{$new_bm['id']}/metadata" );
$response_metadata = rest_do_request( $request_metadata );
$new_bm['metadata'] = $this->prepare_response_for_collection( $response_metadata );
$this->linkCollector['metadata'][] = [
'href' => trailingslashit( $rest_url ) . "{$new_bm['id']}/metadata",
];
unset( $new_bm['metadata']['_links'] );
$back_matter[] = $new_bm;
}
}
return $back_matter;
} | [
"protected",
"function",
"fixBackMatterStructure",
"(",
"array",
"$",
"book_structure",
",",
"$",
"has_permission",
",",
"array",
"$",
"replacement_keys",
")",
"{",
"$",
"base",
"=",
"'back-matter'",
";",
"$",
"rest_url",
"=",
"rest_url",
"(",
"sprintf",
"(",
"'%s/%s'",
",",
"$",
"this",
"->",
"namespace",
",",
"$",
"base",
")",
")",
";",
"$",
"back_matter",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"book_structure",
"[",
"'back-matter'",
"]",
"as",
"$",
"old_bm",
")",
"{",
"if",
"(",
"$",
"has_permission",
"||",
"in_array",
"(",
"$",
"old_bm",
"[",
"'post_status'",
"]",
",",
"[",
"'web-only'",
",",
"'publish'",
"]",
",",
"true",
")",
")",
"{",
"$",
"new_bm",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"old_bm",
"as",
"$",
"old_key",
"=>",
"$",
"val",
")",
"{",
"$",
"new_key",
"=",
"strtr",
"(",
"$",
"old_key",
",",
"$",
"replacement_keys",
")",
";",
"$",
"new_bm",
"[",
"$",
"new_key",
"]",
"=",
"$",
"val",
";",
"}",
"$",
"new_bm",
"[",
"'link'",
"]",
"=",
"get_permalink",
"(",
"$",
"new_bm",
"[",
"'id'",
"]",
")",
";",
"$",
"new_bm",
"[",
"'back-matter-type'",
"]",
"=",
"wp_get_object_terms",
"(",
"$",
"new_bm",
"[",
"'id'",
"]",
",",
"'back-matter-type'",
",",
"[",
"'fields'",
"=>",
"'ids'",
",",
"]",
")",
";",
"$",
"this",
"->",
"linkCollector",
"[",
"'back-matter'",
"]",
"[",
"]",
"=",
"[",
"'href'",
"=>",
"trailingslashit",
"(",
"$",
"rest_url",
")",
".",
"$",
"new_bm",
"[",
"'id'",
"]",
",",
"'embeddable'",
"=>",
"true",
",",
"]",
";",
"// Metadata",
"$",
"request_metadata",
"=",
"new",
"\\",
"WP_REST_Request",
"(",
"'GET'",
",",
"\"/pressbooks/v2/{$base}/{$new_bm['id']}/metadata\"",
")",
";",
"$",
"response_metadata",
"=",
"rest_do_request",
"(",
"$",
"request_metadata",
")",
";",
"$",
"new_bm",
"[",
"'metadata'",
"]",
"=",
"$",
"this",
"->",
"prepare_response_for_collection",
"(",
"$",
"response_metadata",
")",
";",
"$",
"this",
"->",
"linkCollector",
"[",
"'metadata'",
"]",
"[",
"]",
"=",
"[",
"'href'",
"=>",
"trailingslashit",
"(",
"$",
"rest_url",
")",
".",
"\"{$new_bm['id']}/metadata\"",
",",
"]",
";",
"unset",
"(",
"$",
"new_bm",
"[",
"'metadata'",
"]",
"[",
"'_links'",
"]",
")",
";",
"$",
"back_matter",
"[",
"]",
"=",
"$",
"new_bm",
";",
"}",
"}",
"return",
"$",
"back_matter",
";",
"}"
] | @param array $book_structure
@param $has_permission
@param array $replacement_keys
@return array | [
"@param",
"array",
"$book_structure",
"@param",
"$has_permission",
"@param",
"array",
"$replacement_keys"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/api/endpoints/controller/class-toc.php#L435-L473 |
pressbooks/pressbooks | inc/admin/class-publishoptions.php | PublishOptions.init | function init() {
$_option = $this->getSlug();
$_page = $_option;
$_section = $this->getSlug() . '_section';
add_settings_section(
$_section,
'',
[ $this, 'display' ],
$_page
);
add_settings_field(
'amazon',
__( 'Amazon URL', 'pressbooks' ),
[ $this, 'renderAmazonField' ],
$_page,
$_section
);
add_settings_field(
'oreilly',
__( 'O\'Reilly URL', 'pressbooks' ),
[ $this, 'renderOReillyField' ],
$_page,
$_section
);
add_settings_field(
'barnesandnoble',
__( 'Barnes and Noble URL', 'pressbooks' ),
[ $this, 'renderBarnesAndNobleField' ],
$_page,
$_section
);
add_settings_field(
'kobo',
__( 'Kobo URL', 'pressbooks' ),
[ $this, 'renderKoboField' ],
$_page,
$_section
);
add_settings_field(
'ibooks',
__( 'iBooks URL', 'pressbooks' ),
[ $this, 'renderiBooksField' ],
$_page,
$_section
);
add_settings_field(
'otherservice',
__( 'Other Service URL', 'pressbooks' ),
[ $this, 'renderOtherServiceField' ],
$_page,
$_section
);
register_setting(
$_page,
$_option,
[ $this, 'sanitize' ]
);
} | php | function init() {
$_option = $this->getSlug();
$_page = $_option;
$_section = $this->getSlug() . '_section';
add_settings_section(
$_section,
'',
[ $this, 'display' ],
$_page
);
add_settings_field(
'amazon',
__( 'Amazon URL', 'pressbooks' ),
[ $this, 'renderAmazonField' ],
$_page,
$_section
);
add_settings_field(
'oreilly',
__( 'O\'Reilly URL', 'pressbooks' ),
[ $this, 'renderOReillyField' ],
$_page,
$_section
);
add_settings_field(
'barnesandnoble',
__( 'Barnes and Noble URL', 'pressbooks' ),
[ $this, 'renderBarnesAndNobleField' ],
$_page,
$_section
);
add_settings_field(
'kobo',
__( 'Kobo URL', 'pressbooks' ),
[ $this, 'renderKoboField' ],
$_page,
$_section
);
add_settings_field(
'ibooks',
__( 'iBooks URL', 'pressbooks' ),
[ $this, 'renderiBooksField' ],
$_page,
$_section
);
add_settings_field(
'otherservice',
__( 'Other Service URL', 'pressbooks' ),
[ $this, 'renderOtherServiceField' ],
$_page,
$_section
);
register_setting(
$_page,
$_option,
[ $this, 'sanitize' ]
);
} | [
"function",
"init",
"(",
")",
"{",
"$",
"_option",
"=",
"$",
"this",
"->",
"getSlug",
"(",
")",
";",
"$",
"_page",
"=",
"$",
"_option",
";",
"$",
"_section",
"=",
"$",
"this",
"->",
"getSlug",
"(",
")",
".",
"'_section'",
";",
"add_settings_section",
"(",
"$",
"_section",
",",
"''",
",",
"[",
"$",
"this",
",",
"'display'",
"]",
",",
"$",
"_page",
")",
";",
"add_settings_field",
"(",
"'amazon'",
",",
"__",
"(",
"'Amazon URL'",
",",
"'pressbooks'",
")",
",",
"[",
"$",
"this",
",",
"'renderAmazonField'",
"]",
",",
"$",
"_page",
",",
"$",
"_section",
")",
";",
"add_settings_field",
"(",
"'oreilly'",
",",
"__",
"(",
"'O\\'Reilly URL'",
",",
"'pressbooks'",
")",
",",
"[",
"$",
"this",
",",
"'renderOReillyField'",
"]",
",",
"$",
"_page",
",",
"$",
"_section",
")",
";",
"add_settings_field",
"(",
"'barnesandnoble'",
",",
"__",
"(",
"'Barnes and Noble URL'",
",",
"'pressbooks'",
")",
",",
"[",
"$",
"this",
",",
"'renderBarnesAndNobleField'",
"]",
",",
"$",
"_page",
",",
"$",
"_section",
")",
";",
"add_settings_field",
"(",
"'kobo'",
",",
"__",
"(",
"'Kobo URL'",
",",
"'pressbooks'",
")",
",",
"[",
"$",
"this",
",",
"'renderKoboField'",
"]",
",",
"$",
"_page",
",",
"$",
"_section",
")",
";",
"add_settings_field",
"(",
"'ibooks'",
",",
"__",
"(",
"'iBooks URL'",
",",
"'pressbooks'",
")",
",",
"[",
"$",
"this",
",",
"'renderiBooksField'",
"]",
",",
"$",
"_page",
",",
"$",
"_section",
")",
";",
"add_settings_field",
"(",
"'otherservice'",
",",
"__",
"(",
"'Other Service URL'",
",",
"'pressbooks'",
")",
",",
"[",
"$",
"this",
",",
"'renderOtherServiceField'",
"]",
",",
"$",
"_page",
",",
"$",
"_section",
")",
";",
"register_setting",
"(",
"$",
"_page",
",",
"$",
"_option",
",",
"[",
"$",
"this",
",",
"'sanitize'",
"]",
")",
";",
"}"
] | Configure the publish options page using the settings API. | [
"Configure",
"the",
"publish",
"options",
"page",
"using",
"the",
"settings",
"API",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/admin/class-publishoptions.php#L52-L117 |
pressbooks/pressbooks | inc/admin/class-publishoptions.php | PublishOptions.display | function display() {
ob_start(); ?>
<p><?php _e( 'Once your book is finished, you can download the files and submit them to ebookstores and print-on-demand providers.', 'pressbooks' ); ?></p>
<div class="postbox">
<div class="inside">
<h3><?php _e( 'Ebook Stores', 'pressbooks' ); ?></h3>
<p><?php printf( __( 'Once you have downloaded your files, you can either submit them to ebookstores yourself, or use a third-party distributor. Recommended self-serve ebookstores are <a href="%1$1s">Kindle</a>, <a href="%2$2s">Kobo</a>, and <a href="%3$3s">Nook</a>. Other ebook stores include Apple iBooks and Google.', 'pressbooks' ), 'https://kdp.amazon.com', 'https://www.kobo.com/writinglife', 'https://www.nookpress.com' ); ?></p>
<p><?php printf( __( 'If you do not wish to submit your ebooks yourself, we recommend using a third-party distribution service such as <a href="%1s">IngramSpark</a>, which can also make your books available online in print.', 'pressbooks' ), 'https://ingramspark.com' ); ?></p>
<h3><?php _e( 'Print-on-Demand', 'pressbooks' ); ?></h3>
<p><?php printf( __( 'If you wish to sell your printed books online, we recommend going through <a href="%1$1s">IngramSpark</a> or Amazon\'s <a href="%2$2s">CreateSpace</a>.', 'pressbooks' ), 'https://ingramspark.com', 'https://www.createspace.com' ); ?></p>
</div>
</div>
<h3><?php _e( 'Adding BUY Links to Your Pressbooks Web Book', 'pressbooks' ); ?></h3>
<p><?php _e( 'If you would like to add <strong>BUY</strong> links to your Pressbooks web book, add the links to your book at the different retailers below:', 'pressbooks' ); ?></p>
<?php
$output = ob_get_contents();
ob_end_clean();
/**
* Filter the contents of the Dashboard Publish page.
*
* @since 4.3.0
*/
echo apply_filters(
'pb_publish_page',
/**
* Filter the contents of the Dashboard Publish page.
*
* @since 3.9.3
* @deprecated 4.3.0 Use pb_publish_page instead.
*
* @param string $output
*/
apply_filters( 'pressbooks_publish_page', $output )
);
} | php | function display() {
ob_start(); ?>
<p><?php _e( 'Once your book is finished, you can download the files and submit them to ebookstores and print-on-demand providers.', 'pressbooks' ); ?></p>
<div class="postbox">
<div class="inside">
<h3><?php _e( 'Ebook Stores', 'pressbooks' ); ?></h3>
<p><?php printf( __( 'Once you have downloaded your files, you can either submit them to ebookstores yourself, or use a third-party distributor. Recommended self-serve ebookstores are <a href="%1$1s">Kindle</a>, <a href="%2$2s">Kobo</a>, and <a href="%3$3s">Nook</a>. Other ebook stores include Apple iBooks and Google.', 'pressbooks' ), 'https://kdp.amazon.com', 'https://www.kobo.com/writinglife', 'https://www.nookpress.com' ); ?></p>
<p><?php printf( __( 'If you do not wish to submit your ebooks yourself, we recommend using a third-party distribution service such as <a href="%1s">IngramSpark</a>, which can also make your books available online in print.', 'pressbooks' ), 'https://ingramspark.com' ); ?></p>
<h3><?php _e( 'Print-on-Demand', 'pressbooks' ); ?></h3>
<p><?php printf( __( 'If you wish to sell your printed books online, we recommend going through <a href="%1$1s">IngramSpark</a> or Amazon\'s <a href="%2$2s">CreateSpace</a>.', 'pressbooks' ), 'https://ingramspark.com', 'https://www.createspace.com' ); ?></p>
</div>
</div>
<h3><?php _e( 'Adding BUY Links to Your Pressbooks Web Book', 'pressbooks' ); ?></h3>
<p><?php _e( 'If you would like to add <strong>BUY</strong> links to your Pressbooks web book, add the links to your book at the different retailers below:', 'pressbooks' ); ?></p>
<?php
$output = ob_get_contents();
ob_end_clean();
/**
* Filter the contents of the Dashboard Publish page.
*
* @since 4.3.0
*/
echo apply_filters(
'pb_publish_page',
/**
* Filter the contents of the Dashboard Publish page.
*
* @since 3.9.3
* @deprecated 4.3.0 Use pb_publish_page instead.
*
* @param string $output
*/
apply_filters( 'pressbooks_publish_page', $output )
);
} | [
"function",
"display",
"(",
")",
"{",
"ob_start",
"(",
")",
";",
"?>\n\t\t<p><?php",
"_e",
"(",
"'Once your book is finished, you can download the files and submit them to ebookstores and print-on-demand providers.'",
",",
"'pressbooks'",
")",
";",
"?></p>\n\n\t\t<div class=\"postbox\">\n\t\t\t<div class=\"inside\">\n\t\t\t\t<h3><?php",
"_e",
"(",
"'Ebook Stores'",
",",
"'pressbooks'",
")",
";",
"?></h3>\n\t\t\t\t<p><?php",
"printf",
"(",
"__",
"(",
"'Once you have downloaded your files, you can either submit them to ebookstores yourself, or use a third-party distributor. Recommended self-serve ebookstores are <a href=\"%1$1s\">Kindle</a>, <a href=\"%2$2s\">Kobo</a>, and <a href=\"%3$3s\">Nook</a>. Other ebook stores include Apple iBooks and Google.'",
",",
"'pressbooks'",
")",
",",
"'https://kdp.amazon.com'",
",",
"'https://www.kobo.com/writinglife'",
",",
"'https://www.nookpress.com'",
")",
";",
"?></p>\n\t\t\t\t<p><?php",
"printf",
"(",
"__",
"(",
"'If you do not wish to submit your ebooks yourself, we recommend using a third-party distribution service such as <a href=\"%1s\">IngramSpark</a>, which can also make your books available online in print.'",
",",
"'pressbooks'",
")",
",",
"'https://ingramspark.com'",
")",
";",
"?></p>\n\n\t\t\t\t<h3><?php",
"_e",
"(",
"'Print-on-Demand'",
",",
"'pressbooks'",
")",
";",
"?></h3>\n\t\t\t\t<p><?php",
"printf",
"(",
"__",
"(",
"'If you wish to sell your printed books online, we recommend going through <a href=\"%1$1s\">IngramSpark</a> or Amazon\\'s <a href=\"%2$2s\">CreateSpace</a>.'",
",",
"'pressbooks'",
")",
",",
"'https://ingramspark.com'",
",",
"'https://www.createspace.com'",
")",
";",
"?></p>\n\t\t\t</div>\n\t\t</div>\n\n\t\t<h3><?php",
"_e",
"(",
"'Adding BUY Links to Your Pressbooks Web Book'",
",",
"'pressbooks'",
")",
";",
"?></h3>\n\t\t<p><?php",
"_e",
"(",
"'If you would like to add <strong>BUY</strong> links to your Pressbooks web book, add the links to your book at the different retailers below:'",
",",
"'pressbooks'",
")",
";",
"?></p>\n\n\t\t<?php",
"$",
"output",
"=",
"ob_get_contents",
"(",
")",
";",
"ob_end_clean",
"(",
")",
";",
"/**\n\t\t* Filter the contents of the Dashboard Publish page.\n\t\t *\n\t\t * @since 4.3.0\n\t\t */",
"echo",
"apply_filters",
"(",
"'pb_publish_page'",
",",
"/**\n\t\t\t * Filter the contents of the Dashboard Publish page.\n\t\t\t *\n\t\t\t * @since 3.9.3\n\t\t\t * @deprecated 4.3.0 Use pb_publish_page instead.\n\t\t\t *\n\t\t\t * @param string $output\n\t\t\t */",
"apply_filters",
"(",
"'pressbooks_publish_page'",
",",
"$",
"output",
")",
")",
";",
"}"
] | Display the publish options page description. | [
"Display",
"the",
"publish",
"options",
"page",
"description",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/admin/class-publishoptions.php#L122-L161 |
pressbooks/pressbooks | inc/admin/class-publishoptions.php | PublishOptions.renderAmazonField | function renderAmazonField() {
$this->renderField(
[
'id' => 'amazon',
'name' => $this->getSlug(),
'option' => 'amazon',
'value' => ( isset( $this->options['amazon'] ) ) ? $this->options['amazon'] : '',
'type' => 'url',
'class' => 'regular-text code',
]
);
} | php | function renderAmazonField() {
$this->renderField(
[
'id' => 'amazon',
'name' => $this->getSlug(),
'option' => 'amazon',
'value' => ( isset( $this->options['amazon'] ) ) ? $this->options['amazon'] : '',
'type' => 'url',
'class' => 'regular-text code',
]
);
} | [
"function",
"renderAmazonField",
"(",
")",
"{",
"$",
"this",
"->",
"renderField",
"(",
"[",
"'id'",
"=>",
"'amazon'",
",",
"'name'",
"=>",
"$",
"this",
"->",
"getSlug",
"(",
")",
",",
"'option'",
"=>",
"'amazon'",
",",
"'value'",
"=>",
"(",
"isset",
"(",
"$",
"this",
"->",
"options",
"[",
"'amazon'",
"]",
")",
")",
"?",
"$",
"this",
"->",
"options",
"[",
"'amazon'",
"]",
":",
"''",
",",
"'type'",
"=>",
"'url'",
",",
"'class'",
"=>",
"'regular-text code'",
",",
"]",
")",
";",
"}"
] | Render the amazon field. | [
"Render",
"the",
"amazon",
"field",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/admin/class-publishoptions.php#L190-L201 |
pressbooks/pressbooks | inc/admin/class-publishoptions.php | PublishOptions.renderOReillyField | function renderOReillyField() {
$this->renderField(
[
'id' => 'oreilly',
'name' => $this->getSlug(),
'option' => 'oreilly',
'value' => ( isset( $this->options['oreilly'] ) ) ? $this->options['oreilly'] : '',
'type' => 'url',
'class' => 'regular-text code',
]
);
} | php | function renderOReillyField() {
$this->renderField(
[
'id' => 'oreilly',
'name' => $this->getSlug(),
'option' => 'oreilly',
'value' => ( isset( $this->options['oreilly'] ) ) ? $this->options['oreilly'] : '',
'type' => 'url',
'class' => 'regular-text code',
]
);
} | [
"function",
"renderOReillyField",
"(",
")",
"{",
"$",
"this",
"->",
"renderField",
"(",
"[",
"'id'",
"=>",
"'oreilly'",
",",
"'name'",
"=>",
"$",
"this",
"->",
"getSlug",
"(",
")",
",",
"'option'",
"=>",
"'oreilly'",
",",
"'value'",
"=>",
"(",
"isset",
"(",
"$",
"this",
"->",
"options",
"[",
"'oreilly'",
"]",
")",
")",
"?",
"$",
"this",
"->",
"options",
"[",
"'oreilly'",
"]",
":",
"''",
",",
"'type'",
"=>",
"'url'",
",",
"'class'",
"=>",
"'regular-text code'",
",",
"]",
")",
";",
"}"
] | Render the oreilly field. | [
"Render",
"the",
"oreilly",
"field",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/admin/class-publishoptions.php#L206-L217 |
pressbooks/pressbooks | inc/admin/class-publishoptions.php | PublishOptions.renderBarnesAndNobleField | function renderBarnesAndNobleField() {
$this->renderField(
[
'id' => 'barnesandnoble',
'name' => $this->getSlug(),
'option' => 'barnesandnoble',
'value' => ( isset( $this->options['barnesandnoble'] ) ) ? $this->options['barnesandnoble'] : '',
'type' => 'url',
'class' => 'regular-text code',
]
);
} | php | function renderBarnesAndNobleField() {
$this->renderField(
[
'id' => 'barnesandnoble',
'name' => $this->getSlug(),
'option' => 'barnesandnoble',
'value' => ( isset( $this->options['barnesandnoble'] ) ) ? $this->options['barnesandnoble'] : '',
'type' => 'url',
'class' => 'regular-text code',
]
);
} | [
"function",
"renderBarnesAndNobleField",
"(",
")",
"{",
"$",
"this",
"->",
"renderField",
"(",
"[",
"'id'",
"=>",
"'barnesandnoble'",
",",
"'name'",
"=>",
"$",
"this",
"->",
"getSlug",
"(",
")",
",",
"'option'",
"=>",
"'barnesandnoble'",
",",
"'value'",
"=>",
"(",
"isset",
"(",
"$",
"this",
"->",
"options",
"[",
"'barnesandnoble'",
"]",
")",
")",
"?",
"$",
"this",
"->",
"options",
"[",
"'barnesandnoble'",
"]",
":",
"''",
",",
"'type'",
"=>",
"'url'",
",",
"'class'",
"=>",
"'regular-text code'",
",",
"]",
")",
";",
"}"
] | Render the barnesandnoble field. | [
"Render",
"the",
"barnesandnoble",
"field",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/admin/class-publishoptions.php#L222-L233 |
pressbooks/pressbooks | inc/admin/class-publishoptions.php | PublishOptions.renderKoboField | function renderKoboField() {
$this->renderField(
[
'id' => 'kobo',
'name' => $this->getSlug(),
'option' => 'kobo',
'value' => ( isset( $this->options['kobo'] ) ) ? $this->options['kobo'] : '',
'type' => 'url',
'class' => 'regular-text code',
]
);
} | php | function renderKoboField() {
$this->renderField(
[
'id' => 'kobo',
'name' => $this->getSlug(),
'option' => 'kobo',
'value' => ( isset( $this->options['kobo'] ) ) ? $this->options['kobo'] : '',
'type' => 'url',
'class' => 'regular-text code',
]
);
} | [
"function",
"renderKoboField",
"(",
")",
"{",
"$",
"this",
"->",
"renderField",
"(",
"[",
"'id'",
"=>",
"'kobo'",
",",
"'name'",
"=>",
"$",
"this",
"->",
"getSlug",
"(",
")",
",",
"'option'",
"=>",
"'kobo'",
",",
"'value'",
"=>",
"(",
"isset",
"(",
"$",
"this",
"->",
"options",
"[",
"'kobo'",
"]",
")",
")",
"?",
"$",
"this",
"->",
"options",
"[",
"'kobo'",
"]",
":",
"''",
",",
"'type'",
"=>",
"'url'",
",",
"'class'",
"=>",
"'regular-text code'",
",",
"]",
")",
";",
"}"
] | Render the barnesandnoble field. | [
"Render",
"the",
"barnesandnoble",
"field",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/admin/class-publishoptions.php#L238-L249 |
pressbooks/pressbooks | inc/admin/class-publishoptions.php | PublishOptions.renderiBooksField | function renderiBooksField() {
$this->renderField(
[
'id' => 'ibooks',
'name' => $this->getSlug(),
'option' => 'ibooks',
'value' => ( isset( $this->options['ibooks'] ) ) ? $this->options['ibooks'] : '',
'type' => 'url',
'class' => 'regular-text code',
]
);
} | php | function renderiBooksField() {
$this->renderField(
[
'id' => 'ibooks',
'name' => $this->getSlug(),
'option' => 'ibooks',
'value' => ( isset( $this->options['ibooks'] ) ) ? $this->options['ibooks'] : '',
'type' => 'url',
'class' => 'regular-text code',
]
);
} | [
"function",
"renderiBooksField",
"(",
")",
"{",
"$",
"this",
"->",
"renderField",
"(",
"[",
"'id'",
"=>",
"'ibooks'",
",",
"'name'",
"=>",
"$",
"this",
"->",
"getSlug",
"(",
")",
",",
"'option'",
"=>",
"'ibooks'",
",",
"'value'",
"=>",
"(",
"isset",
"(",
"$",
"this",
"->",
"options",
"[",
"'ibooks'",
"]",
")",
")",
"?",
"$",
"this",
"->",
"options",
"[",
"'ibooks'",
"]",
":",
"''",
",",
"'type'",
"=>",
"'url'",
",",
"'class'",
"=>",
"'regular-text code'",
",",
"]",
")",
";",
"}"
] | Render the ibooks field. | [
"Render",
"the",
"ibooks",
"field",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/admin/class-publishoptions.php#L254-L265 |
pressbooks/pressbooks | inc/admin/class-publishoptions.php | PublishOptions.renderOtherServiceField | function renderOtherServiceField() {
$this->renderField(
[
'id' => 'otherservice',
'name' => $this->getSlug(),
'option' => 'otherservice',
'value' => ( isset( $this->options['otherservice'] ) ) ? $this->options['otherservice'] : '',
'type' => 'url',
'class' => 'regular-text code',
]
);
} | php | function renderOtherServiceField() {
$this->renderField(
[
'id' => 'otherservice',
'name' => $this->getSlug(),
'option' => 'otherservice',
'value' => ( isset( $this->options['otherservice'] ) ) ? $this->options['otherservice'] : '',
'type' => 'url',
'class' => 'regular-text code',
]
);
} | [
"function",
"renderOtherServiceField",
"(",
")",
"{",
"$",
"this",
"->",
"renderField",
"(",
"[",
"'id'",
"=>",
"'otherservice'",
",",
"'name'",
"=>",
"$",
"this",
"->",
"getSlug",
"(",
")",
",",
"'option'",
"=>",
"'otherservice'",
",",
"'value'",
"=>",
"(",
"isset",
"(",
"$",
"this",
"->",
"options",
"[",
"'otherservice'",
"]",
")",
")",
"?",
"$",
"this",
"->",
"options",
"[",
"'otherservice'",
"]",
":",
"''",
",",
"'type'",
"=>",
"'url'",
",",
"'class'",
"=>",
"'regular-text code'",
",",
"]",
")",
";",
"}"
] | Render the ibooks field. | [
"Render",
"the",
"ibooks",
"field",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/admin/class-publishoptions.php#L270-L281 |
pressbooks/pressbooks | inc/admin/class-exportoptions.php | ExportOptions.init | function init() {
$_option = $this->getSlug();
$_page = $_option;
$_section = $this->getSlug() . '_section';
add_settings_section(
$_section,
'',
[ $this, 'display' ],
$_page
);
add_settings_field(
'email_validation_logs',
__( 'Email Validation Logs', 'pressbooks' ),
[ $this, 'renderEmailValidationLogsField' ],
$_page,
$_section,
[
'0' => __( 'No. Ignore validation errors.', 'pressbooks' ),
'1' => __( 'Yes.', 'pressbooks' ) . ' ' . __( 'Email me validation error logs on export.', 'pressbooks' ),
]
);
if ( ! \Pressbooks\CustomCss::isCustomCss() ) {
add_settings_field(
'theme_lock',
__( 'Lock Theme', 'pressbooks' ),
[ $this, 'renderThemeLockField' ],
$_page,
$_section,
[
__( 'Lock your theme at its current version.', 'pressbooks' ),
]
);
}
register_setting(
$_page,
$_option,
[ $this, 'sanitize' ]
);
} | php | function init() {
$_option = $this->getSlug();
$_page = $_option;
$_section = $this->getSlug() . '_section';
add_settings_section(
$_section,
'',
[ $this, 'display' ],
$_page
);
add_settings_field(
'email_validation_logs',
__( 'Email Validation Logs', 'pressbooks' ),
[ $this, 'renderEmailValidationLogsField' ],
$_page,
$_section,
[
'0' => __( 'No. Ignore validation errors.', 'pressbooks' ),
'1' => __( 'Yes.', 'pressbooks' ) . ' ' . __( 'Email me validation error logs on export.', 'pressbooks' ),
]
);
if ( ! \Pressbooks\CustomCss::isCustomCss() ) {
add_settings_field(
'theme_lock',
__( 'Lock Theme', 'pressbooks' ),
[ $this, 'renderThemeLockField' ],
$_page,
$_section,
[
__( 'Lock your theme at its current version.', 'pressbooks' ),
]
);
}
register_setting(
$_page,
$_option,
[ $this, 'sanitize' ]
);
} | [
"function",
"init",
"(",
")",
"{",
"$",
"_option",
"=",
"$",
"this",
"->",
"getSlug",
"(",
")",
";",
"$",
"_page",
"=",
"$",
"_option",
";",
"$",
"_section",
"=",
"$",
"this",
"->",
"getSlug",
"(",
")",
".",
"'_section'",
";",
"add_settings_section",
"(",
"$",
"_section",
",",
"''",
",",
"[",
"$",
"this",
",",
"'display'",
"]",
",",
"$",
"_page",
")",
";",
"add_settings_field",
"(",
"'email_validation_logs'",
",",
"__",
"(",
"'Email Validation Logs'",
",",
"'pressbooks'",
")",
",",
"[",
"$",
"this",
",",
"'renderEmailValidationLogsField'",
"]",
",",
"$",
"_page",
",",
"$",
"_section",
",",
"[",
"'0'",
"=>",
"__",
"(",
"'No. Ignore validation errors.'",
",",
"'pressbooks'",
")",
",",
"'1'",
"=>",
"__",
"(",
"'Yes.'",
",",
"'pressbooks'",
")",
".",
"' '",
".",
"__",
"(",
"'Email me validation error logs on export.'",
",",
"'pressbooks'",
")",
",",
"]",
")",
";",
"if",
"(",
"!",
"\\",
"Pressbooks",
"\\",
"CustomCss",
"::",
"isCustomCss",
"(",
")",
")",
"{",
"add_settings_field",
"(",
"'theme_lock'",
",",
"__",
"(",
"'Lock Theme'",
",",
"'pressbooks'",
")",
",",
"[",
"$",
"this",
",",
"'renderThemeLockField'",
"]",
",",
"$",
"_page",
",",
"$",
"_section",
",",
"[",
"__",
"(",
"'Lock your theme at its current version.'",
",",
"'pressbooks'",
")",
",",
"]",
")",
";",
"}",
"register_setting",
"(",
"$",
"_page",
",",
"$",
"_option",
",",
"[",
"$",
"this",
",",
"'sanitize'",
"]",
")",
";",
"}"
] | Configure the export options page using the settings API. | [
"Configure",
"the",
"export",
"options",
"page",
"using",
"the",
"settings",
"API",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/admin/class-exportoptions.php#L52-L94 |
pressbooks/pressbooks | inc/admin/class-exportoptions.php | ExportOptions.renderEmailValidationLogsField | function renderEmailValidationLogsField( $args ) {
$this->renderRadioButtons(
[
'id' => 'email_validation_logs',
'name' => $this->getSlug(),
'option' => 'email_validation_logs',
'value' => ( isset( $this->options['email_validation_logs'] ) ) ? $this->options['email_validation_logs'] : '',
'choices' => $args,
]
);
} | php | function renderEmailValidationLogsField( $args ) {
$this->renderRadioButtons(
[
'id' => 'email_validation_logs',
'name' => $this->getSlug(),
'option' => 'email_validation_logs',
'value' => ( isset( $this->options['email_validation_logs'] ) ) ? $this->options['email_validation_logs'] : '',
'choices' => $args,
]
);
} | [
"function",
"renderEmailValidationLogsField",
"(",
"$",
"args",
")",
"{",
"$",
"this",
"->",
"renderRadioButtons",
"(",
"[",
"'id'",
"=>",
"'email_validation_logs'",
",",
"'name'",
"=>",
"$",
"this",
"->",
"getSlug",
"(",
")",
",",
"'option'",
"=>",
"'email_validation_logs'",
",",
"'value'",
"=>",
"(",
"isset",
"(",
"$",
"this",
"->",
"options",
"[",
"'email_validation_logs'",
"]",
")",
")",
"?",
"$",
"this",
"->",
"options",
"[",
"'email_validation_logs'",
"]",
":",
"''",
",",
"'choices'",
"=>",
"$",
"args",
",",
"]",
")",
";",
"}"
] | Render the email_validation_logs radio buttons.
@param array $args | [
"Render",
"the",
"email_validation_logs",
"radio",
"buttons",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/admin/class-exportoptions.php#L141-L151 |
pressbooks/pressbooks | inc/admin/class-exportoptions.php | ExportOptions.renderThemeLockField | function renderThemeLockField( $args ) {
$this->renderCheckbox(
[
'id' => 'theme_lock',
'name' => $this->getSlug(),
'option' => 'theme_lock',
'value' => ( isset( $this->options['theme_lock'] ) ) ? $this->options['theme_lock'] : '',
'label' => $args[0],
'description' => __( 'This will prevent any changes to your book’s appearance and page count when themes are updated.', 'pressbooks' ),
]
);
} | php | function renderThemeLockField( $args ) {
$this->renderCheckbox(
[
'id' => 'theme_lock',
'name' => $this->getSlug(),
'option' => 'theme_lock',
'value' => ( isset( $this->options['theme_lock'] ) ) ? $this->options['theme_lock'] : '',
'label' => $args[0],
'description' => __( 'This will prevent any changes to your book’s appearance and page count when themes are updated.', 'pressbooks' ),
]
);
} | [
"function",
"renderThemeLockField",
"(",
"$",
"args",
")",
"{",
"$",
"this",
"->",
"renderCheckbox",
"(",
"[",
"'id'",
"=>",
"'theme_lock'",
",",
"'name'",
"=>",
"$",
"this",
"->",
"getSlug",
"(",
")",
",",
"'option'",
"=>",
"'theme_lock'",
",",
"'value'",
"=>",
"(",
"isset",
"(",
"$",
"this",
"->",
"options",
"[",
"'theme_lock'",
"]",
")",
")",
"?",
"$",
"this",
"->",
"options",
"[",
"'theme_lock'",
"]",
":",
"''",
",",
"'label'",
"=>",
"$",
"args",
"[",
"0",
"]",
",",
"'description'",
"=>",
"__",
"(",
"'This will prevent any changes to your book’s appearance and page count when themes are updated.'",
",",
"'pressbooks'",
")",
",",
"]",
")",
";",
"}"
] | Render the lock_theme checkbox.
@param array $args | [
"Render",
"the",
"lock_theme",
"checkbox",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/admin/class-exportoptions.php#L158-L169 |
pressbooks/pressbooks | inc/class-htmlawed.php | HtmLawed.filter | static function filter( $html, array $config = null, $spec = null ) {
return \Htmlawed::filter( $html, $config, $spec );
} | php | static function filter( $html, array $config = null, $spec = null ) {
return \Htmlawed::filter( $html, $config, $spec );
} | [
"static",
"function",
"filter",
"(",
"$",
"html",
",",
"array",
"$",
"config",
"=",
"null",
",",
"$",
"spec",
"=",
"null",
")",
"{",
"return",
"\\",
"Htmlawed",
"::",
"filter",
"(",
"$",
"html",
",",
"$",
"config",
",",
"$",
"spec",
")",
";",
"}"
] | Wrapper for htmLawed() function.
@param string $html
@param int|array $config
@param array|string $spec
@return string | [
"Wrapper",
"for",
"htmLawed",
"()",
"function",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-htmlawed.php#L23-L25 |
pressbooks/pressbooks | inc/class-book.php | Book.getBookInformation | static function getBookInformation( $id = null ) {
if ( ! empty( $id ) && is_int( $id ) ) {
$blog_id = $id;
switch_to_blog( $blog_id );
} else {
global $blog_id;
}
// -----------------------------------------------------------------------------
// Is cached?
// -----------------------------------------------------------------------------
$cache_id = "book-inf-$blog_id";
if ( static::useCache() ) {
$book_information = wp_cache_get( $cache_id, 'pb' );
if ( $book_information ) {
return $book_information;
}
}
// ----------------------------------------------------------------------------
// Book Information
// ----------------------------------------------------------------------------
$book_information = [];
$meta = new Metadata();
$meta_post = $meta->getMetaPost();
if ( $meta_post ) {
// Contributors
$contributors = new Contributors();
foreach ( $contributors->getAll( $meta_post->ID ) as $key => $val ) {
$book_information[ $key ] = $val;
};
// Post Meta
$expected_array = [ 'pb_keywords_tags', 'pb_additional_subjects', 'pb_bisac_subject' ];
$expected_the_content = [ 'pb_custom_copyright', 'pb_about_unlimited' ];
$expected_url = [ 'pb_cover_image' ];
foreach ( get_post_meta( $meta_post->ID ) as $key => $val ) {
// Skip anything not prefixed with pb_
if ( ! preg_match( '/^pb_/', $key ) ) {
continue;
}
// Skip contributor meta (already done, look up)
if ( $contributors->isValid( $key ) || $contributors->isDeprecated( $key ) ) {
continue;
}
// We only care about strings
if ( is_array( $val ) ) {
if ( false !== in_array( $key, $expected_array, true ) ) {
$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, true ) ) {
$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, true ) ) {
$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_authors'] = isset( $author->display_name ) ? $author->display_name : '';
$book_information['pb_cover_image'] = \Pressbooks\Image\default_cover_url();
}
// -----------------------------------------------------------------------------
// Cache & Return
// -----------------------------------------------------------------------------
if ( static::useCache() ) {
wp_cache_set( $cache_id, $book_information, 'pb', DAY_IN_SECONDS );
}
if ( ! empty( $id ) && is_int( $id ) ) {
restore_current_blog();
}
return $book_information;
} | php | static function getBookInformation( $id = null ) {
if ( ! empty( $id ) && is_int( $id ) ) {
$blog_id = $id;
switch_to_blog( $blog_id );
} else {
global $blog_id;
}
// -----------------------------------------------------------------------------
// Is cached?
// -----------------------------------------------------------------------------
$cache_id = "book-inf-$blog_id";
if ( static::useCache() ) {
$book_information = wp_cache_get( $cache_id, 'pb' );
if ( $book_information ) {
return $book_information;
}
}
// ----------------------------------------------------------------------------
// Book Information
// ----------------------------------------------------------------------------
$book_information = [];
$meta = new Metadata();
$meta_post = $meta->getMetaPost();
if ( $meta_post ) {
// Contributors
$contributors = new Contributors();
foreach ( $contributors->getAll( $meta_post->ID ) as $key => $val ) {
$book_information[ $key ] = $val;
};
// Post Meta
$expected_array = [ 'pb_keywords_tags', 'pb_additional_subjects', 'pb_bisac_subject' ];
$expected_the_content = [ 'pb_custom_copyright', 'pb_about_unlimited' ];
$expected_url = [ 'pb_cover_image' ];
foreach ( get_post_meta( $meta_post->ID ) as $key => $val ) {
// Skip anything not prefixed with pb_
if ( ! preg_match( '/^pb_/', $key ) ) {
continue;
}
// Skip contributor meta (already done, look up)
if ( $contributors->isValid( $key ) || $contributors->isDeprecated( $key ) ) {
continue;
}
// We only care about strings
if ( is_array( $val ) ) {
if ( false !== in_array( $key, $expected_array, true ) ) {
$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, true ) ) {
$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, true ) ) {
$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_authors'] = isset( $author->display_name ) ? $author->display_name : '';
$book_information['pb_cover_image'] = \Pressbooks\Image\default_cover_url();
}
// -----------------------------------------------------------------------------
// Cache & Return
// -----------------------------------------------------------------------------
if ( static::useCache() ) {
wp_cache_set( $cache_id, $book_information, 'pb', DAY_IN_SECONDS );
}
if ( ! empty( $id ) && is_int( $id ) ) {
restore_current_blog();
}
return $book_information;
} | [
"static",
"function",
"getBookInformation",
"(",
"$",
"id",
"=",
"null",
")",
"{",
"if",
"(",
"!",
"empty",
"(",
"$",
"id",
")",
"&&",
"is_int",
"(",
"$",
"id",
")",
")",
"{",
"$",
"blog_id",
"=",
"$",
"id",
";",
"switch_to_blog",
"(",
"$",
"blog_id",
")",
";",
"}",
"else",
"{",
"global",
"$",
"blog_id",
";",
"}",
"// -----------------------------------------------------------------------------",
"// Is cached?",
"// -----------------------------------------------------------------------------",
"$",
"cache_id",
"=",
"\"book-inf-$blog_id\"",
";",
"if",
"(",
"static",
"::",
"useCache",
"(",
")",
")",
"{",
"$",
"book_information",
"=",
"wp_cache_get",
"(",
"$",
"cache_id",
",",
"'pb'",
")",
";",
"if",
"(",
"$",
"book_information",
")",
"{",
"return",
"$",
"book_information",
";",
"}",
"}",
"// ----------------------------------------------------------------------------",
"// Book Information",
"// ----------------------------------------------------------------------------",
"$",
"book_information",
"=",
"[",
"]",
";",
"$",
"meta",
"=",
"new",
"Metadata",
"(",
")",
";",
"$",
"meta_post",
"=",
"$",
"meta",
"->",
"getMetaPost",
"(",
")",
";",
"if",
"(",
"$",
"meta_post",
")",
"{",
"// Contributors",
"$",
"contributors",
"=",
"new",
"Contributors",
"(",
")",
";",
"foreach",
"(",
"$",
"contributors",
"->",
"getAll",
"(",
"$",
"meta_post",
"->",
"ID",
")",
"as",
"$",
"key",
"=>",
"$",
"val",
")",
"{",
"$",
"book_information",
"[",
"$",
"key",
"]",
"=",
"$",
"val",
";",
"}",
";",
"// Post Meta",
"$",
"expected_array",
"=",
"[",
"'pb_keywords_tags'",
",",
"'pb_additional_subjects'",
",",
"'pb_bisac_subject'",
"]",
";",
"$",
"expected_the_content",
"=",
"[",
"'pb_custom_copyright'",
",",
"'pb_about_unlimited'",
"]",
";",
"$",
"expected_url",
"=",
"[",
"'pb_cover_image'",
"]",
";",
"foreach",
"(",
"get_post_meta",
"(",
"$",
"meta_post",
"->",
"ID",
")",
"as",
"$",
"key",
"=>",
"$",
"val",
")",
"{",
"// Skip anything not prefixed with pb_",
"if",
"(",
"!",
"preg_match",
"(",
"'/^pb_/'",
",",
"$",
"key",
")",
")",
"{",
"continue",
";",
"}",
"// Skip contributor meta (already done, look up)",
"if",
"(",
"$",
"contributors",
"->",
"isValid",
"(",
"$",
"key",
")",
"||",
"$",
"contributors",
"->",
"isDeprecated",
"(",
"$",
"key",
")",
")",
"{",
"continue",
";",
"}",
"// We only care about strings",
"if",
"(",
"is_array",
"(",
"$",
"val",
")",
")",
"{",
"if",
"(",
"false",
"!==",
"in_array",
"(",
"$",
"key",
",",
"$",
"expected_array",
",",
"true",
")",
")",
"{",
"$",
"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",
",",
"true",
")",
")",
"{",
"$",
"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",
",",
"true",
")",
")",
"{",
"$",
"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_authors'",
"]",
"=",
"isset",
"(",
"$",
"author",
"->",
"display_name",
")",
"?",
"$",
"author",
"->",
"display_name",
":",
"''",
";",
"$",
"book_information",
"[",
"'pb_cover_image'",
"]",
"=",
"\\",
"Pressbooks",
"\\",
"Image",
"\\",
"default_cover_url",
"(",
")",
";",
"}",
"// -----------------------------------------------------------------------------",
"// Cache & Return",
"// -----------------------------------------------------------------------------",
"if",
"(",
"static",
"::",
"useCache",
"(",
")",
")",
"{",
"wp_cache_set",
"(",
"$",
"cache_id",
",",
"$",
"book_information",
",",
"'pb'",
",",
"DAY_IN_SECONDS",
")",
";",
"}",
"if",
"(",
"!",
"empty",
"(",
"$",
"id",
")",
"&&",
"is_int",
"(",
"$",
"id",
")",
")",
"{",
"restore_current_blog",
"(",
")",
";",
"}",
"return",
"$",
"book_information",
";",
"}"
] | Returns book information in a useful, string only, format. Data is converted to HTML.
@param int $id The book ID.
@return array | [
"Returns",
"book",
"information",
"in",
"a",
"useful",
"string",
"only",
"format",
".",
"Data",
"is",
"converted",
"to",
"HTML",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-book.php#L82-L192 |
pressbooks/pressbooks | inc/class-book.php | Book.getBookStructure | static function getBookStructure( $id = null ) {
if ( ! empty( $id ) && is_int( $id ) ) {
$blog_id = $id;
switch_to_blog( $id );
} else {
global $blog_id;
}
// -----------------------------------------------------------------------------
// Is cached?
// -----------------------------------------------------------------------------
$cache_id = "book-str-$blog_id";
if ( static::useCache() ) {
$book_structure = wp_cache_get( $cache_id, 'pb' );
if ( $book_structure ) {
return $book_structure;
}
}
// -----------------------------------------------------------------------------
// Query our custom post types, keep minimal data in $book_structure
// -----------------------------------------------------------------------------
$post_ids_to_export = static::getPostsIdsToExport();
$custom_types = [
'front-matter',
'part',
'chapter',
'back-matter',
];
$book_structure = [];
foreach ( $custom_types as $type ) {
$book_structure[ $type ] = [];
}
$q = new \WP_Query();
$results = $q->query(
[
'post_type' => $custom_types,
'posts_per_page' => -1, // @codingStandardsIgnoreLine
'post_status' => 'any',
'orderby' => 'menu_order',
'order' => 'ASC',
'no_found_rows' => true,
'cache_results' => true,
]
);
/** @var \WP_Post $post */
foreach ( $results as $post ) {
// Fix empty slugs
$post_name = empty( trim( $post->post_name ) ) ? uniqid( 'slug-' ) : $post->post_name;
$book_structure[ $post->post_type ][] = [
'ID' => $post->ID,
'post_title' => $post->post_title,
'post_name' => $post_name,
'post_author' => (int) $post->post_author,
'comment_count' => (int) $post->comment_count,
'menu_order' => $post->menu_order,
'post_status' => $post->post_status,
'post_parent' => $post->post_parent,
'export' => ( isset( $post_ids_to_export[ $post->ID ] ) && 'on' === $post_ids_to_export[ $post->ID ] ) ? true : false,
'has_post_content' => ! empty( trim( $post->post_content ) ),
'word_count' => \Pressbooks\Utility\word_count( $post->post_content ),
];
}
// -----------------------------------------------------------------------------
// Add Chapters to Parts
// -----------------------------------------------------------------------------
foreach ( $book_structure['part'] as $i => $part ) {
// There's no `pb_export` for parts. We instead have `pb_part_invisible` and it doesn't mean the same thing. Force to true.
$book_structure['part'][ $i ]['export'] = true;
$book_structure['part'][ $i ]['chapters'] = [];
}
foreach ( $book_structure['chapter'] as $i => $chapter ) {
foreach ( $book_structure['part'] as $j => $part ) {
if ( $part['ID'] === $chapter['post_parent'] ) {
$book_structure['part'][ $j ]['chapters'][] = $chapter;
unset( $book_structure['chapter'][ $i ] );
continue 2;
}
}
}
/* Track unexpected orphans, unset() chapter from $book_structure and $types */
if ( count( $book_structure['chapter'] ) ) {
$book_structure['__orphans'] = $book_structure['chapter'];
}
unset( $book_structure['chapter'] );
$custom_types = array_diff( $custom_types, [ 'chapter' ] );
// -----------------------------------------------------------------------------
// Create __order arrays, remove post_parent
// -----------------------------------------------------------------------------
$book_structure['__order'] = [];
foreach ( $custom_types as $type ) {
foreach ( $book_structure[ $type ] as $i => $struct ) {
unset( $book_structure[ $type ][ $i ]['post_parent'] );
if ( 'part' !== $type ) {
$book_structure['__order'][ $struct['ID'] ] = [
'export' => $struct['export'],
'post_status' => $struct['post_status'],
'post_name' => $struct['post_name'],
'post_type' => $type,
];
} else {
if ( $struct['has_post_content'] && get_post_meta( $struct['ID'], 'pb_part_invisible', true ) !== 'on' ) {
$book_structure['__order'][ $struct['ID'] ] = [
'export' => $struct['export'],
'post_status' => $struct['post_status'],
'post_name' => $struct['post_name'],
'post_type' => 'part',
];
}
foreach ( $struct['chapters'] as $j => $chapter ) {
unset( $book_structure[ $type ][ $i ]['chapters'][ $j ]['post_parent'] );
if ( $struct['has_post_content'] && get_post_meta( $struct['ID'], 'pb_part_invisible', true ) !== 'on' ) {
$book_structure['__order'][ $struct['ID'] ] = [
'export' => $struct['export'],
'post_status' => $struct['post_status'],
'post_name' => $struct['post_name'],
'post_type' => 'part',
];
}
$book_structure['__order'][ $chapter['ID'] ] = [
'export' => $chapter['export'],
'post_status' => $chapter['post_status'],
'post_name' => $chapter['post_name'],
'post_type' => 'chapter',
];
}
}
}
}
static::$__order = $book_structure['__order'];
// -----------------------------------------------------------------------------
// Cache & Return
// -----------------------------------------------------------------------------
if ( static::useCache() ) {
wp_cache_set( $cache_id, $book_structure, 'pb', DAY_IN_SECONDS );
}
if ( ! empty( $id ) && is_int( $id ) ) {
restore_current_blog();
}
return $book_structure;
} | php | static function getBookStructure( $id = null ) {
if ( ! empty( $id ) && is_int( $id ) ) {
$blog_id = $id;
switch_to_blog( $id );
} else {
global $blog_id;
}
// -----------------------------------------------------------------------------
// Is cached?
// -----------------------------------------------------------------------------
$cache_id = "book-str-$blog_id";
if ( static::useCache() ) {
$book_structure = wp_cache_get( $cache_id, 'pb' );
if ( $book_structure ) {
return $book_structure;
}
}
// -----------------------------------------------------------------------------
// Query our custom post types, keep minimal data in $book_structure
// -----------------------------------------------------------------------------
$post_ids_to_export = static::getPostsIdsToExport();
$custom_types = [
'front-matter',
'part',
'chapter',
'back-matter',
];
$book_structure = [];
foreach ( $custom_types as $type ) {
$book_structure[ $type ] = [];
}
$q = new \WP_Query();
$results = $q->query(
[
'post_type' => $custom_types,
'posts_per_page' => -1, // @codingStandardsIgnoreLine
'post_status' => 'any',
'orderby' => 'menu_order',
'order' => 'ASC',
'no_found_rows' => true,
'cache_results' => true,
]
);
/** @var \WP_Post $post */
foreach ( $results as $post ) {
// Fix empty slugs
$post_name = empty( trim( $post->post_name ) ) ? uniqid( 'slug-' ) : $post->post_name;
$book_structure[ $post->post_type ][] = [
'ID' => $post->ID,
'post_title' => $post->post_title,
'post_name' => $post_name,
'post_author' => (int) $post->post_author,
'comment_count' => (int) $post->comment_count,
'menu_order' => $post->menu_order,
'post_status' => $post->post_status,
'post_parent' => $post->post_parent,
'export' => ( isset( $post_ids_to_export[ $post->ID ] ) && 'on' === $post_ids_to_export[ $post->ID ] ) ? true : false,
'has_post_content' => ! empty( trim( $post->post_content ) ),
'word_count' => \Pressbooks\Utility\word_count( $post->post_content ),
];
}
// -----------------------------------------------------------------------------
// Add Chapters to Parts
// -----------------------------------------------------------------------------
foreach ( $book_structure['part'] as $i => $part ) {
// There's no `pb_export` for parts. We instead have `pb_part_invisible` and it doesn't mean the same thing. Force to true.
$book_structure['part'][ $i ]['export'] = true;
$book_structure['part'][ $i ]['chapters'] = [];
}
foreach ( $book_structure['chapter'] as $i => $chapter ) {
foreach ( $book_structure['part'] as $j => $part ) {
if ( $part['ID'] === $chapter['post_parent'] ) {
$book_structure['part'][ $j ]['chapters'][] = $chapter;
unset( $book_structure['chapter'][ $i ] );
continue 2;
}
}
}
/* Track unexpected orphans, unset() chapter from $book_structure and $types */
if ( count( $book_structure['chapter'] ) ) {
$book_structure['__orphans'] = $book_structure['chapter'];
}
unset( $book_structure['chapter'] );
$custom_types = array_diff( $custom_types, [ 'chapter' ] );
// -----------------------------------------------------------------------------
// Create __order arrays, remove post_parent
// -----------------------------------------------------------------------------
$book_structure['__order'] = [];
foreach ( $custom_types as $type ) {
foreach ( $book_structure[ $type ] as $i => $struct ) {
unset( $book_structure[ $type ][ $i ]['post_parent'] );
if ( 'part' !== $type ) {
$book_structure['__order'][ $struct['ID'] ] = [
'export' => $struct['export'],
'post_status' => $struct['post_status'],
'post_name' => $struct['post_name'],
'post_type' => $type,
];
} else {
if ( $struct['has_post_content'] && get_post_meta( $struct['ID'], 'pb_part_invisible', true ) !== 'on' ) {
$book_structure['__order'][ $struct['ID'] ] = [
'export' => $struct['export'],
'post_status' => $struct['post_status'],
'post_name' => $struct['post_name'],
'post_type' => 'part',
];
}
foreach ( $struct['chapters'] as $j => $chapter ) {
unset( $book_structure[ $type ][ $i ]['chapters'][ $j ]['post_parent'] );
if ( $struct['has_post_content'] && get_post_meta( $struct['ID'], 'pb_part_invisible', true ) !== 'on' ) {
$book_structure['__order'][ $struct['ID'] ] = [
'export' => $struct['export'],
'post_status' => $struct['post_status'],
'post_name' => $struct['post_name'],
'post_type' => 'part',
];
}
$book_structure['__order'][ $chapter['ID'] ] = [
'export' => $chapter['export'],
'post_status' => $chapter['post_status'],
'post_name' => $chapter['post_name'],
'post_type' => 'chapter',
];
}
}
}
}
static::$__order = $book_structure['__order'];
// -----------------------------------------------------------------------------
// Cache & Return
// -----------------------------------------------------------------------------
if ( static::useCache() ) {
wp_cache_set( $cache_id, $book_structure, 'pb', DAY_IN_SECONDS );
}
if ( ! empty( $id ) && is_int( $id ) ) {
restore_current_blog();
}
return $book_structure;
} | [
"static",
"function",
"getBookStructure",
"(",
"$",
"id",
"=",
"null",
")",
"{",
"if",
"(",
"!",
"empty",
"(",
"$",
"id",
")",
"&&",
"is_int",
"(",
"$",
"id",
")",
")",
"{",
"$",
"blog_id",
"=",
"$",
"id",
";",
"switch_to_blog",
"(",
"$",
"id",
")",
";",
"}",
"else",
"{",
"global",
"$",
"blog_id",
";",
"}",
"// -----------------------------------------------------------------------------",
"// Is cached?",
"// -----------------------------------------------------------------------------",
"$",
"cache_id",
"=",
"\"book-str-$blog_id\"",
";",
"if",
"(",
"static",
"::",
"useCache",
"(",
")",
")",
"{",
"$",
"book_structure",
"=",
"wp_cache_get",
"(",
"$",
"cache_id",
",",
"'pb'",
")",
";",
"if",
"(",
"$",
"book_structure",
")",
"{",
"return",
"$",
"book_structure",
";",
"}",
"}",
"// -----------------------------------------------------------------------------",
"// Query our custom post types, keep minimal data in $book_structure",
"// -----------------------------------------------------------------------------",
"$",
"post_ids_to_export",
"=",
"static",
"::",
"getPostsIdsToExport",
"(",
")",
";",
"$",
"custom_types",
"=",
"[",
"'front-matter'",
",",
"'part'",
",",
"'chapter'",
",",
"'back-matter'",
",",
"]",
";",
"$",
"book_structure",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"custom_types",
"as",
"$",
"type",
")",
"{",
"$",
"book_structure",
"[",
"$",
"type",
"]",
"=",
"[",
"]",
";",
"}",
"$",
"q",
"=",
"new",
"\\",
"WP_Query",
"(",
")",
";",
"$",
"results",
"=",
"$",
"q",
"->",
"query",
"(",
"[",
"'post_type'",
"=>",
"$",
"custom_types",
",",
"'posts_per_page'",
"=>",
"-",
"1",
",",
"// @codingStandardsIgnoreLine",
"'post_status'",
"=>",
"'any'",
",",
"'orderby'",
"=>",
"'menu_order'",
",",
"'order'",
"=>",
"'ASC'",
",",
"'no_found_rows'",
"=>",
"true",
",",
"'cache_results'",
"=>",
"true",
",",
"]",
")",
";",
"/** @var \\WP_Post $post */",
"foreach",
"(",
"$",
"results",
"as",
"$",
"post",
")",
"{",
"// Fix empty slugs",
"$",
"post_name",
"=",
"empty",
"(",
"trim",
"(",
"$",
"post",
"->",
"post_name",
")",
")",
"?",
"uniqid",
"(",
"'slug-'",
")",
":",
"$",
"post",
"->",
"post_name",
";",
"$",
"book_structure",
"[",
"$",
"post",
"->",
"post_type",
"]",
"[",
"]",
"=",
"[",
"'ID'",
"=>",
"$",
"post",
"->",
"ID",
",",
"'post_title'",
"=>",
"$",
"post",
"->",
"post_title",
",",
"'post_name'",
"=>",
"$",
"post_name",
",",
"'post_author'",
"=>",
"(",
"int",
")",
"$",
"post",
"->",
"post_author",
",",
"'comment_count'",
"=>",
"(",
"int",
")",
"$",
"post",
"->",
"comment_count",
",",
"'menu_order'",
"=>",
"$",
"post",
"->",
"menu_order",
",",
"'post_status'",
"=>",
"$",
"post",
"->",
"post_status",
",",
"'post_parent'",
"=>",
"$",
"post",
"->",
"post_parent",
",",
"'export'",
"=>",
"(",
"isset",
"(",
"$",
"post_ids_to_export",
"[",
"$",
"post",
"->",
"ID",
"]",
")",
"&&",
"'on'",
"===",
"$",
"post_ids_to_export",
"[",
"$",
"post",
"->",
"ID",
"]",
")",
"?",
"true",
":",
"false",
",",
"'has_post_content'",
"=>",
"!",
"empty",
"(",
"trim",
"(",
"$",
"post",
"->",
"post_content",
")",
")",
",",
"'word_count'",
"=>",
"\\",
"Pressbooks",
"\\",
"Utility",
"\\",
"word_count",
"(",
"$",
"post",
"->",
"post_content",
")",
",",
"]",
";",
"}",
"// -----------------------------------------------------------------------------",
"// Add Chapters to Parts",
"// -----------------------------------------------------------------------------",
"foreach",
"(",
"$",
"book_structure",
"[",
"'part'",
"]",
"as",
"$",
"i",
"=>",
"$",
"part",
")",
"{",
"// There's no `pb_export` for parts. We instead have `pb_part_invisible` and it doesn't mean the same thing. Force to true.",
"$",
"book_structure",
"[",
"'part'",
"]",
"[",
"$",
"i",
"]",
"[",
"'export'",
"]",
"=",
"true",
";",
"$",
"book_structure",
"[",
"'part'",
"]",
"[",
"$",
"i",
"]",
"[",
"'chapters'",
"]",
"=",
"[",
"]",
";",
"}",
"foreach",
"(",
"$",
"book_structure",
"[",
"'chapter'",
"]",
"as",
"$",
"i",
"=>",
"$",
"chapter",
")",
"{",
"foreach",
"(",
"$",
"book_structure",
"[",
"'part'",
"]",
"as",
"$",
"j",
"=>",
"$",
"part",
")",
"{",
"if",
"(",
"$",
"part",
"[",
"'ID'",
"]",
"===",
"$",
"chapter",
"[",
"'post_parent'",
"]",
")",
"{",
"$",
"book_structure",
"[",
"'part'",
"]",
"[",
"$",
"j",
"]",
"[",
"'chapters'",
"]",
"[",
"]",
"=",
"$",
"chapter",
";",
"unset",
"(",
"$",
"book_structure",
"[",
"'chapter'",
"]",
"[",
"$",
"i",
"]",
")",
";",
"continue",
"2",
";",
"}",
"}",
"}",
"/* Track unexpected orphans, unset() chapter from $book_structure and $types */",
"if",
"(",
"count",
"(",
"$",
"book_structure",
"[",
"'chapter'",
"]",
")",
")",
"{",
"$",
"book_structure",
"[",
"'__orphans'",
"]",
"=",
"$",
"book_structure",
"[",
"'chapter'",
"]",
";",
"}",
"unset",
"(",
"$",
"book_structure",
"[",
"'chapter'",
"]",
")",
";",
"$",
"custom_types",
"=",
"array_diff",
"(",
"$",
"custom_types",
",",
"[",
"'chapter'",
"]",
")",
";",
"// -----------------------------------------------------------------------------",
"// Create __order arrays, remove post_parent",
"// -----------------------------------------------------------------------------",
"$",
"book_structure",
"[",
"'__order'",
"]",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"custom_types",
"as",
"$",
"type",
")",
"{",
"foreach",
"(",
"$",
"book_structure",
"[",
"$",
"type",
"]",
"as",
"$",
"i",
"=>",
"$",
"struct",
")",
"{",
"unset",
"(",
"$",
"book_structure",
"[",
"$",
"type",
"]",
"[",
"$",
"i",
"]",
"[",
"'post_parent'",
"]",
")",
";",
"if",
"(",
"'part'",
"!==",
"$",
"type",
")",
"{",
"$",
"book_structure",
"[",
"'__order'",
"]",
"[",
"$",
"struct",
"[",
"'ID'",
"]",
"]",
"=",
"[",
"'export'",
"=>",
"$",
"struct",
"[",
"'export'",
"]",
",",
"'post_status'",
"=>",
"$",
"struct",
"[",
"'post_status'",
"]",
",",
"'post_name'",
"=>",
"$",
"struct",
"[",
"'post_name'",
"]",
",",
"'post_type'",
"=>",
"$",
"type",
",",
"]",
";",
"}",
"else",
"{",
"if",
"(",
"$",
"struct",
"[",
"'has_post_content'",
"]",
"&&",
"get_post_meta",
"(",
"$",
"struct",
"[",
"'ID'",
"]",
",",
"'pb_part_invisible'",
",",
"true",
")",
"!==",
"'on'",
")",
"{",
"$",
"book_structure",
"[",
"'__order'",
"]",
"[",
"$",
"struct",
"[",
"'ID'",
"]",
"]",
"=",
"[",
"'export'",
"=>",
"$",
"struct",
"[",
"'export'",
"]",
",",
"'post_status'",
"=>",
"$",
"struct",
"[",
"'post_status'",
"]",
",",
"'post_name'",
"=>",
"$",
"struct",
"[",
"'post_name'",
"]",
",",
"'post_type'",
"=>",
"'part'",
",",
"]",
";",
"}",
"foreach",
"(",
"$",
"struct",
"[",
"'chapters'",
"]",
"as",
"$",
"j",
"=>",
"$",
"chapter",
")",
"{",
"unset",
"(",
"$",
"book_structure",
"[",
"$",
"type",
"]",
"[",
"$",
"i",
"]",
"[",
"'chapters'",
"]",
"[",
"$",
"j",
"]",
"[",
"'post_parent'",
"]",
")",
";",
"if",
"(",
"$",
"struct",
"[",
"'has_post_content'",
"]",
"&&",
"get_post_meta",
"(",
"$",
"struct",
"[",
"'ID'",
"]",
",",
"'pb_part_invisible'",
",",
"true",
")",
"!==",
"'on'",
")",
"{",
"$",
"book_structure",
"[",
"'__order'",
"]",
"[",
"$",
"struct",
"[",
"'ID'",
"]",
"]",
"=",
"[",
"'export'",
"=>",
"$",
"struct",
"[",
"'export'",
"]",
",",
"'post_status'",
"=>",
"$",
"struct",
"[",
"'post_status'",
"]",
",",
"'post_name'",
"=>",
"$",
"struct",
"[",
"'post_name'",
"]",
",",
"'post_type'",
"=>",
"'part'",
",",
"]",
";",
"}",
"$",
"book_structure",
"[",
"'__order'",
"]",
"[",
"$",
"chapter",
"[",
"'ID'",
"]",
"]",
"=",
"[",
"'export'",
"=>",
"$",
"chapter",
"[",
"'export'",
"]",
",",
"'post_status'",
"=>",
"$",
"chapter",
"[",
"'post_status'",
"]",
",",
"'post_name'",
"=>",
"$",
"chapter",
"[",
"'post_name'",
"]",
",",
"'post_type'",
"=>",
"'chapter'",
",",
"]",
";",
"}",
"}",
"}",
"}",
"static",
"::",
"$",
"__order",
"=",
"$",
"book_structure",
"[",
"'__order'",
"]",
";",
"// -----------------------------------------------------------------------------",
"// Cache & Return",
"// -----------------------------------------------------------------------------",
"if",
"(",
"static",
"::",
"useCache",
"(",
")",
")",
"{",
"wp_cache_set",
"(",
"$",
"cache_id",
",",
"$",
"book_structure",
",",
"'pb'",
",",
"DAY_IN_SECONDS",
")",
";",
"}",
"if",
"(",
"!",
"empty",
"(",
"$",
"id",
")",
"&&",
"is_int",
"(",
"$",
"id",
")",
")",
"{",
"restore_current_blog",
"(",
")",
";",
"}",
"return",
"$",
"book_structure",
";",
"}"
] | Returns an array representing the entire structure of a book, in correct order,
with a minimum amount of fields. Data is raw and must be post-processed.
@see bottom of this file for more info
@param int $id
@return array | [
"Returns",
"an",
"array",
"representing",
"the",
"entire",
"structure",
"of",
"a",
"book",
"in",
"correct",
"order",
"with",
"a",
"minimum",
"amount",
"of",
"fields",
".",
"Data",
"is",
"raw",
"and",
"must",
"be",
"post",
"-",
"processed",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-book.php#L205-L365 |
pressbooks/pressbooks | inc/class-book.php | Book.getBookContents | static function getBookContents() {
global $blog_id;
// -----------------------------------------------------------------------------
// Is cached?
// -----------------------------------------------------------------------------
$cache_id = "book-cnt-$blog_id";
if ( static::useCache() ) {
$book_contents = wp_cache_get( $cache_id, 'pb' );
if ( $book_contents ) {
return $book_contents;
}
}
// -----------------------------------------------------------------------------
// Using + to merge arrays...
// Precedence when using the + operator to merge arrays is from left to right
// -----------------------------------------------------------------------------
$book_contents = static::getBookStructure();
foreach ( $book_contents as $type => $struct ) {
if ( preg_match( '/^__/', $type ) ) {
continue; // Skip __magic keys
}
if ( 'part' === $type ) {
foreach ( $struct as $i => $part ) {
$book_contents[ $type ][ $i ] = $part + get_post( $part['ID'], ARRAY_A );
foreach ( $part['chapters'] as $j => $chapter ) {
$book_contents[ $type ][ $i ]['chapters'][ $j ] = $chapter + get_post( $chapter['ID'], ARRAY_A );
}
}
} else {
foreach ( $struct as $i => $val ) {
$book_contents[ $type ][ $i ] = $val + get_post( $val['ID'], ARRAY_A );
}
}
}
// -----------------------------------------------------------------------------
// Cache & Return
// -----------------------------------------------------------------------------
if ( static::useCache() ) {
wp_cache_set( $cache_id, $book_contents, 'pb', DAY_IN_SECONDS );
}
return $book_contents;
} | php | static function getBookContents() {
global $blog_id;
// -----------------------------------------------------------------------------
// Is cached?
// -----------------------------------------------------------------------------
$cache_id = "book-cnt-$blog_id";
if ( static::useCache() ) {
$book_contents = wp_cache_get( $cache_id, 'pb' );
if ( $book_contents ) {
return $book_contents;
}
}
// -----------------------------------------------------------------------------
// Using + to merge arrays...
// Precedence when using the + operator to merge arrays is from left to right
// -----------------------------------------------------------------------------
$book_contents = static::getBookStructure();
foreach ( $book_contents as $type => $struct ) {
if ( preg_match( '/^__/', $type ) ) {
continue; // Skip __magic keys
}
if ( 'part' === $type ) {
foreach ( $struct as $i => $part ) {
$book_contents[ $type ][ $i ] = $part + get_post( $part['ID'], ARRAY_A );
foreach ( $part['chapters'] as $j => $chapter ) {
$book_contents[ $type ][ $i ]['chapters'][ $j ] = $chapter + get_post( $chapter['ID'], ARRAY_A );
}
}
} else {
foreach ( $struct as $i => $val ) {
$book_contents[ $type ][ $i ] = $val + get_post( $val['ID'], ARRAY_A );
}
}
}
// -----------------------------------------------------------------------------
// Cache & Return
// -----------------------------------------------------------------------------
if ( static::useCache() ) {
wp_cache_set( $cache_id, $book_contents, 'pb', DAY_IN_SECONDS );
}
return $book_contents;
} | [
"static",
"function",
"getBookContents",
"(",
")",
"{",
"global",
"$",
"blog_id",
";",
"// -----------------------------------------------------------------------------",
"// Is cached?",
"// -----------------------------------------------------------------------------",
"$",
"cache_id",
"=",
"\"book-cnt-$blog_id\"",
";",
"if",
"(",
"static",
"::",
"useCache",
"(",
")",
")",
"{",
"$",
"book_contents",
"=",
"wp_cache_get",
"(",
"$",
"cache_id",
",",
"'pb'",
")",
";",
"if",
"(",
"$",
"book_contents",
")",
"{",
"return",
"$",
"book_contents",
";",
"}",
"}",
"// -----------------------------------------------------------------------------",
"// Using + to merge arrays...",
"// Precedence when using the + operator to merge arrays is from left to right",
"// -----------------------------------------------------------------------------",
"$",
"book_contents",
"=",
"static",
"::",
"getBookStructure",
"(",
")",
";",
"foreach",
"(",
"$",
"book_contents",
"as",
"$",
"type",
"=>",
"$",
"struct",
")",
"{",
"if",
"(",
"preg_match",
"(",
"'/^__/'",
",",
"$",
"type",
")",
")",
"{",
"continue",
";",
"// Skip __magic keys",
"}",
"if",
"(",
"'part'",
"===",
"$",
"type",
")",
"{",
"foreach",
"(",
"$",
"struct",
"as",
"$",
"i",
"=>",
"$",
"part",
")",
"{",
"$",
"book_contents",
"[",
"$",
"type",
"]",
"[",
"$",
"i",
"]",
"=",
"$",
"part",
"+",
"get_post",
"(",
"$",
"part",
"[",
"'ID'",
"]",
",",
"ARRAY_A",
")",
";",
"foreach",
"(",
"$",
"part",
"[",
"'chapters'",
"]",
"as",
"$",
"j",
"=>",
"$",
"chapter",
")",
"{",
"$",
"book_contents",
"[",
"$",
"type",
"]",
"[",
"$",
"i",
"]",
"[",
"'chapters'",
"]",
"[",
"$",
"j",
"]",
"=",
"$",
"chapter",
"+",
"get_post",
"(",
"$",
"chapter",
"[",
"'ID'",
"]",
",",
"ARRAY_A",
")",
";",
"}",
"}",
"}",
"else",
"{",
"foreach",
"(",
"$",
"struct",
"as",
"$",
"i",
"=>",
"$",
"val",
")",
"{",
"$",
"book_contents",
"[",
"$",
"type",
"]",
"[",
"$",
"i",
"]",
"=",
"$",
"val",
"+",
"get_post",
"(",
"$",
"val",
"[",
"'ID'",
"]",
",",
"ARRAY_A",
")",
";",
"}",
"}",
"}",
"// -----------------------------------------------------------------------------",
"// Cache & Return",
"// -----------------------------------------------------------------------------",
"if",
"(",
"static",
"::",
"useCache",
"(",
")",
")",
"{",
"wp_cache_set",
"(",
"$",
"cache_id",
",",
"$",
"book_contents",
",",
"'pb'",
",",
"DAY_IN_SECONDS",
")",
";",
"}",
"return",
"$",
"book_contents",
";",
"}"
] | Returns an array representing the entire structure of a book, in correct order,
with a maximum amount of fields. Data is raw and must be post-processed.
@see bottom of this file for more info
@return array | [
"Returns",
"an",
"array",
"representing",
"the",
"entire",
"structure",
"of",
"a",
"book",
"in",
"correct",
"order",
"with",
"a",
"maximum",
"amount",
"of",
"fields",
".",
"Data",
"is",
"raw",
"and",
"must",
"be",
"post",
"-",
"processed",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-book.php#L375-L427 |
pressbooks/pressbooks | inc/class-book.php | Book.wordCount | static function wordCount( $selected_for_export = false ) {
$wc = 0;
$wc_selected_for_export = 0;
foreach ( static::getBookStructure() as $key => $section ) {
if ( $key === 'front-matter' || $key === 'back-matter' ) {
foreach ( $section as $val ) {
$wc += $val['word_count'];
if ( $val['export'] ) {
$wc_selected_for_export += $val['word_count'];
}
}
}
if ( $key === 'part' ) {
foreach ( $section as $part ) {
foreach ( $part['chapters'] as $val ) {
$wc += $val['word_count'];
if ( $val['export'] ) {
$wc_selected_for_export += $val['word_count'];
}
}
}
}
}
return $selected_for_export ? $wc_selected_for_export : $wc;
} | php | static function wordCount( $selected_for_export = false ) {
$wc = 0;
$wc_selected_for_export = 0;
foreach ( static::getBookStructure() as $key => $section ) {
if ( $key === 'front-matter' || $key === 'back-matter' ) {
foreach ( $section as $val ) {
$wc += $val['word_count'];
if ( $val['export'] ) {
$wc_selected_for_export += $val['word_count'];
}
}
}
if ( $key === 'part' ) {
foreach ( $section as $part ) {
foreach ( $part['chapters'] as $val ) {
$wc += $val['word_count'];
if ( $val['export'] ) {
$wc_selected_for_export += $val['word_count'];
}
}
}
}
}
return $selected_for_export ? $wc_selected_for_export : $wc;
} | [
"static",
"function",
"wordCount",
"(",
"$",
"selected_for_export",
"=",
"false",
")",
"{",
"$",
"wc",
"=",
"0",
";",
"$",
"wc_selected_for_export",
"=",
"0",
";",
"foreach",
"(",
"static",
"::",
"getBookStructure",
"(",
")",
"as",
"$",
"key",
"=>",
"$",
"section",
")",
"{",
"if",
"(",
"$",
"key",
"===",
"'front-matter'",
"||",
"$",
"key",
"===",
"'back-matter'",
")",
"{",
"foreach",
"(",
"$",
"section",
"as",
"$",
"val",
")",
"{",
"$",
"wc",
"+=",
"$",
"val",
"[",
"'word_count'",
"]",
";",
"if",
"(",
"$",
"val",
"[",
"'export'",
"]",
")",
"{",
"$",
"wc_selected_for_export",
"+=",
"$",
"val",
"[",
"'word_count'",
"]",
";",
"}",
"}",
"}",
"if",
"(",
"$",
"key",
"===",
"'part'",
")",
"{",
"foreach",
"(",
"$",
"section",
"as",
"$",
"part",
")",
"{",
"foreach",
"(",
"$",
"part",
"[",
"'chapters'",
"]",
"as",
"$",
"val",
")",
"{",
"$",
"wc",
"+=",
"$",
"val",
"[",
"'word_count'",
"]",
";",
"if",
"(",
"$",
"val",
"[",
"'export'",
"]",
")",
"{",
"$",
"wc_selected_for_export",
"+=",
"$",
"val",
"[",
"'word_count'",
"]",
";",
"}",
"}",
"}",
"}",
"}",
"return",
"$",
"selected_for_export",
"?",
"$",
"wc_selected_for_export",
":",
"$",
"wc",
";",
"}"
] | @param bool $selected_for_export (optional)
@return int | [
"@param",
"bool",
"$selected_for_export",
"(",
"optional",
")"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-book.php#L434-L459 |
pressbooks/pressbooks | inc/class-book.php | Book.deleteBookObjectCache | static function deleteBookObjectCache() {
global $blog_id;
// Book Object
wp_cache_delete( "book-inf-$blog_id", 'pb' ); // Delete the cached value for getBookInfo()
wp_cache_delete( "book-str-$blog_id", 'pb' ); // Delete the cached value for getBookStructure()
wp_cache_delete( "book-cnt-$blog_id", 'pb' ); // Delete the cached value for getBookContents()
static::$__order = [];
// Subsections
delete_transient( static::SUBSECTIONS_TRANSIENT );
// User Catalog
( new Catalog() )->deleteCacheByBookId( $blog_id );
// Output buffers
delete_transient( Xhtml11::TRANSIENT );
/**
* @since 5.0.0
*
* @param int $blog_id
*/
do_action( 'pb_cache_delete', $blog_id );
set_transient( 'pb_cache_deleted', time(), DAY_IN_SECONDS );
} | php | static function deleteBookObjectCache() {
global $blog_id;
// Book Object
wp_cache_delete( "book-inf-$blog_id", 'pb' ); // Delete the cached value for getBookInfo()
wp_cache_delete( "book-str-$blog_id", 'pb' ); // Delete the cached value for getBookStructure()
wp_cache_delete( "book-cnt-$blog_id", 'pb' ); // Delete the cached value for getBookContents()
static::$__order = [];
// Subsections
delete_transient( static::SUBSECTIONS_TRANSIENT );
// User Catalog
( new Catalog() )->deleteCacheByBookId( $blog_id );
// Output buffers
delete_transient( Xhtml11::TRANSIENT );
/**
* @since 5.0.0
*
* @param int $blog_id
*/
do_action( 'pb_cache_delete', $blog_id );
set_transient( 'pb_cache_deleted', time(), DAY_IN_SECONDS );
} | [
"static",
"function",
"deleteBookObjectCache",
"(",
")",
"{",
"global",
"$",
"blog_id",
";",
"// Book Object",
"wp_cache_delete",
"(",
"\"book-inf-$blog_id\"",
",",
"'pb'",
")",
";",
"// Delete the cached value for getBookInfo()",
"wp_cache_delete",
"(",
"\"book-str-$blog_id\"",
",",
"'pb'",
")",
";",
"// Delete the cached value for getBookStructure()",
"wp_cache_delete",
"(",
"\"book-cnt-$blog_id\"",
",",
"'pb'",
")",
";",
"// Delete the cached value for getBookContents()",
"static",
"::",
"$",
"__order",
"=",
"[",
"]",
";",
"// Subsections",
"delete_transient",
"(",
"static",
"::",
"SUBSECTIONS_TRANSIENT",
")",
";",
"// User Catalog",
"(",
"new",
"Catalog",
"(",
")",
")",
"->",
"deleteCacheByBookId",
"(",
"$",
"blog_id",
")",
";",
"// Output buffers",
"delete_transient",
"(",
"Xhtml11",
"::",
"TRANSIENT",
")",
";",
"/**\n\t\t * @since 5.0.0\n\t\t *\n\t\t * @param int $blog_id\n\t\t */",
"do_action",
"(",
"'pb_cache_delete'",
",",
"$",
"blog_id",
")",
";",
"set_transient",
"(",
"'pb_cache_deleted'",
",",
"time",
"(",
")",
",",
"DAY_IN_SECONDS",
")",
";",
"}"
] | Delete the Book Object cache(s) | [
"Delete",
"the",
"Book",
"Object",
"cache",
"(",
"s",
")"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-book.php#L476-L502 |
pressbooks/pressbooks | inc/class-book.php | Book.getSubsections | static function getSubsections( $id ) {
$parent = get_post( $id );
if ( empty( $parent ) ) {
return false;
}
$has_shortcode = has_shortcode( $parent->post_content, 'heading' );
if ( stripos( $parent->post_content, '<h1' ) === false && $has_shortcode === false ) { // No <h1> or [heading] shortcode
return false;
}
$type = $parent->post_type;
$content = ( $has_shortcode ) ? apply_filters( 'the_content', $parent->post_content ) : $parent->post_content; // Only render shortcodes if we have to
$content = strip_tags( $content, '<h1>' ); // Strip everything except h1 to speed up load time
$output = [];
$s = 1;
$doc = new HtmlParser( true ); // Because we are not saving, use internal parser to speed up load time
$dom = $doc->loadHTML( $content );
$sections = $dom->getElementsByTagName( 'h1' );
foreach ( $sections as $section ) {
/** @var $section \DOMElement */
$output[ $type . '-' . $id . '-section-' . $s ] = wptexturize( $section->textContent );
$s++;
}
if ( empty( $output ) ) {
return false;
}
return $output;
} | php | static function getSubsections( $id ) {
$parent = get_post( $id );
if ( empty( $parent ) ) {
return false;
}
$has_shortcode = has_shortcode( $parent->post_content, 'heading' );
if ( stripos( $parent->post_content, '<h1' ) === false && $has_shortcode === false ) { // No <h1> or [heading] shortcode
return false;
}
$type = $parent->post_type;
$content = ( $has_shortcode ) ? apply_filters( 'the_content', $parent->post_content ) : $parent->post_content; // Only render shortcodes if we have to
$content = strip_tags( $content, '<h1>' ); // Strip everything except h1 to speed up load time
$output = [];
$s = 1;
$doc = new HtmlParser( true ); // Because we are not saving, use internal parser to speed up load time
$dom = $doc->loadHTML( $content );
$sections = $dom->getElementsByTagName( 'h1' );
foreach ( $sections as $section ) {
/** @var $section \DOMElement */
$output[ $type . '-' . $id . '-section-' . $s ] = wptexturize( $section->textContent );
$s++;
}
if ( empty( $output ) ) {
return false;
}
return $output;
} | [
"static",
"function",
"getSubsections",
"(",
"$",
"id",
")",
"{",
"$",
"parent",
"=",
"get_post",
"(",
"$",
"id",
")",
";",
"if",
"(",
"empty",
"(",
"$",
"parent",
")",
")",
"{",
"return",
"false",
";",
"}",
"$",
"has_shortcode",
"=",
"has_shortcode",
"(",
"$",
"parent",
"->",
"post_content",
",",
"'heading'",
")",
";",
"if",
"(",
"stripos",
"(",
"$",
"parent",
"->",
"post_content",
",",
"'<h1'",
")",
"===",
"false",
"&&",
"$",
"has_shortcode",
"===",
"false",
")",
"{",
"// No <h1> or [heading] shortcode",
"return",
"false",
";",
"}",
"$",
"type",
"=",
"$",
"parent",
"->",
"post_type",
";",
"$",
"content",
"=",
"(",
"$",
"has_shortcode",
")",
"?",
"apply_filters",
"(",
"'the_content'",
",",
"$",
"parent",
"->",
"post_content",
")",
":",
"$",
"parent",
"->",
"post_content",
";",
"// Only render shortcodes if we have to",
"$",
"content",
"=",
"strip_tags",
"(",
"$",
"content",
",",
"'<h1>'",
")",
";",
"// Strip everything except h1 to speed up load time",
"$",
"output",
"=",
"[",
"]",
";",
"$",
"s",
"=",
"1",
";",
"$",
"doc",
"=",
"new",
"HtmlParser",
"(",
"true",
")",
";",
"// Because we are not saving, use internal parser to speed up load time",
"$",
"dom",
"=",
"$",
"doc",
"->",
"loadHTML",
"(",
"$",
"content",
")",
";",
"$",
"sections",
"=",
"$",
"dom",
"->",
"getElementsByTagName",
"(",
"'h1'",
")",
";",
"foreach",
"(",
"$",
"sections",
"as",
"$",
"section",
")",
"{",
"/** @var $section \\DOMElement */",
"$",
"output",
"[",
"$",
"type",
".",
"'-'",
".",
"$",
"id",
".",
"'-section-'",
".",
"$",
"s",
"]",
"=",
"wptexturize",
"(",
"$",
"section",
"->",
"textContent",
")",
";",
"$",
"s",
"++",
";",
"}",
"if",
"(",
"empty",
"(",
"$",
"output",
")",
")",
"{",
"return",
"false",
";",
"}",
"return",
"$",
"output",
";",
"}"
] | Returns an array of subsections in front matter, back matter, or chapters.
@param $id
@return array|false | [
"Returns",
"an",
"array",
"of",
"subsections",
"in",
"front",
"matter",
"back",
"matter",
"or",
"chapters",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-book.php#L511-L541 |
pressbooks/pressbooks | inc/class-book.php | Book.getAllSubsections | static function getAllSubsections( $book_structure ) {
if ( Export::shouldParseSubsections() ) {
$book_subsections_transient = \Pressbooks\Book::SUBSECTIONS_TRANSIENT;
$subsection_processing_transient = \Pressbooks\Book::SUBSECTION_PROCESSING_TRANSIENT;
$book_subsections = get_transient( $book_subsections_transient );
if ( ! $book_subsections ) {
$book_subsections = [];
if ( ! get_transient( $subsection_processing_transient ) ) {
set_transient( $subsection_processing_transient, 1, 5 * MINUTE_IN_SECONDS );
foreach ( $book_structure['front-matter'] as $section ) {
$subsections = \Pressbooks\Book::getSubsections( $section['ID'] );
if ( $subsections ) {
$book_subsections['front-matter'][ $section['ID'] ] = $subsections;
}
}
foreach ( $book_structure['part'] as $key => $part ) {
if ( ! empty( $part['chapters'] ) ) {
foreach ( $part['chapters'] as $section ) {
$subsections = \Pressbooks\Book::getSubsections( $section['ID'] );
if ( $subsections ) {
$book_subsections['chapters'][ $section['ID'] ] = $subsections;
}
}
}
}
foreach ( $book_structure['back-matter'] as $section ) {
$subsections = \Pressbooks\Book::getSubsections( $section['ID'] );
if ( $subsections ) {
$book_subsections['back-matter'][ $section['ID'] ] = $subsections;
}
}
delete_transient( $subsection_processing_transient );
}
}
set_transient( $book_subsections_transient, $book_subsections );
return $book_subsections;
}
return [];
} | php | static function getAllSubsections( $book_structure ) {
if ( Export::shouldParseSubsections() ) {
$book_subsections_transient = \Pressbooks\Book::SUBSECTIONS_TRANSIENT;
$subsection_processing_transient = \Pressbooks\Book::SUBSECTION_PROCESSING_TRANSIENT;
$book_subsections = get_transient( $book_subsections_transient );
if ( ! $book_subsections ) {
$book_subsections = [];
if ( ! get_transient( $subsection_processing_transient ) ) {
set_transient( $subsection_processing_transient, 1, 5 * MINUTE_IN_SECONDS );
foreach ( $book_structure['front-matter'] as $section ) {
$subsections = \Pressbooks\Book::getSubsections( $section['ID'] );
if ( $subsections ) {
$book_subsections['front-matter'][ $section['ID'] ] = $subsections;
}
}
foreach ( $book_structure['part'] as $key => $part ) {
if ( ! empty( $part['chapters'] ) ) {
foreach ( $part['chapters'] as $section ) {
$subsections = \Pressbooks\Book::getSubsections( $section['ID'] );
if ( $subsections ) {
$book_subsections['chapters'][ $section['ID'] ] = $subsections;
}
}
}
}
foreach ( $book_structure['back-matter'] as $section ) {
$subsections = \Pressbooks\Book::getSubsections( $section['ID'] );
if ( $subsections ) {
$book_subsections['back-matter'][ $section['ID'] ] = $subsections;
}
}
delete_transient( $subsection_processing_transient );
}
}
set_transient( $book_subsections_transient, $book_subsections );
return $book_subsections;
}
return [];
} | [
"static",
"function",
"getAllSubsections",
"(",
"$",
"book_structure",
")",
"{",
"if",
"(",
"Export",
"::",
"shouldParseSubsections",
"(",
")",
")",
"{",
"$",
"book_subsections_transient",
"=",
"\\",
"Pressbooks",
"\\",
"Book",
"::",
"SUBSECTIONS_TRANSIENT",
";",
"$",
"subsection_processing_transient",
"=",
"\\",
"Pressbooks",
"\\",
"Book",
"::",
"SUBSECTION_PROCESSING_TRANSIENT",
";",
"$",
"book_subsections",
"=",
"get_transient",
"(",
"$",
"book_subsections_transient",
")",
";",
"if",
"(",
"!",
"$",
"book_subsections",
")",
"{",
"$",
"book_subsections",
"=",
"[",
"]",
";",
"if",
"(",
"!",
"get_transient",
"(",
"$",
"subsection_processing_transient",
")",
")",
"{",
"set_transient",
"(",
"$",
"subsection_processing_transient",
",",
"1",
",",
"5",
"*",
"MINUTE_IN_SECONDS",
")",
";",
"foreach",
"(",
"$",
"book_structure",
"[",
"'front-matter'",
"]",
"as",
"$",
"section",
")",
"{",
"$",
"subsections",
"=",
"\\",
"Pressbooks",
"\\",
"Book",
"::",
"getSubsections",
"(",
"$",
"section",
"[",
"'ID'",
"]",
")",
";",
"if",
"(",
"$",
"subsections",
")",
"{",
"$",
"book_subsections",
"[",
"'front-matter'",
"]",
"[",
"$",
"section",
"[",
"'ID'",
"]",
"]",
"=",
"$",
"subsections",
";",
"}",
"}",
"foreach",
"(",
"$",
"book_structure",
"[",
"'part'",
"]",
"as",
"$",
"key",
"=>",
"$",
"part",
")",
"{",
"if",
"(",
"!",
"empty",
"(",
"$",
"part",
"[",
"'chapters'",
"]",
")",
")",
"{",
"foreach",
"(",
"$",
"part",
"[",
"'chapters'",
"]",
"as",
"$",
"section",
")",
"{",
"$",
"subsections",
"=",
"\\",
"Pressbooks",
"\\",
"Book",
"::",
"getSubsections",
"(",
"$",
"section",
"[",
"'ID'",
"]",
")",
";",
"if",
"(",
"$",
"subsections",
")",
"{",
"$",
"book_subsections",
"[",
"'chapters'",
"]",
"[",
"$",
"section",
"[",
"'ID'",
"]",
"]",
"=",
"$",
"subsections",
";",
"}",
"}",
"}",
"}",
"foreach",
"(",
"$",
"book_structure",
"[",
"'back-matter'",
"]",
"as",
"$",
"section",
")",
"{",
"$",
"subsections",
"=",
"\\",
"Pressbooks",
"\\",
"Book",
"::",
"getSubsections",
"(",
"$",
"section",
"[",
"'ID'",
"]",
")",
";",
"if",
"(",
"$",
"subsections",
")",
"{",
"$",
"book_subsections",
"[",
"'back-matter'",
"]",
"[",
"$",
"section",
"[",
"'ID'",
"]",
"]",
"=",
"$",
"subsections",
";",
"}",
"}",
"delete_transient",
"(",
"$",
"subsection_processing_transient",
")",
";",
"}",
"}",
"set_transient",
"(",
"$",
"book_subsections_transient",
",",
"$",
"book_subsections",
")",
";",
"return",
"$",
"book_subsections",
";",
"}",
"return",
"[",
"]",
";",
"}"
] | Returns an array of front matter, chapters, and back matter which contain subsections.
@param array $book_structure The book structure from getBookStructure()
@return array The subsections, grouped by parent post type | [
"Returns",
"an",
"array",
"of",
"front",
"matter",
"chapters",
"and",
"back",
"matter",
"which",
"contain",
"subsections",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-book.php#L549-L587 |
pressbooks/pressbooks | inc/class-book.php | Book.tagSubsections | static function tagSubsections( $content, $id ) {
$parent = get_post( $id );
if ( empty( $parent ) ) {
return false;
}
if ( stripos( $content, '<h1' ) === false ) {
return false;
}
$type = $parent->post_type;
$s = 1;
$doc = new HtmlParser();
$dom = $doc->loadHTML( $content );
$sections = $dom->getElementsByTagName( 'h1' );
foreach ( $sections as $section ) {
/** @var $section \DOMElement */
$old_id = $section->getAttribute( 'id' );
$old_class = $section->getAttribute( 'class' );
$new_id = "{$type}-{$id}-section-" . $s++;
$new_class = trim( "section-header {$old_class} {$old_id}" );
$section->setAttribute( 'id', $new_id );
$section->setAttribute( 'class', $new_class );
}
return $doc->saveHTML( $dom );
} | php | static function tagSubsections( $content, $id ) {
$parent = get_post( $id );
if ( empty( $parent ) ) {
return false;
}
if ( stripos( $content, '<h1' ) === false ) {
return false;
}
$type = $parent->post_type;
$s = 1;
$doc = new HtmlParser();
$dom = $doc->loadHTML( $content );
$sections = $dom->getElementsByTagName( 'h1' );
foreach ( $sections as $section ) {
/** @var $section \DOMElement */
$old_id = $section->getAttribute( 'id' );
$old_class = $section->getAttribute( 'class' );
$new_id = "{$type}-{$id}-section-" . $s++;
$new_class = trim( "section-header {$old_class} {$old_id}" );
$section->setAttribute( 'id', $new_id );
$section->setAttribute( 'class', $new_class );
}
return $doc->saveHTML( $dom );
} | [
"static",
"function",
"tagSubsections",
"(",
"$",
"content",
",",
"$",
"id",
")",
"{",
"$",
"parent",
"=",
"get_post",
"(",
"$",
"id",
")",
";",
"if",
"(",
"empty",
"(",
"$",
"parent",
")",
")",
"{",
"return",
"false",
";",
"}",
"if",
"(",
"stripos",
"(",
"$",
"content",
",",
"'<h1'",
")",
"===",
"false",
")",
"{",
"return",
"false",
";",
"}",
"$",
"type",
"=",
"$",
"parent",
"->",
"post_type",
";",
"$",
"s",
"=",
"1",
";",
"$",
"doc",
"=",
"new",
"HtmlParser",
"(",
")",
";",
"$",
"dom",
"=",
"$",
"doc",
"->",
"loadHTML",
"(",
"$",
"content",
")",
";",
"$",
"sections",
"=",
"$",
"dom",
"->",
"getElementsByTagName",
"(",
"'h1'",
")",
";",
"foreach",
"(",
"$",
"sections",
"as",
"$",
"section",
")",
"{",
"/** @var $section \\DOMElement */",
"$",
"old_id",
"=",
"$",
"section",
"->",
"getAttribute",
"(",
"'id'",
")",
";",
"$",
"old_class",
"=",
"$",
"section",
"->",
"getAttribute",
"(",
"'class'",
")",
";",
"$",
"new_id",
"=",
"\"{$type}-{$id}-section-\"",
".",
"$",
"s",
"++",
";",
"$",
"new_class",
"=",
"trim",
"(",
"\"section-header {$old_class} {$old_id}\"",
")",
";",
"$",
"section",
"->",
"setAttribute",
"(",
"'id'",
",",
"$",
"new_id",
")",
";",
"$",
"section",
"->",
"setAttribute",
"(",
"'class'",
",",
"$",
"new_class",
")",
";",
"}",
"return",
"$",
"doc",
"->",
"saveHTML",
"(",
"$",
"dom",
")",
";",
"}"
] | Returns chapter, front or back matter content with section ID and classes added.
@param string $content
@param int $id
@return string|false | [
"Returns",
"chapter",
"front",
"or",
"back",
"matter",
"content",
"with",
"section",
"ID",
"and",
"classes",
"added",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-book.php#L597-L623 |
pressbooks/pressbooks | inc/class-book.php | Book.get | static function get( $what = 'next', $return_post_id = false, $admin_mode = false ) {
if ( 'first' === $what ) {
return static::getFirst( $return_post_id, $admin_mode );
}
global $blog_id;
global $post;
$current_post_id = $post->ID;
$book_structure = static::getBookStructure();
$order = $book_structure['__order'];
$pos = array_keys( $order );
$what = ( 'next' === $what ? 'next' : 'prev' );
// Move internal pointer to correct position
reset( $pos );
while ( $find_me = current( $pos ) ) {
if ( (int) $find_me === (int) $current_post_id ) {
break;
} else {
next( $pos );
}
}
// Get next/previous
$what( $pos );
while ( $post_id = current( $pos ) ) {
if ( $admin_mode ) {
if ( current_user_can( 'edit_post', $post_id ) ) {
break;
} else {
$what( $pos );
}
} else {
if ( in_array( $order[ $post_id ]['post_status'], [ 'publish', 'web-only' ], true ) ) {
break;
} elseif ( current_user_can_for_blog( $blog_id, 'read_private_posts' ) ) {
break;
} elseif ( get_option( 'permissive_private_content' ) && current_user_can_for_blog( $blog_id, 'read' ) ) {
break;
} else {
$what( $pos );
}
}
}
if ( $return_post_id ) {
return (int) $post_id;
} else {
return ( empty( $post_id ) ) ? '/' : get_permalink( $post_id );
}
} | php | static function get( $what = 'next', $return_post_id = false, $admin_mode = false ) {
if ( 'first' === $what ) {
return static::getFirst( $return_post_id, $admin_mode );
}
global $blog_id;
global $post;
$current_post_id = $post->ID;
$book_structure = static::getBookStructure();
$order = $book_structure['__order'];
$pos = array_keys( $order );
$what = ( 'next' === $what ? 'next' : 'prev' );
// Move internal pointer to correct position
reset( $pos );
while ( $find_me = current( $pos ) ) {
if ( (int) $find_me === (int) $current_post_id ) {
break;
} else {
next( $pos );
}
}
// Get next/previous
$what( $pos );
while ( $post_id = current( $pos ) ) {
if ( $admin_mode ) {
if ( current_user_can( 'edit_post', $post_id ) ) {
break;
} else {
$what( $pos );
}
} else {
if ( in_array( $order[ $post_id ]['post_status'], [ 'publish', 'web-only' ], true ) ) {
break;
} elseif ( current_user_can_for_blog( $blog_id, 'read_private_posts' ) ) {
break;
} elseif ( get_option( 'permissive_private_content' ) && current_user_can_for_blog( $blog_id, 'read' ) ) {
break;
} else {
$what( $pos );
}
}
}
if ( $return_post_id ) {
return (int) $post_id;
} else {
return ( empty( $post_id ) ) ? '/' : get_permalink( $post_id );
}
} | [
"static",
"function",
"get",
"(",
"$",
"what",
"=",
"'next'",
",",
"$",
"return_post_id",
"=",
"false",
",",
"$",
"admin_mode",
"=",
"false",
")",
"{",
"if",
"(",
"'first'",
"===",
"$",
"what",
")",
"{",
"return",
"static",
"::",
"getFirst",
"(",
"$",
"return_post_id",
",",
"$",
"admin_mode",
")",
";",
"}",
"global",
"$",
"blog_id",
";",
"global",
"$",
"post",
";",
"$",
"current_post_id",
"=",
"$",
"post",
"->",
"ID",
";",
"$",
"book_structure",
"=",
"static",
"::",
"getBookStructure",
"(",
")",
";",
"$",
"order",
"=",
"$",
"book_structure",
"[",
"'__order'",
"]",
";",
"$",
"pos",
"=",
"array_keys",
"(",
"$",
"order",
")",
";",
"$",
"what",
"=",
"(",
"'next'",
"===",
"$",
"what",
"?",
"'next'",
":",
"'prev'",
")",
";",
"// Move internal pointer to correct position",
"reset",
"(",
"$",
"pos",
")",
";",
"while",
"(",
"$",
"find_me",
"=",
"current",
"(",
"$",
"pos",
")",
")",
"{",
"if",
"(",
"(",
"int",
")",
"$",
"find_me",
"===",
"(",
"int",
")",
"$",
"current_post_id",
")",
"{",
"break",
";",
"}",
"else",
"{",
"next",
"(",
"$",
"pos",
")",
";",
"}",
"}",
"// Get next/previous",
"$",
"what",
"(",
"$",
"pos",
")",
";",
"while",
"(",
"$",
"post_id",
"=",
"current",
"(",
"$",
"pos",
")",
")",
"{",
"if",
"(",
"$",
"admin_mode",
")",
"{",
"if",
"(",
"current_user_can",
"(",
"'edit_post'",
",",
"$",
"post_id",
")",
")",
"{",
"break",
";",
"}",
"else",
"{",
"$",
"what",
"(",
"$",
"pos",
")",
";",
"}",
"}",
"else",
"{",
"if",
"(",
"in_array",
"(",
"$",
"order",
"[",
"$",
"post_id",
"]",
"[",
"'post_status'",
"]",
",",
"[",
"'publish'",
",",
"'web-only'",
"]",
",",
"true",
")",
")",
"{",
"break",
";",
"}",
"elseif",
"(",
"current_user_can_for_blog",
"(",
"$",
"blog_id",
",",
"'read_private_posts'",
")",
")",
"{",
"break",
";",
"}",
"elseif",
"(",
"get_option",
"(",
"'permissive_private_content'",
")",
"&&",
"current_user_can_for_blog",
"(",
"$",
"blog_id",
",",
"'read'",
")",
")",
"{",
"break",
";",
"}",
"else",
"{",
"$",
"what",
"(",
"$",
"pos",
")",
";",
"}",
"}",
"}",
"if",
"(",
"$",
"return_post_id",
")",
"{",
"return",
"(",
"int",
")",
"$",
"post_id",
";",
"}",
"else",
"{",
"return",
"(",
"empty",
"(",
"$",
"post_id",
")",
")",
"?",
"'/'",
":",
"get_permalink",
"(",
"$",
"post_id",
")",
";",
"}",
"}"
] | Fetch next, previous or first post
@param string $what prev, next or first
@param bool $return_post_id (optional)
@param bool $admin_mode (optional)
@return mixed URL of requested post, or Post ID if $return_post_id is set to true | [
"Fetch",
"next",
"previous",
"or",
"first",
"post"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-book.php#L647-L701 |
pressbooks/pressbooks | inc/class-book.php | Book.getFirst | static function getFirst( $return_post_id = false, $admin_mode = false ) {
global $blog_id;
$book_structure = static::getBookStructure();
$order = $book_structure['__order'];
$pos = array_keys( $order );
reset( $pos );
while ( $first_id = current( $pos ) ) {
if ( $admin_mode ) {
if ( current_user_can( 'edit_post', $first_id ) ) {
break;
} else {
next( $pos );
}
} else {
if ( in_array( $order[ $first_id ]['post_status'], [ 'publish', 'web-only' ], true ) ) {
break;
} elseif ( current_user_can_for_blog( $blog_id, 'read_private_posts' ) ) {
break;
} elseif ( get_option( 'permissive_private_content' ) && current_user_can_for_blog( $blog_id, 'read' ) ) {
break;
} else {
next( $pos );
}
}
}
if ( $return_post_id ) {
return (int) $first_id;
} else {
return ( empty( $first_id ) ) ? '/' : get_permalink( $first_id );
}
} | php | static function getFirst( $return_post_id = false, $admin_mode = false ) {
global $blog_id;
$book_structure = static::getBookStructure();
$order = $book_structure['__order'];
$pos = array_keys( $order );
reset( $pos );
while ( $first_id = current( $pos ) ) {
if ( $admin_mode ) {
if ( current_user_can( 'edit_post', $first_id ) ) {
break;
} else {
next( $pos );
}
} else {
if ( in_array( $order[ $first_id ]['post_status'], [ 'publish', 'web-only' ], true ) ) {
break;
} elseif ( current_user_can_for_blog( $blog_id, 'read_private_posts' ) ) {
break;
} elseif ( get_option( 'permissive_private_content' ) && current_user_can_for_blog( $blog_id, 'read' ) ) {
break;
} else {
next( $pos );
}
}
}
if ( $return_post_id ) {
return (int) $first_id;
} else {
return ( empty( $first_id ) ) ? '/' : get_permalink( $first_id );
}
} | [
"static",
"function",
"getFirst",
"(",
"$",
"return_post_id",
"=",
"false",
",",
"$",
"admin_mode",
"=",
"false",
")",
"{",
"global",
"$",
"blog_id",
";",
"$",
"book_structure",
"=",
"static",
"::",
"getBookStructure",
"(",
")",
";",
"$",
"order",
"=",
"$",
"book_structure",
"[",
"'__order'",
"]",
";",
"$",
"pos",
"=",
"array_keys",
"(",
"$",
"order",
")",
";",
"reset",
"(",
"$",
"pos",
")",
";",
"while",
"(",
"$",
"first_id",
"=",
"current",
"(",
"$",
"pos",
")",
")",
"{",
"if",
"(",
"$",
"admin_mode",
")",
"{",
"if",
"(",
"current_user_can",
"(",
"'edit_post'",
",",
"$",
"first_id",
")",
")",
"{",
"break",
";",
"}",
"else",
"{",
"next",
"(",
"$",
"pos",
")",
";",
"}",
"}",
"else",
"{",
"if",
"(",
"in_array",
"(",
"$",
"order",
"[",
"$",
"first_id",
"]",
"[",
"'post_status'",
"]",
",",
"[",
"'publish'",
",",
"'web-only'",
"]",
",",
"true",
")",
")",
"{",
"break",
";",
"}",
"elseif",
"(",
"current_user_can_for_blog",
"(",
"$",
"blog_id",
",",
"'read_private_posts'",
")",
")",
"{",
"break",
";",
"}",
"elseif",
"(",
"get_option",
"(",
"'permissive_private_content'",
")",
"&&",
"current_user_can_for_blog",
"(",
"$",
"blog_id",
",",
"'read'",
")",
")",
"{",
"break",
";",
"}",
"else",
"{",
"next",
"(",
"$",
"pos",
")",
";",
"}",
"}",
"}",
"if",
"(",
"$",
"return_post_id",
")",
"{",
"return",
"(",
"int",
")",
"$",
"first_id",
";",
"}",
"else",
"{",
"return",
"(",
"empty",
"(",
"$",
"first_id",
")",
")",
"?",
"'/'",
":",
"get_permalink",
"(",
"$",
"first_id",
")",
";",
"}",
"}"
] | Select the very first post in a book. May be a chapter or a front matter post
@param bool $return_post_id (optional)
@param bool $admin_mode (optional)
@return mixed URL of first post, or Post ID if $return_post_id is set to true | [
"Select",
"the",
"very",
"first",
"post",
"in",
"a",
"book",
".",
"May",
"be",
"a",
"chapter",
"or",
"a",
"front",
"matter",
"post"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-book.php#L712-L747 |
pressbooks/pressbooks | inc/class-book.php | Book.getChapterNumber | static function getChapterNumber( $post_id, $type_of = 'webbook' ) {
if ( empty( static::$__order ) ) {
self::$__order = static::getBookStructure()['__order'];
}
$lookup = static::$__order;
if ( $type_of === 'webbook' ) {
$post_statii = [ 'web-only', 'publish' ];
} else {
$post_statii = [ 'private', 'publish' ];
}
// Sometimes the chapter number is zero, these are the reasons:
if (
empty( get_option( 'pressbooks_theme_options_global', [] )['chapter_numbers'] ) ||
empty( $lookup[ $post_id ] ) ||
$lookup[ $post_id ]['post_type'] !== 'chapter' ||
! in_array( $lookup[ $post_id ]['post_status'], $post_statii, true )
) {
return 0;
}
// Calculate chapter number
$i = 0;
$type = 'standard';
$found = array_merge( [ 'ID' => $post_id ], $lookup[ $post_id ] ); // @codingStandardsIgnoreLine
foreach ( $lookup as $post_id => $val ) {
if (
$val['post_type'] !== 'chapter' ||
! in_array( $val['post_status'], $post_statii, true )
) {
continue; // Skip
}
$type = \Pressbooks\Taxonomy::init()->getChapterType( $post_id );
if ( 'numberless' !== $type ) {
++$i; // Increase real chapter number
}
if ( $post_id === $found['ID'] ) {
break;
}
}
return ( $type === 'numberless' ) ? 0 : $i;
} | php | static function getChapterNumber( $post_id, $type_of = 'webbook' ) {
if ( empty( static::$__order ) ) {
self::$__order = static::getBookStructure()['__order'];
}
$lookup = static::$__order;
if ( $type_of === 'webbook' ) {
$post_statii = [ 'web-only', 'publish' ];
} else {
$post_statii = [ 'private', 'publish' ];
}
// Sometimes the chapter number is zero, these are the reasons:
if (
empty( get_option( 'pressbooks_theme_options_global', [] )['chapter_numbers'] ) ||
empty( $lookup[ $post_id ] ) ||
$lookup[ $post_id ]['post_type'] !== 'chapter' ||
! in_array( $lookup[ $post_id ]['post_status'], $post_statii, true )
) {
return 0;
}
// Calculate chapter number
$i = 0;
$type = 'standard';
$found = array_merge( [ 'ID' => $post_id ], $lookup[ $post_id ] ); // @codingStandardsIgnoreLine
foreach ( $lookup as $post_id => $val ) {
if (
$val['post_type'] !== 'chapter' ||
! in_array( $val['post_status'], $post_statii, true )
) {
continue; // Skip
}
$type = \Pressbooks\Taxonomy::init()->getChapterType( $post_id );
if ( 'numberless' !== $type ) {
++$i; // Increase real chapter number
}
if ( $post_id === $found['ID'] ) {
break;
}
}
return ( $type === 'numberless' ) ? 0 : $i;
} | [
"static",
"function",
"getChapterNumber",
"(",
"$",
"post_id",
",",
"$",
"type_of",
"=",
"'webbook'",
")",
"{",
"if",
"(",
"empty",
"(",
"static",
"::",
"$",
"__order",
")",
")",
"{",
"self",
"::",
"$",
"__order",
"=",
"static",
"::",
"getBookStructure",
"(",
")",
"[",
"'__order'",
"]",
";",
"}",
"$",
"lookup",
"=",
"static",
"::",
"$",
"__order",
";",
"if",
"(",
"$",
"type_of",
"===",
"'webbook'",
")",
"{",
"$",
"post_statii",
"=",
"[",
"'web-only'",
",",
"'publish'",
"]",
";",
"}",
"else",
"{",
"$",
"post_statii",
"=",
"[",
"'private'",
",",
"'publish'",
"]",
";",
"}",
"// Sometimes the chapter number is zero, these are the reasons:",
"if",
"(",
"empty",
"(",
"get_option",
"(",
"'pressbooks_theme_options_global'",
",",
"[",
"]",
")",
"[",
"'chapter_numbers'",
"]",
")",
"||",
"empty",
"(",
"$",
"lookup",
"[",
"$",
"post_id",
"]",
")",
"||",
"$",
"lookup",
"[",
"$",
"post_id",
"]",
"[",
"'post_type'",
"]",
"!==",
"'chapter'",
"||",
"!",
"in_array",
"(",
"$",
"lookup",
"[",
"$",
"post_id",
"]",
"[",
"'post_status'",
"]",
",",
"$",
"post_statii",
",",
"true",
")",
")",
"{",
"return",
"0",
";",
"}",
"// Calculate chapter number",
"$",
"i",
"=",
"0",
";",
"$",
"type",
"=",
"'standard'",
";",
"$",
"found",
"=",
"array_merge",
"(",
"[",
"'ID'",
"=>",
"$",
"post_id",
"]",
",",
"$",
"lookup",
"[",
"$",
"post_id",
"]",
")",
";",
"// @codingStandardsIgnoreLine",
"foreach",
"(",
"$",
"lookup",
"as",
"$",
"post_id",
"=>",
"$",
"val",
")",
"{",
"if",
"(",
"$",
"val",
"[",
"'post_type'",
"]",
"!==",
"'chapter'",
"||",
"!",
"in_array",
"(",
"$",
"val",
"[",
"'post_status'",
"]",
",",
"$",
"post_statii",
",",
"true",
")",
")",
"{",
"continue",
";",
"// Skip",
"}",
"$",
"type",
"=",
"\\",
"Pressbooks",
"\\",
"Taxonomy",
"::",
"init",
"(",
")",
"->",
"getChapterType",
"(",
"$",
"post_id",
")",
";",
"if",
"(",
"'numberless'",
"!==",
"$",
"type",
")",
"{",
"++",
"$",
"i",
";",
"// Increase real chapter number",
"}",
"if",
"(",
"$",
"post_id",
"===",
"$",
"found",
"[",
"'ID'",
"]",
")",
"{",
"break",
";",
"}",
"}",
"return",
"(",
"$",
"type",
"===",
"'numberless'",
")",
"?",
"0",
":",
"$",
"i",
";",
"}"
] | @since 5.2.0
@param $post_id
@param string $type_of (optional) webbook, exports
@return int | [
"@since",
"5",
".",
"2",
".",
"0"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-book.php#L757-L801 |
pressbooks/pressbooks | inc/class-book.php | Book.consolidatePost | static function consolidatePost( $pid, $post ) {
if ( false === Book::isBook() || wp_is_post_revision( $pid ) || 'auto-draft' === get_post_status( $pid ) ) {
return false;
}
/** @var $wpdb \wpdb */
global $wpdb;
$success = true;
// if this is a new post, set its order
if ( empty( $post->menu_order ) ) {
if ( 'chapter' === $post->post_type ) {
$new = $wpdb->get_var(
$wpdb->prepare(
"SELECT max({$wpdb->posts}.menu_order) + 1
FROM {$wpdb->posts}
WHERE {$wpdb->posts}.post_type = %s
AND NOT {$wpdb->posts}.post_status = 'trash'
AND {$wpdb->posts}.post_parent = %s ",
$post->post_type,
$post->post_parent
)
);
} else {
$new = $wpdb->get_var(
$wpdb->prepare(
"SELECT max({$wpdb->posts}.menu_order) + 1
FROM {$wpdb->posts}
WHERE {$wpdb->posts}.post_type = %s
AND NOT {$wpdb->posts}.post_status = 'trash' ",
$post->post_type
)
);
}
if ( empty( $new ) ) {
$new = 1;
} else {
$new = absint( $new );
}
$success = $wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->posts}
SET {$wpdb->posts}.menu_order = %d
WHERE {$wpdb->posts}.ID = %d ",
$new,
$post->ID
)
);
clean_post_cache( $post );
}
return $success ? true : false;
} | php | static function consolidatePost( $pid, $post ) {
if ( false === Book::isBook() || wp_is_post_revision( $pid ) || 'auto-draft' === get_post_status( $pid ) ) {
return false;
}
/** @var $wpdb \wpdb */
global $wpdb;
$success = true;
// if this is a new post, set its order
if ( empty( $post->menu_order ) ) {
if ( 'chapter' === $post->post_type ) {
$new = $wpdb->get_var(
$wpdb->prepare(
"SELECT max({$wpdb->posts}.menu_order) + 1
FROM {$wpdb->posts}
WHERE {$wpdb->posts}.post_type = %s
AND NOT {$wpdb->posts}.post_status = 'trash'
AND {$wpdb->posts}.post_parent = %s ",
$post->post_type,
$post->post_parent
)
);
} else {
$new = $wpdb->get_var(
$wpdb->prepare(
"SELECT max({$wpdb->posts}.menu_order) + 1
FROM {$wpdb->posts}
WHERE {$wpdb->posts}.post_type = %s
AND NOT {$wpdb->posts}.post_status = 'trash' ",
$post->post_type
)
);
}
if ( empty( $new ) ) {
$new = 1;
} else {
$new = absint( $new );
}
$success = $wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->posts}
SET {$wpdb->posts}.menu_order = %d
WHERE {$wpdb->posts}.ID = %d ",
$new,
$post->ID
)
);
clean_post_cache( $post );
}
return $success ? true : false;
} | [
"static",
"function",
"consolidatePost",
"(",
"$",
"pid",
",",
"$",
"post",
")",
"{",
"if",
"(",
"false",
"===",
"Book",
"::",
"isBook",
"(",
")",
"||",
"wp_is_post_revision",
"(",
"$",
"pid",
")",
"||",
"'auto-draft'",
"===",
"get_post_status",
"(",
"$",
"pid",
")",
")",
"{",
"return",
"false",
";",
"}",
"/** @var $wpdb \\wpdb */",
"global",
"$",
"wpdb",
";",
"$",
"success",
"=",
"true",
";",
"// if this is a new post, set its order",
"if",
"(",
"empty",
"(",
"$",
"post",
"->",
"menu_order",
")",
")",
"{",
"if",
"(",
"'chapter'",
"===",
"$",
"post",
"->",
"post_type",
")",
"{",
"$",
"new",
"=",
"$",
"wpdb",
"->",
"get_var",
"(",
"$",
"wpdb",
"->",
"prepare",
"(",
"\"SELECT max({$wpdb->posts}.menu_order) + 1\n\t\t\t\t\t\tFROM {$wpdb->posts}\n\t\t\t\t\t\tWHERE {$wpdb->posts}.post_type = %s\n\t\t\t\t\t\tAND NOT {$wpdb->posts}.post_status = 'trash'\n\t\t\t\t\t\tAND {$wpdb->posts}.post_parent = %s \"",
",",
"$",
"post",
"->",
"post_type",
",",
"$",
"post",
"->",
"post_parent",
")",
")",
";",
"}",
"else",
"{",
"$",
"new",
"=",
"$",
"wpdb",
"->",
"get_var",
"(",
"$",
"wpdb",
"->",
"prepare",
"(",
"\"SELECT max({$wpdb->posts}.menu_order) + 1\n\t\t\t\t\t\tFROM {$wpdb->posts}\n\t\t\t\t\t\tWHERE {$wpdb->posts}.post_type = %s\n\t\t\t\t\t\tAND NOT {$wpdb->posts}.post_status = 'trash' \"",
",",
"$",
"post",
"->",
"post_type",
")",
")",
";",
"}",
"if",
"(",
"empty",
"(",
"$",
"new",
")",
")",
"{",
"$",
"new",
"=",
"1",
";",
"}",
"else",
"{",
"$",
"new",
"=",
"absint",
"(",
"$",
"new",
")",
";",
"}",
"$",
"success",
"=",
"$",
"wpdb",
"->",
"query",
"(",
"$",
"wpdb",
"->",
"prepare",
"(",
"\"UPDATE {$wpdb->posts}\n\t\t\t\t\tSET {$wpdb->posts}.menu_order = %d\n\t\t\t\t\tWHERE {$wpdb->posts}.ID = %d \"",
",",
"$",
"new",
",",
"$",
"post",
"->",
"ID",
")",
")",
";",
"clean_post_cache",
"(",
"$",
"post",
")",
";",
"}",
"return",
"$",
"success",
"?",
"true",
":",
"false",
";",
"}"
] | Ensures this chapter/part/front matter has a "menu_order" when it is saved
@param integer $pid Post ID
@param \WP_Post $post Post
@return bool | [
"Ensures",
"this",
"chapter",
"/",
"part",
"/",
"front",
"matter",
"has",
"a",
"menu_order",
"when",
"it",
"is",
"saved"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-book.php#L812-L868 |
pressbooks/pressbooks | inc/class-book.php | Book.deletePost | static function deletePost( $pid ) {
if ( false === Book::isBook() || wp_is_post_revision( $pid ) || 'auto-draft' === get_post_status( $pid ) ) {
return false;
}
/** @var $wpdb \wpdb */
global $wpdb;
// remove chapter/part/front matter
// decrement order of everything with a higher order, and if chapter, only within that part
$post_to_delete = get_post( $pid );
$order = $post_to_delete->menu_order;
$type = $post_to_delete->post_type;
$parent = $post_to_delete->post_parent;
if ( 'chapter' === $type ) {
$success = $wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->posts} SET menu_order = menu_order - 1 WHERE menu_order > %d AND post_type = %s AND post_parent = %d ",
$order,
$type,
$parent
)
);
} else {
$success = $wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->posts} SET menu_order = menu_order - 1 WHERE menu_order > %d AND post_type = %s ",
$order,
$type
)
);
}
clean_post_cache( $post_to_delete );
if ( 'part' === $type ) {
// We're setting two things here - the new post_parent (to the first part)
// And the new menu order for the chapters that were in the part being deleted.
$new_parent_id = $wpdb->get_var(
$wpdb->prepare(
"SELECT ID FROM {$wpdb->posts} WHERE post_type = 'part' AND post_status = 'publish' AND NOT ID = %d ORDER BY menu_order LIMIT 1 ",
$pid
)
);
if ( $new_parent_id ) {
$existing_numposts = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(1) AS numposts FROM {$wpdb->posts} WHERE post_type = 'chapter' AND post_parent = %d ",
$new_parent_id
)
);
$success = $wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->posts} SET post_parent = %d, menu_order = menu_order + %d WHERE post_parent = %d AND post_type = 'chapter' ",
$new_parent_id,
$existing_numposts,
$pid
)
);
} else {
$success = $wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->posts} SET post_status = 'trash' WHERE post_parent = %d AND post_type = 'chapter' ",
$pid
)
);
}
wp_cache_flush();
}
static::deleteBookObjectCache();
return $success ? true : false;
} | php | static function deletePost( $pid ) {
if ( false === Book::isBook() || wp_is_post_revision( $pid ) || 'auto-draft' === get_post_status( $pid ) ) {
return false;
}
/** @var $wpdb \wpdb */
global $wpdb;
// remove chapter/part/front matter
// decrement order of everything with a higher order, and if chapter, only within that part
$post_to_delete = get_post( $pid );
$order = $post_to_delete->menu_order;
$type = $post_to_delete->post_type;
$parent = $post_to_delete->post_parent;
if ( 'chapter' === $type ) {
$success = $wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->posts} SET menu_order = menu_order - 1 WHERE menu_order > %d AND post_type = %s AND post_parent = %d ",
$order,
$type,
$parent
)
);
} else {
$success = $wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->posts} SET menu_order = menu_order - 1 WHERE menu_order > %d AND post_type = %s ",
$order,
$type
)
);
}
clean_post_cache( $post_to_delete );
if ( 'part' === $type ) {
// We're setting two things here - the new post_parent (to the first part)
// And the new menu order for the chapters that were in the part being deleted.
$new_parent_id = $wpdb->get_var(
$wpdb->prepare(
"SELECT ID FROM {$wpdb->posts} WHERE post_type = 'part' AND post_status = 'publish' AND NOT ID = %d ORDER BY menu_order LIMIT 1 ",
$pid
)
);
if ( $new_parent_id ) {
$existing_numposts = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(1) AS numposts FROM {$wpdb->posts} WHERE post_type = 'chapter' AND post_parent = %d ",
$new_parent_id
)
);
$success = $wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->posts} SET post_parent = %d, menu_order = menu_order + %d WHERE post_parent = %d AND post_type = 'chapter' ",
$new_parent_id,
$existing_numposts,
$pid
)
);
} else {
$success = $wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->posts} SET post_status = 'trash' WHERE post_parent = %d AND post_type = 'chapter' ",
$pid
)
);
}
wp_cache_flush();
}
static::deleteBookObjectCache();
return $success ? true : false;
} | [
"static",
"function",
"deletePost",
"(",
"$",
"pid",
")",
"{",
"if",
"(",
"false",
"===",
"Book",
"::",
"isBook",
"(",
")",
"||",
"wp_is_post_revision",
"(",
"$",
"pid",
")",
"||",
"'auto-draft'",
"===",
"get_post_status",
"(",
"$",
"pid",
")",
")",
"{",
"return",
"false",
";",
"}",
"/** @var $wpdb \\wpdb */",
"global",
"$",
"wpdb",
";",
"// remove chapter/part/front matter",
"// decrement order of everything with a higher order, and if chapter, only within that part",
"$",
"post_to_delete",
"=",
"get_post",
"(",
"$",
"pid",
")",
";",
"$",
"order",
"=",
"$",
"post_to_delete",
"->",
"menu_order",
";",
"$",
"type",
"=",
"$",
"post_to_delete",
"->",
"post_type",
";",
"$",
"parent",
"=",
"$",
"post_to_delete",
"->",
"post_parent",
";",
"if",
"(",
"'chapter'",
"===",
"$",
"type",
")",
"{",
"$",
"success",
"=",
"$",
"wpdb",
"->",
"query",
"(",
"$",
"wpdb",
"->",
"prepare",
"(",
"\"UPDATE {$wpdb->posts} SET menu_order = menu_order - 1 WHERE menu_order > %d AND post_type = %s AND post_parent = %d \"",
",",
"$",
"order",
",",
"$",
"type",
",",
"$",
"parent",
")",
")",
";",
"}",
"else",
"{",
"$",
"success",
"=",
"$",
"wpdb",
"->",
"query",
"(",
"$",
"wpdb",
"->",
"prepare",
"(",
"\"UPDATE {$wpdb->posts} SET menu_order = menu_order - 1 WHERE menu_order > %d AND post_type = %s \"",
",",
"$",
"order",
",",
"$",
"type",
")",
")",
";",
"}",
"clean_post_cache",
"(",
"$",
"post_to_delete",
")",
";",
"if",
"(",
"'part'",
"===",
"$",
"type",
")",
"{",
"// We're setting two things here - the new post_parent (to the first part)",
"// And the new menu order for the chapters that were in the part being deleted.",
"$",
"new_parent_id",
"=",
"$",
"wpdb",
"->",
"get_var",
"(",
"$",
"wpdb",
"->",
"prepare",
"(",
"\"SELECT ID FROM {$wpdb->posts} WHERE post_type = 'part' AND post_status = 'publish' AND NOT ID = %d ORDER BY menu_order LIMIT 1 \"",
",",
"$",
"pid",
")",
")",
";",
"if",
"(",
"$",
"new_parent_id",
")",
"{",
"$",
"existing_numposts",
"=",
"$",
"wpdb",
"->",
"get_var",
"(",
"$",
"wpdb",
"->",
"prepare",
"(",
"\"SELECT COUNT(1) AS numposts FROM {$wpdb->posts} WHERE post_type = 'chapter' AND post_parent = %d \"",
",",
"$",
"new_parent_id",
")",
")",
";",
"$",
"success",
"=",
"$",
"wpdb",
"->",
"query",
"(",
"$",
"wpdb",
"->",
"prepare",
"(",
"\"UPDATE {$wpdb->posts} SET post_parent = %d, menu_order = menu_order + %d WHERE post_parent = %d AND post_type = 'chapter' \"",
",",
"$",
"new_parent_id",
",",
"$",
"existing_numposts",
",",
"$",
"pid",
")",
")",
";",
"}",
"else",
"{",
"$",
"success",
"=",
"$",
"wpdb",
"->",
"query",
"(",
"$",
"wpdb",
"->",
"prepare",
"(",
"\"UPDATE {$wpdb->posts} SET post_status = 'trash' WHERE post_parent = %d AND post_type = 'chapter' \"",
",",
"$",
"pid",
")",
")",
";",
"}",
"wp_cache_flush",
"(",
")",
";",
"}",
"static",
"::",
"deleteBookObjectCache",
"(",
")",
";",
"return",
"$",
"success",
"?",
"true",
":",
"false",
";",
"}"
] | Put a Part/Chapter/Front Matter/Back Matter in the trash
@param int $pid
@return bool | [
"Put",
"a",
"Part",
"/",
"Chapter",
"/",
"Front",
"Matter",
"/",
"Back",
"Matter",
"in",
"the",
"trash"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-book.php#L878-L958 |
pressbooks/pressbooks | inc/class-book.php | Book.getPostsIdsToExport | static protected function getPostsIdsToExport() {
$post_ids_to_export = [];
global $wpdb;
$results = $wpdb->get_results(
$wpdb->prepare(
"SELECT ID FROM {$wpdb->posts} WHERE post_status IN (%s, %s) AND post_type IN (%s, %s, %s, %s, %s)",
[ 'private', 'publish', 'front-matter', 'part', 'chapter', 'back-matter', 'glossary' ]
), ARRAY_A
);
foreach ( $results as $val ) {
$post_ids_to_export[ $val['ID'] ] = 'on';
}
return $post_ids_to_export;
} | php | static protected function getPostsIdsToExport() {
$post_ids_to_export = [];
global $wpdb;
$results = $wpdb->get_results(
$wpdb->prepare(
"SELECT ID FROM {$wpdb->posts} WHERE post_status IN (%s, %s) AND post_type IN (%s, %s, %s, %s, %s)",
[ 'private', 'publish', 'front-matter', 'part', 'chapter', 'back-matter', 'glossary' ]
), ARRAY_A
);
foreach ( $results as $val ) {
$post_ids_to_export[ $val['ID'] ] = 'on';
}
return $post_ids_to_export;
} | [
"static",
"protected",
"function",
"getPostsIdsToExport",
"(",
")",
"{",
"$",
"post_ids_to_export",
"=",
"[",
"]",
";",
"global",
"$",
"wpdb",
";",
"$",
"results",
"=",
"$",
"wpdb",
"->",
"get_results",
"(",
"$",
"wpdb",
"->",
"prepare",
"(",
"\"SELECT ID FROM {$wpdb->posts} WHERE post_status IN (%s, %s) AND post_type IN (%s, %s, %s, %s, %s)\"",
",",
"[",
"'private'",
",",
"'publish'",
",",
"'front-matter'",
",",
"'part'",
",",
"'chapter'",
",",
"'back-matter'",
",",
"'glossary'",
"]",
")",
",",
"ARRAY_A",
")",
";",
"foreach",
"(",
"$",
"results",
"as",
"$",
"val",
")",
"{",
"$",
"post_ids_to_export",
"[",
"$",
"val",
"[",
"'ID'",
"]",
"]",
"=",
"'on'",
";",
"}",
"return",
"$",
"post_ids_to_export",
";",
"}"
] | Fetch all pb_export meta values for this book
@return array | [
"Fetch",
"all",
"pb_export",
"meta",
"values",
"for",
"this",
"book"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-book.php#L965-L981 |
pressbooks/pressbooks | inc/shortcodes/footnotes/class-footnotes.php | Footnotes.shortcodeHandler | function shortcodeHandler( $atts, $content = '' ) {
global $id; // This is the Post ID, [@see WP_Query::setup_postdata, ...]
$a = shortcode_atts(
[
'numbered' => 'yes',
'symbol' => '*',
'suptext' => ' ',
], $atts
);
if ( ! $content ) {
return '';
}
if ( ! isset( $this->footnotes[ $id ] ) ) {
$this->footnotes[ $id ] = [];
if ( 'no' === $a['numbered'] ) {
$this->numbered[ $id ] = false;
} else {
$this->numbered[ $id ] = true;
}
}
$this->footnotes[ $id ][] = $content;
$footnotes = $this->footnotes[ $id ];
$num = count( $footnotes );
$numlabel = "$id-$num";
$retval = '<a class="footnote" title="' . \Pressbooks\Sanitize\sanitize_xml_attribute( wp_strip_all_tags( $content ) ) . '" id="return-footnote-' . $numlabel . '" href="#footnote-' . $numlabel . '"><sup class="footnote">[';
if ( $this->numbered[ $id ] ) {
$retval .= $num;
} else {
$retval .= $a['symbol'];
}
if ( trim( $a['suptext'] ) ) {
if ( $this->numbered[ $id ] ) {
$retval .= '. ';
}
$retval .= $a['suptext'];
}
$retval .= ']</sup></a>';
return $retval;
} | php | function shortcodeHandler( $atts, $content = '' ) {
global $id; // This is the Post ID, [@see WP_Query::setup_postdata, ...]
$a = shortcode_atts(
[
'numbered' => 'yes',
'symbol' => '*',
'suptext' => ' ',
], $atts
);
if ( ! $content ) {
return '';
}
if ( ! isset( $this->footnotes[ $id ] ) ) {
$this->footnotes[ $id ] = [];
if ( 'no' === $a['numbered'] ) {
$this->numbered[ $id ] = false;
} else {
$this->numbered[ $id ] = true;
}
}
$this->footnotes[ $id ][] = $content;
$footnotes = $this->footnotes[ $id ];
$num = count( $footnotes );
$numlabel = "$id-$num";
$retval = '<a class="footnote" title="' . \Pressbooks\Sanitize\sanitize_xml_attribute( wp_strip_all_tags( $content ) ) . '" id="return-footnote-' . $numlabel . '" href="#footnote-' . $numlabel . '"><sup class="footnote">[';
if ( $this->numbered[ $id ] ) {
$retval .= $num;
} else {
$retval .= $a['symbol'];
}
if ( trim( $a['suptext'] ) ) {
if ( $this->numbered[ $id ] ) {
$retval .= '. ';
}
$retval .= $a['suptext'];
}
$retval .= ']</sup></a>';
return $retval;
} | [
"function",
"shortcodeHandler",
"(",
"$",
"atts",
",",
"$",
"content",
"=",
"''",
")",
"{",
"global",
"$",
"id",
";",
"// This is the Post ID, [@see WP_Query::setup_postdata, ...]",
"$",
"a",
"=",
"shortcode_atts",
"(",
"[",
"'numbered'",
"=>",
"'yes'",
",",
"'symbol'",
"=>",
"'*'",
",",
"'suptext'",
"=>",
"' '",
",",
"]",
",",
"$",
"atts",
")",
";",
"if",
"(",
"!",
"$",
"content",
")",
"{",
"return",
"''",
";",
"}",
"if",
"(",
"!",
"isset",
"(",
"$",
"this",
"->",
"footnotes",
"[",
"$",
"id",
"]",
")",
")",
"{",
"$",
"this",
"->",
"footnotes",
"[",
"$",
"id",
"]",
"=",
"[",
"]",
";",
"if",
"(",
"'no'",
"===",
"$",
"a",
"[",
"'numbered'",
"]",
")",
"{",
"$",
"this",
"->",
"numbered",
"[",
"$",
"id",
"]",
"=",
"false",
";",
"}",
"else",
"{",
"$",
"this",
"->",
"numbered",
"[",
"$",
"id",
"]",
"=",
"true",
";",
"}",
"}",
"$",
"this",
"->",
"footnotes",
"[",
"$",
"id",
"]",
"[",
"]",
"=",
"$",
"content",
";",
"$",
"footnotes",
"=",
"$",
"this",
"->",
"footnotes",
"[",
"$",
"id",
"]",
";",
"$",
"num",
"=",
"count",
"(",
"$",
"footnotes",
")",
";",
"$",
"numlabel",
"=",
"\"$id-$num\"",
";",
"$",
"retval",
"=",
"'<a class=\"footnote\" title=\"'",
".",
"\\",
"Pressbooks",
"\\",
"Sanitize",
"\\",
"sanitize_xml_attribute",
"(",
"wp_strip_all_tags",
"(",
"$",
"content",
")",
")",
".",
"'\" id=\"return-footnote-'",
".",
"$",
"numlabel",
".",
"'\" href=\"#footnote-'",
".",
"$",
"numlabel",
".",
"'\"><sup class=\"footnote\">['",
";",
"if",
"(",
"$",
"this",
"->",
"numbered",
"[",
"$",
"id",
"]",
")",
"{",
"$",
"retval",
".=",
"$",
"num",
";",
"}",
"else",
"{",
"$",
"retval",
".=",
"$",
"a",
"[",
"'symbol'",
"]",
";",
"}",
"if",
"(",
"trim",
"(",
"$",
"a",
"[",
"'suptext'",
"]",
")",
")",
"{",
"if",
"(",
"$",
"this",
"->",
"numbered",
"[",
"$",
"id",
"]",
")",
"{",
"$",
"retval",
".=",
"'. '",
";",
"}",
"$",
"retval",
".=",
"$",
"a",
"[",
"'suptext'",
"]",
";",
"}",
"$",
"retval",
".=",
"']</sup></a>'",
";",
"return",
"$",
"retval",
";",
"}"
] | Pre-process footnote shortcode
@param array $atts
@param string $content
@return string | [
"Pre",
"-",
"process",
"footnote",
"shortcode"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/shortcodes/footnotes/class-footnotes.php#L70-L118 |
pressbooks/pressbooks | inc/shortcodes/footnotes/class-footnotes.php | Footnotes.footnoteContent | function footnoteContent( $content ) {
global $id; // This is the Post ID, [@see WP_Query::setup_postdata, ...]
if ( ! empty( $this->footnotes ) && isset( $this->footnotes[ $id ] ) ) {
$footnotes = $this->footnotes[ $id ];
} else {
return $content;
}
if ( $this->numbered[ $id ] ) {
$content .= '<hr class="before-footnotes" /><div class="footnotes"><ol>';
} else {
$content .= '<hr class="before-footnotes" /><div class="footnotes"><ul>';
}
foreach ( $footnotes as $num => $footnote ) {
$num++;
$numlabel = "$id-$num";
$content .= '<li id="footnote-' . $numlabel . '">' . make_clickable( $footnote ) . ' <a href="#return-footnote-' . $numlabel . '" class="return-footnote">↵</a></li>';
}
if ( $this->numbered[ $id ] ) {
$content .= '</ol></div>';
} else {
$content .= '</ul></div>';
}
unset( $this->footnotes[ $id ] ); // Done, reset
return $content;
} | php | function footnoteContent( $content ) {
global $id; // This is the Post ID, [@see WP_Query::setup_postdata, ...]
if ( ! empty( $this->footnotes ) && isset( $this->footnotes[ $id ] ) ) {
$footnotes = $this->footnotes[ $id ];
} else {
return $content;
}
if ( $this->numbered[ $id ] ) {
$content .= '<hr class="before-footnotes" /><div class="footnotes"><ol>';
} else {
$content .= '<hr class="before-footnotes" /><div class="footnotes"><ul>';
}
foreach ( $footnotes as $num => $footnote ) {
$num++;
$numlabel = "$id-$num";
$content .= '<li id="footnote-' . $numlabel . '">' . make_clickable( $footnote ) . ' <a href="#return-footnote-' . $numlabel . '" class="return-footnote">↵</a></li>';
}
if ( $this->numbered[ $id ] ) {
$content .= '</ol></div>';
} else {
$content .= '</ul></div>';
}
unset( $this->footnotes[ $id ] ); // Done, reset
return $content;
} | [
"function",
"footnoteContent",
"(",
"$",
"content",
")",
"{",
"global",
"$",
"id",
";",
"// This is the Post ID, [@see WP_Query::setup_postdata, ...]",
"if",
"(",
"!",
"empty",
"(",
"$",
"this",
"->",
"footnotes",
")",
"&&",
"isset",
"(",
"$",
"this",
"->",
"footnotes",
"[",
"$",
"id",
"]",
")",
")",
"{",
"$",
"footnotes",
"=",
"$",
"this",
"->",
"footnotes",
"[",
"$",
"id",
"]",
";",
"}",
"else",
"{",
"return",
"$",
"content",
";",
"}",
"if",
"(",
"$",
"this",
"->",
"numbered",
"[",
"$",
"id",
"]",
")",
"{",
"$",
"content",
".=",
"'<hr class=\"before-footnotes\" /><div class=\"footnotes\"><ol>'",
";",
"}",
"else",
"{",
"$",
"content",
".=",
"'<hr class=\"before-footnotes\" /><div class=\"footnotes\"><ul>'",
";",
"}",
"foreach",
"(",
"$",
"footnotes",
"as",
"$",
"num",
"=>",
"$",
"footnote",
")",
"{",
"$",
"num",
"++",
";",
"$",
"numlabel",
"=",
"\"$id-$num\"",
";",
"$",
"content",
".=",
"'<li id=\"footnote-'",
".",
"$",
"numlabel",
".",
"'\">'",
".",
"make_clickable",
"(",
"$",
"footnote",
")",
".",
"' <a href=\"#return-footnote-'",
".",
"$",
"numlabel",
".",
"'\" class=\"return-footnote\">↵</a></li>'",
";",
"}",
"if",
"(",
"$",
"this",
"->",
"numbered",
"[",
"$",
"id",
"]",
")",
"{",
"$",
"content",
".=",
"'</ol></div>'",
";",
"}",
"else",
"{",
"$",
"content",
".=",
"'</ul></div>'",
";",
"}",
"unset",
"(",
"$",
"this",
"->",
"footnotes",
"[",
"$",
"id",
"]",
")",
";",
"// Done, reset",
"return",
"$",
"content",
";",
"}"
] | Post-process footnote shortcode
@param $content
@return string | [
"Post",
"-",
"process",
"footnote",
"shortcode"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/shortcodes/footnotes/class-footnotes.php#L128-L159 |
pressbooks/pressbooks | inc/shortcodes/footnotes/class-footnotes.php | Footnotes.convertWordFootnotes | static function convertWordFootnotes() {
if ( ! current_user_can( 'edit_posts' ) || ! check_ajax_referer( 'pb-footnote-convert', false, false ) ) {
static::ajaxFailure( __( 'Invalid permissions.', 'pressbooks' ) );
return;
}
$html = urldecode( stripslashes( $_POST['content'] ) );
/**
* Regular expression tip:
* (?: ), in contrast to ( ), is used to avoid capturing text.
*
* A $pattern must capture:
* [0] => ... full capture ...
* [1] => #_ftnref130
* [2] => 130
* [3] => ... the text we want to move ...
*
* Known MS Word variations:
* href="#_ftn123" (-> #_ftnref123)
* href="#_edn123" (-> #_ednref123)
* href="/Users/foo/Documents/bar/9781426766497.doc#_ftn123" (-> .doc#_ftnref123)
* href="/Users/foo/Documents/bar/9781426766497.docx#_edn123" (-> .docx#_ednref123)
*
* Known Libre Office variations:
* href="#sdfootnote123sym" (-> #sdfootnote123anc)
*/
$patterns = [
'~<a[\s]+[^>]*?href[\s]?=[\s"\']+(#_ftnref([0-9]+))["\']+.*?>(?:[^<]+|.*?)?</a>(.*?)</div>~si', // MS Word
'~<a[\s]+[^>]*?href[\s]?=[\s"\']+.*?[\.doc|\.docx](#_ftnref([0-9]+))["\']+.*?>(?:[^<]+|.*?)?</a>(.*?)</div>~si', // MS Word
'~<a[\s]+[^>]*?href[\s]?=[\s"\']+(#_ednref([0-9]+))["\']+.*?>(?:[^<]+|.*?)?</a>(.*?)</div>~si', // MS Word
'~<a[\s]+[^>]*?href[\s]?=[\s"\']+.*?[\.doc|\.docx](#_ednref([0-9]+))["\']+.*?>(?:[^<]+|.*?)?</a>(.*?)</div>~si', // MS Word
'~<a[\s]+[^>]*?href[\s]?=[\s"\']+(#sdfootnote([0-9]+)anc)["\']+.*?>(?:[^<]+|.*?)?</a>(.*?)</div>~si', // Libre Office
];
/**
* A $replacer must be in the same position as a corresponding $pattern above,
* use __REPLACE_ME__ to substitute for what we don't know yet.
*/
$replacers = [
'~<a[\s]+[^>]*?href[\s]?=[\s"\']+(?:#_ftn__REPLACE_ME__)["\']+.*?>(?:[^<]+|.*?)?</a>~si', // MS Word
'~<a[\s]+[^>]*?href[\s]?=[\s"\']+.*?[\.doc|\.docx](?:#_ftn__REPLACE_ME__)["\']+.*?>(?:[^<]+|.*?)?</a>~si', // MS Word
'~<a[\s]+[^>]*?href[\s]?=[\s"\']+(?:#_edn__REPLACE_ME__)["\']+.*?>(?:[^<]+|.*?)?</a>~si', // MS Word
'~<a[\s]+[^>]*?href[\s]?=[\s"\']+.*?[\.doc|\.docx](?:#_edn__REPLACE_ME__)["\']+.*?>(?:[^<]+|.*?)?</a>~si', // MS Word
'~<a[\s]+[^>]*?href[\s]?=[\s"\']+(?:#sdfootnote__REPLACE_ME__sym)["\']+.*?>(?:[^<]+|.*?)?</a>~si', // Libre Office
];
$footnotes = [];
$find = [];
$replace = [];
foreach ( $patterns as $i => $pattern ) {
preg_match_all( $pattern, $html, $footnotes, PREG_SET_ORDER );
foreach ( $footnotes as $footnote ) {
$tmp = wp_kses(
$footnote[3], [
'b' => [],
'em' => [],
'i' => [],
'strong' => [],
]
);
$tmp = \Pressbooks\Sanitize\remove_control_characters( $tmp );
$tmp = trim( preg_replace( '/\s+/', ' ', $tmp ) ); // Normalize white spaces
$find[] = str_replace( '__REPLACE_ME__', preg_quote( $footnote[2] ), $replacers[ $i ] );
$replace[] = '[footnote]' . $tmp . '[/footnote]';
}
// Remove originals when done
$find[] = $pattern;
$replace[] = '…</div>';
}
// Twerk it
$html = preg_replace( $find, $replace, $html );
// Important, complex regular expressions have been known to, literally, crash PHP.
// When testing, make sure this function exits as expected.
// Send back JSON
header( 'Content-Type: application/json' );
$json = wp_json_encode(
[
'content' => $html,
]
);
echo $json;
wp_die();
} | php | static function convertWordFootnotes() {
if ( ! current_user_can( 'edit_posts' ) || ! check_ajax_referer( 'pb-footnote-convert', false, false ) ) {
static::ajaxFailure( __( 'Invalid permissions.', 'pressbooks' ) );
return;
}
$html = urldecode( stripslashes( $_POST['content'] ) );
/**
* Regular expression tip:
* (?: ), in contrast to ( ), is used to avoid capturing text.
*
* A $pattern must capture:
* [0] => ... full capture ...
* [1] => #_ftnref130
* [2] => 130
* [3] => ... the text we want to move ...
*
* Known MS Word variations:
* href="#_ftn123" (-> #_ftnref123)
* href="#_edn123" (-> #_ednref123)
* href="/Users/foo/Documents/bar/9781426766497.doc#_ftn123" (-> .doc#_ftnref123)
* href="/Users/foo/Documents/bar/9781426766497.docx#_edn123" (-> .docx#_ednref123)
*
* Known Libre Office variations:
* href="#sdfootnote123sym" (-> #sdfootnote123anc)
*/
$patterns = [
'~<a[\s]+[^>]*?href[\s]?=[\s"\']+(#_ftnref([0-9]+))["\']+.*?>(?:[^<]+|.*?)?</a>(.*?)</div>~si', // MS Word
'~<a[\s]+[^>]*?href[\s]?=[\s"\']+.*?[\.doc|\.docx](#_ftnref([0-9]+))["\']+.*?>(?:[^<]+|.*?)?</a>(.*?)</div>~si', // MS Word
'~<a[\s]+[^>]*?href[\s]?=[\s"\']+(#_ednref([0-9]+))["\']+.*?>(?:[^<]+|.*?)?</a>(.*?)</div>~si', // MS Word
'~<a[\s]+[^>]*?href[\s]?=[\s"\']+.*?[\.doc|\.docx](#_ednref([0-9]+))["\']+.*?>(?:[^<]+|.*?)?</a>(.*?)</div>~si', // MS Word
'~<a[\s]+[^>]*?href[\s]?=[\s"\']+(#sdfootnote([0-9]+)anc)["\']+.*?>(?:[^<]+|.*?)?</a>(.*?)</div>~si', // Libre Office
];
/**
* A $replacer must be in the same position as a corresponding $pattern above,
* use __REPLACE_ME__ to substitute for what we don't know yet.
*/
$replacers = [
'~<a[\s]+[^>]*?href[\s]?=[\s"\']+(?:#_ftn__REPLACE_ME__)["\']+.*?>(?:[^<]+|.*?)?</a>~si', // MS Word
'~<a[\s]+[^>]*?href[\s]?=[\s"\']+.*?[\.doc|\.docx](?:#_ftn__REPLACE_ME__)["\']+.*?>(?:[^<]+|.*?)?</a>~si', // MS Word
'~<a[\s]+[^>]*?href[\s]?=[\s"\']+(?:#_edn__REPLACE_ME__)["\']+.*?>(?:[^<]+|.*?)?</a>~si', // MS Word
'~<a[\s]+[^>]*?href[\s]?=[\s"\']+.*?[\.doc|\.docx](?:#_edn__REPLACE_ME__)["\']+.*?>(?:[^<]+|.*?)?</a>~si', // MS Word
'~<a[\s]+[^>]*?href[\s]?=[\s"\']+(?:#sdfootnote__REPLACE_ME__sym)["\']+.*?>(?:[^<]+|.*?)?</a>~si', // Libre Office
];
$footnotes = [];
$find = [];
$replace = [];
foreach ( $patterns as $i => $pattern ) {
preg_match_all( $pattern, $html, $footnotes, PREG_SET_ORDER );
foreach ( $footnotes as $footnote ) {
$tmp = wp_kses(
$footnote[3], [
'b' => [],
'em' => [],
'i' => [],
'strong' => [],
]
);
$tmp = \Pressbooks\Sanitize\remove_control_characters( $tmp );
$tmp = trim( preg_replace( '/\s+/', ' ', $tmp ) ); // Normalize white spaces
$find[] = str_replace( '__REPLACE_ME__', preg_quote( $footnote[2] ), $replacers[ $i ] );
$replace[] = '[footnote]' . $tmp . '[/footnote]';
}
// Remove originals when done
$find[] = $pattern;
$replace[] = '…</div>';
}
// Twerk it
$html = preg_replace( $find, $replace, $html );
// Important, complex regular expressions have been known to, literally, crash PHP.
// When testing, make sure this function exits as expected.
// Send back JSON
header( 'Content-Type: application/json' );
$json = wp_json_encode(
[
'content' => $html,
]
);
echo $json;
wp_die();
} | [
"static",
"function",
"convertWordFootnotes",
"(",
")",
"{",
"if",
"(",
"!",
"current_user_can",
"(",
"'edit_posts'",
")",
"||",
"!",
"check_ajax_referer",
"(",
"'pb-footnote-convert'",
",",
"false",
",",
"false",
")",
")",
"{",
"static",
"::",
"ajaxFailure",
"(",
"__",
"(",
"'Invalid permissions.'",
",",
"'pressbooks'",
")",
")",
";",
"return",
";",
"}",
"$",
"html",
"=",
"urldecode",
"(",
"stripslashes",
"(",
"$",
"_POST",
"[",
"'content'",
"]",
")",
")",
";",
"/**\n\t\t * Regular expression tip:\n\t\t * (?: ), in contrast to ( ), is used to avoid capturing text.\n\t\t *\n\t\t * A $pattern must capture:\n\t\t * [0] => ... full capture ...\n\t\t * [1] => #_ftnref130\n\t\t * [2] => 130\n\t\t * [3] => ... the text we want to move ...\n\t\t *\n\t\t * Known MS Word variations:\n\t\t * href=\"#_ftn123\" (-> #_ftnref123)\n\t\t * href=\"#_edn123\" (-> #_ednref123)\n\t\t * href=\"/Users/foo/Documents/bar/9781426766497.doc#_ftn123\" (-> .doc#_ftnref123)\n\t\t * href=\"/Users/foo/Documents/bar/9781426766497.docx#_edn123\" (-> .docx#_ednref123)\n\t\t *\n\t\t * Known Libre Office variations:\n\t\t * href=\"#sdfootnote123sym\" (-> #sdfootnote123anc)\n\t\t */",
"$",
"patterns",
"=",
"[",
"'~<a[\\s]+[^>]*?href[\\s]?=[\\s\"\\']+(#_ftnref([0-9]+))[\"\\']+.*?>(?:[^<]+|.*?)?</a>(.*?)</div>~si'",
",",
"// MS Word",
"'~<a[\\s]+[^>]*?href[\\s]?=[\\s\"\\']+.*?[\\.doc|\\.docx](#_ftnref([0-9]+))[\"\\']+.*?>(?:[^<]+|.*?)?</a>(.*?)</div>~si'",
",",
"// MS Word",
"'~<a[\\s]+[^>]*?href[\\s]?=[\\s\"\\']+(#_ednref([0-9]+))[\"\\']+.*?>(?:[^<]+|.*?)?</a>(.*?)</div>~si'",
",",
"// MS Word",
"'~<a[\\s]+[^>]*?href[\\s]?=[\\s\"\\']+.*?[\\.doc|\\.docx](#_ednref([0-9]+))[\"\\']+.*?>(?:[^<]+|.*?)?</a>(.*?)</div>~si'",
",",
"// MS Word",
"'~<a[\\s]+[^>]*?href[\\s]?=[\\s\"\\']+(#sdfootnote([0-9]+)anc)[\"\\']+.*?>(?:[^<]+|.*?)?</a>(.*?)</div>~si'",
",",
"// Libre Office",
"]",
";",
"/**\n\t\t * A $replacer must be in the same position as a corresponding $pattern above,\n\t\t * use __REPLACE_ME__ to substitute for what we don't know yet.\n\t\t */",
"$",
"replacers",
"=",
"[",
"'~<a[\\s]+[^>]*?href[\\s]?=[\\s\"\\']+(?:#_ftn__REPLACE_ME__)[\"\\']+.*?>(?:[^<]+|.*?)?</a>~si'",
",",
"// MS Word",
"'~<a[\\s]+[^>]*?href[\\s]?=[\\s\"\\']+.*?[\\.doc|\\.docx](?:#_ftn__REPLACE_ME__)[\"\\']+.*?>(?:[^<]+|.*?)?</a>~si'",
",",
"// MS Word",
"'~<a[\\s]+[^>]*?href[\\s]?=[\\s\"\\']+(?:#_edn__REPLACE_ME__)[\"\\']+.*?>(?:[^<]+|.*?)?</a>~si'",
",",
"// MS Word",
"'~<a[\\s]+[^>]*?href[\\s]?=[\\s\"\\']+.*?[\\.doc|\\.docx](?:#_edn__REPLACE_ME__)[\"\\']+.*?>(?:[^<]+|.*?)?</a>~si'",
",",
"// MS Word",
"'~<a[\\s]+[^>]*?href[\\s]?=[\\s\"\\']+(?:#sdfootnote__REPLACE_ME__sym)[\"\\']+.*?>(?:[^<]+|.*?)?</a>~si'",
",",
"// Libre Office",
"]",
";",
"$",
"footnotes",
"=",
"[",
"]",
";",
"$",
"find",
"=",
"[",
"]",
";",
"$",
"replace",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"patterns",
"as",
"$",
"i",
"=>",
"$",
"pattern",
")",
"{",
"preg_match_all",
"(",
"$",
"pattern",
",",
"$",
"html",
",",
"$",
"footnotes",
",",
"PREG_SET_ORDER",
")",
";",
"foreach",
"(",
"$",
"footnotes",
"as",
"$",
"footnote",
")",
"{",
"$",
"tmp",
"=",
"wp_kses",
"(",
"$",
"footnote",
"[",
"3",
"]",
",",
"[",
"'b'",
"=>",
"[",
"]",
",",
"'em'",
"=>",
"[",
"]",
",",
"'i'",
"=>",
"[",
"]",
",",
"'strong'",
"=>",
"[",
"]",
",",
"]",
")",
";",
"$",
"tmp",
"=",
"\\",
"Pressbooks",
"\\",
"Sanitize",
"\\",
"remove_control_characters",
"(",
"$",
"tmp",
")",
";",
"$",
"tmp",
"=",
"trim",
"(",
"preg_replace",
"(",
"'/\\s+/'",
",",
"' '",
",",
"$",
"tmp",
")",
")",
";",
"// Normalize white spaces",
"$",
"find",
"[",
"]",
"=",
"str_replace",
"(",
"'__REPLACE_ME__'",
",",
"preg_quote",
"(",
"$",
"footnote",
"[",
"2",
"]",
")",
",",
"$",
"replacers",
"[",
"$",
"i",
"]",
")",
";",
"$",
"replace",
"[",
"]",
"=",
"'[footnote]'",
".",
"$",
"tmp",
".",
"'[/footnote]'",
";",
"}",
"// Remove originals when done",
"$",
"find",
"[",
"]",
"=",
"$",
"pattern",
";",
"$",
"replace",
"[",
"]",
"=",
"'…</div>'",
";",
"}",
"// Twerk it",
"$",
"html",
"=",
"preg_replace",
"(",
"$",
"find",
",",
"$",
"replace",
",",
"$",
"html",
")",
";",
"// Important, complex regular expressions have been known to, literally, crash PHP.",
"// When testing, make sure this function exits as expected.",
"// Send back JSON",
"header",
"(",
"'Content-Type: application/json'",
")",
";",
"$",
"json",
"=",
"wp_json_encode",
"(",
"[",
"'content'",
"=>",
"$",
"html",
",",
"]",
")",
";",
"echo",
"$",
"json",
";",
"wp_die",
"(",
")",
";",
"}"
] | WP_Ajax hook. Convert MS Word footnotes to Pressbooks compatible [footnotes] | [
"WP_Ajax",
"hook",
".",
"Convert",
"MS",
"Word",
"footnotes",
"to",
"Pressbooks",
"compatible",
"[",
"footnotes",
"]"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/shortcodes/footnotes/class-footnotes.php#L181-L275 |
Athari/YaLinqo | YaLinqo/EnumerablePagination.php | EnumerablePagination.elementAt | public function elementAt($key)
{
/** @var $it \Iterator|\ArrayAccess */
$it = $this->getIterator();
if ($it instanceof \ArrayAccess) {
if (!$it->offsetExists($key))
throw new \UnexpectedValueException(Errors::NO_KEY);
return $it->offsetGet($key);
}
foreach ($it as $k => $v) {
if ($k === $key)
return $v;
}
throw new \UnexpectedValueException(Errors::NO_KEY);
} | php | public function elementAt($key)
{
/** @var $it \Iterator|\ArrayAccess */
$it = $this->getIterator();
if ($it instanceof \ArrayAccess) {
if (!$it->offsetExists($key))
throw new \UnexpectedValueException(Errors::NO_KEY);
return $it->offsetGet($key);
}
foreach ($it as $k => $v) {
if ($k === $key)
return $v;
}
throw new \UnexpectedValueException(Errors::NO_KEY);
} | [
"public",
"function",
"elementAt",
"(",
"$",
"key",
")",
"{",
"/** @var $it \\Iterator|\\ArrayAccess */",
"$",
"it",
"=",
"$",
"this",
"->",
"getIterator",
"(",
")",
";",
"if",
"(",
"$",
"it",
"instanceof",
"\\",
"ArrayAccess",
")",
"{",
"if",
"(",
"!",
"$",
"it",
"->",
"offsetExists",
"(",
"$",
"key",
")",
")",
"throw",
"new",
"\\",
"UnexpectedValueException",
"(",
"Errors",
"::",
"NO_KEY",
")",
";",
"return",
"$",
"it",
"->",
"offsetGet",
"(",
"$",
"key",
")",
";",
"}",
"foreach",
"(",
"$",
"it",
"as",
"$",
"k",
"=>",
"$",
"v",
")",
"{",
"if",
"(",
"$",
"k",
"===",
"$",
"key",
")",
"return",
"$",
"v",
";",
"}",
"throw",
"new",
"\\",
"UnexpectedValueException",
"(",
"Errors",
"::",
"NO_KEY",
")",
";",
"}"
] | Returns the value at a specified key in a sequence.
<p><b>Syntax</b>: elementAt (key)
<p>Returns the value at a specified key in a sequence.
<p>If the type of source iterator implements {@link ArrayAccess}, that implementation is used to obtain the value at the specified key. Otherwise, this method obtains the specified value.
<p>This method throws an exception if key is not found. To instead return a default value when the specified key is not found, use the {@link elementAtOrDefault} method.
@param mixed $key The key of the value to retrieve.
@throws \UnexpectedValueException If sequence does not contain value with specified key.
@return mixed The value at the key in the source sequence.
@package YaLinqo\Pagination | [
"Returns",
"the",
"value",
"at",
"a",
"specified",
"key",
"in",
"a",
"sequence",
".",
"<p",
">",
"<b",
">",
"Syntax<",
"/",
"b",
">",
":",
"elementAt",
"(",
"key",
")",
"<p",
">",
"Returns",
"the",
"value",
"at",
"a",
"specified",
"key",
"in",
"a",
"sequence",
".",
"<p",
">",
"If",
"the",
"type",
"of",
"source",
"iterator",
"implements",
"{"
] | train | https://github.com/Athari/YaLinqo/blob/939469772b36d4d454836681e0be2f220cf1e5cb/YaLinqo/EnumerablePagination.php#L29-L45 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.