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/modules/themeoptions/class-pdfoptions.php | PDFOptions.renderRunningContentIntroductionRightField | function renderRunningContentIntroductionRightField( $args ) {
unset( $args['label_for'], $args['class'] );
$this->renderCustomSelect(
[
'id' => 'running_content_introduction_right',
'name' => 'running_content_introduction_right',
'value' => getset( $this->options, 'running_content_introduction_right' ),
'choices' => $args,
]
);
$this->renderField(
[
'id' => 'running_content_introduction_right_custom',
'name' => 'pressbooks_theme_options_' . $this->getSlug(),
'option' => 'running_content_introduction_right',
'value' => getset( $this->options, 'running_content_introduction_right' ),
'type' => 'text',
'class' => 'regular-text code',
]
);
} | php | function renderRunningContentIntroductionRightField( $args ) {
unset( $args['label_for'], $args['class'] );
$this->renderCustomSelect(
[
'id' => 'running_content_introduction_right',
'name' => 'running_content_introduction_right',
'value' => getset( $this->options, 'running_content_introduction_right' ),
'choices' => $args,
]
);
$this->renderField(
[
'id' => 'running_content_introduction_right_custom',
'name' => 'pressbooks_theme_options_' . $this->getSlug(),
'option' => 'running_content_introduction_right',
'value' => getset( $this->options, 'running_content_introduction_right' ),
'type' => 'text',
'class' => 'regular-text code',
]
);
} | [
"function",
"renderRunningContentIntroductionRightField",
"(",
"$",
"args",
")",
"{",
"unset",
"(",
"$",
"args",
"[",
"'label_for'",
"]",
",",
"$",
"args",
"[",
"'class'",
"]",
")",
";",
"$",
"this",
"->",
"renderCustomSelect",
"(",
"[",
"'id'",
"=>",
"'running_content_introduction_right'",
",",
"'name'",
"=>",
"'running_content_introduction_right'",
",",
"'value'",
"=>",
"getset",
"(",
"$",
"this",
"->",
"options",
",",
"'running_content_introduction_right'",
")",
",",
"'choices'",
"=>",
"$",
"args",
",",
"]",
")",
";",
"$",
"this",
"->",
"renderField",
"(",
"[",
"'id'",
"=>",
"'running_content_introduction_right_custom'",
",",
"'name'",
"=>",
"'pressbooks_theme_options_'",
".",
"$",
"this",
"->",
"getSlug",
"(",
")",
",",
"'option'",
"=>",
"'running_content_introduction_right'",
",",
"'value'",
"=>",
"getset",
"(",
"$",
"this",
"->",
"options",
",",
"'running_content_introduction_right'",
")",
",",
"'type'",
"=>",
"'text'",
",",
"'class'",
"=>",
"'regular-text code'",
",",
"]",
")",
";",
"}"
] | Render the running_content_introduction_right input.
@param array $args | [
"Render",
"the",
"running_content_introduction_right",
"input",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/themeoptions/class-pdfoptions.php#L1187-L1207 |
pressbooks/pressbooks | inc/modules/themeoptions/class-pdfoptions.php | PDFOptions.renderRunningContentPartLeftField | function renderRunningContentPartLeftField( $args ) {
unset( $args['label_for'], $args['class'] );
$this->renderCustomSelect(
[
'id' => 'running_content_part_left',
'name' => 'running_content_part_left',
'value' => getset( $this->options, 'running_content_part_left' ),
'choices' => $args,
]
);
$this->renderField(
[
'id' => 'running_content_part_left_custom',
'name' => 'pressbooks_theme_options_' . $this->getSlug(),
'option' => 'running_content_part_left',
'value' => getset( $this->options, 'running_content_part_left' ),
'type' => 'text',
'class' => 'regular-text code',
]
);
} | php | function renderRunningContentPartLeftField( $args ) {
unset( $args['label_for'], $args['class'] );
$this->renderCustomSelect(
[
'id' => 'running_content_part_left',
'name' => 'running_content_part_left',
'value' => getset( $this->options, 'running_content_part_left' ),
'choices' => $args,
]
);
$this->renderField(
[
'id' => 'running_content_part_left_custom',
'name' => 'pressbooks_theme_options_' . $this->getSlug(),
'option' => 'running_content_part_left',
'value' => getset( $this->options, 'running_content_part_left' ),
'type' => 'text',
'class' => 'regular-text code',
]
);
} | [
"function",
"renderRunningContentPartLeftField",
"(",
"$",
"args",
")",
"{",
"unset",
"(",
"$",
"args",
"[",
"'label_for'",
"]",
",",
"$",
"args",
"[",
"'class'",
"]",
")",
";",
"$",
"this",
"->",
"renderCustomSelect",
"(",
"[",
"'id'",
"=>",
"'running_content_part_left'",
",",
"'name'",
"=>",
"'running_content_part_left'",
",",
"'value'",
"=>",
"getset",
"(",
"$",
"this",
"->",
"options",
",",
"'running_content_part_left'",
")",
",",
"'choices'",
"=>",
"$",
"args",
",",
"]",
")",
";",
"$",
"this",
"->",
"renderField",
"(",
"[",
"'id'",
"=>",
"'running_content_part_left_custom'",
",",
"'name'",
"=>",
"'pressbooks_theme_options_'",
".",
"$",
"this",
"->",
"getSlug",
"(",
")",
",",
"'option'",
"=>",
"'running_content_part_left'",
",",
"'value'",
"=>",
"getset",
"(",
"$",
"this",
"->",
"options",
",",
"'running_content_part_left'",
")",
",",
"'type'",
"=>",
"'text'",
",",
"'class'",
"=>",
"'regular-text code'",
",",
"]",
")",
";",
"}"
] | Render the running_content_part_left input.
@param array $args | [
"Render",
"the",
"running_content_part_left",
"input",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/themeoptions/class-pdfoptions.php#L1214-L1234 |
pressbooks/pressbooks | inc/modules/themeoptions/class-pdfoptions.php | PDFOptions.renderRunningContentPartRightField | function renderRunningContentPartRightField( $args ) {
unset( $args['label_for'], $args['class'] );
$this->renderCustomSelect(
[
'id' => 'running_content_part_right',
'name' => 'running_content_part_right',
'value' => getset( $this->options, 'running_content_part_right' ),
'choices' => $args,
]
);
$this->renderField(
[
'id' => 'running_content_part_right_custom',
'name' => 'pressbooks_theme_options_' . $this->getSlug(),
'option' => 'running_content_part_right',
'value' => getset( $this->options, 'running_content_part_right' ),
'type' => 'text',
'class' => 'regular-text code',
]
);
} | php | function renderRunningContentPartRightField( $args ) {
unset( $args['label_for'], $args['class'] );
$this->renderCustomSelect(
[
'id' => 'running_content_part_right',
'name' => 'running_content_part_right',
'value' => getset( $this->options, 'running_content_part_right' ),
'choices' => $args,
]
);
$this->renderField(
[
'id' => 'running_content_part_right_custom',
'name' => 'pressbooks_theme_options_' . $this->getSlug(),
'option' => 'running_content_part_right',
'value' => getset( $this->options, 'running_content_part_right' ),
'type' => 'text',
'class' => 'regular-text code',
]
);
} | [
"function",
"renderRunningContentPartRightField",
"(",
"$",
"args",
")",
"{",
"unset",
"(",
"$",
"args",
"[",
"'label_for'",
"]",
",",
"$",
"args",
"[",
"'class'",
"]",
")",
";",
"$",
"this",
"->",
"renderCustomSelect",
"(",
"[",
"'id'",
"=>",
"'running_content_part_right'",
",",
"'name'",
"=>",
"'running_content_part_right'",
",",
"'value'",
"=>",
"getset",
"(",
"$",
"this",
"->",
"options",
",",
"'running_content_part_right'",
")",
",",
"'choices'",
"=>",
"$",
"args",
",",
"]",
")",
";",
"$",
"this",
"->",
"renderField",
"(",
"[",
"'id'",
"=>",
"'running_content_part_right_custom'",
",",
"'name'",
"=>",
"'pressbooks_theme_options_'",
".",
"$",
"this",
"->",
"getSlug",
"(",
")",
",",
"'option'",
"=>",
"'running_content_part_right'",
",",
"'value'",
"=>",
"getset",
"(",
"$",
"this",
"->",
"options",
",",
"'running_content_part_right'",
")",
",",
"'type'",
"=>",
"'text'",
",",
"'class'",
"=>",
"'regular-text code'",
",",
"]",
")",
";",
"}"
] | Render the running_content_part_right input.
@param array $args | [
"Render",
"the",
"running_content_part_right",
"input",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/themeoptions/class-pdfoptions.php#L1241-L1261 |
pressbooks/pressbooks | inc/modules/themeoptions/class-pdfoptions.php | PDFOptions.renderRunningContentChapterLeftField | function renderRunningContentChapterLeftField( $args ) {
unset( $args['label_for'], $args['class'] );
$this->renderCustomSelect(
[
'id' => 'running_content_chapter_left',
'name' => 'running_content_chapter_left',
'value' => getset( $this->options, 'running_content_chapter_left' ),
'choices' => $args,
]
);
$this->renderField(
[
'id' => 'running_content_chapter_left_custom',
'name' => 'pressbooks_theme_options_' . $this->getSlug(),
'option' => 'running_content_chapter_left',
'value' => getset( $this->options, 'running_content_chapter_left' ),
'type' => 'text',
'class' => 'regular-text code',
]
);
} | php | function renderRunningContentChapterLeftField( $args ) {
unset( $args['label_for'], $args['class'] );
$this->renderCustomSelect(
[
'id' => 'running_content_chapter_left',
'name' => 'running_content_chapter_left',
'value' => getset( $this->options, 'running_content_chapter_left' ),
'choices' => $args,
]
);
$this->renderField(
[
'id' => 'running_content_chapter_left_custom',
'name' => 'pressbooks_theme_options_' . $this->getSlug(),
'option' => 'running_content_chapter_left',
'value' => getset( $this->options, 'running_content_chapter_left' ),
'type' => 'text',
'class' => 'regular-text code',
]
);
} | [
"function",
"renderRunningContentChapterLeftField",
"(",
"$",
"args",
")",
"{",
"unset",
"(",
"$",
"args",
"[",
"'label_for'",
"]",
",",
"$",
"args",
"[",
"'class'",
"]",
")",
";",
"$",
"this",
"->",
"renderCustomSelect",
"(",
"[",
"'id'",
"=>",
"'running_content_chapter_left'",
",",
"'name'",
"=>",
"'running_content_chapter_left'",
",",
"'value'",
"=>",
"getset",
"(",
"$",
"this",
"->",
"options",
",",
"'running_content_chapter_left'",
")",
",",
"'choices'",
"=>",
"$",
"args",
",",
"]",
")",
";",
"$",
"this",
"->",
"renderField",
"(",
"[",
"'id'",
"=>",
"'running_content_chapter_left_custom'",
",",
"'name'",
"=>",
"'pressbooks_theme_options_'",
".",
"$",
"this",
"->",
"getSlug",
"(",
")",
",",
"'option'",
"=>",
"'running_content_chapter_left'",
",",
"'value'",
"=>",
"getset",
"(",
"$",
"this",
"->",
"options",
",",
"'running_content_chapter_left'",
")",
",",
"'type'",
"=>",
"'text'",
",",
"'class'",
"=>",
"'regular-text code'",
",",
"]",
")",
";",
"}"
] | Render the running_content_chapter_left input.
@param array $args | [
"Render",
"the",
"running_content_chapter_left",
"input",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/themeoptions/class-pdfoptions.php#L1268-L1288 |
pressbooks/pressbooks | inc/modules/themeoptions/class-pdfoptions.php | PDFOptions.renderRunningContentChapterRightField | function renderRunningContentChapterRightField( $args ) {
unset( $args['label_for'], $args['class'] );
$this->renderCustomSelect(
[
'id' => 'running_content_chapter_right',
'name' => 'running_content_chapter_right',
'value' => getset( $this->options, 'running_content_chapter_right' ),
'choices' => $args,
]
);
$this->renderField(
[
'id' => 'running_content_chapter_right_custom',
'name' => 'pressbooks_theme_options_' . $this->getSlug(),
'option' => 'running_content_chapter_right',
'value' => getset( $this->options, 'running_content_chapter_right' ),
'type' => 'text',
'class' => 'regular-text code',
]
);
} | php | function renderRunningContentChapterRightField( $args ) {
unset( $args['label_for'], $args['class'] );
$this->renderCustomSelect(
[
'id' => 'running_content_chapter_right',
'name' => 'running_content_chapter_right',
'value' => getset( $this->options, 'running_content_chapter_right' ),
'choices' => $args,
]
);
$this->renderField(
[
'id' => 'running_content_chapter_right_custom',
'name' => 'pressbooks_theme_options_' . $this->getSlug(),
'option' => 'running_content_chapter_right',
'value' => getset( $this->options, 'running_content_chapter_right' ),
'type' => 'text',
'class' => 'regular-text code',
]
);
} | [
"function",
"renderRunningContentChapterRightField",
"(",
"$",
"args",
")",
"{",
"unset",
"(",
"$",
"args",
"[",
"'label_for'",
"]",
",",
"$",
"args",
"[",
"'class'",
"]",
")",
";",
"$",
"this",
"->",
"renderCustomSelect",
"(",
"[",
"'id'",
"=>",
"'running_content_chapter_right'",
",",
"'name'",
"=>",
"'running_content_chapter_right'",
",",
"'value'",
"=>",
"getset",
"(",
"$",
"this",
"->",
"options",
",",
"'running_content_chapter_right'",
")",
",",
"'choices'",
"=>",
"$",
"args",
",",
"]",
")",
";",
"$",
"this",
"->",
"renderField",
"(",
"[",
"'id'",
"=>",
"'running_content_chapter_right_custom'",
",",
"'name'",
"=>",
"'pressbooks_theme_options_'",
".",
"$",
"this",
"->",
"getSlug",
"(",
")",
",",
"'option'",
"=>",
"'running_content_chapter_right'",
",",
"'value'",
"=>",
"getset",
"(",
"$",
"this",
"->",
"options",
",",
"'running_content_chapter_right'",
")",
",",
"'type'",
"=>",
"'text'",
",",
"'class'",
"=>",
"'regular-text code'",
",",
"]",
")",
";",
"}"
] | Render the running_content_chapter_right input.
@param array $args | [
"Render",
"the",
"running_content_chapter_right",
"input",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/themeoptions/class-pdfoptions.php#L1295-L1315 |
pressbooks/pressbooks | inc/modules/themeoptions/class-pdfoptions.php | PDFOptions.renderRunningContentBackMatterLeftField | function renderRunningContentBackMatterLeftField( $args ) {
unset( $args['label_for'], $args['class'] );
$this->renderCustomSelect(
[
'id' => 'running_content_back_matter_left',
'name' => 'running_content_back_matter_left',
'value' => getset( $this->options, 'running_content_back_matter_left' ),
'choices' => $args,
]
);
$this->renderField(
[
'id' => 'running_content_back_matter_left_custom',
'name' => 'pressbooks_theme_options_' . $this->getSlug(),
'option' => 'running_content_back_matter_left',
'value' => getset( $this->options, 'running_content_back_matter_left' ),
'type' => 'text',
'class' => 'regular-text code',
]
);
} | php | function renderRunningContentBackMatterLeftField( $args ) {
unset( $args['label_for'], $args['class'] );
$this->renderCustomSelect(
[
'id' => 'running_content_back_matter_left',
'name' => 'running_content_back_matter_left',
'value' => getset( $this->options, 'running_content_back_matter_left' ),
'choices' => $args,
]
);
$this->renderField(
[
'id' => 'running_content_back_matter_left_custom',
'name' => 'pressbooks_theme_options_' . $this->getSlug(),
'option' => 'running_content_back_matter_left',
'value' => getset( $this->options, 'running_content_back_matter_left' ),
'type' => 'text',
'class' => 'regular-text code',
]
);
} | [
"function",
"renderRunningContentBackMatterLeftField",
"(",
"$",
"args",
")",
"{",
"unset",
"(",
"$",
"args",
"[",
"'label_for'",
"]",
",",
"$",
"args",
"[",
"'class'",
"]",
")",
";",
"$",
"this",
"->",
"renderCustomSelect",
"(",
"[",
"'id'",
"=>",
"'running_content_back_matter_left'",
",",
"'name'",
"=>",
"'running_content_back_matter_left'",
",",
"'value'",
"=>",
"getset",
"(",
"$",
"this",
"->",
"options",
",",
"'running_content_back_matter_left'",
")",
",",
"'choices'",
"=>",
"$",
"args",
",",
"]",
")",
";",
"$",
"this",
"->",
"renderField",
"(",
"[",
"'id'",
"=>",
"'running_content_back_matter_left_custom'",
",",
"'name'",
"=>",
"'pressbooks_theme_options_'",
".",
"$",
"this",
"->",
"getSlug",
"(",
")",
",",
"'option'",
"=>",
"'running_content_back_matter_left'",
",",
"'value'",
"=>",
"getset",
"(",
"$",
"this",
"->",
"options",
",",
"'running_content_back_matter_left'",
")",
",",
"'type'",
"=>",
"'text'",
",",
"'class'",
"=>",
"'regular-text code'",
",",
"]",
")",
";",
"}"
] | Render the running_content_back_matter_left input.
@param array $args | [
"Render",
"the",
"running_content_back_matter_left",
"input",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/themeoptions/class-pdfoptions.php#L1322-L1342 |
pressbooks/pressbooks | inc/modules/themeoptions/class-pdfoptions.php | PDFOptions.renderRunningContentBackMatterRightField | function renderRunningContentBackMatterRightField( $args ) {
unset( $args['label_for'], $args['class'] );
$this->renderCustomSelect(
[
'id' => 'running_content_back_matter_right',
'name' => 'running_content_back_matter_right',
'value' => getset( $this->options, 'running_content_back_matter_right' ),
'choices' => $args,
]
);
$this->renderField(
[
'id' => 'running_content_back_matter_right_custom',
'name' => 'pressbooks_theme_options_' . $this->getSlug(),
'option' => 'running_content_back_matter_right',
'value' => getset( $this->options, 'running_content_back_matter_right' ),
'type' => 'text',
'class' => 'regular-text code',
]
);
} | php | function renderRunningContentBackMatterRightField( $args ) {
unset( $args['label_for'], $args['class'] );
$this->renderCustomSelect(
[
'id' => 'running_content_back_matter_right',
'name' => 'running_content_back_matter_right',
'value' => getset( $this->options, 'running_content_back_matter_right' ),
'choices' => $args,
]
);
$this->renderField(
[
'id' => 'running_content_back_matter_right_custom',
'name' => 'pressbooks_theme_options_' . $this->getSlug(),
'option' => 'running_content_back_matter_right',
'value' => getset( $this->options, 'running_content_back_matter_right' ),
'type' => 'text',
'class' => 'regular-text code',
]
);
} | [
"function",
"renderRunningContentBackMatterRightField",
"(",
"$",
"args",
")",
"{",
"unset",
"(",
"$",
"args",
"[",
"'label_for'",
"]",
",",
"$",
"args",
"[",
"'class'",
"]",
")",
";",
"$",
"this",
"->",
"renderCustomSelect",
"(",
"[",
"'id'",
"=>",
"'running_content_back_matter_right'",
",",
"'name'",
"=>",
"'running_content_back_matter_right'",
",",
"'value'",
"=>",
"getset",
"(",
"$",
"this",
"->",
"options",
",",
"'running_content_back_matter_right'",
")",
",",
"'choices'",
"=>",
"$",
"args",
",",
"]",
")",
";",
"$",
"this",
"->",
"renderField",
"(",
"[",
"'id'",
"=>",
"'running_content_back_matter_right_custom'",
",",
"'name'",
"=>",
"'pressbooks_theme_options_'",
".",
"$",
"this",
"->",
"getSlug",
"(",
")",
",",
"'option'",
"=>",
"'running_content_back_matter_right'",
",",
"'value'",
"=>",
"getset",
"(",
"$",
"this",
"->",
"options",
",",
"'running_content_back_matter_right'",
")",
",",
"'type'",
"=>",
"'text'",
",",
"'class'",
"=>",
"'regular-text code'",
",",
"]",
")",
";",
"}"
] | Render the running_content_back_matter_right input.
@param array $args | [
"Render",
"the",
"running_content_back_matter_right",
"input",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/themeoptions/class-pdfoptions.php#L1349-L1369 |
pressbooks/pressbooks | inc/modules/themeoptions/class-pdfoptions.php | PDFOptions.renderFontSizeField | function renderFontSizeField( $args ) {
unset( $args['label_for'], $args['class'] );
$this->renderCheckbox(
[
'id' => 'pdf_fontsize',
'name' => 'pressbooks_theme_options_' . $this->getSlug(),
'option' => 'pdf_fontsize',
'value' => getset( $this->options, 'pdf_fontsize' ),
'label' => $args[0],
]
);
} | php | function renderFontSizeField( $args ) {
unset( $args['label_for'], $args['class'] );
$this->renderCheckbox(
[
'id' => 'pdf_fontsize',
'name' => 'pressbooks_theme_options_' . $this->getSlug(),
'option' => 'pdf_fontsize',
'value' => getset( $this->options, 'pdf_fontsize' ),
'label' => $args[0],
]
);
} | [
"function",
"renderFontSizeField",
"(",
"$",
"args",
")",
"{",
"unset",
"(",
"$",
"args",
"[",
"'label_for'",
"]",
",",
"$",
"args",
"[",
"'class'",
"]",
")",
";",
"$",
"this",
"->",
"renderCheckbox",
"(",
"[",
"'id'",
"=>",
"'pdf_fontsize'",
",",
"'name'",
"=>",
"'pressbooks_theme_options_'",
".",
"$",
"this",
"->",
"getSlug",
"(",
")",
",",
"'option'",
"=>",
"'pdf_fontsize'",
",",
"'value'",
"=>",
"getset",
"(",
"$",
"this",
"->",
"options",
",",
"'pdf_fontsize'",
")",
",",
"'label'",
"=>",
"$",
"args",
"[",
"0",
"]",
",",
"]",
")",
";",
"}"
] | Render the pdf_fontsize checkbox.
@param array $args | [
"Render",
"the",
"pdf_fontsize",
"checkbox",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/themeoptions/class-pdfoptions.php#L1376-L1387 |
pressbooks/pressbooks | inc/modules/themeoptions/class-pdfoptions.php | PDFOptions.filterDefaults | static function filterDefaults( $defaults ) {
// SASS => WP
$overrides = [
'body-font-size' => 'pdf_body_font_size',
'body-line-height' => 'pdf_body_line_height',
'page-margin-top' => 'pdf_page_margin_top',
'page-margin-inside' => 'pdf_page_margin_inside',
'page-margin-bottom' => 'pdf_page_margin_bottom',
'page-margin-outside' => 'pdf_page_margin_outside',
'front-matter-running-content-left' => 'running_content_front_matter_left',
'front-matter-running-content-right' => 'running_content_front_matter_right',
'introduction-running-content-left' => 'running_content_introduction_left',
'introduction-running-content-right' => 'running_content_introduction_right',
'part-running-content-left' => 'running_content_part_left',
'part-running-content-right' => 'running_content_part_right',
'chapter-running-content-left' => 'running_content_chapter_left',
'chapter-running-content-right' => 'running_content_chapter_right',
'back-matter-running-content-left' => 'running_content_back_matter_left',
'back-matter-running-content-right' => 'running_content_back_matter_right',
];
$transient_name = 'pressbooks_theme_options_pdf_parsed_sass_variables';
$parsed_sass_variables = get_transient( $transient_name );
if ( $parsed_sass_variables === false ) {
// Order of files matter. If a variable is duplicated in other files then the last one takes precedence
$parsed_sass_variables = [];
$sass = Container::get( 'Sass' );
$path_to_global = $sass->pathToGlobals();
$path_to_theme = get_stylesheet_directory();
$files = [
$path_to_global . '/variables/_elements.scss',
$path_to_global . '/variables/_structure.scss',
$path_to_theme . '/assets/styles/components/_elements.scss',
$path_to_theme . '/assets/styles/components/_structure.scss',
];
foreach ( $files as $file ) {
if ( file_exists( $file ) ) {
$parsed_sass_variables[] = $sass->parseVariables( \Pressbooks\Utility\get_contents( $file ) );
}
}
set_transient( $transient_name, $parsed_sass_variables );
}
foreach ( $parsed_sass_variables as $parsed_variables ) {
foreach ( $overrides as $sass_var => $wp_option ) {
if ( isset( $parsed_variables[ $sass_var ] ) ) {
$val = self::parseSassValue( $parsed_variables[ $sass_var ] );
if ( ! empty( $val ) ) {
if ( in_array( $wp_option, self::getFloatOptions(), true ) ) {
$val = (float) preg_replace( '/[^0-9.]/', '', $val ); // Extract digits and periods
} elseif ( in_array( $wp_option, self::getIntegerOptions(), true ) ) {
$val = (int) preg_replace( '/[^0-9]/', '', $val ); // Extract digits
} elseif ( in_array( $wp_option, self::getBooleanOptions(), true ) ) {
$val = filter_var( $val, FILTER_VALIDATE_BOOLEAN ); // Convert to boolean
} elseif ( strpos( $wp_option, 'running_content', true ) ) {
$val = self::replaceRunningContentStrings( $val );
}
$defaults[ $wp_option ] = $val; // Override default with new value
}
}
}
}
return $defaults;
} | php | static function filterDefaults( $defaults ) {
// SASS => WP
$overrides = [
'body-font-size' => 'pdf_body_font_size',
'body-line-height' => 'pdf_body_line_height',
'page-margin-top' => 'pdf_page_margin_top',
'page-margin-inside' => 'pdf_page_margin_inside',
'page-margin-bottom' => 'pdf_page_margin_bottom',
'page-margin-outside' => 'pdf_page_margin_outside',
'front-matter-running-content-left' => 'running_content_front_matter_left',
'front-matter-running-content-right' => 'running_content_front_matter_right',
'introduction-running-content-left' => 'running_content_introduction_left',
'introduction-running-content-right' => 'running_content_introduction_right',
'part-running-content-left' => 'running_content_part_left',
'part-running-content-right' => 'running_content_part_right',
'chapter-running-content-left' => 'running_content_chapter_left',
'chapter-running-content-right' => 'running_content_chapter_right',
'back-matter-running-content-left' => 'running_content_back_matter_left',
'back-matter-running-content-right' => 'running_content_back_matter_right',
];
$transient_name = 'pressbooks_theme_options_pdf_parsed_sass_variables';
$parsed_sass_variables = get_transient( $transient_name );
if ( $parsed_sass_variables === false ) {
// Order of files matter. If a variable is duplicated in other files then the last one takes precedence
$parsed_sass_variables = [];
$sass = Container::get( 'Sass' );
$path_to_global = $sass->pathToGlobals();
$path_to_theme = get_stylesheet_directory();
$files = [
$path_to_global . '/variables/_elements.scss',
$path_to_global . '/variables/_structure.scss',
$path_to_theme . '/assets/styles/components/_elements.scss',
$path_to_theme . '/assets/styles/components/_structure.scss',
];
foreach ( $files as $file ) {
if ( file_exists( $file ) ) {
$parsed_sass_variables[] = $sass->parseVariables( \Pressbooks\Utility\get_contents( $file ) );
}
}
set_transient( $transient_name, $parsed_sass_variables );
}
foreach ( $parsed_sass_variables as $parsed_variables ) {
foreach ( $overrides as $sass_var => $wp_option ) {
if ( isset( $parsed_variables[ $sass_var ] ) ) {
$val = self::parseSassValue( $parsed_variables[ $sass_var ] );
if ( ! empty( $val ) ) {
if ( in_array( $wp_option, self::getFloatOptions(), true ) ) {
$val = (float) preg_replace( '/[^0-9.]/', '', $val ); // Extract digits and periods
} elseif ( in_array( $wp_option, self::getIntegerOptions(), true ) ) {
$val = (int) preg_replace( '/[^0-9]/', '', $val ); // Extract digits
} elseif ( in_array( $wp_option, self::getBooleanOptions(), true ) ) {
$val = filter_var( $val, FILTER_VALIDATE_BOOLEAN ); // Convert to boolean
} elseif ( strpos( $wp_option, 'running_content', true ) ) {
$val = self::replaceRunningContentStrings( $val );
}
$defaults[ $wp_option ] = $val; // Override default with new value
}
}
}
}
return $defaults;
} | [
"static",
"function",
"filterDefaults",
"(",
"$",
"defaults",
")",
"{",
"// SASS => WP",
"$",
"overrides",
"=",
"[",
"'body-font-size'",
"=>",
"'pdf_body_font_size'",
",",
"'body-line-height'",
"=>",
"'pdf_body_line_height'",
",",
"'page-margin-top'",
"=>",
"'pdf_page_margin_top'",
",",
"'page-margin-inside'",
"=>",
"'pdf_page_margin_inside'",
",",
"'page-margin-bottom'",
"=>",
"'pdf_page_margin_bottom'",
",",
"'page-margin-outside'",
"=>",
"'pdf_page_margin_outside'",
",",
"'front-matter-running-content-left'",
"=>",
"'running_content_front_matter_left'",
",",
"'front-matter-running-content-right'",
"=>",
"'running_content_front_matter_right'",
",",
"'introduction-running-content-left'",
"=>",
"'running_content_introduction_left'",
",",
"'introduction-running-content-right'",
"=>",
"'running_content_introduction_right'",
",",
"'part-running-content-left'",
"=>",
"'running_content_part_left'",
",",
"'part-running-content-right'",
"=>",
"'running_content_part_right'",
",",
"'chapter-running-content-left'",
"=>",
"'running_content_chapter_left'",
",",
"'chapter-running-content-right'",
"=>",
"'running_content_chapter_right'",
",",
"'back-matter-running-content-left'",
"=>",
"'running_content_back_matter_left'",
",",
"'back-matter-running-content-right'",
"=>",
"'running_content_back_matter_right'",
",",
"]",
";",
"$",
"transient_name",
"=",
"'pressbooks_theme_options_pdf_parsed_sass_variables'",
";",
"$",
"parsed_sass_variables",
"=",
"get_transient",
"(",
"$",
"transient_name",
")",
";",
"if",
"(",
"$",
"parsed_sass_variables",
"===",
"false",
")",
"{",
"// Order of files matter. If a variable is duplicated in other files then the last one takes precedence",
"$",
"parsed_sass_variables",
"=",
"[",
"]",
";",
"$",
"sass",
"=",
"Container",
"::",
"get",
"(",
"'Sass'",
")",
";",
"$",
"path_to_global",
"=",
"$",
"sass",
"->",
"pathToGlobals",
"(",
")",
";",
"$",
"path_to_theme",
"=",
"get_stylesheet_directory",
"(",
")",
";",
"$",
"files",
"=",
"[",
"$",
"path_to_global",
".",
"'/variables/_elements.scss'",
",",
"$",
"path_to_global",
".",
"'/variables/_structure.scss'",
",",
"$",
"path_to_theme",
".",
"'/assets/styles/components/_elements.scss'",
",",
"$",
"path_to_theme",
".",
"'/assets/styles/components/_structure.scss'",
",",
"]",
";",
"foreach",
"(",
"$",
"files",
"as",
"$",
"file",
")",
"{",
"if",
"(",
"file_exists",
"(",
"$",
"file",
")",
")",
"{",
"$",
"parsed_sass_variables",
"[",
"]",
"=",
"$",
"sass",
"->",
"parseVariables",
"(",
"\\",
"Pressbooks",
"\\",
"Utility",
"\\",
"get_contents",
"(",
"$",
"file",
")",
")",
";",
"}",
"}",
"set_transient",
"(",
"$",
"transient_name",
",",
"$",
"parsed_sass_variables",
")",
";",
"}",
"foreach",
"(",
"$",
"parsed_sass_variables",
"as",
"$",
"parsed_variables",
")",
"{",
"foreach",
"(",
"$",
"overrides",
"as",
"$",
"sass_var",
"=>",
"$",
"wp_option",
")",
"{",
"if",
"(",
"isset",
"(",
"$",
"parsed_variables",
"[",
"$",
"sass_var",
"]",
")",
")",
"{",
"$",
"val",
"=",
"self",
"::",
"parseSassValue",
"(",
"$",
"parsed_variables",
"[",
"$",
"sass_var",
"]",
")",
";",
"if",
"(",
"!",
"empty",
"(",
"$",
"val",
")",
")",
"{",
"if",
"(",
"in_array",
"(",
"$",
"wp_option",
",",
"self",
"::",
"getFloatOptions",
"(",
")",
",",
"true",
")",
")",
"{",
"$",
"val",
"=",
"(",
"float",
")",
"preg_replace",
"(",
"'/[^0-9.]/'",
",",
"''",
",",
"$",
"val",
")",
";",
"// Extract digits and periods",
"}",
"elseif",
"(",
"in_array",
"(",
"$",
"wp_option",
",",
"self",
"::",
"getIntegerOptions",
"(",
")",
",",
"true",
")",
")",
"{",
"$",
"val",
"=",
"(",
"int",
")",
"preg_replace",
"(",
"'/[^0-9]/'",
",",
"''",
",",
"$",
"val",
")",
";",
"// Extract digits",
"}",
"elseif",
"(",
"in_array",
"(",
"$",
"wp_option",
",",
"self",
"::",
"getBooleanOptions",
"(",
")",
",",
"true",
")",
")",
"{",
"$",
"val",
"=",
"filter_var",
"(",
"$",
"val",
",",
"FILTER_VALIDATE_BOOLEAN",
")",
";",
"// Convert to boolean",
"}",
"elseif",
"(",
"strpos",
"(",
"$",
"wp_option",
",",
"'running_content'",
",",
"true",
")",
")",
"{",
"$",
"val",
"=",
"self",
"::",
"replaceRunningContentStrings",
"(",
"$",
"val",
")",
";",
"}",
"$",
"defaults",
"[",
"$",
"wp_option",
"]",
"=",
"$",
"val",
";",
"// Override default with new value",
"}",
"}",
"}",
"}",
"return",
"$",
"defaults",
";",
"}"
] | Filter the array of default values for the PDF options tab.
@param array $defaults
@return array $defaults | [
"Filter",
"the",
"array",
"of",
"default",
"values",
"for",
"the",
"PDF",
"options",
"tab",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/themeoptions/class-pdfoptions.php#L1459-L1524 |
pressbooks/pressbooks | inc/modules/themeoptions/class-pdfoptions.php | PDFOptions.parseSassValue | static protected function parseSassValue( $val ) {
if ( substr( $val, 0, 1 ) === '(' ) {
// We think this is a Sass Map
preg_match( '/prince:([^,]+)/', $val, $matches );
if ( ! empty( $matches[1] ) ) {
return trim( $matches[1] );
}
return ''; // Did not find prince mapping
}
if ( substr( $val, 0, 7 ) === 'string(' ) {
// We think this is one of our running content variables
preg_match( '/string\((.+?)\)/', $val, $matches );
if ( ! empty( $matches[1] ) ) {
return trim( str_replace( '-', '_', "%{$matches[1]}%" ) );
}
return ''; // Did not find what we were looking for
}
// Use as is
return $val;
} | php | static protected function parseSassValue( $val ) {
if ( substr( $val, 0, 1 ) === '(' ) {
// We think this is a Sass Map
preg_match( '/prince:([^,]+)/', $val, $matches );
if ( ! empty( $matches[1] ) ) {
return trim( $matches[1] );
}
return ''; // Did not find prince mapping
}
if ( substr( $val, 0, 7 ) === 'string(' ) {
// We think this is one of our running content variables
preg_match( '/string\((.+?)\)/', $val, $matches );
if ( ! empty( $matches[1] ) ) {
return trim( str_replace( '-', '_', "%{$matches[1]}%" ) );
}
return ''; // Did not find what we were looking for
}
// Use as is
return $val;
} | [
"static",
"protected",
"function",
"parseSassValue",
"(",
"$",
"val",
")",
"{",
"if",
"(",
"substr",
"(",
"$",
"val",
",",
"0",
",",
"1",
")",
"===",
"'('",
")",
"{",
"// We think this is a Sass Map",
"preg_match",
"(",
"'/prince:([^,]+)/'",
",",
"$",
"val",
",",
"$",
"matches",
")",
";",
"if",
"(",
"!",
"empty",
"(",
"$",
"matches",
"[",
"1",
"]",
")",
")",
"{",
"return",
"trim",
"(",
"$",
"matches",
"[",
"1",
"]",
")",
";",
"}",
"return",
"''",
";",
"// Did not find prince mapping",
"}",
"if",
"(",
"substr",
"(",
"$",
"val",
",",
"0",
",",
"7",
")",
"===",
"'string('",
")",
"{",
"// We think this is one of our running content variables",
"preg_match",
"(",
"'/string\\((.+?)\\)/'",
",",
"$",
"val",
",",
"$",
"matches",
")",
";",
"if",
"(",
"!",
"empty",
"(",
"$",
"matches",
"[",
"1",
"]",
")",
")",
"{",
"return",
"trim",
"(",
"str_replace",
"(",
"'-'",
",",
"'_'",
",",
"\"%{$matches[1]}%\"",
")",
")",
";",
"}",
"return",
"''",
";",
"// Did not find what we were looking for",
"}",
"// Use as is",
"return",
"$",
"val",
";",
"}"
] | @param string $val
@return string | [
"@param",
"string",
"$val"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/themeoptions/class-pdfoptions.php#L1531-L1553 |
pressbooks/pressbooks | inc/modules/themeoptions/class-pdfoptions.php | PDFOptions.scssOverrides | static function scssOverrides( $scss ) {
$styles = Container::get( 'Styles' );
$v2_compatible = $styles->isCurrentThemeCompatible( 2 );
if ( ! $v2_compatible ) {
$scss .= "/* Theme Options */\n";
}
// --------------------------------------------------------------------
// Global Options
$options = get_option( 'pressbooks_theme_options_global' );
// Should we display chapter numbers? True (default) or false.
if ( ! $options['chapter_numbers'] ) {
if ( $v2_compatible ) {
$styles->getSass()->setVariables(
[
'chapter-number-display' => 'none',
'part-number-display' => 'none',
'toc-left-left-gutter' => '0',
'toc-chapter-number-display' => 'none',
'toc-left-chapter-number-display' => 'none',
'toc-center-chapter-number-display' => 'none',
'toc-part-number-display' => 'none',
'toc-left-part-number-display' => 'none',
'toc-center-part-number-display' => 'none',
]
);
} else {
$scss .= "div.part-title-wrap > .part-number, div.chapter-title-wrap > .chapter-number, #toc .part a::before, #toc .chapter a::before { display: none !important; } \n"; // TODO: NO
}
}
// Textbox colours.
if ( $v2_compatible ) {
foreach ( [
'edu_textbox_examples_header_color' => 'examples-header-color',
'edu_textbox_examples_header_background' => 'examples-header-background',
'edu_textbox_examples_background' => 'examples-background',
'edu_textbox_exercises_header_color' => 'exercises-header-color',
'edu_textbox_exercises_header_background' => 'exercises-header-background',
'edu_textbox_exercises_background' => 'exercises-background',
'edu_textbox_objectives_header_color' => 'learning-objectives-header-color',
'edu_textbox_objectives_header_background' => 'learning-objectives-header-background',
'edu_textbox_objectives_background' => 'learning-objectives-background',
'edu_textbox_takeaways_header_color' => 'key-takeaways-header-color',
'edu_textbox_takeaways_header_background' => 'key-takeaways-header-background',
'edu_textbox_takeaways_background' => 'key-takeaways-background',
] as $option => $variable ) {
if ( isset( $options[ $option ] ) ) {
$styles->getSass()->setVariables(
[
"$variable" => $options[ $option ],
]
);
}
}
}
// --------------------------------------------------------------------
// PDF Options
$options = get_option( 'pressbooks_theme_options_pdf' );
// Change body font size
if ( $v2_compatible && isset( $options['pdf_body_font_size'] ) ) {
$fontsize = $options['pdf_body_font_size'] . 'pt';
$styles->getSass()->setVariables(
[
'body-font-size' => "(epub: medium, prince: $fontsize, web: 14pt)",
]
);
}
// Change body line height
if ( $v2_compatible && isset( $options['pdf_body_line_height'] ) ) {
$lineheight = $options['pdf_body_line_height'] . 'em';
$styles->getSass()->setVariables(
[
'body-line-height' => "(epub: 1.4em, prince: $lineheight, web: 1.8em)",
]
);
}
// Page dimensions
$width = $options['pdf_page_width'];
$height = $options['pdf_page_height'];
if ( $v2_compatible ) {
$styles->getSass()->setVariables(
[
'page-width' => $width,
'page-height' => $height,
]
);
} else {
$scss .= "@page { size: $width $height; } \n";
}
// Margins
if ( $v2_compatible ) {
$styles->getSass()->setVariables(
[
'page-margin-top' => ( isset( $options['pdf_page_margin_top'] ) ) ? $options['pdf_page_margin_top'] : '2cm',
'page-margin-inside' => ( isset( $options['pdf_page_margin_inside'] ) ) ? $options['pdf_page_margin_inside'] : '2cm',
'page-margin-bottom' => ( isset( $options['pdf_page_margin_bottom'] ) ) ? $options['pdf_page_margin_bottom'] : '2cm',
'page-margin-outside' => ( isset( $options['pdf_page_margin_outside'] ) ) ? $options['pdf_page_margin_outside'] : '2cm',
]
);
}
// Should we display crop marks? True or false (default).
if ( 1 === absint( $options['pdf_crop_marks'] ) ) {
if ( $v2_compatible ) {
$styles->getSass()->setVariables(
[
'page-cropmarks' => 'crop',
]
);
} else {
$scss .= "@page { marks: crop } \n";
}
}
// Hyphens?
if ( 1 === absint( $options['pdf_hyphens'] ) ) {
if ( $v2_compatible ) {
$styles->getSass()->setVariables(
[
'para-hyphens' => 'auto',
]
);
} else {
$scss .= "p { hyphens: auto; } \n";
}
} else {
if ( $v2_compatible ) {
$styles->getSass()->setVariables(
[
'para-hyphens' => 'manual',
]
);
} else {
$scss .= "p { hyphens: manual; } \n";
}
}
// Indent paragraphs?
$paragraph_separation = $options['pdf_paragraph_separation'] ?? 'indent';
if ( 'skiplines' === $paragraph_separation ) {
if ( $v2_compatible ) {
$styles->getSass()->setVariables(
[
'para-margin-top' => '1em',
'para-indent' => '0',
]
);
} else {
$scss .= "p + p { text-indent: 0em; margin-top: 1em; } \n";
}
} elseif ( 'indent' === $paragraph_separation ) {
if ( $v2_compatible ) {
$styles->getSass()->setVariables(
[
'para-margin-top' => '0',
'para-indent' => '1em',
]
);
} else {
$scss .= "p + p { text-indent: 1em; margin-top: 0em; } \n";
}
}
// Include blank pages?
if ( isset( $options['pdf_sectionopenings'] ) ) {
if ( 'openright' === $options['pdf_sectionopenings'] ) {
if ( $v2_compatible ) {
$styles->getSass()->setVariables(
[
'recto-verso-standard-opening' => 'right',
'recto-verso-first-section-opening' => 'right',
'recto-verso-section-opening' => 'right',
]
);
} else {
$scss .= "#title-page, #toc, div.part, div.front-matter, div.front-matter.introduction, div.front-matter + div.front-matter, div.chapter, div.chapter + div.chapter, div.back-matter, div.back-matter + div.back-matter, #half-title-page h1.title:first-of-type { page-break-before: right; } \n";
$scss .= "#copyright-page { page-break-before: left; }\n";
}
} elseif ( 'remove' === $options['pdf_sectionopenings'] ) {
if ( $v2_compatible ) {
$styles->getSass()->setVariables(
[
'recto-verso-standard-opening' => 'auto',
'recto-verso-first-section-opening' => 'auto',
'recto-verso-section-opening' => 'auto',
'recto-verso-copyright-page-opening' => 'auto',
]
);
} else {
$scss .= "#title-page, #copyright-page, #toc, div.part, div.front-matter, div.back-matter, div.chapter, #half-title-page h1.title:first-of-type { page-break-before: auto; } \n";
}
}
}
// Should we display the TOC? True (default) or false.
if ( ! $options['pdf_toc'] ) {
if ( $v2_compatible ) {
$styles->getSass()->setVariables(
[
'toc-display' => 'none',
]
);
} else {
$scss .= "#toc { display: none; } \n";
}
}
// Widows
if ( isset( $options['widows'] ) ) {
if ( $v2_compatible ) {
$styles->getSass()->setVariables(
[
'widows' => $options['widows'],
]
);
} else {
$scss .= "p { widows: {$options['widows']}; }\n";
}
} else {
if ( ! $v2_compatible ) {
$scss .= 'p { widows: 2; }' . "\n";
}
}
// Orphans
if ( isset( $options['orphans'] ) ) {
if ( $v2_compatible ) {
$styles->getSass()->setVariables(
[
'orphans' => $options['orphans'],
]
);
} else {
$scss .= "p { orphans: {$options['orphans']}; }\n";
}
} else {
if ( ! $v2_compatible ) {
$scss .= 'p { orphans: 1; }' . "\n";
}
}
// Running Content
if ( $v2_compatible ) {
$front_matter_running_content_left = ( isset( $options['running_content_front_matter_left'] ) ) ? self::replaceRunningContentTags( $options['running_content_front_matter_left'] ) : 'string(book-title)';
$front_matter_running_content_right = ( isset( $options['running_content_front_matter_right'] ) ) ? self::replaceRunningContentTags( $options['running_content_front_matter_right'] ) : 'string(section-title)';
$introduction_running_content_left = ( isset( $options['running_content_introduction_left'] ) ) ? self::replaceRunningContentTags( $options['running_content_introduction_left'] ) : 'string(book-title)';
$introduction_running_content_right = ( isset( $options['running_content_introduction_right'] ) ) ? self::replaceRunningContentTags( $options['running_content_introduction_right'] ) : 'string(section-title)';
$part_running_content_left = ( isset( $options['running_content_part_left'] ) ) ? self::replaceRunningContentTags( $options['running_content_part_left'] ) : 'string(book-title)';
$part_running_content_right = ( isset( $options['running_content_part_right'] ) ) ? self::replaceRunningContentTags( $options['running_content_part_right'] ) : 'string(part-title)';
$chapter_running_content_left = ( isset( $options['running_content_chapter_left'] ) ) ? self::replaceRunningContentTags( $options['running_content_chapter_left'] ) : 'string(book-title)';
$chapter_running_content_right = ( isset( $options['running_content_chapter_right'] ) ) ? self::replaceRunningContentTags( $options['running_content_chapter_right'] ) : 'string(section-title)';
$back_matter_running_content_left = ( isset( $options['running_content_back_matter_left'] ) ) ? self::replaceRunningContentTags( $options['running_content_back_matter_left'] ) : 'string(book-title)';
$back_matter_running_content_right = ( isset( $options['running_content_back_matter_right'] ) ) ? self::replaceRunningContentTags( $options['running_content_back_matter_right'] ) : 'string(section-title)';
$styles->getSass()->setVariables(
[
'front-matter-running-content-left' => $front_matter_running_content_left,
'front-matter-running-content-right' => $front_matter_running_content_right,
'introduction-running-content-left' => $introduction_running_content_left,
'introduction-running-content-right' => $introduction_running_content_right,
'part-running-content-left' => $part_running_content_left,
'part-running-content-right' => $part_running_content_right,
'chapter-running-content-left' => $chapter_running_content_left,
'chapter-running-content-right' => $chapter_running_content_right,
'back-matter-running-content-left' => $back_matter_running_content_left,
'back-matter-running-content-right' => $back_matter_running_content_right,
]
);
}
// a11y Font Size
if ( ! empty( $options['pdf_fontsize'] ) ) {
if ( ! $v2_compatible ) {
$scss .= 'body { font-size: 1.3em; line-height: 1.3; }' . "\n";
}
}
return $scss;
} | php | static function scssOverrides( $scss ) {
$styles = Container::get( 'Styles' );
$v2_compatible = $styles->isCurrentThemeCompatible( 2 );
if ( ! $v2_compatible ) {
$scss .= "/* Theme Options */\n";
}
// --------------------------------------------------------------------
// Global Options
$options = get_option( 'pressbooks_theme_options_global' );
// Should we display chapter numbers? True (default) or false.
if ( ! $options['chapter_numbers'] ) {
if ( $v2_compatible ) {
$styles->getSass()->setVariables(
[
'chapter-number-display' => 'none',
'part-number-display' => 'none',
'toc-left-left-gutter' => '0',
'toc-chapter-number-display' => 'none',
'toc-left-chapter-number-display' => 'none',
'toc-center-chapter-number-display' => 'none',
'toc-part-number-display' => 'none',
'toc-left-part-number-display' => 'none',
'toc-center-part-number-display' => 'none',
]
);
} else {
$scss .= "div.part-title-wrap > .part-number, div.chapter-title-wrap > .chapter-number, #toc .part a::before, #toc .chapter a::before { display: none !important; } \n"; // TODO: NO
}
}
// Textbox colours.
if ( $v2_compatible ) {
foreach ( [
'edu_textbox_examples_header_color' => 'examples-header-color',
'edu_textbox_examples_header_background' => 'examples-header-background',
'edu_textbox_examples_background' => 'examples-background',
'edu_textbox_exercises_header_color' => 'exercises-header-color',
'edu_textbox_exercises_header_background' => 'exercises-header-background',
'edu_textbox_exercises_background' => 'exercises-background',
'edu_textbox_objectives_header_color' => 'learning-objectives-header-color',
'edu_textbox_objectives_header_background' => 'learning-objectives-header-background',
'edu_textbox_objectives_background' => 'learning-objectives-background',
'edu_textbox_takeaways_header_color' => 'key-takeaways-header-color',
'edu_textbox_takeaways_header_background' => 'key-takeaways-header-background',
'edu_textbox_takeaways_background' => 'key-takeaways-background',
] as $option => $variable ) {
if ( isset( $options[ $option ] ) ) {
$styles->getSass()->setVariables(
[
"$variable" => $options[ $option ],
]
);
}
}
}
// --------------------------------------------------------------------
// PDF Options
$options = get_option( 'pressbooks_theme_options_pdf' );
// Change body font size
if ( $v2_compatible && isset( $options['pdf_body_font_size'] ) ) {
$fontsize = $options['pdf_body_font_size'] . 'pt';
$styles->getSass()->setVariables(
[
'body-font-size' => "(epub: medium, prince: $fontsize, web: 14pt)",
]
);
}
// Change body line height
if ( $v2_compatible && isset( $options['pdf_body_line_height'] ) ) {
$lineheight = $options['pdf_body_line_height'] . 'em';
$styles->getSass()->setVariables(
[
'body-line-height' => "(epub: 1.4em, prince: $lineheight, web: 1.8em)",
]
);
}
// Page dimensions
$width = $options['pdf_page_width'];
$height = $options['pdf_page_height'];
if ( $v2_compatible ) {
$styles->getSass()->setVariables(
[
'page-width' => $width,
'page-height' => $height,
]
);
} else {
$scss .= "@page { size: $width $height; } \n";
}
// Margins
if ( $v2_compatible ) {
$styles->getSass()->setVariables(
[
'page-margin-top' => ( isset( $options['pdf_page_margin_top'] ) ) ? $options['pdf_page_margin_top'] : '2cm',
'page-margin-inside' => ( isset( $options['pdf_page_margin_inside'] ) ) ? $options['pdf_page_margin_inside'] : '2cm',
'page-margin-bottom' => ( isset( $options['pdf_page_margin_bottom'] ) ) ? $options['pdf_page_margin_bottom'] : '2cm',
'page-margin-outside' => ( isset( $options['pdf_page_margin_outside'] ) ) ? $options['pdf_page_margin_outside'] : '2cm',
]
);
}
// Should we display crop marks? True or false (default).
if ( 1 === absint( $options['pdf_crop_marks'] ) ) {
if ( $v2_compatible ) {
$styles->getSass()->setVariables(
[
'page-cropmarks' => 'crop',
]
);
} else {
$scss .= "@page { marks: crop } \n";
}
}
// Hyphens?
if ( 1 === absint( $options['pdf_hyphens'] ) ) {
if ( $v2_compatible ) {
$styles->getSass()->setVariables(
[
'para-hyphens' => 'auto',
]
);
} else {
$scss .= "p { hyphens: auto; } \n";
}
} else {
if ( $v2_compatible ) {
$styles->getSass()->setVariables(
[
'para-hyphens' => 'manual',
]
);
} else {
$scss .= "p { hyphens: manual; } \n";
}
}
// Indent paragraphs?
$paragraph_separation = $options['pdf_paragraph_separation'] ?? 'indent';
if ( 'skiplines' === $paragraph_separation ) {
if ( $v2_compatible ) {
$styles->getSass()->setVariables(
[
'para-margin-top' => '1em',
'para-indent' => '0',
]
);
} else {
$scss .= "p + p { text-indent: 0em; margin-top: 1em; } \n";
}
} elseif ( 'indent' === $paragraph_separation ) {
if ( $v2_compatible ) {
$styles->getSass()->setVariables(
[
'para-margin-top' => '0',
'para-indent' => '1em',
]
);
} else {
$scss .= "p + p { text-indent: 1em; margin-top: 0em; } \n";
}
}
// Include blank pages?
if ( isset( $options['pdf_sectionopenings'] ) ) {
if ( 'openright' === $options['pdf_sectionopenings'] ) {
if ( $v2_compatible ) {
$styles->getSass()->setVariables(
[
'recto-verso-standard-opening' => 'right',
'recto-verso-first-section-opening' => 'right',
'recto-verso-section-opening' => 'right',
]
);
} else {
$scss .= "#title-page, #toc, div.part, div.front-matter, div.front-matter.introduction, div.front-matter + div.front-matter, div.chapter, div.chapter + div.chapter, div.back-matter, div.back-matter + div.back-matter, #half-title-page h1.title:first-of-type { page-break-before: right; } \n";
$scss .= "#copyright-page { page-break-before: left; }\n";
}
} elseif ( 'remove' === $options['pdf_sectionopenings'] ) {
if ( $v2_compatible ) {
$styles->getSass()->setVariables(
[
'recto-verso-standard-opening' => 'auto',
'recto-verso-first-section-opening' => 'auto',
'recto-verso-section-opening' => 'auto',
'recto-verso-copyright-page-opening' => 'auto',
]
);
} else {
$scss .= "#title-page, #copyright-page, #toc, div.part, div.front-matter, div.back-matter, div.chapter, #half-title-page h1.title:first-of-type { page-break-before: auto; } \n";
}
}
}
// Should we display the TOC? True (default) or false.
if ( ! $options['pdf_toc'] ) {
if ( $v2_compatible ) {
$styles->getSass()->setVariables(
[
'toc-display' => 'none',
]
);
} else {
$scss .= "#toc { display: none; } \n";
}
}
// Widows
if ( isset( $options['widows'] ) ) {
if ( $v2_compatible ) {
$styles->getSass()->setVariables(
[
'widows' => $options['widows'],
]
);
} else {
$scss .= "p { widows: {$options['widows']}; }\n";
}
} else {
if ( ! $v2_compatible ) {
$scss .= 'p { widows: 2; }' . "\n";
}
}
// Orphans
if ( isset( $options['orphans'] ) ) {
if ( $v2_compatible ) {
$styles->getSass()->setVariables(
[
'orphans' => $options['orphans'],
]
);
} else {
$scss .= "p { orphans: {$options['orphans']}; }\n";
}
} else {
if ( ! $v2_compatible ) {
$scss .= 'p { orphans: 1; }' . "\n";
}
}
// Running Content
if ( $v2_compatible ) {
$front_matter_running_content_left = ( isset( $options['running_content_front_matter_left'] ) ) ? self::replaceRunningContentTags( $options['running_content_front_matter_left'] ) : 'string(book-title)';
$front_matter_running_content_right = ( isset( $options['running_content_front_matter_right'] ) ) ? self::replaceRunningContentTags( $options['running_content_front_matter_right'] ) : 'string(section-title)';
$introduction_running_content_left = ( isset( $options['running_content_introduction_left'] ) ) ? self::replaceRunningContentTags( $options['running_content_introduction_left'] ) : 'string(book-title)';
$introduction_running_content_right = ( isset( $options['running_content_introduction_right'] ) ) ? self::replaceRunningContentTags( $options['running_content_introduction_right'] ) : 'string(section-title)';
$part_running_content_left = ( isset( $options['running_content_part_left'] ) ) ? self::replaceRunningContentTags( $options['running_content_part_left'] ) : 'string(book-title)';
$part_running_content_right = ( isset( $options['running_content_part_right'] ) ) ? self::replaceRunningContentTags( $options['running_content_part_right'] ) : 'string(part-title)';
$chapter_running_content_left = ( isset( $options['running_content_chapter_left'] ) ) ? self::replaceRunningContentTags( $options['running_content_chapter_left'] ) : 'string(book-title)';
$chapter_running_content_right = ( isset( $options['running_content_chapter_right'] ) ) ? self::replaceRunningContentTags( $options['running_content_chapter_right'] ) : 'string(section-title)';
$back_matter_running_content_left = ( isset( $options['running_content_back_matter_left'] ) ) ? self::replaceRunningContentTags( $options['running_content_back_matter_left'] ) : 'string(book-title)';
$back_matter_running_content_right = ( isset( $options['running_content_back_matter_right'] ) ) ? self::replaceRunningContentTags( $options['running_content_back_matter_right'] ) : 'string(section-title)';
$styles->getSass()->setVariables(
[
'front-matter-running-content-left' => $front_matter_running_content_left,
'front-matter-running-content-right' => $front_matter_running_content_right,
'introduction-running-content-left' => $introduction_running_content_left,
'introduction-running-content-right' => $introduction_running_content_right,
'part-running-content-left' => $part_running_content_left,
'part-running-content-right' => $part_running_content_right,
'chapter-running-content-left' => $chapter_running_content_left,
'chapter-running-content-right' => $chapter_running_content_right,
'back-matter-running-content-left' => $back_matter_running_content_left,
'back-matter-running-content-right' => $back_matter_running_content_right,
]
);
}
// a11y Font Size
if ( ! empty( $options['pdf_fontsize'] ) ) {
if ( ! $v2_compatible ) {
$scss .= 'body { font-size: 1.3em; line-height: 1.3; }' . "\n";
}
}
return $scss;
} | [
"static",
"function",
"scssOverrides",
"(",
"$",
"scss",
")",
"{",
"$",
"styles",
"=",
"Container",
"::",
"get",
"(",
"'Styles'",
")",
";",
"$",
"v2_compatible",
"=",
"$",
"styles",
"->",
"isCurrentThemeCompatible",
"(",
"2",
")",
";",
"if",
"(",
"!",
"$",
"v2_compatible",
")",
"{",
"$",
"scss",
".=",
"\"/* Theme Options */\\n\"",
";",
"}",
"// --------------------------------------------------------------------",
"// Global Options",
"$",
"options",
"=",
"get_option",
"(",
"'pressbooks_theme_options_global'",
")",
";",
"// Should we display chapter numbers? True (default) or false.",
"if",
"(",
"!",
"$",
"options",
"[",
"'chapter_numbers'",
"]",
")",
"{",
"if",
"(",
"$",
"v2_compatible",
")",
"{",
"$",
"styles",
"->",
"getSass",
"(",
")",
"->",
"setVariables",
"(",
"[",
"'chapter-number-display'",
"=>",
"'none'",
",",
"'part-number-display'",
"=>",
"'none'",
",",
"'toc-left-left-gutter'",
"=>",
"'0'",
",",
"'toc-chapter-number-display'",
"=>",
"'none'",
",",
"'toc-left-chapter-number-display'",
"=>",
"'none'",
",",
"'toc-center-chapter-number-display'",
"=>",
"'none'",
",",
"'toc-part-number-display'",
"=>",
"'none'",
",",
"'toc-left-part-number-display'",
"=>",
"'none'",
",",
"'toc-center-part-number-display'",
"=>",
"'none'",
",",
"]",
")",
";",
"}",
"else",
"{",
"$",
"scss",
".=",
"\"div.part-title-wrap > .part-number, div.chapter-title-wrap > .chapter-number, #toc .part a::before, #toc .chapter a::before { display: none !important; } \\n\"",
";",
"// TODO: NO",
"}",
"}",
"// Textbox colours.",
"if",
"(",
"$",
"v2_compatible",
")",
"{",
"foreach",
"(",
"[",
"'edu_textbox_examples_header_color'",
"=>",
"'examples-header-color'",
",",
"'edu_textbox_examples_header_background'",
"=>",
"'examples-header-background'",
",",
"'edu_textbox_examples_background'",
"=>",
"'examples-background'",
",",
"'edu_textbox_exercises_header_color'",
"=>",
"'exercises-header-color'",
",",
"'edu_textbox_exercises_header_background'",
"=>",
"'exercises-header-background'",
",",
"'edu_textbox_exercises_background'",
"=>",
"'exercises-background'",
",",
"'edu_textbox_objectives_header_color'",
"=>",
"'learning-objectives-header-color'",
",",
"'edu_textbox_objectives_header_background'",
"=>",
"'learning-objectives-header-background'",
",",
"'edu_textbox_objectives_background'",
"=>",
"'learning-objectives-background'",
",",
"'edu_textbox_takeaways_header_color'",
"=>",
"'key-takeaways-header-color'",
",",
"'edu_textbox_takeaways_header_background'",
"=>",
"'key-takeaways-header-background'",
",",
"'edu_textbox_takeaways_background'",
"=>",
"'key-takeaways-background'",
",",
"]",
"as",
"$",
"option",
"=>",
"$",
"variable",
")",
"{",
"if",
"(",
"isset",
"(",
"$",
"options",
"[",
"$",
"option",
"]",
")",
")",
"{",
"$",
"styles",
"->",
"getSass",
"(",
")",
"->",
"setVariables",
"(",
"[",
"\"$variable\"",
"=>",
"$",
"options",
"[",
"$",
"option",
"]",
",",
"]",
")",
";",
"}",
"}",
"}",
"// --------------------------------------------------------------------",
"// PDF Options",
"$",
"options",
"=",
"get_option",
"(",
"'pressbooks_theme_options_pdf'",
")",
";",
"// Change body font size",
"if",
"(",
"$",
"v2_compatible",
"&&",
"isset",
"(",
"$",
"options",
"[",
"'pdf_body_font_size'",
"]",
")",
")",
"{",
"$",
"fontsize",
"=",
"$",
"options",
"[",
"'pdf_body_font_size'",
"]",
".",
"'pt'",
";",
"$",
"styles",
"->",
"getSass",
"(",
")",
"->",
"setVariables",
"(",
"[",
"'body-font-size'",
"=>",
"\"(epub: medium, prince: $fontsize, web: 14pt)\"",
",",
"]",
")",
";",
"}",
"// Change body line height",
"if",
"(",
"$",
"v2_compatible",
"&&",
"isset",
"(",
"$",
"options",
"[",
"'pdf_body_line_height'",
"]",
")",
")",
"{",
"$",
"lineheight",
"=",
"$",
"options",
"[",
"'pdf_body_line_height'",
"]",
".",
"'em'",
";",
"$",
"styles",
"->",
"getSass",
"(",
")",
"->",
"setVariables",
"(",
"[",
"'body-line-height'",
"=>",
"\"(epub: 1.4em, prince: $lineheight, web: 1.8em)\"",
",",
"]",
")",
";",
"}",
"// Page dimensions",
"$",
"width",
"=",
"$",
"options",
"[",
"'pdf_page_width'",
"]",
";",
"$",
"height",
"=",
"$",
"options",
"[",
"'pdf_page_height'",
"]",
";",
"if",
"(",
"$",
"v2_compatible",
")",
"{",
"$",
"styles",
"->",
"getSass",
"(",
")",
"->",
"setVariables",
"(",
"[",
"'page-width'",
"=>",
"$",
"width",
",",
"'page-height'",
"=>",
"$",
"height",
",",
"]",
")",
";",
"}",
"else",
"{",
"$",
"scss",
".=",
"\"@page { size: $width $height; } \\n\"",
";",
"}",
"// Margins",
"if",
"(",
"$",
"v2_compatible",
")",
"{",
"$",
"styles",
"->",
"getSass",
"(",
")",
"->",
"setVariables",
"(",
"[",
"'page-margin-top'",
"=>",
"(",
"isset",
"(",
"$",
"options",
"[",
"'pdf_page_margin_top'",
"]",
")",
")",
"?",
"$",
"options",
"[",
"'pdf_page_margin_top'",
"]",
":",
"'2cm'",
",",
"'page-margin-inside'",
"=>",
"(",
"isset",
"(",
"$",
"options",
"[",
"'pdf_page_margin_inside'",
"]",
")",
")",
"?",
"$",
"options",
"[",
"'pdf_page_margin_inside'",
"]",
":",
"'2cm'",
",",
"'page-margin-bottom'",
"=>",
"(",
"isset",
"(",
"$",
"options",
"[",
"'pdf_page_margin_bottom'",
"]",
")",
")",
"?",
"$",
"options",
"[",
"'pdf_page_margin_bottom'",
"]",
":",
"'2cm'",
",",
"'page-margin-outside'",
"=>",
"(",
"isset",
"(",
"$",
"options",
"[",
"'pdf_page_margin_outside'",
"]",
")",
")",
"?",
"$",
"options",
"[",
"'pdf_page_margin_outside'",
"]",
":",
"'2cm'",
",",
"]",
")",
";",
"}",
"// Should we display crop marks? True or false (default).",
"if",
"(",
"1",
"===",
"absint",
"(",
"$",
"options",
"[",
"'pdf_crop_marks'",
"]",
")",
")",
"{",
"if",
"(",
"$",
"v2_compatible",
")",
"{",
"$",
"styles",
"->",
"getSass",
"(",
")",
"->",
"setVariables",
"(",
"[",
"'page-cropmarks'",
"=>",
"'crop'",
",",
"]",
")",
";",
"}",
"else",
"{",
"$",
"scss",
".=",
"\"@page { marks: crop } \\n\"",
";",
"}",
"}",
"// Hyphens?",
"if",
"(",
"1",
"===",
"absint",
"(",
"$",
"options",
"[",
"'pdf_hyphens'",
"]",
")",
")",
"{",
"if",
"(",
"$",
"v2_compatible",
")",
"{",
"$",
"styles",
"->",
"getSass",
"(",
")",
"->",
"setVariables",
"(",
"[",
"'para-hyphens'",
"=>",
"'auto'",
",",
"]",
")",
";",
"}",
"else",
"{",
"$",
"scss",
".=",
"\"p { hyphens: auto; } \\n\"",
";",
"}",
"}",
"else",
"{",
"if",
"(",
"$",
"v2_compatible",
")",
"{",
"$",
"styles",
"->",
"getSass",
"(",
")",
"->",
"setVariables",
"(",
"[",
"'para-hyphens'",
"=>",
"'manual'",
",",
"]",
")",
";",
"}",
"else",
"{",
"$",
"scss",
".=",
"\"p { hyphens: manual; } \\n\"",
";",
"}",
"}",
"// Indent paragraphs?",
"$",
"paragraph_separation",
"=",
"$",
"options",
"[",
"'pdf_paragraph_separation'",
"]",
"??",
"'indent'",
";",
"if",
"(",
"'skiplines'",
"===",
"$",
"paragraph_separation",
")",
"{",
"if",
"(",
"$",
"v2_compatible",
")",
"{",
"$",
"styles",
"->",
"getSass",
"(",
")",
"->",
"setVariables",
"(",
"[",
"'para-margin-top'",
"=>",
"'1em'",
",",
"'para-indent'",
"=>",
"'0'",
",",
"]",
")",
";",
"}",
"else",
"{",
"$",
"scss",
".=",
"\"p + p { text-indent: 0em; margin-top: 1em; } \\n\"",
";",
"}",
"}",
"elseif",
"(",
"'indent'",
"===",
"$",
"paragraph_separation",
")",
"{",
"if",
"(",
"$",
"v2_compatible",
")",
"{",
"$",
"styles",
"->",
"getSass",
"(",
")",
"->",
"setVariables",
"(",
"[",
"'para-margin-top'",
"=>",
"'0'",
",",
"'para-indent'",
"=>",
"'1em'",
",",
"]",
")",
";",
"}",
"else",
"{",
"$",
"scss",
".=",
"\"p + p { text-indent: 1em; margin-top: 0em; } \\n\"",
";",
"}",
"}",
"// Include blank pages?",
"if",
"(",
"isset",
"(",
"$",
"options",
"[",
"'pdf_sectionopenings'",
"]",
")",
")",
"{",
"if",
"(",
"'openright'",
"===",
"$",
"options",
"[",
"'pdf_sectionopenings'",
"]",
")",
"{",
"if",
"(",
"$",
"v2_compatible",
")",
"{",
"$",
"styles",
"->",
"getSass",
"(",
")",
"->",
"setVariables",
"(",
"[",
"'recto-verso-standard-opening'",
"=>",
"'right'",
",",
"'recto-verso-first-section-opening'",
"=>",
"'right'",
",",
"'recto-verso-section-opening'",
"=>",
"'right'",
",",
"]",
")",
";",
"}",
"else",
"{",
"$",
"scss",
".=",
"\"#title-page, #toc, div.part, div.front-matter, div.front-matter.introduction, div.front-matter + div.front-matter, div.chapter, div.chapter + div.chapter, div.back-matter, div.back-matter + div.back-matter, #half-title-page h1.title:first-of-type { page-break-before: right; } \\n\"",
";",
"$",
"scss",
".=",
"\"#copyright-page { page-break-before: left; }\\n\"",
";",
"}",
"}",
"elseif",
"(",
"'remove'",
"===",
"$",
"options",
"[",
"'pdf_sectionopenings'",
"]",
")",
"{",
"if",
"(",
"$",
"v2_compatible",
")",
"{",
"$",
"styles",
"->",
"getSass",
"(",
")",
"->",
"setVariables",
"(",
"[",
"'recto-verso-standard-opening'",
"=>",
"'auto'",
",",
"'recto-verso-first-section-opening'",
"=>",
"'auto'",
",",
"'recto-verso-section-opening'",
"=>",
"'auto'",
",",
"'recto-verso-copyright-page-opening'",
"=>",
"'auto'",
",",
"]",
")",
";",
"}",
"else",
"{",
"$",
"scss",
".=",
"\"#title-page, #copyright-page, #toc, div.part, div.front-matter, div.back-matter, div.chapter, #half-title-page h1.title:first-of-type { page-break-before: auto; } \\n\"",
";",
"}",
"}",
"}",
"// Should we display the TOC? True (default) or false.",
"if",
"(",
"!",
"$",
"options",
"[",
"'pdf_toc'",
"]",
")",
"{",
"if",
"(",
"$",
"v2_compatible",
")",
"{",
"$",
"styles",
"->",
"getSass",
"(",
")",
"->",
"setVariables",
"(",
"[",
"'toc-display'",
"=>",
"'none'",
",",
"]",
")",
";",
"}",
"else",
"{",
"$",
"scss",
".=",
"\"#toc { display: none; } \\n\"",
";",
"}",
"}",
"// Widows",
"if",
"(",
"isset",
"(",
"$",
"options",
"[",
"'widows'",
"]",
")",
")",
"{",
"if",
"(",
"$",
"v2_compatible",
")",
"{",
"$",
"styles",
"->",
"getSass",
"(",
")",
"->",
"setVariables",
"(",
"[",
"'widows'",
"=>",
"$",
"options",
"[",
"'widows'",
"]",
",",
"]",
")",
";",
"}",
"else",
"{",
"$",
"scss",
".=",
"\"p { widows: {$options['widows']}; }\\n\"",
";",
"}",
"}",
"else",
"{",
"if",
"(",
"!",
"$",
"v2_compatible",
")",
"{",
"$",
"scss",
".=",
"'p { widows: 2; }'",
".",
"\"\\n\"",
";",
"}",
"}",
"// Orphans",
"if",
"(",
"isset",
"(",
"$",
"options",
"[",
"'orphans'",
"]",
")",
")",
"{",
"if",
"(",
"$",
"v2_compatible",
")",
"{",
"$",
"styles",
"->",
"getSass",
"(",
")",
"->",
"setVariables",
"(",
"[",
"'orphans'",
"=>",
"$",
"options",
"[",
"'orphans'",
"]",
",",
"]",
")",
";",
"}",
"else",
"{",
"$",
"scss",
".=",
"\"p { orphans: {$options['orphans']}; }\\n\"",
";",
"}",
"}",
"else",
"{",
"if",
"(",
"!",
"$",
"v2_compatible",
")",
"{",
"$",
"scss",
".=",
"'p { orphans: 1; }'",
".",
"\"\\n\"",
";",
"}",
"}",
"// Running Content",
"if",
"(",
"$",
"v2_compatible",
")",
"{",
"$",
"front_matter_running_content_left",
"=",
"(",
"isset",
"(",
"$",
"options",
"[",
"'running_content_front_matter_left'",
"]",
")",
")",
"?",
"self",
"::",
"replaceRunningContentTags",
"(",
"$",
"options",
"[",
"'running_content_front_matter_left'",
"]",
")",
":",
"'string(book-title)'",
";",
"$",
"front_matter_running_content_right",
"=",
"(",
"isset",
"(",
"$",
"options",
"[",
"'running_content_front_matter_right'",
"]",
")",
")",
"?",
"self",
"::",
"replaceRunningContentTags",
"(",
"$",
"options",
"[",
"'running_content_front_matter_right'",
"]",
")",
":",
"'string(section-title)'",
";",
"$",
"introduction_running_content_left",
"=",
"(",
"isset",
"(",
"$",
"options",
"[",
"'running_content_introduction_left'",
"]",
")",
")",
"?",
"self",
"::",
"replaceRunningContentTags",
"(",
"$",
"options",
"[",
"'running_content_introduction_left'",
"]",
")",
":",
"'string(book-title)'",
";",
"$",
"introduction_running_content_right",
"=",
"(",
"isset",
"(",
"$",
"options",
"[",
"'running_content_introduction_right'",
"]",
")",
")",
"?",
"self",
"::",
"replaceRunningContentTags",
"(",
"$",
"options",
"[",
"'running_content_introduction_right'",
"]",
")",
":",
"'string(section-title)'",
";",
"$",
"part_running_content_left",
"=",
"(",
"isset",
"(",
"$",
"options",
"[",
"'running_content_part_left'",
"]",
")",
")",
"?",
"self",
"::",
"replaceRunningContentTags",
"(",
"$",
"options",
"[",
"'running_content_part_left'",
"]",
")",
":",
"'string(book-title)'",
";",
"$",
"part_running_content_right",
"=",
"(",
"isset",
"(",
"$",
"options",
"[",
"'running_content_part_right'",
"]",
")",
")",
"?",
"self",
"::",
"replaceRunningContentTags",
"(",
"$",
"options",
"[",
"'running_content_part_right'",
"]",
")",
":",
"'string(part-title)'",
";",
"$",
"chapter_running_content_left",
"=",
"(",
"isset",
"(",
"$",
"options",
"[",
"'running_content_chapter_left'",
"]",
")",
")",
"?",
"self",
"::",
"replaceRunningContentTags",
"(",
"$",
"options",
"[",
"'running_content_chapter_left'",
"]",
")",
":",
"'string(book-title)'",
";",
"$",
"chapter_running_content_right",
"=",
"(",
"isset",
"(",
"$",
"options",
"[",
"'running_content_chapter_right'",
"]",
")",
")",
"?",
"self",
"::",
"replaceRunningContentTags",
"(",
"$",
"options",
"[",
"'running_content_chapter_right'",
"]",
")",
":",
"'string(section-title)'",
";",
"$",
"back_matter_running_content_left",
"=",
"(",
"isset",
"(",
"$",
"options",
"[",
"'running_content_back_matter_left'",
"]",
")",
")",
"?",
"self",
"::",
"replaceRunningContentTags",
"(",
"$",
"options",
"[",
"'running_content_back_matter_left'",
"]",
")",
":",
"'string(book-title)'",
";",
"$",
"back_matter_running_content_right",
"=",
"(",
"isset",
"(",
"$",
"options",
"[",
"'running_content_back_matter_right'",
"]",
")",
")",
"?",
"self",
"::",
"replaceRunningContentTags",
"(",
"$",
"options",
"[",
"'running_content_back_matter_right'",
"]",
")",
":",
"'string(section-title)'",
";",
"$",
"styles",
"->",
"getSass",
"(",
")",
"->",
"setVariables",
"(",
"[",
"'front-matter-running-content-left'",
"=>",
"$",
"front_matter_running_content_left",
",",
"'front-matter-running-content-right'",
"=>",
"$",
"front_matter_running_content_right",
",",
"'introduction-running-content-left'",
"=>",
"$",
"introduction_running_content_left",
",",
"'introduction-running-content-right'",
"=>",
"$",
"introduction_running_content_right",
",",
"'part-running-content-left'",
"=>",
"$",
"part_running_content_left",
",",
"'part-running-content-right'",
"=>",
"$",
"part_running_content_right",
",",
"'chapter-running-content-left'",
"=>",
"$",
"chapter_running_content_left",
",",
"'chapter-running-content-right'",
"=>",
"$",
"chapter_running_content_right",
",",
"'back-matter-running-content-left'",
"=>",
"$",
"back_matter_running_content_left",
",",
"'back-matter-running-content-right'",
"=>",
"$",
"back_matter_running_content_right",
",",
"]",
")",
";",
"}",
"// a11y Font Size",
"if",
"(",
"!",
"empty",
"(",
"$",
"options",
"[",
"'pdf_fontsize'",
"]",
")",
")",
"{",
"if",
"(",
"!",
"$",
"v2_compatible",
")",
"{",
"$",
"scss",
".=",
"'body { font-size: 1.3em; line-height: 1.3; }'",
".",
"\"\\n\"",
";",
"}",
"}",
"return",
"$",
"scss",
";",
"}"
] | Apply overrides.
@param string $scss
@return string
@since 3.9.8 | [
"Apply",
"overrides",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/themeoptions/class-pdfoptions.php#L1763-L2053 |
pressbooks/pressbooks | inc/htmlbook/class-validator.php | Validator.validate | public function validate( string $path ) {
$command = PB_XMLLINT_COMMAND . ' --noout --schema ' . escapeshellcmd( $this->getSchemaPath() ) . ' ' . escapeshellcmd( $path ) . ' 2>&1';
// Execute command
$this->errors = [];
$output = [];
$return_var = 0;
exec( $command, $output, $return_var );
if ( isset( $output[0] ) && str_ends_with( $output[0], ' validates' ) ) {
return true;
} else {
$this->errors = $output;
return false;
}
} | php | public function validate( string $path ) {
$command = PB_XMLLINT_COMMAND . ' --noout --schema ' . escapeshellcmd( $this->getSchemaPath() ) . ' ' . escapeshellcmd( $path ) . ' 2>&1';
// Execute command
$this->errors = [];
$output = [];
$return_var = 0;
exec( $command, $output, $return_var );
if ( isset( $output[0] ) && str_ends_with( $output[0], ' validates' ) ) {
return true;
} else {
$this->errors = $output;
return false;
}
} | [
"public",
"function",
"validate",
"(",
"string",
"$",
"path",
")",
"{",
"$",
"command",
"=",
"PB_XMLLINT_COMMAND",
".",
"' --noout --schema '",
".",
"escapeshellcmd",
"(",
"$",
"this",
"->",
"getSchemaPath",
"(",
")",
")",
".",
"' '",
".",
"escapeshellcmd",
"(",
"$",
"path",
")",
".",
"' 2>&1'",
";",
"// Execute command",
"$",
"this",
"->",
"errors",
"=",
"[",
"]",
";",
"$",
"output",
"=",
"[",
"]",
";",
"$",
"return_var",
"=",
"0",
";",
"exec",
"(",
"$",
"command",
",",
"$",
"output",
",",
"$",
"return_var",
")",
";",
"if",
"(",
"isset",
"(",
"$",
"output",
"[",
"0",
"]",
")",
"&&",
"str_ends_with",
"(",
"$",
"output",
"[",
"0",
"]",
",",
"' validates'",
")",
")",
"{",
"return",
"true",
";",
"}",
"else",
"{",
"$",
"this",
"->",
"errors",
"=",
"$",
"output",
";",
"return",
"false",
";",
"}",
"}"
] | Validate an HTMLBook file
@param string $path
@return bool | [
"Validate",
"an",
"HTMLBook",
"file"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/htmlbook/class-validator.php#L53-L69 |
pressbooks/pressbooks | inc/covergenerator/class-isbn.php | Isbn.createBarcode | public function createBarcode( $isbn_number ) {
if ( ! $this->validateIsbnNumber( $isbn_number ) ) {
\Pressbooks\add_error( __( 'There was a problem creating the barcode: Invalid ISBN number.', 'pressbooks' ) );
return false;
}
$this->isbnNumber = $this->fixIsbnNumber( $isbn_number );
$ps = \Pressbooks\Utility\create_tmp_file();
$png = \Pressbooks\Utility\create_tmp_file();
$this->compile( $ps );
$this->gs( $ps, $png, $this->dpi );
$this->crop( $png );
$old_id = \Pressbooks\Image\attachment_id_from_url( get_option( 'pressbooks_cg_isbn' ) );
if ( $old_id ) {
wp_delete_attachment( $old_id, true );
}
if ( ! function_exists( 'media_handle_sideload' ) ) {
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
}
$pid = media_handle_sideload(
[
'name' => "{$this->isbnNumber}.png",
'tmp_name' => $png,
], 0
);
if ( is_wp_error( $pid ) ) {
throw new \Exception(
$pid->get_error_message()
);
}
$src = wp_get_attachment_url( $pid );
if ( false === $src ) {
throw new \Exception( 'No attachment url.' );
}
update_option( 'pressbooks_cg_isbn', $src );
return $src;
} | php | public function createBarcode( $isbn_number ) {
if ( ! $this->validateIsbnNumber( $isbn_number ) ) {
\Pressbooks\add_error( __( 'There was a problem creating the barcode: Invalid ISBN number.', 'pressbooks' ) );
return false;
}
$this->isbnNumber = $this->fixIsbnNumber( $isbn_number );
$ps = \Pressbooks\Utility\create_tmp_file();
$png = \Pressbooks\Utility\create_tmp_file();
$this->compile( $ps );
$this->gs( $ps, $png, $this->dpi );
$this->crop( $png );
$old_id = \Pressbooks\Image\attachment_id_from_url( get_option( 'pressbooks_cg_isbn' ) );
if ( $old_id ) {
wp_delete_attachment( $old_id, true );
}
if ( ! function_exists( 'media_handle_sideload' ) ) {
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
}
$pid = media_handle_sideload(
[
'name' => "{$this->isbnNumber}.png",
'tmp_name' => $png,
], 0
);
if ( is_wp_error( $pid ) ) {
throw new \Exception(
$pid->get_error_message()
);
}
$src = wp_get_attachment_url( $pid );
if ( false === $src ) {
throw new \Exception( 'No attachment url.' );
}
update_option( 'pressbooks_cg_isbn', $src );
return $src;
} | [
"public",
"function",
"createBarcode",
"(",
"$",
"isbn_number",
")",
"{",
"if",
"(",
"!",
"$",
"this",
"->",
"validateIsbnNumber",
"(",
"$",
"isbn_number",
")",
")",
"{",
"\\",
"Pressbooks",
"\\",
"add_error",
"(",
"__",
"(",
"'There was a problem creating the barcode: Invalid ISBN number.'",
",",
"'pressbooks'",
")",
")",
";",
"return",
"false",
";",
"}",
"$",
"this",
"->",
"isbnNumber",
"=",
"$",
"this",
"->",
"fixIsbnNumber",
"(",
"$",
"isbn_number",
")",
";",
"$",
"ps",
"=",
"\\",
"Pressbooks",
"\\",
"Utility",
"\\",
"create_tmp_file",
"(",
")",
";",
"$",
"png",
"=",
"\\",
"Pressbooks",
"\\",
"Utility",
"\\",
"create_tmp_file",
"(",
")",
";",
"$",
"this",
"->",
"compile",
"(",
"$",
"ps",
")",
";",
"$",
"this",
"->",
"gs",
"(",
"$",
"ps",
",",
"$",
"png",
",",
"$",
"this",
"->",
"dpi",
")",
";",
"$",
"this",
"->",
"crop",
"(",
"$",
"png",
")",
";",
"$",
"old_id",
"=",
"\\",
"Pressbooks",
"\\",
"Image",
"\\",
"attachment_id_from_url",
"(",
"get_option",
"(",
"'pressbooks_cg_isbn'",
")",
")",
";",
"if",
"(",
"$",
"old_id",
")",
"{",
"wp_delete_attachment",
"(",
"$",
"old_id",
",",
"true",
")",
";",
"}",
"if",
"(",
"!",
"function_exists",
"(",
"'media_handle_sideload'",
")",
")",
"{",
"require_once",
"(",
"ABSPATH",
".",
"'wp-admin/includes/image.php'",
")",
";",
"require_once",
"(",
"ABSPATH",
".",
"'wp-admin/includes/file.php'",
")",
";",
"require_once",
"(",
"ABSPATH",
".",
"'wp-admin/includes/media.php'",
")",
";",
"}",
"$",
"pid",
"=",
"media_handle_sideload",
"(",
"[",
"'name'",
"=>",
"\"{$this->isbnNumber}.png\"",
",",
"'tmp_name'",
"=>",
"$",
"png",
",",
"]",
",",
"0",
")",
";",
"if",
"(",
"is_wp_error",
"(",
"$",
"pid",
")",
")",
"{",
"throw",
"new",
"\\",
"Exception",
"(",
"$",
"pid",
"->",
"get_error_message",
"(",
")",
")",
";",
"}",
"$",
"src",
"=",
"wp_get_attachment_url",
"(",
"$",
"pid",
")",
";",
"if",
"(",
"false",
"===",
"$",
"src",
")",
"{",
"throw",
"new",
"\\",
"Exception",
"(",
"'No attachment url.'",
")",
";",
"}",
"update_option",
"(",
"'pressbooks_cg_isbn'",
",",
"$",
"src",
")",
";",
"return",
"$",
"src",
";",
"}"
] | Create an ISBN png and sideload it into WordPress
@param string $isbn_number
@throws \Exception
@return string | [
"Create",
"an",
"ISBN",
"png",
"and",
"sideload",
"it",
"into",
"WordPress"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/covergenerator/class-isbn.php#L59-L107 |
pressbooks/pressbooks | inc/covergenerator/class-isbn.php | Isbn.validateIsbnNumber | public function validateIsbnNumber( $isbn_number ) {
// Regex to split a string only by the last whitespace character
@list( $isbn_number, $addon ) = preg_split( '/\s+(?=\S*+$)/', trim( $isbn_number ) ); // @codingStandardsIgnoreLine
$is_valid_isbn = ( new \Isbn\Isbn() )->validation->isbn( $isbn_number );
$is_valid_addon = true;
if ( $addon ) {
if ( ! preg_match( '/^([0-9]{2}|[0-9]{5})$/', $addon ) ) {
$is_valid_addon = false;
}
}
return $is_valid_isbn && $is_valid_addon;
} | php | public function validateIsbnNumber( $isbn_number ) {
// Regex to split a string only by the last whitespace character
@list( $isbn_number, $addon ) = preg_split( '/\s+(?=\S*+$)/', trim( $isbn_number ) ); // @codingStandardsIgnoreLine
$is_valid_isbn = ( new \Isbn\Isbn() )->validation->isbn( $isbn_number );
$is_valid_addon = true;
if ( $addon ) {
if ( ! preg_match( '/^([0-9]{2}|[0-9]{5})$/', $addon ) ) {
$is_valid_addon = false;
}
}
return $is_valid_isbn && $is_valid_addon;
} | [
"public",
"function",
"validateIsbnNumber",
"(",
"$",
"isbn_number",
")",
"{",
"// Regex to split a string only by the last whitespace character",
"@",
"list",
"(",
"$",
"isbn_number",
",",
"$",
"addon",
")",
"=",
"preg_split",
"(",
"'/\\s+(?=\\S*+$)/'",
",",
"trim",
"(",
"$",
"isbn_number",
")",
")",
";",
"// @codingStandardsIgnoreLine",
"$",
"is_valid_isbn",
"=",
"(",
"new",
"\\",
"Isbn",
"\\",
"Isbn",
"(",
")",
")",
"->",
"validation",
"->",
"isbn",
"(",
"$",
"isbn_number",
")",
";",
"$",
"is_valid_addon",
"=",
"true",
";",
"if",
"(",
"$",
"addon",
")",
"{",
"if",
"(",
"!",
"preg_match",
"(",
"'/^([0-9]{2}|[0-9]{5})$/'",
",",
"$",
"addon",
")",
")",
"{",
"$",
"is_valid_addon",
"=",
"false",
";",
"}",
"}",
"return",
"$",
"is_valid_isbn",
"&&",
"$",
"is_valid_addon",
";",
"}"
] | Validate an ISBN string
@param $isbn_number
@return bool | [
"Validate",
"an",
"ISBN",
"string"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/covergenerator/class-isbn.php#L117-L132 |
pressbooks/pressbooks | inc/covergenerator/class-isbn.php | Isbn.fixIsbnNumber | public function fixIsbnNumber( $isbn_number ) {
// Regex to split a string only by the last whitespace character
@list( $isbn_number, $addon ) = preg_split( '/\s+(?=\S*+$)/', trim( $isbn_number ) ); // @codingStandardsIgnoreLine
$isbn_number = ( new \Isbn\Isbn() )->hyphens->fixHyphens( $isbn_number );
if ( $addon ) {
$isbn_number .= " $addon";
}
return $isbn_number;
} | php | public function fixIsbnNumber( $isbn_number ) {
// Regex to split a string only by the last whitespace character
@list( $isbn_number, $addon ) = preg_split( '/\s+(?=\S*+$)/', trim( $isbn_number ) ); // @codingStandardsIgnoreLine
$isbn_number = ( new \Isbn\Isbn() )->hyphens->fixHyphens( $isbn_number );
if ( $addon ) {
$isbn_number .= " $addon";
}
return $isbn_number;
} | [
"public",
"function",
"fixIsbnNumber",
"(",
"$",
"isbn_number",
")",
"{",
"// Regex to split a string only by the last whitespace character",
"@",
"list",
"(",
"$",
"isbn_number",
",",
"$",
"addon",
")",
"=",
"preg_split",
"(",
"'/\\s+(?=\\S*+$)/'",
",",
"trim",
"(",
"$",
"isbn_number",
")",
")",
";",
"// @codingStandardsIgnoreLine",
"$",
"isbn_number",
"=",
"(",
"new",
"\\",
"Isbn",
"\\",
"Isbn",
"(",
")",
")",
"->",
"hyphens",
"->",
"fixHyphens",
"(",
"$",
"isbn_number",
")",
";",
"if",
"(",
"$",
"addon",
")",
"{",
"$",
"isbn_number",
".=",
"\" $addon\"",
";",
"}",
"return",
"$",
"isbn_number",
";",
"}"
] | Fix an ISBN string
@param $isbn_number
@return string | [
"Fix",
"an",
"ISBN",
"string"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/covergenerator/class-isbn.php#L141-L153 |
pressbooks/pressbooks | inc/covergenerator/class-isbn.php | Isbn.invocation | public function invocation( $isbn, $isbn_text_font, $isbn_text_size, $text_font, $text_size ) {
$ps[] = "50 50 moveto ({$isbn}) (includetext isbntextfont={$isbn_text_font} isbntextsize={$isbn_text_size} textfont={$text_font} textsize={$text_size})";
$ps[] = '/isbn /uk.co.terryburton.bwipp findresource exec';
return implode( "\n", $ps ) . "\n";
} | php | public function invocation( $isbn, $isbn_text_font, $isbn_text_size, $text_font, $text_size ) {
$ps[] = "50 50 moveto ({$isbn}) (includetext isbntextfont={$isbn_text_font} isbntextsize={$isbn_text_size} textfont={$text_font} textsize={$text_size})";
$ps[] = '/isbn /uk.co.terryburton.bwipp findresource exec';
return implode( "\n", $ps ) . "\n";
} | [
"public",
"function",
"invocation",
"(",
"$",
"isbn",
",",
"$",
"isbn_text_font",
",",
"$",
"isbn_text_size",
",",
"$",
"text_font",
",",
"$",
"text_size",
")",
"{",
"$",
"ps",
"[",
"]",
"=",
"\"50 50 moveto ({$isbn}) (includetext isbntextfont={$isbn_text_font} isbntextsize={$isbn_text_size} textfont={$text_font} textsize={$text_size})\"",
";",
"$",
"ps",
"[",
"]",
"=",
"'/isbn /uk.co.terryburton.bwipp findresource exec'",
";",
"return",
"implode",
"(",
"\"\\n\"",
",",
"$",
"ps",
")",
".",
"\"\\n\"",
";",
"}"
] | ISBN Invocation Code.
@see https://github.com/bwipp/postscriptbarcode/wiki/ISBN
@see https://github.com/bwipp/postscriptbarcode/wiki/Symbol-Dimensions
@see https://github.com/bwipp/postscriptbarcode/wiki/Text-Properties
@param string $isbn
@param string $isbn_text_font
@param float $isbn_text_size
@param string $text_font
@param float $text_size
@return string | [
"ISBN",
"Invocation",
"Code",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/covergenerator/class-isbn.php#L171-L177 |
pressbooks/pressbooks | inc/covergenerator/class-isbn.php | Isbn.compile | public function compile( $path_to_ps ) {
if ( empty( $this->isbnNumber ) ) {
throw new \LogicException( '$this->isbnNumber is not set' );
}
$isbn = \Pressbooks\Utility\get_contents( PB_PLUGIN_DIR . 'symbionts/postscriptbarcode/isbn.ps' );
$invocation = $this->invocation(
$this->isbnNumber,
$this->isbnTextFont,
$this->isbnTextSize,
$this->textFont,
$this->textSize
);
// @codingStandardsIgnoreStart
file_put_contents( $path_to_ps, $isbn );
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->isbnNumber ) ) {
throw new \LogicException( '$this->isbnNumber is not set' );
}
$isbn = \Pressbooks\Utility\get_contents( PB_PLUGIN_DIR . 'symbionts/postscriptbarcode/isbn.ps' );
$invocation = $this->invocation(
$this->isbnNumber,
$this->isbnTextFont,
$this->isbnTextSize,
$this->textFont,
$this->textSize
);
// @codingStandardsIgnoreStart
file_put_contents( $path_to_ps, $isbn );
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",
"->",
"isbnNumber",
")",
")",
"{",
"throw",
"new",
"\\",
"LogicException",
"(",
"'$this->isbnNumber is not set'",
")",
";",
"}",
"$",
"isbn",
"=",
"\\",
"Pressbooks",
"\\",
"Utility",
"\\",
"get_contents",
"(",
"PB_PLUGIN_DIR",
".",
"'symbionts/postscriptbarcode/isbn.ps'",
")",
";",
"$",
"invocation",
"=",
"$",
"this",
"->",
"invocation",
"(",
"$",
"this",
"->",
"isbnNumber",
",",
"$",
"this",
"->",
"isbnTextFont",
",",
"$",
"this",
"->",
"isbnTextSize",
",",
"$",
"this",
"->",
"textFont",
",",
"$",
"this",
"->",
"textSize",
")",
";",
"// @codingStandardsIgnoreStart",
"file_put_contents",
"(",
"$",
"path_to_ps",
",",
"$",
"isbn",
")",
";",
"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 an ISBN Postscript file
@param string $path_to_ps
@throws \LogicException | [
"Compile",
"an",
"ISBN",
"Postscript",
"file"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/covergenerator/class-isbn.php#L187-L208 |
pressbooks/pressbooks | inc/covergenerator/class-isbn.php | Isbn.gs | public function gs( $input_path_to_ps, $output_path_to_png, $dpi ) {
$dpi = (int) $dpi;
$command = PB_GS_COMMAND . " -dQUIET -dNOPAUSE -dSAFER -dBATCH -sDEVICE=pnggray -r{$dpi} -sOutputFile=" . escapeshellarg( $output_path_to_png ) . ' ' . escapeshellarg( $input_path_to_ps );
// Execute command
$output = [];
$return_var = 0;
exec( $command, $output, $return_var ); // @codingStandardsIgnoreLine
if ( ! empty( $output ) ) {
// @codingStandardsIgnoreStart
debug_error_log( $command );
debug_error_log( print_r( $output, true ) );
// @codingStandardsIgnoreEnd
}
} | php | public function gs( $input_path_to_ps, $output_path_to_png, $dpi ) {
$dpi = (int) $dpi;
$command = PB_GS_COMMAND . " -dQUIET -dNOPAUSE -dSAFER -dBATCH -sDEVICE=pnggray -r{$dpi} -sOutputFile=" . escapeshellarg( $output_path_to_png ) . ' ' . escapeshellarg( $input_path_to_ps );
// Execute command
$output = [];
$return_var = 0;
exec( $command, $output, $return_var ); // @codingStandardsIgnoreLine
if ( ! empty( $output ) ) {
// @codingStandardsIgnoreStart
debug_error_log( $command );
debug_error_log( print_r( $output, true ) );
// @codingStandardsIgnoreEnd
}
} | [
"public",
"function",
"gs",
"(",
"$",
"input_path_to_ps",
",",
"$",
"output_path_to_png",
",",
"$",
"dpi",
")",
"{",
"$",
"dpi",
"=",
"(",
"int",
")",
"$",
"dpi",
";",
"$",
"command",
"=",
"PB_GS_COMMAND",
".",
"\" -dQUIET -dNOPAUSE -dSAFER -dBATCH -sDEVICE=pnggray -r{$dpi} -sOutputFile=\"",
".",
"escapeshellarg",
"(",
"$",
"output_path_to_png",
")",
".",
"' '",
".",
"escapeshellarg",
"(",
"$",
"input_path_to_ps",
")",
";",
"// Execute command",
"$",
"output",
"=",
"[",
"]",
";",
"$",
"return_var",
"=",
"0",
";",
"exec",
"(",
"$",
"command",
",",
"$",
"output",
",",
"$",
"return_var",
")",
";",
"// @codingStandardsIgnoreLine",
"if",
"(",
"!",
"empty",
"(",
"$",
"output",
")",
")",
"{",
"// @codingStandardsIgnoreStart",
"debug_error_log",
"(",
"$",
"command",
")",
";",
"debug_error_log",
"(",
"print_r",
"(",
"$",
"output",
",",
"true",
")",
")",
";",
"// @codingStandardsIgnoreEnd",
"}",
"}"
] | Use Ghostscript to convert a PostScript file into a grayscale PNG file
@param string $input_path_to_ps
@param string $output_path_to_png
@param int $dpi | [
"Use",
"Ghostscript",
"to",
"convert",
"a",
"PostScript",
"file",
"into",
"a",
"grayscale",
"PNG",
"file"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/covergenerator/class-isbn.php#L218-L234 |
pressbooks/pressbooks | inc/covergenerator/class-isbn.php | Isbn.crop | public function crop( $path_to_png, $border = '20x20' ) {
$command = PB_CONVERT_COMMAND . ' ' . escapeshellarg( $path_to_png ) . " -trim +repage -bordercolor white -border {$border} " . escapeshellarg( $path_to_png );
// Execute command
$output = [];
$return_var = 0;
exec( $command, $output, $return_var ); // @codingStandardsIgnoreLine
if ( ! empty( $output ) ) {
// @codingStandardsIgnoreStart
debug_error_log( $command );
debug_error_log( print_r( $output, true ) );
// @codingStandardsIgnoreEnd
}
} | php | public function crop( $path_to_png, $border = '20x20' ) {
$command = PB_CONVERT_COMMAND . ' ' . escapeshellarg( $path_to_png ) . " -trim +repage -bordercolor white -border {$border} " . escapeshellarg( $path_to_png );
// Execute command
$output = [];
$return_var = 0;
exec( $command, $output, $return_var ); // @codingStandardsIgnoreLine
if ( ! empty( $output ) ) {
// @codingStandardsIgnoreStart
debug_error_log( $command );
debug_error_log( print_r( $output, true ) );
// @codingStandardsIgnoreEnd
}
} | [
"public",
"function",
"crop",
"(",
"$",
"path_to_png",
",",
"$",
"border",
"=",
"'20x20'",
")",
"{",
"$",
"command",
"=",
"PB_CONVERT_COMMAND",
".",
"' '",
".",
"escapeshellarg",
"(",
"$",
"path_to_png",
")",
".",
"\" -trim +repage -bordercolor white -border {$border} \"",
".",
"escapeshellarg",
"(",
"$",
"path_to_png",
")",
";",
"// Execute command",
"$",
"output",
"=",
"[",
"]",
";",
"$",
"return_var",
"=",
"0",
";",
"exec",
"(",
"$",
"command",
",",
"$",
"output",
",",
"$",
"return_var",
")",
";",
"// @codingStandardsIgnoreLine",
"if",
"(",
"!",
"empty",
"(",
"$",
"output",
")",
")",
"{",
"// @codingStandardsIgnoreStart",
"debug_error_log",
"(",
"$",
"command",
")",
";",
"debug_error_log",
"(",
"print_r",
"(",
"$",
"output",
",",
"true",
")",
")",
";",
"// @codingStandardsIgnoreEnd",
"}",
"}"
] | Use ImageMagick to automatically crop & pad a PNG file with a white border
@param string $path_to_png
@param string $border | [
"Use",
"ImageMagick",
"to",
"automatically",
"crop",
"&",
"pad",
"a",
"PNG",
"file",
"with",
"a",
"white",
"border"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/covergenerator/class-isbn.php#L243-L258 |
pressbooks/pressbooks | inc/covergenerator/class-docraptorjpg.php | DocraptorJpg.generate | public function generate() {
$tmp_pdf_path = create_tmp_file();
$this->generateWithDocraptor( $this->pdfProfile, $this->generateHtml(), $tmp_pdf_path );
$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();
$this->generateWithDocraptor( $this->pdfProfile, $this->generateHtml(), $tmp_pdf_path );
$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",
"(",
")",
";",
"$",
"this",
"->",
"generateWithDocraptor",
"(",
"$",
"this",
"->",
"pdfProfile",
",",
"$",
"this",
"->",
"generateHtml",
"(",
")",
",",
"$",
"tmp_pdf_path",
")",
";",
"$",
"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-docraptorjpg.php#L77-L84 |
pressbooks/pressbooks | inc/covergenerator/class-docraptorjpg.php | DocraptorJpg.generateCss | protected function generateCss() {
$styles = Container::get( 'Styles' );
$scss = $this->getScssVars();
if ( $styles->isCurrentThemeCompatible( 1 ) ) {
$scss .= "@import 'fonts-prince'; \n";
} elseif ( $styles->isCurrentThemeCompatible( 2 ) ) {
$scss .= "@import 'fonts'; \n";
}
$scss .= \Pressbooks\Utility\get_contents( PB_PLUGIN_DIR . 'assets/covergenerator/_jpg.scss' );
$css = $styles->customize( 'prince', $scss );
$css = \Pressbooks\Sanitize\normalize_css_urls( $css );
if ( WP_DEBUG ) {
Container::get( 'Sass' )->debug( $css, $scss, 'cover-jpg' );
}
return $css;
} | php | protected function generateCss() {
$styles = Container::get( 'Styles' );
$scss = $this->getScssVars();
if ( $styles->isCurrentThemeCompatible( 1 ) ) {
$scss .= "@import 'fonts-prince'; \n";
} elseif ( $styles->isCurrentThemeCompatible( 2 ) ) {
$scss .= "@import 'fonts'; \n";
}
$scss .= \Pressbooks\Utility\get_contents( PB_PLUGIN_DIR . 'assets/covergenerator/_jpg.scss' );
$css = $styles->customize( 'prince', $scss );
$css = \Pressbooks\Sanitize\normalize_css_urls( $css );
if ( WP_DEBUG ) {
Container::get( 'Sass' )->debug( $css, $scss, 'cover-jpg' );
}
return $css;
} | [
"protected",
"function",
"generateCss",
"(",
")",
"{",
"$",
"styles",
"=",
"Container",
"::",
"get",
"(",
"'Styles'",
")",
";",
"$",
"scss",
"=",
"$",
"this",
"->",
"getScssVars",
"(",
")",
";",
"if",
"(",
"$",
"styles",
"->",
"isCurrentThemeCompatible",
"(",
"1",
")",
")",
"{",
"$",
"scss",
".=",
"\"@import 'fonts-prince'; \\n\"",
";",
"}",
"elseif",
"(",
"$",
"styles",
"->",
"isCurrentThemeCompatible",
"(",
"2",
")",
")",
"{",
"$",
"scss",
".=",
"\"@import 'fonts'; \\n\"",
";",
"}",
"$",
"scss",
".=",
"\\",
"Pressbooks",
"\\",
"Utility",
"\\",
"get_contents",
"(",
"PB_PLUGIN_DIR",
".",
"'assets/covergenerator/_jpg.scss'",
")",
";",
"$",
"css",
"=",
"$",
"styles",
"->",
"customize",
"(",
"'prince'",
",",
"$",
"scss",
")",
";",
"$",
"css",
"=",
"\\",
"Pressbooks",
"\\",
"Sanitize",
"\\",
"normalize_css_urls",
"(",
"$",
"css",
")",
";",
"if",
"(",
"WP_DEBUG",
")",
"{",
"Container",
"::",
"get",
"(",
"'Sass'",
")",
"->",
"debug",
"(",
"$",
"css",
",",
"$",
"scss",
",",
"'cover-jpg'",
")",
";",
"}",
"return",
"$",
"css",
";",
"}"
] | Generate CSS for Ebook JPG cover
@return string the generated CSS | [
"Generate",
"CSS",
"for",
"Ebook",
"JPG",
"cover"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/covergenerator/class-docraptorjpg.php#L92-L111 |
pressbooks/pressbooks | inc/covergenerator/class-docraptorjpg.php | DocraptorJpg.generateHtml | protected function generateHtml() {
$vars = $this->getHtmlTemplateVars();
$vars['css'] = apply_filters( 'pb_epub_cover_css_override', $this->generateCss() );
$html = template( PB_PLUGIN_DIR . 'templates/covergenerator/jpg-cover.php', $vars );
return $html;
} | php | protected function generateHtml() {
$vars = $this->getHtmlTemplateVars();
$vars['css'] = apply_filters( 'pb_epub_cover_css_override', $this->generateCss() );
$html = template( PB_PLUGIN_DIR . 'templates/covergenerator/jpg-cover.php', $vars );
return $html;
} | [
"protected",
"function",
"generateHtml",
"(",
")",
"{",
"$",
"vars",
"=",
"$",
"this",
"->",
"getHtmlTemplateVars",
"(",
")",
";",
"$",
"vars",
"[",
"'css'",
"]",
"=",
"apply_filters",
"(",
"'pb_epub_cover_css_override'",
",",
"$",
"this",
"->",
"generateCss",
"(",
")",
")",
";",
"$",
"html",
"=",
"template",
"(",
"PB_PLUGIN_DIR",
".",
"'templates/covergenerator/jpg-cover.php'",
",",
"$",
"vars",
")",
";",
"return",
"$",
"html",
";",
"}"
] | Generate HTML for Ebook JPG cover
@return string the generated Html | [
"Generate",
"HTML",
"for",
"Ebook",
"JPG",
"cover"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/covergenerator/class-docraptorjpg.php#L119-L124 |
pressbooks/pressbooks | inc/covergenerator/class-docraptorjpg.php | DocraptorJpg.convert | public function convert( $path_to_pdf, $path_to_jpg, $resize = '2500x3750' ) {
// Convert using Imagemagick (TODO: fix jagged ugly fonts)
// $command = PB_CONVERT_COMMAND . ' ' . escapeshellarg( $pathToPdf ) . " -density 96 -resize {$resize} -define jpeg:extent=2MB " . escapeshellarg( $pathToJpg );
// Convert using pdfToPpm
list( $x, $y ) = explode( 'x', $resize );
$path_to_jpg = rtrim( $path_to_jpg, '.jpg' ); // Remove extension because it is auto-generated by pdfToPpm
$command = PB_PDFTOPPM_COMMAND . ' -jpeg -singlefile -scale-to-x ' . (int) $x . ' -scale-to-y ' . (int) $y . ' ' . escapeshellarg( $path_to_pdf ) . ' ' . escapeshellarg( $path_to_jpg );
// Execute command
$output = [];
$return_var = 0;
exec( $command, $output, $return_var ); // @codingStandardsIgnoreLine
$post = ( new \Pressbooks\Metadata() )->getMetaPost();
if ( $post ) {
$jpg = \Pressbooks\Utility\create_tmp_file();
copy( $path_to_jpg . '.jpg', $jpg );
$old_id = \Pressbooks\Image\attachment_id_from_url( get_post_meta( $post->ID, 'pb_cover_image', true ) );
if ( $old_id ) {
wp_delete_attachment( $old_id, true );
}
if ( ! function_exists( 'media_handle_sideload' ) ) {
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
}
$pid = media_handle_sideload(
[
'name' => 'cover.jpg',
'tmp_name' => $jpg,
], 0
);
if ( is_wp_error( $pid ) ) {
throw new \Exception(
$pid->get_error_message()
);
}
$src = wp_get_attachment_url( $pid );
if ( false === $src ) {
throw new \Exception( 'No attachment url.' );
}
update_post_meta( $post->ID, 'pb_cover_image', $src );
}
if ( ! empty( $output ) ) {
// @codingStandardsIgnoreStart
debug_error_log( $command );
debug_error_log( print_r( $output, true ) );
// @codingStandardsIgnoreEnd
}
} | php | public function convert( $path_to_pdf, $path_to_jpg, $resize = '2500x3750' ) {
// Convert using Imagemagick (TODO: fix jagged ugly fonts)
// $command = PB_CONVERT_COMMAND . ' ' . escapeshellarg( $pathToPdf ) . " -density 96 -resize {$resize} -define jpeg:extent=2MB " . escapeshellarg( $pathToJpg );
// Convert using pdfToPpm
list( $x, $y ) = explode( 'x', $resize );
$path_to_jpg = rtrim( $path_to_jpg, '.jpg' ); // Remove extension because it is auto-generated by pdfToPpm
$command = PB_PDFTOPPM_COMMAND . ' -jpeg -singlefile -scale-to-x ' . (int) $x . ' -scale-to-y ' . (int) $y . ' ' . escapeshellarg( $path_to_pdf ) . ' ' . escapeshellarg( $path_to_jpg );
// Execute command
$output = [];
$return_var = 0;
exec( $command, $output, $return_var ); // @codingStandardsIgnoreLine
$post = ( new \Pressbooks\Metadata() )->getMetaPost();
if ( $post ) {
$jpg = \Pressbooks\Utility\create_tmp_file();
copy( $path_to_jpg . '.jpg', $jpg );
$old_id = \Pressbooks\Image\attachment_id_from_url( get_post_meta( $post->ID, 'pb_cover_image', true ) );
if ( $old_id ) {
wp_delete_attachment( $old_id, true );
}
if ( ! function_exists( 'media_handle_sideload' ) ) {
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
}
$pid = media_handle_sideload(
[
'name' => 'cover.jpg',
'tmp_name' => $jpg,
], 0
);
if ( is_wp_error( $pid ) ) {
throw new \Exception(
$pid->get_error_message()
);
}
$src = wp_get_attachment_url( $pid );
if ( false === $src ) {
throw new \Exception( 'No attachment url.' );
}
update_post_meta( $post->ID, 'pb_cover_image', $src );
}
if ( ! empty( $output ) ) {
// @codingStandardsIgnoreStart
debug_error_log( $command );
debug_error_log( print_r( $output, true ) );
// @codingStandardsIgnoreEnd
}
} | [
"public",
"function",
"convert",
"(",
"$",
"path_to_pdf",
",",
"$",
"path_to_jpg",
",",
"$",
"resize",
"=",
"'2500x3750'",
")",
"{",
"// Convert using Imagemagick (TODO: fix jagged ugly fonts)",
"// $command = PB_CONVERT_COMMAND . ' ' . escapeshellarg( $pathToPdf ) . \" -density 96 -resize {$resize} -define jpeg:extent=2MB \" . escapeshellarg( $pathToJpg );",
"// Convert using pdfToPpm",
"list",
"(",
"$",
"x",
",",
"$",
"y",
")",
"=",
"explode",
"(",
"'x'",
",",
"$",
"resize",
")",
";",
"$",
"path_to_jpg",
"=",
"rtrim",
"(",
"$",
"path_to_jpg",
",",
"'.jpg'",
")",
";",
"// Remove extension because it is auto-generated by pdfToPpm",
"$",
"command",
"=",
"PB_PDFTOPPM_COMMAND",
".",
"' -jpeg -singlefile -scale-to-x '",
".",
"(",
"int",
")",
"$",
"x",
".",
"' -scale-to-y '",
".",
"(",
"int",
")",
"$",
"y",
".",
"' '",
".",
"escapeshellarg",
"(",
"$",
"path_to_pdf",
")",
".",
"' '",
".",
"escapeshellarg",
"(",
"$",
"path_to_jpg",
")",
";",
"// Execute command",
"$",
"output",
"=",
"[",
"]",
";",
"$",
"return_var",
"=",
"0",
";",
"exec",
"(",
"$",
"command",
",",
"$",
"output",
",",
"$",
"return_var",
")",
";",
"// @codingStandardsIgnoreLine",
"$",
"post",
"=",
"(",
"new",
"\\",
"Pressbooks",
"\\",
"Metadata",
"(",
")",
")",
"->",
"getMetaPost",
"(",
")",
";",
"if",
"(",
"$",
"post",
")",
"{",
"$",
"jpg",
"=",
"\\",
"Pressbooks",
"\\",
"Utility",
"\\",
"create_tmp_file",
"(",
")",
";",
"copy",
"(",
"$",
"path_to_jpg",
".",
"'.jpg'",
",",
"$",
"jpg",
")",
";",
"$",
"old_id",
"=",
"\\",
"Pressbooks",
"\\",
"Image",
"\\",
"attachment_id_from_url",
"(",
"get_post_meta",
"(",
"$",
"post",
"->",
"ID",
",",
"'pb_cover_image'",
",",
"true",
")",
")",
";",
"if",
"(",
"$",
"old_id",
")",
"{",
"wp_delete_attachment",
"(",
"$",
"old_id",
",",
"true",
")",
";",
"}",
"if",
"(",
"!",
"function_exists",
"(",
"'media_handle_sideload'",
")",
")",
"{",
"require_once",
"(",
"ABSPATH",
".",
"'wp-admin/includes/image.php'",
")",
";",
"require_once",
"(",
"ABSPATH",
".",
"'wp-admin/includes/file.php'",
")",
";",
"require_once",
"(",
"ABSPATH",
".",
"'wp-admin/includes/media.php'",
")",
";",
"}",
"$",
"pid",
"=",
"media_handle_sideload",
"(",
"[",
"'name'",
"=>",
"'cover.jpg'",
",",
"'tmp_name'",
"=>",
"$",
"jpg",
",",
"]",
",",
"0",
")",
";",
"if",
"(",
"is_wp_error",
"(",
"$",
"pid",
")",
")",
"{",
"throw",
"new",
"\\",
"Exception",
"(",
"$",
"pid",
"->",
"get_error_message",
"(",
")",
")",
";",
"}",
"$",
"src",
"=",
"wp_get_attachment_url",
"(",
"$",
"pid",
")",
";",
"if",
"(",
"false",
"===",
"$",
"src",
")",
"{",
"throw",
"new",
"\\",
"Exception",
"(",
"'No attachment url.'",
")",
";",
"}",
"update_post_meta",
"(",
"$",
"post",
"->",
"ID",
",",
"'pb_cover_image'",
",",
"$",
"src",
")",
";",
"}",
"if",
"(",
"!",
"empty",
"(",
"$",
"output",
")",
")",
"{",
"// @codingStandardsIgnoreStart",
"debug_error_log",
"(",
"$",
"command",
")",
";",
"debug_error_log",
"(",
"print_r",
"(",
"$",
"output",
",",
"true",
")",
")",
";",
"// @codingStandardsIgnoreEnd",
"}",
"}"
] | Use ImageMagick to convert a PDF to a JPG (max 2MB)
@param string $path_to_pdf Input path to PDF
@param string $path_to_jpg Output path to JPG
@param string $resize
@throws \Exception | [
"Use",
"ImageMagick",
"to",
"convert",
"a",
"PDF",
"to",
"a",
"JPG",
"(",
"max",
"2MB",
")"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/covergenerator/class-docraptorjpg.php#L140-L200 |
pressbooks/pressbooks | inc/cloner/class-cloner.php | Cloner.dependencies | public function dependencies( $h5p = null, $downloads = null, $contributors = null ) {
$this->h5p = $h5p ? $h5p : \Pressbooks\Interactive\Content::init()->getH5P();
$this->downloads = $downloads ? $downloads : new Downloads( $this, $this->h5p );
$this->contributors = $contributors ? $contributors : new \Pressbooks\Contributors();
} | php | public function dependencies( $h5p = null, $downloads = null, $contributors = null ) {
$this->h5p = $h5p ? $h5p : \Pressbooks\Interactive\Content::init()->getH5P();
$this->downloads = $downloads ? $downloads : new Downloads( $this, $this->h5p );
$this->contributors = $contributors ? $contributors : new \Pressbooks\Contributors();
} | [
"public",
"function",
"dependencies",
"(",
"$",
"h5p",
"=",
"null",
",",
"$",
"downloads",
"=",
"null",
",",
"$",
"contributors",
"=",
"null",
")",
"{",
"$",
"this",
"->",
"h5p",
"=",
"$",
"h5p",
"?",
"$",
"h5p",
":",
"\\",
"Pressbooks",
"\\",
"Interactive",
"\\",
"Content",
"::",
"init",
"(",
")",
"->",
"getH5P",
"(",
")",
";",
"$",
"this",
"->",
"downloads",
"=",
"$",
"downloads",
"?",
"$",
"downloads",
":",
"new",
"Downloads",
"(",
"$",
"this",
",",
"$",
"this",
"->",
"h5p",
")",
";",
"$",
"this",
"->",
"contributors",
"=",
"$",
"contributors",
"?",
"$",
"contributors",
":",
"new",
"\\",
"Pressbooks",
"\\",
"Contributors",
"(",
")",
";",
"}"
] | For testing, ability to mock objects
@param null \Pressbooks\Interactive\H5P $h5p
@param null Downloads $downloads
@param null \Pressbooks\Contributors $contributors | [
"For",
"testing",
"ability",
"to",
"mock",
"objects"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/cloner/class-cloner.php#L278-L282 |
pressbooks/pressbooks | inc/cloner/class-cloner.php | Cloner.cloneBook | public function cloneBook() {
try {
foreach ( $this->cloneBookGenerator() as $percentage => $info ) {
// Do nothing, this is a compatibility wrapper that makes the generator work like a regular function
}
} catch ( \Exception $e ) {
return false;
}
return true;
} | php | public function cloneBook() {
try {
foreach ( $this->cloneBookGenerator() as $percentage => $info ) {
// Do nothing, this is a compatibility wrapper that makes the generator work like a regular function
}
} catch ( \Exception $e ) {
return false;
}
return true;
} | [
"public",
"function",
"cloneBook",
"(",
")",
"{",
"try",
"{",
"foreach",
"(",
"$",
"this",
"->",
"cloneBookGenerator",
"(",
")",
"as",
"$",
"percentage",
"=>",
"$",
"info",
")",
"{",
"// Do nothing, this is a compatibility wrapper that makes the generator work like a regular function",
"}",
"}",
"catch",
"(",
"\\",
"Exception",
"$",
"e",
")",
"{",
"return",
"false",
";",
"}",
"return",
"true",
";",
"}"
] | Clone a book in its entirety.
@since 4.1.0
@return bool | [
"Clone",
"a",
"book",
"in",
"its",
"entirety",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/cloner/class-cloner.php#L376-L385 |
pressbooks/pressbooks | inc/cloner/class-cloner.php | Cloner.cloneBookGenerator | public function cloneBookGenerator() : \Generator {
yield 1 => __( 'Looking up the source book', 'pressbooks' );
if ( ! $this->setupSource() ) {
throw new \Exception( ! empty( $_SESSION['pb_errors'][0] ) ? $_SESSION['pb_errors'][0] : __( 'Failed to setup source', 'pressbooks' ) );
}
// Create Book
yield 10 => __( 'Creating the target book', 'pressbooks' );
$this->targetBookId = $this->createBook();
$this->targetBookUrl = get_blogaddress_by_id( $this->targetBookId );
switch_to_blog( $this->targetBookId );
wp_defer_term_counting( true );
// Pre-processor
$this->clonePreProcess();
// Clone Metadata
yield 20 => __( 'Cloning metadata', 'pressbooks' );
$this->clonedItems['metadata'][] = $this->cloneMetadata();
// Clone Taxonomy Terms
$y = new PercentageYield( 30, 40, count( $this->sourceBookTerms ) );
$this->targetBookTerms = $this->getBookTerms( $this->targetBookUrl );
foreach ( $this->sourceBookTerms as $term ) {
yield from $y->tick( __( 'Cloning contributors and licenses', 'pressbooks' ) );
$new_term = $this->cloneTerm( $term['id'] );
if ( $new_term ) {
$this->termMap[ $term['id'] ] = $new_term;
$this->clonedItems['terms'][] = $new_term;
}
}
// Clone Front Matter
$y = new PercentageYield( 40, 50, count( $this->sourceBookStructure['front-matter'] ) );
foreach ( $this->sourceBookStructure['front-matter'] as $frontmatter ) {
yield from $y->tick( __( 'Cloning front matter', 'pressbooks' ) );
$new_frontmatter = $this->cloneFrontMatter( $frontmatter['id'] );
if ( $new_frontmatter !== false ) {
$this->clonedItems['front-matter'][] = $new_frontmatter;
}
}
// Clone Parts and chapters
$ticks = 0;
foreach ( $this->sourceBookStructure['parts'] as $key => $part ) {
$ticks += 1 + count( $this->sourceBookStructure['parts'][ $key ]['chapters'] );
}
$y = new PercentageYield( 50, 80, $ticks );
foreach ( $this->sourceBookStructure['parts'] as $key => $part ) {
yield from $y->tick( __( 'Cloning parts and chapters', 'pressbooks' ) );
$new_part = $this->clonePart( $part['id'] );
if ( $new_part !== false ) {
$this->clonedItems['parts'][] = $new_part;
foreach ( $this->sourceBookStructure['parts'][ $key ]['chapters'] as $chapter ) {
yield from $y->tick( __( 'Cloning parts and chapters', 'pressbooks' ) );
$new_chapter = $this->cloneChapter( $chapter['id'], $new_part );
if ( $new_chapter !== false ) {
$this->clonedItems['chapters'][] = $new_chapter;
}
}
}
}
// Clone Back Matter
$y = new PercentageYield( 80, 90, count( $this->sourceBookStructure['back-matter'] ) );
foreach ( $this->sourceBookStructure['back-matter'] as $backmatter ) {
yield from $y->tick( __( 'Cloning back matter', 'pressbooks' ) );
$new_backmatter = $this->cloneBackMatter( $backmatter['id'] );
if ( $new_backmatter !== false ) {
$this->clonedItems['back-matter'][] = $new_backmatter;
}
}
// Clone Glossary
$y = new PercentageYield( 90, 100, count( $this->sourceBookGlossary ) );
foreach ( $this->sourceBookGlossary as $glossary ) {
yield from $y->tick( __( 'Cloning glossary terms' ) );
$new_glossary = $this->cloneGlossary( $glossary['id'] );
if ( $new_glossary !== false ) {
$this->clonedItems['glossary'][] = $new_glossary;
}
}
// Post-processor
$this->clonePostProcess();
wp_defer_term_counting( false ); // Flush
restore_current_blog();
yield 100 => __( 'Finishing up', 'pressbooks' );
} | php | public function cloneBookGenerator() : \Generator {
yield 1 => __( 'Looking up the source book', 'pressbooks' );
if ( ! $this->setupSource() ) {
throw new \Exception( ! empty( $_SESSION['pb_errors'][0] ) ? $_SESSION['pb_errors'][0] : __( 'Failed to setup source', 'pressbooks' ) );
}
// Create Book
yield 10 => __( 'Creating the target book', 'pressbooks' );
$this->targetBookId = $this->createBook();
$this->targetBookUrl = get_blogaddress_by_id( $this->targetBookId );
switch_to_blog( $this->targetBookId );
wp_defer_term_counting( true );
// Pre-processor
$this->clonePreProcess();
// Clone Metadata
yield 20 => __( 'Cloning metadata', 'pressbooks' );
$this->clonedItems['metadata'][] = $this->cloneMetadata();
// Clone Taxonomy Terms
$y = new PercentageYield( 30, 40, count( $this->sourceBookTerms ) );
$this->targetBookTerms = $this->getBookTerms( $this->targetBookUrl );
foreach ( $this->sourceBookTerms as $term ) {
yield from $y->tick( __( 'Cloning contributors and licenses', 'pressbooks' ) );
$new_term = $this->cloneTerm( $term['id'] );
if ( $new_term ) {
$this->termMap[ $term['id'] ] = $new_term;
$this->clonedItems['terms'][] = $new_term;
}
}
// Clone Front Matter
$y = new PercentageYield( 40, 50, count( $this->sourceBookStructure['front-matter'] ) );
foreach ( $this->sourceBookStructure['front-matter'] as $frontmatter ) {
yield from $y->tick( __( 'Cloning front matter', 'pressbooks' ) );
$new_frontmatter = $this->cloneFrontMatter( $frontmatter['id'] );
if ( $new_frontmatter !== false ) {
$this->clonedItems['front-matter'][] = $new_frontmatter;
}
}
// Clone Parts and chapters
$ticks = 0;
foreach ( $this->sourceBookStructure['parts'] as $key => $part ) {
$ticks += 1 + count( $this->sourceBookStructure['parts'][ $key ]['chapters'] );
}
$y = new PercentageYield( 50, 80, $ticks );
foreach ( $this->sourceBookStructure['parts'] as $key => $part ) {
yield from $y->tick( __( 'Cloning parts and chapters', 'pressbooks' ) );
$new_part = $this->clonePart( $part['id'] );
if ( $new_part !== false ) {
$this->clonedItems['parts'][] = $new_part;
foreach ( $this->sourceBookStructure['parts'][ $key ]['chapters'] as $chapter ) {
yield from $y->tick( __( 'Cloning parts and chapters', 'pressbooks' ) );
$new_chapter = $this->cloneChapter( $chapter['id'], $new_part );
if ( $new_chapter !== false ) {
$this->clonedItems['chapters'][] = $new_chapter;
}
}
}
}
// Clone Back Matter
$y = new PercentageYield( 80, 90, count( $this->sourceBookStructure['back-matter'] ) );
foreach ( $this->sourceBookStructure['back-matter'] as $backmatter ) {
yield from $y->tick( __( 'Cloning back matter', 'pressbooks' ) );
$new_backmatter = $this->cloneBackMatter( $backmatter['id'] );
if ( $new_backmatter !== false ) {
$this->clonedItems['back-matter'][] = $new_backmatter;
}
}
// Clone Glossary
$y = new PercentageYield( 90, 100, count( $this->sourceBookGlossary ) );
foreach ( $this->sourceBookGlossary as $glossary ) {
yield from $y->tick( __( 'Cloning glossary terms' ) );
$new_glossary = $this->cloneGlossary( $glossary['id'] );
if ( $new_glossary !== false ) {
$this->clonedItems['glossary'][] = $new_glossary;
}
}
// Post-processor
$this->clonePostProcess();
wp_defer_term_counting( false ); // Flush
restore_current_blog();
yield 100 => __( 'Finishing up', 'pressbooks' );
} | [
"public",
"function",
"cloneBookGenerator",
"(",
")",
":",
"\\",
"Generator",
"{",
"yield",
"1",
"=>",
"__",
"(",
"'Looking up the source book'",
",",
"'pressbooks'",
")",
";",
"if",
"(",
"!",
"$",
"this",
"->",
"setupSource",
"(",
")",
")",
"{",
"throw",
"new",
"\\",
"Exception",
"(",
"!",
"empty",
"(",
"$",
"_SESSION",
"[",
"'pb_errors'",
"]",
"[",
"0",
"]",
")",
"?",
"$",
"_SESSION",
"[",
"'pb_errors'",
"]",
"[",
"0",
"]",
":",
"__",
"(",
"'Failed to setup source'",
",",
"'pressbooks'",
")",
")",
";",
"}",
"// Create Book",
"yield",
"10",
"=>",
"__",
"(",
"'Creating the target book'",
",",
"'pressbooks'",
")",
";",
"$",
"this",
"->",
"targetBookId",
"=",
"$",
"this",
"->",
"createBook",
"(",
")",
";",
"$",
"this",
"->",
"targetBookUrl",
"=",
"get_blogaddress_by_id",
"(",
"$",
"this",
"->",
"targetBookId",
")",
";",
"switch_to_blog",
"(",
"$",
"this",
"->",
"targetBookId",
")",
";",
"wp_defer_term_counting",
"(",
"true",
")",
";",
"// Pre-processor",
"$",
"this",
"->",
"clonePreProcess",
"(",
")",
";",
"// Clone Metadata",
"yield",
"20",
"=>",
"__",
"(",
"'Cloning metadata'",
",",
"'pressbooks'",
")",
";",
"$",
"this",
"->",
"clonedItems",
"[",
"'metadata'",
"]",
"[",
"]",
"=",
"$",
"this",
"->",
"cloneMetadata",
"(",
")",
";",
"// Clone Taxonomy Terms",
"$",
"y",
"=",
"new",
"PercentageYield",
"(",
"30",
",",
"40",
",",
"count",
"(",
"$",
"this",
"->",
"sourceBookTerms",
")",
")",
";",
"$",
"this",
"->",
"targetBookTerms",
"=",
"$",
"this",
"->",
"getBookTerms",
"(",
"$",
"this",
"->",
"targetBookUrl",
")",
";",
"foreach",
"(",
"$",
"this",
"->",
"sourceBookTerms",
"as",
"$",
"term",
")",
"{",
"yield",
"from",
"$",
"y",
"->",
"tick",
"(",
"__",
"(",
"'Cloning contributors and licenses'",
",",
"'pressbooks'",
")",
")",
";",
"$",
"new_term",
"=",
"$",
"this",
"->",
"cloneTerm",
"(",
"$",
"term",
"[",
"'id'",
"]",
")",
";",
"if",
"(",
"$",
"new_term",
")",
"{",
"$",
"this",
"->",
"termMap",
"[",
"$",
"term",
"[",
"'id'",
"]",
"]",
"=",
"$",
"new_term",
";",
"$",
"this",
"->",
"clonedItems",
"[",
"'terms'",
"]",
"[",
"]",
"=",
"$",
"new_term",
";",
"}",
"}",
"// Clone Front Matter",
"$",
"y",
"=",
"new",
"PercentageYield",
"(",
"40",
",",
"50",
",",
"count",
"(",
"$",
"this",
"->",
"sourceBookStructure",
"[",
"'front-matter'",
"]",
")",
")",
";",
"foreach",
"(",
"$",
"this",
"->",
"sourceBookStructure",
"[",
"'front-matter'",
"]",
"as",
"$",
"frontmatter",
")",
"{",
"yield",
"from",
"$",
"y",
"->",
"tick",
"(",
"__",
"(",
"'Cloning front matter'",
",",
"'pressbooks'",
")",
")",
";",
"$",
"new_frontmatter",
"=",
"$",
"this",
"->",
"cloneFrontMatter",
"(",
"$",
"frontmatter",
"[",
"'id'",
"]",
")",
";",
"if",
"(",
"$",
"new_frontmatter",
"!==",
"false",
")",
"{",
"$",
"this",
"->",
"clonedItems",
"[",
"'front-matter'",
"]",
"[",
"]",
"=",
"$",
"new_frontmatter",
";",
"}",
"}",
"// Clone Parts and chapters",
"$",
"ticks",
"=",
"0",
";",
"foreach",
"(",
"$",
"this",
"->",
"sourceBookStructure",
"[",
"'parts'",
"]",
"as",
"$",
"key",
"=>",
"$",
"part",
")",
"{",
"$",
"ticks",
"+=",
"1",
"+",
"count",
"(",
"$",
"this",
"->",
"sourceBookStructure",
"[",
"'parts'",
"]",
"[",
"$",
"key",
"]",
"[",
"'chapters'",
"]",
")",
";",
"}",
"$",
"y",
"=",
"new",
"PercentageYield",
"(",
"50",
",",
"80",
",",
"$",
"ticks",
")",
";",
"foreach",
"(",
"$",
"this",
"->",
"sourceBookStructure",
"[",
"'parts'",
"]",
"as",
"$",
"key",
"=>",
"$",
"part",
")",
"{",
"yield",
"from",
"$",
"y",
"->",
"tick",
"(",
"__",
"(",
"'Cloning parts and chapters'",
",",
"'pressbooks'",
")",
")",
";",
"$",
"new_part",
"=",
"$",
"this",
"->",
"clonePart",
"(",
"$",
"part",
"[",
"'id'",
"]",
")",
";",
"if",
"(",
"$",
"new_part",
"!==",
"false",
")",
"{",
"$",
"this",
"->",
"clonedItems",
"[",
"'parts'",
"]",
"[",
"]",
"=",
"$",
"new_part",
";",
"foreach",
"(",
"$",
"this",
"->",
"sourceBookStructure",
"[",
"'parts'",
"]",
"[",
"$",
"key",
"]",
"[",
"'chapters'",
"]",
"as",
"$",
"chapter",
")",
"{",
"yield",
"from",
"$",
"y",
"->",
"tick",
"(",
"__",
"(",
"'Cloning parts and chapters'",
",",
"'pressbooks'",
")",
")",
";",
"$",
"new_chapter",
"=",
"$",
"this",
"->",
"cloneChapter",
"(",
"$",
"chapter",
"[",
"'id'",
"]",
",",
"$",
"new_part",
")",
";",
"if",
"(",
"$",
"new_chapter",
"!==",
"false",
")",
"{",
"$",
"this",
"->",
"clonedItems",
"[",
"'chapters'",
"]",
"[",
"]",
"=",
"$",
"new_chapter",
";",
"}",
"}",
"}",
"}",
"// Clone Back Matter",
"$",
"y",
"=",
"new",
"PercentageYield",
"(",
"80",
",",
"90",
",",
"count",
"(",
"$",
"this",
"->",
"sourceBookStructure",
"[",
"'back-matter'",
"]",
")",
")",
";",
"foreach",
"(",
"$",
"this",
"->",
"sourceBookStructure",
"[",
"'back-matter'",
"]",
"as",
"$",
"backmatter",
")",
"{",
"yield",
"from",
"$",
"y",
"->",
"tick",
"(",
"__",
"(",
"'Cloning back matter'",
",",
"'pressbooks'",
")",
")",
";",
"$",
"new_backmatter",
"=",
"$",
"this",
"->",
"cloneBackMatter",
"(",
"$",
"backmatter",
"[",
"'id'",
"]",
")",
";",
"if",
"(",
"$",
"new_backmatter",
"!==",
"false",
")",
"{",
"$",
"this",
"->",
"clonedItems",
"[",
"'back-matter'",
"]",
"[",
"]",
"=",
"$",
"new_backmatter",
";",
"}",
"}",
"// Clone Glossary",
"$",
"y",
"=",
"new",
"PercentageYield",
"(",
"90",
",",
"100",
",",
"count",
"(",
"$",
"this",
"->",
"sourceBookGlossary",
")",
")",
";",
"foreach",
"(",
"$",
"this",
"->",
"sourceBookGlossary",
"as",
"$",
"glossary",
")",
"{",
"yield",
"from",
"$",
"y",
"->",
"tick",
"(",
"__",
"(",
"'Cloning glossary terms'",
")",
")",
";",
"$",
"new_glossary",
"=",
"$",
"this",
"->",
"cloneGlossary",
"(",
"$",
"glossary",
"[",
"'id'",
"]",
")",
";",
"if",
"(",
"$",
"new_glossary",
"!==",
"false",
")",
"{",
"$",
"this",
"->",
"clonedItems",
"[",
"'glossary'",
"]",
"[",
"]",
"=",
"$",
"new_glossary",
";",
"}",
"}",
"// Post-processor",
"$",
"this",
"->",
"clonePostProcess",
"(",
")",
";",
"wp_defer_term_counting",
"(",
"false",
")",
";",
"// Flush",
"restore_current_blog",
"(",
")",
";",
"yield",
"100",
"=>",
"__",
"(",
"'Finishing up'",
",",
"'pressbooks'",
")",
";",
"}"
] | Generator that yields values between 1-100, represents the percentage of progress when cloning a book in its entirety
@since 5.7.0
@throws \Exception
@return \Generator | [
"Generator",
"that",
"yields",
"values",
"between",
"1",
"-",
"100",
"represents",
"the",
"percentage",
"of",
"progress",
"when",
"cloning",
"a",
"book",
"in",
"its",
"entirety"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/cloner/class-cloner.php#L394-L486 |
pressbooks/pressbooks | inc/cloner/class-cloner.php | Cloner.setupSource | public function setupSource( $respect_book_license = true ) {
if ( ! empty( $this->sourceBookId ) ) {
// Local book
switch_to_blog( $this->sourceBookId );
} elseif ( ! $this->isCompatible( $this->sourceBookUrl ) ) {
// Remote is not compatible, bail.
$_SESSION['pb_errors'][] = __( 'You can only clone from a book hosted by Pressbooks 4.1 or later. Please ensure that your source book meets these requirements.', 'pressbooks' );
return false;
}
// Set up $this->sourceBookMetadata
$this->sourceBookMetadata = $this->getBookMetadata( $this->sourceBookUrl );
if ( empty( $this->sourceBookMetadata ) ) {
$_SESSION['pb_errors'][] = sprintf( __( 'Could not retrieve metadata from %s.', 'pressbooks' ), sprintf( '<em>%s</em>', $this->sourceBookUrl ) );
$this->maybeRestoreCurrentBlog();
return false;
}
if ( $respect_book_license ) {
// Verify license or network administrator override
if ( ! $this->isSourceCloneable( $this->sourceBookMetadata['license'] ) ) {
$_SESSION['pb_errors'][] = sprintf( __( '%s is not licensed for cloning.', 'pressbooks' ), sprintf( '<em>%s</em>', $this->sourceBookMetadata['name'] ) );
$this->maybeRestoreCurrentBlog();
return false;
}
}
// Set up $this->sourceBookStructure
$this->sourceBookStructure = $this->getBookStructure( $this->sourceBookUrl );
if ( empty( $this->sourceBookStructure ) ) {
$_SESSION['pb_errors'][] = sprintf( __( 'Could not retrieve contents and structure from %s.', 'pressbooks' ), sprintf( '<em>%s</em>', $this->sourceBookMetadata['name'] ) );
$this->maybeRestoreCurrentBlog();
return false;
}
// Set up $this->sourceBookTerms
$this->sourceBookTerms = $this->getBookTerms( $this->sourceBookUrl );
if ( empty( $this->sourceBookTerms ) ) {
$_SESSION['pb_errors'][] = sprintf( __( 'Could not retrieve taxonomies from %s.', 'pressbooks' ), sprintf( '<em>%s</em>', $this->sourceBookMetadata['name'] ) );
$this->maybeRestoreCurrentBlog();
return false;
}
// Media
$this->knownMedia = $this->buildListOfKnownMedia( $this->sourceBookUrl );
if ( $this->knownMedia === false ) {
$_SESSION['pb_errors'][] = sprintf( __( 'Could not retrieve media from %s.', 'pressbooks' ), sprintf( '<em>%s</em>', $this->sourceBookMetadata['name'] ) );
$this->maybeRestoreCurrentBlog();
return false;
}
// Sort by the length of sourceUrls for better, left to right, search and replace loops
$known_media_sorted = $this->knownMedia;
uasort(
$known_media_sorted, function ( $a, $b ) {
return strlen( $b->sourceUrl ) <=> strlen( $a->sourceUrl );
}
);
$this->knownMedia = $known_media_sorted;
// H5P
$this->knownH5P = $this->buildListOfKnownH5P( $this->sourceBookUrl );
if ( $this->knownH5P === false ) {
// No H5P endpoint was found
$this->knownH5P = [];
} else {
$this->sourceHasH5pApi = true;
}
// Set up $this->sourceBookGlossary
$this->sourceBookGlossary = $this->getBookGlossary( $this->sourceBookUrl );
$this->maybeRestoreCurrentBlog();
return true;
} | php | public function setupSource( $respect_book_license = true ) {
if ( ! empty( $this->sourceBookId ) ) {
// Local book
switch_to_blog( $this->sourceBookId );
} elseif ( ! $this->isCompatible( $this->sourceBookUrl ) ) {
// Remote is not compatible, bail.
$_SESSION['pb_errors'][] = __( 'You can only clone from a book hosted by Pressbooks 4.1 or later. Please ensure that your source book meets these requirements.', 'pressbooks' );
return false;
}
// Set up $this->sourceBookMetadata
$this->sourceBookMetadata = $this->getBookMetadata( $this->sourceBookUrl );
if ( empty( $this->sourceBookMetadata ) ) {
$_SESSION['pb_errors'][] = sprintf( __( 'Could not retrieve metadata from %s.', 'pressbooks' ), sprintf( '<em>%s</em>', $this->sourceBookUrl ) );
$this->maybeRestoreCurrentBlog();
return false;
}
if ( $respect_book_license ) {
// Verify license or network administrator override
if ( ! $this->isSourceCloneable( $this->sourceBookMetadata['license'] ) ) {
$_SESSION['pb_errors'][] = sprintf( __( '%s is not licensed for cloning.', 'pressbooks' ), sprintf( '<em>%s</em>', $this->sourceBookMetadata['name'] ) );
$this->maybeRestoreCurrentBlog();
return false;
}
}
// Set up $this->sourceBookStructure
$this->sourceBookStructure = $this->getBookStructure( $this->sourceBookUrl );
if ( empty( $this->sourceBookStructure ) ) {
$_SESSION['pb_errors'][] = sprintf( __( 'Could not retrieve contents and structure from %s.', 'pressbooks' ), sprintf( '<em>%s</em>', $this->sourceBookMetadata['name'] ) );
$this->maybeRestoreCurrentBlog();
return false;
}
// Set up $this->sourceBookTerms
$this->sourceBookTerms = $this->getBookTerms( $this->sourceBookUrl );
if ( empty( $this->sourceBookTerms ) ) {
$_SESSION['pb_errors'][] = sprintf( __( 'Could not retrieve taxonomies from %s.', 'pressbooks' ), sprintf( '<em>%s</em>', $this->sourceBookMetadata['name'] ) );
$this->maybeRestoreCurrentBlog();
return false;
}
// Media
$this->knownMedia = $this->buildListOfKnownMedia( $this->sourceBookUrl );
if ( $this->knownMedia === false ) {
$_SESSION['pb_errors'][] = sprintf( __( 'Could not retrieve media from %s.', 'pressbooks' ), sprintf( '<em>%s</em>', $this->sourceBookMetadata['name'] ) );
$this->maybeRestoreCurrentBlog();
return false;
}
// Sort by the length of sourceUrls for better, left to right, search and replace loops
$known_media_sorted = $this->knownMedia;
uasort(
$known_media_sorted, function ( $a, $b ) {
return strlen( $b->sourceUrl ) <=> strlen( $a->sourceUrl );
}
);
$this->knownMedia = $known_media_sorted;
// H5P
$this->knownH5P = $this->buildListOfKnownH5P( $this->sourceBookUrl );
if ( $this->knownH5P === false ) {
// No H5P endpoint was found
$this->knownH5P = [];
} else {
$this->sourceHasH5pApi = true;
}
// Set up $this->sourceBookGlossary
$this->sourceBookGlossary = $this->getBookGlossary( $this->sourceBookUrl );
$this->maybeRestoreCurrentBlog();
return true;
} | [
"public",
"function",
"setupSource",
"(",
"$",
"respect_book_license",
"=",
"true",
")",
"{",
"if",
"(",
"!",
"empty",
"(",
"$",
"this",
"->",
"sourceBookId",
")",
")",
"{",
"// Local book",
"switch_to_blog",
"(",
"$",
"this",
"->",
"sourceBookId",
")",
";",
"}",
"elseif",
"(",
"!",
"$",
"this",
"->",
"isCompatible",
"(",
"$",
"this",
"->",
"sourceBookUrl",
")",
")",
"{",
"// Remote is not compatible, bail.",
"$",
"_SESSION",
"[",
"'pb_errors'",
"]",
"[",
"]",
"=",
"__",
"(",
"'You can only clone from a book hosted by Pressbooks 4.1 or later. Please ensure that your source book meets these requirements.'",
",",
"'pressbooks'",
")",
";",
"return",
"false",
";",
"}",
"// Set up $this->sourceBookMetadata",
"$",
"this",
"->",
"sourceBookMetadata",
"=",
"$",
"this",
"->",
"getBookMetadata",
"(",
"$",
"this",
"->",
"sourceBookUrl",
")",
";",
"if",
"(",
"empty",
"(",
"$",
"this",
"->",
"sourceBookMetadata",
")",
")",
"{",
"$",
"_SESSION",
"[",
"'pb_errors'",
"]",
"[",
"]",
"=",
"sprintf",
"(",
"__",
"(",
"'Could not retrieve metadata from %s.'",
",",
"'pressbooks'",
")",
",",
"sprintf",
"(",
"'<em>%s</em>'",
",",
"$",
"this",
"->",
"sourceBookUrl",
")",
")",
";",
"$",
"this",
"->",
"maybeRestoreCurrentBlog",
"(",
")",
";",
"return",
"false",
";",
"}",
"if",
"(",
"$",
"respect_book_license",
")",
"{",
"// Verify license or network administrator override",
"if",
"(",
"!",
"$",
"this",
"->",
"isSourceCloneable",
"(",
"$",
"this",
"->",
"sourceBookMetadata",
"[",
"'license'",
"]",
")",
")",
"{",
"$",
"_SESSION",
"[",
"'pb_errors'",
"]",
"[",
"]",
"=",
"sprintf",
"(",
"__",
"(",
"'%s is not licensed for cloning.'",
",",
"'pressbooks'",
")",
",",
"sprintf",
"(",
"'<em>%s</em>'",
",",
"$",
"this",
"->",
"sourceBookMetadata",
"[",
"'name'",
"]",
")",
")",
";",
"$",
"this",
"->",
"maybeRestoreCurrentBlog",
"(",
")",
";",
"return",
"false",
";",
"}",
"}",
"// Set up $this->sourceBookStructure",
"$",
"this",
"->",
"sourceBookStructure",
"=",
"$",
"this",
"->",
"getBookStructure",
"(",
"$",
"this",
"->",
"sourceBookUrl",
")",
";",
"if",
"(",
"empty",
"(",
"$",
"this",
"->",
"sourceBookStructure",
")",
")",
"{",
"$",
"_SESSION",
"[",
"'pb_errors'",
"]",
"[",
"]",
"=",
"sprintf",
"(",
"__",
"(",
"'Could not retrieve contents and structure from %s.'",
",",
"'pressbooks'",
")",
",",
"sprintf",
"(",
"'<em>%s</em>'",
",",
"$",
"this",
"->",
"sourceBookMetadata",
"[",
"'name'",
"]",
")",
")",
";",
"$",
"this",
"->",
"maybeRestoreCurrentBlog",
"(",
")",
";",
"return",
"false",
";",
"}",
"// Set up $this->sourceBookTerms",
"$",
"this",
"->",
"sourceBookTerms",
"=",
"$",
"this",
"->",
"getBookTerms",
"(",
"$",
"this",
"->",
"sourceBookUrl",
")",
";",
"if",
"(",
"empty",
"(",
"$",
"this",
"->",
"sourceBookTerms",
")",
")",
"{",
"$",
"_SESSION",
"[",
"'pb_errors'",
"]",
"[",
"]",
"=",
"sprintf",
"(",
"__",
"(",
"'Could not retrieve taxonomies from %s.'",
",",
"'pressbooks'",
")",
",",
"sprintf",
"(",
"'<em>%s</em>'",
",",
"$",
"this",
"->",
"sourceBookMetadata",
"[",
"'name'",
"]",
")",
")",
";",
"$",
"this",
"->",
"maybeRestoreCurrentBlog",
"(",
")",
";",
"return",
"false",
";",
"}",
"// Media",
"$",
"this",
"->",
"knownMedia",
"=",
"$",
"this",
"->",
"buildListOfKnownMedia",
"(",
"$",
"this",
"->",
"sourceBookUrl",
")",
";",
"if",
"(",
"$",
"this",
"->",
"knownMedia",
"===",
"false",
")",
"{",
"$",
"_SESSION",
"[",
"'pb_errors'",
"]",
"[",
"]",
"=",
"sprintf",
"(",
"__",
"(",
"'Could not retrieve media from %s.'",
",",
"'pressbooks'",
")",
",",
"sprintf",
"(",
"'<em>%s</em>'",
",",
"$",
"this",
"->",
"sourceBookMetadata",
"[",
"'name'",
"]",
")",
")",
";",
"$",
"this",
"->",
"maybeRestoreCurrentBlog",
"(",
")",
";",
"return",
"false",
";",
"}",
"// Sort by the length of sourceUrls for better, left to right, search and replace loops",
"$",
"known_media_sorted",
"=",
"$",
"this",
"->",
"knownMedia",
";",
"uasort",
"(",
"$",
"known_media_sorted",
",",
"function",
"(",
"$",
"a",
",",
"$",
"b",
")",
"{",
"return",
"strlen",
"(",
"$",
"b",
"->",
"sourceUrl",
")",
"<=>",
"strlen",
"(",
"$",
"a",
"->",
"sourceUrl",
")",
";",
"}",
")",
";",
"$",
"this",
"->",
"knownMedia",
"=",
"$",
"known_media_sorted",
";",
"// H5P",
"$",
"this",
"->",
"knownH5P",
"=",
"$",
"this",
"->",
"buildListOfKnownH5P",
"(",
"$",
"this",
"->",
"sourceBookUrl",
")",
";",
"if",
"(",
"$",
"this",
"->",
"knownH5P",
"===",
"false",
")",
"{",
"// No H5P endpoint was found",
"$",
"this",
"->",
"knownH5P",
"=",
"[",
"]",
";",
"}",
"else",
"{",
"$",
"this",
"->",
"sourceHasH5pApi",
"=",
"true",
";",
"}",
"// Set up $this->sourceBookGlossary",
"$",
"this",
"->",
"sourceBookGlossary",
"=",
"$",
"this",
"->",
"getBookGlossary",
"(",
"$",
"this",
"->",
"sourceBookUrl",
")",
";",
"$",
"this",
"->",
"maybeRestoreCurrentBlog",
"(",
")",
";",
"return",
"true",
";",
"}"
] | @since 5.0.0
@param bool $respect_book_license
@return bool | [
"@since",
"5",
".",
"0",
".",
"0"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/cloner/class-cloner.php#L495-L568 |
pressbooks/pressbooks | inc/cloner/class-cloner.php | Cloner.cloneTerm | public function cloneTerm( $term_id ) {
// Retrieve term
foreach ( $this->sourceBookTerms as $k => $v ) {
if ( $v['id'] === absint( $term_id ) ) {
$term = $this->sourceBookTerms[ $k ];
break;
}
};
if ( empty( $term['slug'] ) || empty( $term['taxonomy'] ) ) {
// Doing it wrong...
return false;
}
// Check for matching term
foreach ( $this->targetBookTerms as $k => $v ) {
if ( $v['slug'] === $term['slug'] && $v['taxonomy'] === $term['taxonomy'] ) {
return $v['id'];
}
};
// Set endpoint
$endpoint = $term['taxonomy'];
// _links key needs to be removed, pop it out into an ignored variable
$_links = array_pop( $term );
// Remove source-specific properties
$bad_keys = [ 'id', 'count', 'link', 'parent', 'taxonomy' ];
foreach ( $bad_keys as $bad_key ) {
unset( $term[ $bad_key ] );
}
// POST internal request
$request = new \WP_REST_Request( 'POST', "/pressbooks/v2/$endpoint" );
$request->set_body_params( $term );
$request->set_param( '_fields', 'id' );
$response = rest_do_request( $request )->get_data();
// Inform user of failure, bail
if ( is_wp_error( $response ) || @$response['data']['status'] >= 400 ) { // @codingStandardsIgnoreLine
return false;
} else {
return $response['id'];
}
} | php | public function cloneTerm( $term_id ) {
// Retrieve term
foreach ( $this->sourceBookTerms as $k => $v ) {
if ( $v['id'] === absint( $term_id ) ) {
$term = $this->sourceBookTerms[ $k ];
break;
}
};
if ( empty( $term['slug'] ) || empty( $term['taxonomy'] ) ) {
// Doing it wrong...
return false;
}
// Check for matching term
foreach ( $this->targetBookTerms as $k => $v ) {
if ( $v['slug'] === $term['slug'] && $v['taxonomy'] === $term['taxonomy'] ) {
return $v['id'];
}
};
// Set endpoint
$endpoint = $term['taxonomy'];
// _links key needs to be removed, pop it out into an ignored variable
$_links = array_pop( $term );
// Remove source-specific properties
$bad_keys = [ 'id', 'count', 'link', 'parent', 'taxonomy' ];
foreach ( $bad_keys as $bad_key ) {
unset( $term[ $bad_key ] );
}
// POST internal request
$request = new \WP_REST_Request( 'POST', "/pressbooks/v2/$endpoint" );
$request->set_body_params( $term );
$request->set_param( '_fields', 'id' );
$response = rest_do_request( $request )->get_data();
// Inform user of failure, bail
if ( is_wp_error( $response ) || @$response['data']['status'] >= 400 ) { // @codingStandardsIgnoreLine
return false;
} else {
return $response['id'];
}
} | [
"public",
"function",
"cloneTerm",
"(",
"$",
"term_id",
")",
"{",
"// Retrieve term",
"foreach",
"(",
"$",
"this",
"->",
"sourceBookTerms",
"as",
"$",
"k",
"=>",
"$",
"v",
")",
"{",
"if",
"(",
"$",
"v",
"[",
"'id'",
"]",
"===",
"absint",
"(",
"$",
"term_id",
")",
")",
"{",
"$",
"term",
"=",
"$",
"this",
"->",
"sourceBookTerms",
"[",
"$",
"k",
"]",
";",
"break",
";",
"}",
"}",
";",
"if",
"(",
"empty",
"(",
"$",
"term",
"[",
"'slug'",
"]",
")",
"||",
"empty",
"(",
"$",
"term",
"[",
"'taxonomy'",
"]",
")",
")",
"{",
"// Doing it wrong...",
"return",
"false",
";",
"}",
"// Check for matching term",
"foreach",
"(",
"$",
"this",
"->",
"targetBookTerms",
"as",
"$",
"k",
"=>",
"$",
"v",
")",
"{",
"if",
"(",
"$",
"v",
"[",
"'slug'",
"]",
"===",
"$",
"term",
"[",
"'slug'",
"]",
"&&",
"$",
"v",
"[",
"'taxonomy'",
"]",
"===",
"$",
"term",
"[",
"'taxonomy'",
"]",
")",
"{",
"return",
"$",
"v",
"[",
"'id'",
"]",
";",
"}",
"}",
";",
"// Set endpoint",
"$",
"endpoint",
"=",
"$",
"term",
"[",
"'taxonomy'",
"]",
";",
"// _links key needs to be removed, pop it out into an ignored variable",
"$",
"_links",
"=",
"array_pop",
"(",
"$",
"term",
")",
";",
"// Remove source-specific properties",
"$",
"bad_keys",
"=",
"[",
"'id'",
",",
"'count'",
",",
"'link'",
",",
"'parent'",
",",
"'taxonomy'",
"]",
";",
"foreach",
"(",
"$",
"bad_keys",
"as",
"$",
"bad_key",
")",
"{",
"unset",
"(",
"$",
"term",
"[",
"$",
"bad_key",
"]",
")",
";",
"}",
"// POST internal request",
"$",
"request",
"=",
"new",
"\\",
"WP_REST_Request",
"(",
"'POST'",
",",
"\"/pressbooks/v2/$endpoint\"",
")",
";",
"$",
"request",
"->",
"set_body_params",
"(",
"$",
"term",
")",
";",
"$",
"request",
"->",
"set_param",
"(",
"'_fields'",
",",
"'id'",
")",
";",
"$",
"response",
"=",
"rest_do_request",
"(",
"$",
"request",
")",
"->",
"get_data",
"(",
")",
";",
"// Inform user of failure, bail",
"if",
"(",
"is_wp_error",
"(",
"$",
"response",
")",
"||",
"@",
"$",
"response",
"[",
"'data'",
"]",
"[",
"'status'",
"]",
">=",
"400",
")",
"{",
"// @codingStandardsIgnoreLine",
"return",
"false",
";",
"}",
"else",
"{",
"return",
"$",
"response",
"[",
"'id'",
"]",
";",
"}",
"}"
] | Clone term from a source book to a target book.
@since 4.1.0
@param int $term_id The ID of the term within the source book.
@return bool | int False if creating a new term failed; the ID of the new term if it the clone succeeded or the ID of a matching term if it exists. | [
"Clone",
"term",
"from",
"a",
"source",
"book",
"to",
"a",
"target",
"book",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/cloner/class-cloner.php#L589-L634 |
pressbooks/pressbooks | inc/cloner/class-cloner.php | Cloner.clonePostProcess | public function clonePostProcess() {
$this->fixInternalShortcodes();
// H5P
if ( $this->sourceHasH5p === true && ( $this->sourceHasH5pApi === false || $this->targetHasH5pApi === false || $this->targetHasFetchedAllTheH5p === false ) ) {
// Add a notice to the user indicating that the H5P could not be cloned
\Pressbooks\add_notice( __( 'The source book contained H5P content that could not be cloned. Please review the cloned version of your book carefully, as missing H5P content will be indicated. You may want to remove or replace these elements.', 'pressbooks' ) );
}
} | php | public function clonePostProcess() {
$this->fixInternalShortcodes();
// H5P
if ( $this->sourceHasH5p === true && ( $this->sourceHasH5pApi === false || $this->targetHasH5pApi === false || $this->targetHasFetchedAllTheH5p === false ) ) {
// Add a notice to the user indicating that the H5P could not be cloned
\Pressbooks\add_notice( __( 'The source book contained H5P content that could not be cloned. Please review the cloned version of your book carefully, as missing H5P content will be indicated. You may want to remove or replace these elements.', 'pressbooks' ) );
}
} | [
"public",
"function",
"clonePostProcess",
"(",
")",
"{",
"$",
"this",
"->",
"fixInternalShortcodes",
"(",
")",
";",
"// H5P",
"if",
"(",
"$",
"this",
"->",
"sourceHasH5p",
"===",
"true",
"&&",
"(",
"$",
"this",
"->",
"sourceHasH5pApi",
"===",
"false",
"||",
"$",
"this",
"->",
"targetHasH5pApi",
"===",
"false",
"||",
"$",
"this",
"->",
"targetHasFetchedAllTheH5p",
"===",
"false",
")",
")",
"{",
"// Add a notice to the user indicating that the H5P could not be cloned",
"\\",
"Pressbooks",
"\\",
"add_notice",
"(",
"__",
"(",
"'The source book contained H5P content that could not be cloned. Please review the cloned version of your book carefully, as missing H5P content will be indicated. You may want to remove or replace these elements.'",
",",
"'pressbooks'",
")",
")",
";",
"}",
"}"
] | Post-processor | [
"Post",
"-",
"processor"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/cloner/class-cloner.php#L705-L712 |
pressbooks/pressbooks | inc/cloner/class-cloner.php | Cloner.buildListOfKnownMedia | public function buildListOfKnownMedia( $url ) {
// Handle request (local or global)
$params = [
'per_page' => 100,
];
$response = $this->handleGetRequest( $url, 'wp/v2', 'media', $params );
// Handle errors
if ( is_wp_error( $response ) ) {
$_SESSION['pb_errors'][] = sprintf(
'<p>%1$s</p><p>%2$s</p>',
__( 'The source book’s media could not be read.', 'pressbooks' ),
$response->get_error_message()
);
return false;
}
$known_media = [];
foreach ( $response as $item ) {
$m = $this->createMediaEntity( $item );
if ( $item['media_type'] === 'image' ) {
foreach ( $item['media_details']['sizes'] as $size => $info ) {
$attached_file = image_strip_baseurl( $info['source_url'] ); // 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( $url ) {
// Handle request (local or global)
$params = [
'per_page' => 100,
];
$response = $this->handleGetRequest( $url, 'wp/v2', 'media', $params );
// Handle errors
if ( is_wp_error( $response ) ) {
$_SESSION['pb_errors'][] = sprintf(
'<p>%1$s</p><p>%2$s</p>',
__( 'The source book’s media could not be read.', 'pressbooks' ),
$response->get_error_message()
);
return false;
}
$known_media = [];
foreach ( $response as $item ) {
$m = $this->createMediaEntity( $item );
if ( $item['media_type'] === 'image' ) {
foreach ( $item['media_details']['sizes'] as $size => $info ) {
$attached_file = image_strip_baseurl( $info['source_url'] ); // 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",
"(",
"$",
"url",
")",
"{",
"// Handle request (local or global)",
"$",
"params",
"=",
"[",
"'per_page'",
"=>",
"100",
",",
"]",
";",
"$",
"response",
"=",
"$",
"this",
"->",
"handleGetRequest",
"(",
"$",
"url",
",",
"'wp/v2'",
",",
"'media'",
",",
"$",
"params",
")",
";",
"// Handle errors",
"if",
"(",
"is_wp_error",
"(",
"$",
"response",
")",
")",
"{",
"$",
"_SESSION",
"[",
"'pb_errors'",
"]",
"[",
"]",
"=",
"sprintf",
"(",
"'<p>%1$s</p><p>%2$s</p>'",
",",
"__",
"(",
"'The source book’s media could not be read.'",
",",
"'pressbooks'",
")",
",",
"$",
"response",
"->",
"get_error_message",
"(",
")",
")",
";",
"return",
"false",
";",
"}",
"$",
"known_media",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"response",
"as",
"$",
"item",
")",
"{",
"$",
"m",
"=",
"$",
"this",
"->",
"createMediaEntity",
"(",
"$",
"item",
")",
";",
"if",
"(",
"$",
"item",
"[",
"'media_type'",
"]",
"===",
"'image'",
")",
"{",
"foreach",
"(",
"$",
"item",
"[",
"'media_details'",
"]",
"[",
"'sizes'",
"]",
"as",
"$",
"size",
"=>",
"$",
"info",
")",
"{",
"$",
"attached_file",
"=",
"image_strip_baseurl",
"(",
"$",
"info",
"[",
"'source_url'",
"]",
")",
";",
"// 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",
";",
"}"
] | Use media endpoint to build an array of known media
@param string $url The URL of the book.
@return bool|\Pressbooks\Entities\Cloner\Media[] False if the operation failed; known images assoc array if succeeded. | [
"Use",
"media",
"endpoint",
"to",
"build",
"an",
"array",
"of",
"known",
"media"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/cloner/class-cloner.php#L721-L753 |
pressbooks/pressbooks | inc/cloner/class-cloner.php | Cloner.buildListOfKnownH5P | public function buildListOfKnownH5P( $url ) {
$response = $this->handleGetRequest( $url, 'h5p/v1', 'all' );
if ( is_wp_error( $response ) || @$response['data']['status'] >= 400 ) { // @codingStandardsIgnoreLine
return false;
}
$known_h5p = [];
foreach ( $response as $item ) {
$known_h5p[] = $this->createH5PEntity( $item );
}
return $known_h5p;
} | php | public function buildListOfKnownH5P( $url ) {
$response = $this->handleGetRequest( $url, 'h5p/v1', 'all' );
if ( is_wp_error( $response ) || @$response['data']['status'] >= 400 ) { // @codingStandardsIgnoreLine
return false;
}
$known_h5p = [];
foreach ( $response as $item ) {
$known_h5p[] = $this->createH5PEntity( $item );
}
return $known_h5p;
} | [
"public",
"function",
"buildListOfKnownH5P",
"(",
"$",
"url",
")",
"{",
"$",
"response",
"=",
"$",
"this",
"->",
"handleGetRequest",
"(",
"$",
"url",
",",
"'h5p/v1'",
",",
"'all'",
")",
";",
"if",
"(",
"is_wp_error",
"(",
"$",
"response",
")",
"||",
"@",
"$",
"response",
"[",
"'data'",
"]",
"[",
"'status'",
"]",
">=",
"400",
")",
"{",
"// @codingStandardsIgnoreLine",
"return",
"false",
";",
"}",
"$",
"known_h5p",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"response",
"as",
"$",
"item",
")",
"{",
"$",
"known_h5p",
"[",
"]",
"=",
"$",
"this",
"->",
"createH5PEntity",
"(",
"$",
"item",
")",
";",
"}",
"return",
"$",
"known_h5p",
";",
"}"
] | @param $url
@return bool|\Pressbooks\Entities\Cloner\H5P[] False if the operation failed; known H5P array if succeeded. | [
"@param",
"$url"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/cloner/class-cloner.php#L760-L771 |
pressbooks/pressbooks | inc/cloner/class-cloner.php | Cloner.createTransition | public function createTransition( $type, $old_id, $new_id ) {
$transition = new \Pressbooks\Entities\Cloner\Transition();
$transition->type = $type;
$transition->oldId = $old_id;
$transition->newId = $new_id;
$this->transitions[] = $transition;
} | php | public function createTransition( $type, $old_id, $new_id ) {
$transition = new \Pressbooks\Entities\Cloner\Transition();
$transition->type = $type;
$transition->oldId = $old_id;
$transition->newId = $new_id;
$this->transitions[] = $transition;
} | [
"public",
"function",
"createTransition",
"(",
"$",
"type",
",",
"$",
"old_id",
",",
"$",
"new_id",
")",
"{",
"$",
"transition",
"=",
"new",
"\\",
"Pressbooks",
"\\",
"Entities",
"\\",
"Cloner",
"\\",
"Transition",
"(",
")",
";",
"$",
"transition",
"->",
"type",
"=",
"$",
"type",
";",
"$",
"transition",
"->",
"oldId",
"=",
"$",
"old_id",
";",
"$",
"transition",
"->",
"newId",
"=",
"$",
"new_id",
";",
"$",
"this",
"->",
"transitions",
"[",
"]",
"=",
"$",
"transition",
";",
"}"
] | When cloning from one book to another, the IDs change
Use this method to add a transition, that we can do something with later, if needed
@param string $type
@param int $old_id
@param int $new_id | [
"When",
"cloning",
"from",
"one",
"book",
"to",
"another",
"the",
"IDs",
"change",
"Use",
"this",
"method",
"to",
"add",
"a",
"transition",
"that",
"we",
"can",
"do",
"something",
"with",
"later",
"if",
"needed"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/cloner/class-cloner.php#L781-L787 |
pressbooks/pressbooks | inc/cloner/class-cloner.php | Cloner.getBookMetadata | public function getBookMetadata( $url ) {
// Handle request (local or global)
$response = $this->handleGetRequest( $url, 'pressbooks/v2', 'metadata' );
// Handle errors
if ( is_wp_error( $response ) ) {
$_SESSION['pb_errors'][] = sprintf(
'<p>%1$s</p><p>%2$s</p>',
__( 'The source book’s metadata could not be read.', 'pressbooks' ),
$response->get_error_message()
);
return false;
}
// Return successful response
return $response;
} | php | public function getBookMetadata( $url ) {
// Handle request (local or global)
$response = $this->handleGetRequest( $url, 'pressbooks/v2', 'metadata' );
// Handle errors
if ( is_wp_error( $response ) ) {
$_SESSION['pb_errors'][] = sprintf(
'<p>%1$s</p><p>%2$s</p>',
__( 'The source book’s metadata could not be read.', 'pressbooks' ),
$response->get_error_message()
);
return false;
}
// Return successful response
return $response;
} | [
"public",
"function",
"getBookMetadata",
"(",
"$",
"url",
")",
"{",
"// Handle request (local or global)",
"$",
"response",
"=",
"$",
"this",
"->",
"handleGetRequest",
"(",
"$",
"url",
",",
"'pressbooks/v2'",
",",
"'metadata'",
")",
";",
"// Handle errors",
"if",
"(",
"is_wp_error",
"(",
"$",
"response",
")",
")",
"{",
"$",
"_SESSION",
"[",
"'pb_errors'",
"]",
"[",
"]",
"=",
"sprintf",
"(",
"'<p>%1$s</p><p>%2$s</p>'",
",",
"__",
"(",
"'The source book’s metadata could not be read.'",
",",
"'pressbooks'",
")",
",",
"$",
"response",
"->",
"get_error_message",
"(",
")",
")",
";",
"return",
"false",
";",
"}",
"// Return successful response",
"return",
"$",
"response",
";",
"}"
] | Fetch an array containing the metadata of a book.
@since 4.1.0
@param string $url The URL of the book.
@return bool | array False if the operation failed; the metadata array if it succeeded. | [
"Fetch",
"an",
"array",
"containing",
"the",
"metadata",
"of",
"a",
"book",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/cloner/class-cloner.php#L798-L814 |
pressbooks/pressbooks | inc/cloner/class-cloner.php | Cloner.getBookTerms | public function getBookTerms( $url ) {
$terms = [];
foreach ( [ 'front-matter-type', 'chapter-type', 'back-matter-type', 'glossary-type' ] as $taxonomy ) {
// Handle request (local or global)
$response = $this->handleGetRequest(
$url, 'pressbooks/v2', "$taxonomy", [
'per_page' => 25,
]
);
if ( is_wp_error( $response ) ) {
continue;
}
// Remove links
unset( $response['_links'] );
// Process response
$terms = array_merge( $terms, $response );
}
if ( empty( $terms ) ) {
$_SESSION['pb_errors'][] = sprintf( '<p>%1$s</p>', __( 'The source book’s taxonomies could not be read.', 'pressbooks' ) );
}
return $terms;
} | php | public function getBookTerms( $url ) {
$terms = [];
foreach ( [ 'front-matter-type', 'chapter-type', 'back-matter-type', 'glossary-type' ] as $taxonomy ) {
// Handle request (local or global)
$response = $this->handleGetRequest(
$url, 'pressbooks/v2', "$taxonomy", [
'per_page' => 25,
]
);
if ( is_wp_error( $response ) ) {
continue;
}
// Remove links
unset( $response['_links'] );
// Process response
$terms = array_merge( $terms, $response );
}
if ( empty( $terms ) ) {
$_SESSION['pb_errors'][] = sprintf( '<p>%1$s</p>', __( 'The source book’s taxonomies could not be read.', 'pressbooks' ) );
}
return $terms;
} | [
"public",
"function",
"getBookTerms",
"(",
"$",
"url",
")",
"{",
"$",
"terms",
"=",
"[",
"]",
";",
"foreach",
"(",
"[",
"'front-matter-type'",
",",
"'chapter-type'",
",",
"'back-matter-type'",
",",
"'glossary-type'",
"]",
"as",
"$",
"taxonomy",
")",
"{",
"// Handle request (local or global)",
"$",
"response",
"=",
"$",
"this",
"->",
"handleGetRequest",
"(",
"$",
"url",
",",
"'pressbooks/v2'",
",",
"\"$taxonomy\"",
",",
"[",
"'per_page'",
"=>",
"25",
",",
"]",
")",
";",
"if",
"(",
"is_wp_error",
"(",
"$",
"response",
")",
")",
"{",
"continue",
";",
"}",
"// Remove links",
"unset",
"(",
"$",
"response",
"[",
"'_links'",
"]",
")",
";",
"// Process response",
"$",
"terms",
"=",
"array_merge",
"(",
"$",
"terms",
",",
"$",
"response",
")",
";",
"}",
"if",
"(",
"empty",
"(",
"$",
"terms",
")",
")",
"{",
"$",
"_SESSION",
"[",
"'pb_errors'",
"]",
"[",
"]",
"=",
"sprintf",
"(",
"'<p>%1$s</p>'",
",",
"__",
"(",
"'The source book’s taxonomies could not be read.'",
",",
"'pressbooks'",
")",
")",
";",
"}",
"return",
"$",
"terms",
";",
"}"
] | Fetch an array containing the terms of a book.
@since 4.1.0
@param string $url The URL of the book.
@return array | [
"Fetch",
"an",
"array",
"containing",
"the",
"terms",
"of",
"a",
"book",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/cloner/class-cloner.php#L856-L883 |
pressbooks/pressbooks | inc/cloner/class-cloner.php | Cloner.isSourceCloneable | public function isSourceCloneable( $metadata_license ) {
$restrictive_licenses = [
'https://creativecommons.org/licenses/by-nd/4.0/',
'https://creativecommons.org/licenses/by-nc-nd/4.0/',
'https://choosealicense.com/no-license/',
];
if ( is_array( $metadata_license ) ) {
$license_url = $metadata_license['url'];
} else { // Backwards compatibility.
$license_url = $metadata_license;
}
$license_url = trailingslashit( trim( $license_url ) );
if ( ! empty( $this->sourceBookId ) ) {
if ( $this->isSuperAdmin ) {
return true; // Network administrators can clone local books no matter how they're licensed
} elseif ( ! in_array( $license_url, $restrictive_licenses, true ) ) {
return true; // Anyone can clone local books that aren't restrictively licensed
} else {
return false;
}
} elseif ( in_array( $license_url, $restrictive_licenses, true ) ) {
return false; // No one can clone global books that are restrictively licensed
}
return true;
} | php | public function isSourceCloneable( $metadata_license ) {
$restrictive_licenses = [
'https://creativecommons.org/licenses/by-nd/4.0/',
'https://creativecommons.org/licenses/by-nc-nd/4.0/',
'https://choosealicense.com/no-license/',
];
if ( is_array( $metadata_license ) ) {
$license_url = $metadata_license['url'];
} else { // Backwards compatibility.
$license_url = $metadata_license;
}
$license_url = trailingslashit( trim( $license_url ) );
if ( ! empty( $this->sourceBookId ) ) {
if ( $this->isSuperAdmin ) {
return true; // Network administrators can clone local books no matter how they're licensed
} elseif ( ! in_array( $license_url, $restrictive_licenses, true ) ) {
return true; // Anyone can clone local books that aren't restrictively licensed
} else {
return false;
}
} elseif ( in_array( $license_url, $restrictive_licenses, true ) ) {
return false; // No one can clone global books that are restrictively licensed
}
return true;
} | [
"public",
"function",
"isSourceCloneable",
"(",
"$",
"metadata_license",
")",
"{",
"$",
"restrictive_licenses",
"=",
"[",
"'https://creativecommons.org/licenses/by-nd/4.0/'",
",",
"'https://creativecommons.org/licenses/by-nc-nd/4.0/'",
",",
"'https://choosealicense.com/no-license/'",
",",
"]",
";",
"if",
"(",
"is_array",
"(",
"$",
"metadata_license",
")",
")",
"{",
"$",
"license_url",
"=",
"$",
"metadata_license",
"[",
"'url'",
"]",
";",
"}",
"else",
"{",
"// Backwards compatibility.",
"$",
"license_url",
"=",
"$",
"metadata_license",
";",
"}",
"$",
"license_url",
"=",
"trailingslashit",
"(",
"trim",
"(",
"$",
"license_url",
")",
")",
";",
"if",
"(",
"!",
"empty",
"(",
"$",
"this",
"->",
"sourceBookId",
")",
")",
"{",
"if",
"(",
"$",
"this",
"->",
"isSuperAdmin",
")",
"{",
"return",
"true",
";",
"// Network administrators can clone local books no matter how they're licensed",
"}",
"elseif",
"(",
"!",
"in_array",
"(",
"$",
"license_url",
",",
"$",
"restrictive_licenses",
",",
"true",
")",
")",
"{",
"return",
"true",
";",
"// Anyone can clone local books that aren't restrictively licensed",
"}",
"else",
"{",
"return",
"false",
";",
"}",
"}",
"elseif",
"(",
"in_array",
"(",
"$",
"license_url",
",",
"$",
"restrictive_licenses",
",",
"true",
")",
")",
"{",
"return",
"false",
";",
"// No one can clone global books that are restrictively licensed",
"}",
"return",
"true",
";",
"}"
] | Is the source book cloneable?
@since 4.1.0
@param mixed $metadata_license
@return bool Whether or not the book is public and licensed for cloning (or true if the current user is a network administrator and the book is in the current network). | [
"Is",
"the",
"source",
"book",
"cloneable?"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/cloner/class-cloner.php#L917-L943 |
pressbooks/pressbooks | inc/cloner/class-cloner.php | Cloner.retrieveSectionMetadata | public function retrieveSectionMetadata( $section_id, $post_type ) {
if ( in_array( $post_type, [ 'front-matter', 'back-matter' ], true ) ) {
foreach ( $this->sourceBookStructure[ $post_type ] as $k => $v ) {
if ( $v['id'] === absint( $section_id ) ) {
return $v['metadata'];
}
}
} elseif ( $post_type === 'chapter' ) {
foreach ( $this->sourceBookStructure['parts'] as $key => $part ) {
foreach ( $part['chapters'] as $k => $v ) {
if ( $v['id'] === absint( $section_id ) ) {
return $v['metadata'];
}
}
}
} elseif ( $post_type === 'glossary ' ) {
foreach ( $this->sourceBookGlossary as $k => $v ) {
if ( $v['id'] === absint( $section_id ) ) {
return $v['metadata'];
}
}
}
return []; // Nothing was found
} | php | public function retrieveSectionMetadata( $section_id, $post_type ) {
if ( in_array( $post_type, [ 'front-matter', 'back-matter' ], true ) ) {
foreach ( $this->sourceBookStructure[ $post_type ] as $k => $v ) {
if ( $v['id'] === absint( $section_id ) ) {
return $v['metadata'];
}
}
} elseif ( $post_type === 'chapter' ) {
foreach ( $this->sourceBookStructure['parts'] as $key => $part ) {
foreach ( $part['chapters'] as $k => $v ) {
if ( $v['id'] === absint( $section_id ) ) {
return $v['metadata'];
}
}
}
} elseif ( $post_type === 'glossary ' ) {
foreach ( $this->sourceBookGlossary as $k => $v ) {
if ( $v['id'] === absint( $section_id ) ) {
return $v['metadata'];
}
}
}
return []; // Nothing was found
} | [
"public",
"function",
"retrieveSectionMetadata",
"(",
"$",
"section_id",
",",
"$",
"post_type",
")",
"{",
"if",
"(",
"in_array",
"(",
"$",
"post_type",
",",
"[",
"'front-matter'",
",",
"'back-matter'",
"]",
",",
"true",
")",
")",
"{",
"foreach",
"(",
"$",
"this",
"->",
"sourceBookStructure",
"[",
"$",
"post_type",
"]",
"as",
"$",
"k",
"=>",
"$",
"v",
")",
"{",
"if",
"(",
"$",
"v",
"[",
"'id'",
"]",
"===",
"absint",
"(",
"$",
"section_id",
")",
")",
"{",
"return",
"$",
"v",
"[",
"'metadata'",
"]",
";",
"}",
"}",
"}",
"elseif",
"(",
"$",
"post_type",
"===",
"'chapter'",
")",
"{",
"foreach",
"(",
"$",
"this",
"->",
"sourceBookStructure",
"[",
"'parts'",
"]",
"as",
"$",
"key",
"=>",
"$",
"part",
")",
"{",
"foreach",
"(",
"$",
"part",
"[",
"'chapters'",
"]",
"as",
"$",
"k",
"=>",
"$",
"v",
")",
"{",
"if",
"(",
"$",
"v",
"[",
"'id'",
"]",
"===",
"absint",
"(",
"$",
"section_id",
")",
")",
"{",
"return",
"$",
"v",
"[",
"'metadata'",
"]",
";",
"}",
"}",
"}",
"}",
"elseif",
"(",
"$",
"post_type",
"===",
"'glossary '",
")",
"{",
"foreach",
"(",
"$",
"this",
"->",
"sourceBookGlossary",
"as",
"$",
"k",
"=>",
"$",
"v",
")",
"{",
"if",
"(",
"$",
"v",
"[",
"'id'",
"]",
"===",
"absint",
"(",
"$",
"section_id",
")",
")",
"{",
"return",
"$",
"v",
"[",
"'metadata'",
"]",
";",
"}",
"}",
"}",
"return",
"[",
"]",
";",
"// Nothing was found",
"}"
] | Retrieve metadata
@since 5.0.0
@param int $section_id The ID of the section within the source book.
@param string $post_type The post type of the section.
@return array | [
"Retrieve",
"metadata"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/cloner/class-cloner.php#L955-L978 |
pressbooks/pressbooks | inc/cloner/class-cloner.php | Cloner.discoverWordPressApi | public function discoverWordPressApi( $url ) {
// Use redirection because our servers redirect when missing a trailing slash
$response = wp_safe_remote_head(
$url, [
'redirection' => 2,
]
);
if ( is_wp_error( $response ) ) {
return false;
}
$headers = wp_remote_retrieve_headers( $response );
if ( isset( $headers['link'] ) ) {
if ( ! is_array( $headers['link'] ) ) {
$headers['link'] = [ $headers['link'] ];
}
foreach ( $headers['link'] as $link ) {
// Parse: <http://example.com/wp-json/>; rel="https://api.w.org/">, <http://example.com/?rest_route=/>; rel="https://api.w.org/"
if ( strpos( $link, 'rel="https://api.w.org/"' ) !== false || strpos( $link, "rel='https://api.w.org/'" ) !== false ) {
preg_match( '#\<(.*?)\>.*?//api\.w\.org/#', $link, $matches );
if ( empty( $matches[1] ) ) {
return false;
}
// Remove REST base
if ( str_ends_with( $matches[1], "/{$this->restBase}/" ) ) {
$fixed_url = esc_url( str_lreplace( "/{$this->restBase}/", '', $matches[1] ) ); // Ends with slash
} elseif ( str_ends_with( $matches[1], "/{$this->restBase}" ) ) {
$fixed_url = esc_url( str_lreplace( "/{$this->restBase}", '', $matches[1] ) ); // Doesn't end with slash
} else {
$fixed_url = esc_url( $matches[1] ); // Could not find rest base, use as is
}
return untrailingslashit( $fixed_url );
}
}
}
return false;
} | php | public function discoverWordPressApi( $url ) {
// Use redirection because our servers redirect when missing a trailing slash
$response = wp_safe_remote_head(
$url, [
'redirection' => 2,
]
);
if ( is_wp_error( $response ) ) {
return false;
}
$headers = wp_remote_retrieve_headers( $response );
if ( isset( $headers['link'] ) ) {
if ( ! is_array( $headers['link'] ) ) {
$headers['link'] = [ $headers['link'] ];
}
foreach ( $headers['link'] as $link ) {
// Parse: <http://example.com/wp-json/>; rel="https://api.w.org/">, <http://example.com/?rest_route=/>; rel="https://api.w.org/"
if ( strpos( $link, 'rel="https://api.w.org/"' ) !== false || strpos( $link, "rel='https://api.w.org/'" ) !== false ) {
preg_match( '#\<(.*?)\>.*?//api\.w\.org/#', $link, $matches );
if ( empty( $matches[1] ) ) {
return false;
}
// Remove REST base
if ( str_ends_with( $matches[1], "/{$this->restBase}/" ) ) {
$fixed_url = esc_url( str_lreplace( "/{$this->restBase}/", '', $matches[1] ) ); // Ends with slash
} elseif ( str_ends_with( $matches[1], "/{$this->restBase}" ) ) {
$fixed_url = esc_url( str_lreplace( "/{$this->restBase}", '', $matches[1] ) ); // Doesn't end with slash
} else {
$fixed_url = esc_url( $matches[1] ); // Could not find rest base, use as is
}
return untrailingslashit( $fixed_url );
}
}
}
return false;
} | [
"public",
"function",
"discoverWordPressApi",
"(",
"$",
"url",
")",
"{",
"// Use redirection because our servers redirect when missing a trailing slash",
"$",
"response",
"=",
"wp_safe_remote_head",
"(",
"$",
"url",
",",
"[",
"'redirection'",
"=>",
"2",
",",
"]",
")",
";",
"if",
"(",
"is_wp_error",
"(",
"$",
"response",
")",
")",
"{",
"return",
"false",
";",
"}",
"$",
"headers",
"=",
"wp_remote_retrieve_headers",
"(",
"$",
"response",
")",
";",
"if",
"(",
"isset",
"(",
"$",
"headers",
"[",
"'link'",
"]",
")",
")",
"{",
"if",
"(",
"!",
"is_array",
"(",
"$",
"headers",
"[",
"'link'",
"]",
")",
")",
"{",
"$",
"headers",
"[",
"'link'",
"]",
"=",
"[",
"$",
"headers",
"[",
"'link'",
"]",
"]",
";",
"}",
"foreach",
"(",
"$",
"headers",
"[",
"'link'",
"]",
"as",
"$",
"link",
")",
"{",
"// Parse: <http://example.com/wp-json/>; rel=\"https://api.w.org/\">, <http://example.com/?rest_route=/>; rel=\"https://api.w.org/\"",
"if",
"(",
"strpos",
"(",
"$",
"link",
",",
"'rel=\"https://api.w.org/\"'",
")",
"!==",
"false",
"||",
"strpos",
"(",
"$",
"link",
",",
"\"rel='https://api.w.org/'\"",
")",
"!==",
"false",
")",
"{",
"preg_match",
"(",
"'#\\<(.*?)\\>.*?//api\\.w\\.org/#'",
",",
"$",
"link",
",",
"$",
"matches",
")",
";",
"if",
"(",
"empty",
"(",
"$",
"matches",
"[",
"1",
"]",
")",
")",
"{",
"return",
"false",
";",
"}",
"// Remove REST base",
"if",
"(",
"str_ends_with",
"(",
"$",
"matches",
"[",
"1",
"]",
",",
"\"/{$this->restBase}/\"",
")",
")",
"{",
"$",
"fixed_url",
"=",
"esc_url",
"(",
"str_lreplace",
"(",
"\"/{$this->restBase}/\"",
",",
"''",
",",
"$",
"matches",
"[",
"1",
"]",
")",
")",
";",
"// Ends with slash",
"}",
"elseif",
"(",
"str_ends_with",
"(",
"$",
"matches",
"[",
"1",
"]",
",",
"\"/{$this->restBase}\"",
")",
")",
"{",
"$",
"fixed_url",
"=",
"esc_url",
"(",
"str_lreplace",
"(",
"\"/{$this->restBase}\"",
",",
"''",
",",
"$",
"matches",
"[",
"1",
"]",
")",
")",
";",
"// Doesn't end with slash",
"}",
"else",
"{",
"$",
"fixed_url",
"=",
"esc_url",
"(",
"$",
"matches",
"[",
"1",
"]",
")",
";",
"// Could not find rest base, use as is",
"}",
"return",
"untrailingslashit",
"(",
"$",
"fixed_url",
")",
";",
"}",
"}",
"}",
"return",
"false",
";",
"}"
] | Discover WordPress API
@see https://developer.wordpress.org/rest-api/using-the-rest-api/discovery/
@param string $url
@return string|false Returns (corrected) URL on success, false on failure | [
"Discover",
"WordPress",
"API"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/cloner/class-cloner.php#L989-L1026 |
pressbooks/pressbooks | inc/cloner/class-cloner.php | Cloner.isCompatible | public function isCompatible( $url ) {
$new_url = $this->discoverWordPressApi( $url );
if ( $new_url ) {
$url = $new_url;
$this->sourceBookUrl = $new_url;
}
// Check for taxonomies introduced in Pressbooks 4.1
// We specifically check for 404 Not Found.
// If we get another kind of error it will be caught later because we want to know what went wrong.
$response = $this->handleGetRequest( $url, 'pressbooks/v2', 'chapter-type', [ 'per_page' => 1 ], false );
if ( is_wp_error( $response ) && in_array( (int) $response->get_error_code(), [ 404 ], true ) ) {
return false;
}
return true;
} | php | public function isCompatible( $url ) {
$new_url = $this->discoverWordPressApi( $url );
if ( $new_url ) {
$url = $new_url;
$this->sourceBookUrl = $new_url;
}
// Check for taxonomies introduced in Pressbooks 4.1
// We specifically check for 404 Not Found.
// If we get another kind of error it will be caught later because we want to know what went wrong.
$response = $this->handleGetRequest( $url, 'pressbooks/v2', 'chapter-type', [ 'per_page' => 1 ], false );
if ( is_wp_error( $response ) && in_array( (int) $response->get_error_code(), [ 404 ], true ) ) {
return false;
}
return true;
} | [
"public",
"function",
"isCompatible",
"(",
"$",
"url",
")",
"{",
"$",
"new_url",
"=",
"$",
"this",
"->",
"discoverWordPressApi",
"(",
"$",
"url",
")",
";",
"if",
"(",
"$",
"new_url",
")",
"{",
"$",
"url",
"=",
"$",
"new_url",
";",
"$",
"this",
"->",
"sourceBookUrl",
"=",
"$",
"new_url",
";",
"}",
"// Check for taxonomies introduced in Pressbooks 4.1",
"// We specifically check for 404 Not Found.",
"// If we get another kind of error it will be caught later because we want to know what went wrong.",
"$",
"response",
"=",
"$",
"this",
"->",
"handleGetRequest",
"(",
"$",
"url",
",",
"'pressbooks/v2'",
",",
"'chapter-type'",
",",
"[",
"'per_page'",
"=>",
"1",
"]",
",",
"false",
")",
";",
"if",
"(",
"is_wp_error",
"(",
"$",
"response",
")",
"&&",
"in_array",
"(",
"(",
"int",
")",
"$",
"response",
"->",
"get_error_code",
"(",
")",
",",
"[",
"404",
"]",
",",
"true",
")",
")",
"{",
"return",
"false",
";",
"}",
"return",
"true",
";",
"}"
] | @param string $url
@return bool | [
"@param",
"string",
"$url"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/cloner/class-cloner.php#L1033-L1050 |
pressbooks/pressbooks | inc/cloner/class-cloner.php | Cloner.getBookId | public function getBookId( $url ) {
return get_blog_id_from_url(
wp_parse_url( $url, PHP_URL_HOST ),
trailingslashit( wp_parse_url( $url, PHP_URL_PATH ) )
);
} | php | public function getBookId( $url ) {
return get_blog_id_from_url(
wp_parse_url( $url, PHP_URL_HOST ),
trailingslashit( wp_parse_url( $url, PHP_URL_PATH ) )
);
} | [
"public",
"function",
"getBookId",
"(",
"$",
"url",
")",
"{",
"return",
"get_blog_id_from_url",
"(",
"wp_parse_url",
"(",
"$",
"url",
",",
"PHP_URL_HOST",
")",
",",
"trailingslashit",
"(",
"wp_parse_url",
"(",
"$",
"url",
",",
"PHP_URL_PATH",
")",
")",
")",
";",
"}"
] | Get a book ID from its URL.
@since 4.1.0
@param string $url
@return int 0 of no blog was found, or the ID of the matched book. | [
"Get",
"a",
"book",
"ID",
"from",
"its",
"URL",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/cloner/class-cloner.php#L1061-L1066 |
pressbooks/pressbooks | inc/cloner/class-cloner.php | Cloner.getSubdomainOrSubDirectory | public function getSubdomainOrSubDirectory( $url ) {
$url = untrailingslashit( $url );
$host = wp_parse_url( $url, PHP_URL_HOST );
$path = wp_parse_url( $url, PHP_URL_PATH );
if ( $path ) {
return ltrim( $path, '/\\' );
} else {
$host = explode( '.', $host );
$subdomain = array_shift( $host );
return $subdomain;
}
} | php | public function getSubdomainOrSubDirectory( $url ) {
$url = untrailingslashit( $url );
$host = wp_parse_url( $url, PHP_URL_HOST );
$path = wp_parse_url( $url, PHP_URL_PATH );
if ( $path ) {
return ltrim( $path, '/\\' );
} else {
$host = explode( '.', $host );
$subdomain = array_shift( $host );
return $subdomain;
}
} | [
"public",
"function",
"getSubdomainOrSubDirectory",
"(",
"$",
"url",
")",
"{",
"$",
"url",
"=",
"untrailingslashit",
"(",
"$",
"url",
")",
";",
"$",
"host",
"=",
"wp_parse_url",
"(",
"$",
"url",
",",
"PHP_URL_HOST",
")",
";",
"$",
"path",
"=",
"wp_parse_url",
"(",
"$",
"url",
",",
"PHP_URL_PATH",
")",
";",
"if",
"(",
"$",
"path",
")",
"{",
"return",
"ltrim",
"(",
"$",
"path",
",",
"'/\\\\'",
")",
";",
"}",
"else",
"{",
"$",
"host",
"=",
"explode",
"(",
"'.'",
",",
"$",
"host",
")",
";",
"$",
"subdomain",
"=",
"array_shift",
"(",
"$",
"host",
")",
";",
"return",
"$",
"subdomain",
";",
"}",
"}"
] | Given a URL, get the subdomain or subdirectory (depending on the type of multisite install).
@since 4.1.0
@param string $url
@return string | [
"Given",
"a",
"URL",
"get",
"the",
"subdomain",
"or",
"subdirectory",
"(",
"depending",
"on",
"the",
"type",
"of",
"multisite",
"install",
")",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/cloner/class-cloner.php#L1077-L1088 |
pressbooks/pressbooks | inc/cloner/class-cloner.php | Cloner.createMediaEntity | protected function createMediaEntity( $item ) {
$m = new \Pressbooks\Entities\Cloner\Media();
if ( isset( $item['id'] ) ) {
$m->id = $item['id'];
}
if ( isset( $item['title'], $item['title']['raw'] ) ) {
$m->title = $item['title']['raw'];
}
if ( isset( $item['description'], $item['description']['raw'] ) ) {
$m->description = $item['description']['raw'];
}
if ( isset( $item['caption'], $item['caption']['raw'] ) ) {
$m->caption = $item['caption']['raw'];
}
if ( isset( $item['meta'] ) ) {
$m->meta = $item['meta'];
}
if ( isset( $item['alt_text'] ) ) {
$m->altText = $item['alt_text'];
}
if ( isset( $item['source_url'] ) ) {
$m->sourceUrl = $item['source_url'];
}
return $m;
} | php | protected function createMediaEntity( $item ) {
$m = new \Pressbooks\Entities\Cloner\Media();
if ( isset( $item['id'] ) ) {
$m->id = $item['id'];
}
if ( isset( $item['title'], $item['title']['raw'] ) ) {
$m->title = $item['title']['raw'];
}
if ( isset( $item['description'], $item['description']['raw'] ) ) {
$m->description = $item['description']['raw'];
}
if ( isset( $item['caption'], $item['caption']['raw'] ) ) {
$m->caption = $item['caption']['raw'];
}
if ( isset( $item['meta'] ) ) {
$m->meta = $item['meta'];
}
if ( isset( $item['alt_text'] ) ) {
$m->altText = $item['alt_text'];
}
if ( isset( $item['source_url'] ) ) {
$m->sourceUrl = $item['source_url'];
}
return $m;
} | [
"protected",
"function",
"createMediaEntity",
"(",
"$",
"item",
")",
"{",
"$",
"m",
"=",
"new",
"\\",
"Pressbooks",
"\\",
"Entities",
"\\",
"Cloner",
"\\",
"Media",
"(",
")",
";",
"if",
"(",
"isset",
"(",
"$",
"item",
"[",
"'id'",
"]",
")",
")",
"{",
"$",
"m",
"->",
"id",
"=",
"$",
"item",
"[",
"'id'",
"]",
";",
"}",
"if",
"(",
"isset",
"(",
"$",
"item",
"[",
"'title'",
"]",
",",
"$",
"item",
"[",
"'title'",
"]",
"[",
"'raw'",
"]",
")",
")",
"{",
"$",
"m",
"->",
"title",
"=",
"$",
"item",
"[",
"'title'",
"]",
"[",
"'raw'",
"]",
";",
"}",
"if",
"(",
"isset",
"(",
"$",
"item",
"[",
"'description'",
"]",
",",
"$",
"item",
"[",
"'description'",
"]",
"[",
"'raw'",
"]",
")",
")",
"{",
"$",
"m",
"->",
"description",
"=",
"$",
"item",
"[",
"'description'",
"]",
"[",
"'raw'",
"]",
";",
"}",
"if",
"(",
"isset",
"(",
"$",
"item",
"[",
"'caption'",
"]",
",",
"$",
"item",
"[",
"'caption'",
"]",
"[",
"'raw'",
"]",
")",
")",
"{",
"$",
"m",
"->",
"caption",
"=",
"$",
"item",
"[",
"'caption'",
"]",
"[",
"'raw'",
"]",
";",
"}",
"if",
"(",
"isset",
"(",
"$",
"item",
"[",
"'meta'",
"]",
")",
")",
"{",
"$",
"m",
"->",
"meta",
"=",
"$",
"item",
"[",
"'meta'",
"]",
";",
"}",
"if",
"(",
"isset",
"(",
"$",
"item",
"[",
"'alt_text'",
"]",
")",
")",
"{",
"$",
"m",
"->",
"altText",
"=",
"$",
"item",
"[",
"'alt_text'",
"]",
";",
"}",
"if",
"(",
"isset",
"(",
"$",
"item",
"[",
"'source_url'",
"]",
")",
")",
"{",
"$",
"m",
"->",
"sourceUrl",
"=",
"$",
"item",
"[",
"'source_url'",
"]",
";",
"}",
"return",
"$",
"m",
";",
"}"
] | @param array $item
@return \Pressbooks\Entities\Cloner\Media | [
"@param",
"array",
"$item"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/cloner/class-cloner.php#L1121-L1145 |
pressbooks/pressbooks | inc/cloner/class-cloner.php | Cloner.createH5PEntity | protected function createH5PEntity( $item ) {
$h5p = new \Pressbooks\Entities\Cloner\H5P();
if ( isset( $item['id'] ) ) {
$h5p->id = $item['id'];
}
if ( isset( $item['url'] ) ) {
$h5p->url = $item['url'];
}
return $h5p;
} | php | protected function createH5PEntity( $item ) {
$h5p = new \Pressbooks\Entities\Cloner\H5P();
if ( isset( $item['id'] ) ) {
$h5p->id = $item['id'];
}
if ( isset( $item['url'] ) ) {
$h5p->url = $item['url'];
}
return $h5p;
} | [
"protected",
"function",
"createH5PEntity",
"(",
"$",
"item",
")",
"{",
"$",
"h5p",
"=",
"new",
"\\",
"Pressbooks",
"\\",
"Entities",
"\\",
"Cloner",
"\\",
"H5P",
"(",
")",
";",
"if",
"(",
"isset",
"(",
"$",
"item",
"[",
"'id'",
"]",
")",
")",
"{",
"$",
"h5p",
"->",
"id",
"=",
"$",
"item",
"[",
"'id'",
"]",
";",
"}",
"if",
"(",
"isset",
"(",
"$",
"item",
"[",
"'url'",
"]",
")",
")",
"{",
"$",
"h5p",
"->",
"url",
"=",
"$",
"item",
"[",
"'url'",
"]",
";",
"}",
"return",
"$",
"h5p",
";",
"}"
] | @param array $item
@return \Pressbooks\Entities\Cloner\H5P | [
"@param",
"array",
"$item"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/cloner/class-cloner.php#L1152-L1161 |
pressbooks/pressbooks | inc/cloner/class-cloner.php | Cloner.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;
}
// H5P
if ( has_shortcode( $html, \Pressbooks\Interactive\H5P::SHORTCODE ) ) {
$this->postsWithH5PShortcodesToFix[] = $post_id;
$this->sourceHasH5p = true;
}
} | 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;
}
// H5P
if ( has_shortcode( $html, \Pressbooks\Interactive\H5P::SHORTCODE ) ) {
$this->postsWithH5PShortcodesToFix[] = $post_id;
$this->sourceHasH5p = true;
}
} | [
"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",
";",
"}",
"// H5P",
"if",
"(",
"has_shortcode",
"(",
"$",
"html",
",",
"\\",
"Pressbooks",
"\\",
"Interactive",
"\\",
"H5P",
"::",
"SHORTCODE",
")",
")",
"{",
"$",
"this",
"->",
"postsWithH5PShortcodesToFix",
"[",
"]",
"=",
"$",
"post_id",
";",
"$",
"this",
"->",
"sourceHasH5p",
"=",
"true",
";",
"}",
"}"
] | 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/cloner/class-cloner.php#L1169-L1183 |
pressbooks/pressbooks | inc/cloner/class-cloner.php | Cloner.createBook | protected function createBook() {
$host = wp_parse_url( network_home_url(), PHP_URL_HOST );
if ( is_subdomain_install() ) {
$domain = $this->getSubdomainOrSubDirectory( $this->targetBookUrl ) . '.' . $host;
$path = '/';
} else {
$domain = $host;
$path = '/' . $this->getSubdomainOrSubDirectory( $this->targetBookUrl );
}
if ( ! $this->targetBookTitle ) {
$this->targetBookTitle = $this->sourceBookMetadata['name'];
}
$user_id = get_current_user_id();
// Disable automatic redirect to new book dashboard
add_filter(
'pb_redirect_to_new_book', function () {
return false;
}
);
// Remove default content so that the book only contains the results of the clone operation
add_filter( 'pb_default_book_content', [ $this, 'removeDefaultBookContent' ] );
$result = wpmu_create_blog( $domain, $path, $this->targetBookTitle, $user_id );
remove_all_filters( 'pb_redirect_to_new_book' );
remove_filter( 'pb_default_book_content', [ $this, 'removeDefaultBookContent' ] );
if ( is_wp_error( $result ) ) {
return false;
}
return $result;
} | php | protected function createBook() {
$host = wp_parse_url( network_home_url(), PHP_URL_HOST );
if ( is_subdomain_install() ) {
$domain = $this->getSubdomainOrSubDirectory( $this->targetBookUrl ) . '.' . $host;
$path = '/';
} else {
$domain = $host;
$path = '/' . $this->getSubdomainOrSubDirectory( $this->targetBookUrl );
}
if ( ! $this->targetBookTitle ) {
$this->targetBookTitle = $this->sourceBookMetadata['name'];
}
$user_id = get_current_user_id();
// Disable automatic redirect to new book dashboard
add_filter(
'pb_redirect_to_new_book', function () {
return false;
}
);
// Remove default content so that the book only contains the results of the clone operation
add_filter( 'pb_default_book_content', [ $this, 'removeDefaultBookContent' ] );
$result = wpmu_create_blog( $domain, $path, $this->targetBookTitle, $user_id );
remove_all_filters( 'pb_redirect_to_new_book' );
remove_filter( 'pb_default_book_content', [ $this, 'removeDefaultBookContent' ] );
if ( is_wp_error( $result ) ) {
return false;
}
return $result;
} | [
"protected",
"function",
"createBook",
"(",
")",
"{",
"$",
"host",
"=",
"wp_parse_url",
"(",
"network_home_url",
"(",
")",
",",
"PHP_URL_HOST",
")",
";",
"if",
"(",
"is_subdomain_install",
"(",
")",
")",
"{",
"$",
"domain",
"=",
"$",
"this",
"->",
"getSubdomainOrSubDirectory",
"(",
"$",
"this",
"->",
"targetBookUrl",
")",
".",
"'.'",
".",
"$",
"host",
";",
"$",
"path",
"=",
"'/'",
";",
"}",
"else",
"{",
"$",
"domain",
"=",
"$",
"host",
";",
"$",
"path",
"=",
"'/'",
".",
"$",
"this",
"->",
"getSubdomainOrSubDirectory",
"(",
"$",
"this",
"->",
"targetBookUrl",
")",
";",
"}",
"if",
"(",
"!",
"$",
"this",
"->",
"targetBookTitle",
")",
"{",
"$",
"this",
"->",
"targetBookTitle",
"=",
"$",
"this",
"->",
"sourceBookMetadata",
"[",
"'name'",
"]",
";",
"}",
"$",
"user_id",
"=",
"get_current_user_id",
"(",
")",
";",
"// Disable automatic redirect to new book dashboard",
"add_filter",
"(",
"'pb_redirect_to_new_book'",
",",
"function",
"(",
")",
"{",
"return",
"false",
";",
"}",
")",
";",
"// Remove default content so that the book only contains the results of the clone operation",
"add_filter",
"(",
"'pb_default_book_content'",
",",
"[",
"$",
"this",
",",
"'removeDefaultBookContent'",
"]",
")",
";",
"$",
"result",
"=",
"wpmu_create_blog",
"(",
"$",
"domain",
",",
"$",
"path",
",",
"$",
"this",
"->",
"targetBookTitle",
",",
"$",
"user_id",
")",
";",
"remove_all_filters",
"(",
"'pb_redirect_to_new_book'",
")",
";",
"remove_filter",
"(",
"'pb_default_book_content'",
",",
"[",
"$",
"this",
",",
"'removeDefaultBookContent'",
"]",
")",
";",
"if",
"(",
"is_wp_error",
"(",
"$",
"result",
")",
")",
"{",
"return",
"false",
";",
"}",
"return",
"$",
"result",
";",
"}"
] | Create target book if it doesn't already exist.
@since 4.1.0
@return bool | int False if the creation failed; the ID of the new book if it succeeded. | [
"Create",
"target",
"book",
"if",
"it",
"doesn",
"t",
"already",
"exist",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/cloner/class-cloner.php#L1245-L1276 |
pressbooks/pressbooks | inc/cloner/class-cloner.php | Cloner.cloneMetadata | protected function cloneMetadata() {
$metadata_post_id = ( new \Pressbooks\Metadata )->getMetaPostId();
if ( ! $metadata_post_id ) {
return false;
}
$book_information = schema_to_book_information( $this->sourceBookMetadata );
// Cover image
if ( ! \Pressbooks\Image\is_default_cover( $book_information['pb_cover_image'] ) ) {
$new_cover_id = $this->downloads->fetchAndSaveUniqueImage( $book_information['pb_cover_image'] );
if ( $new_cover_id > 0 ) {
$this->clonedItems['media'][] = $new_cover_id; // Counter
$book_information['pb_cover_image'] = wp_get_attachment_url( $new_cover_id );
} else {
$book_information['pb_cover_image'] = default_cover_url();
}
} else {
$book_information['pb_cover_image'] = default_cover_url();
}
// Everything else
$book_information['pb_is_based_on'] = $this->sourceBookUrl;
$metadata_array_values = [ 'pb_keywords_tags', 'pb_bisac_subject', 'pb_additional_subjects' ];
foreach ( $book_information as $key => $value ) {
if ( $this->contributors->isValid( $key ) ) {
$values = oxford_comma_explode( $value );
foreach ( $values as $v ) {
$this->contributors->insert( $v, $metadata_post_id, $key );
}
} elseif ( in_array( $key, $metadata_array_values, true ) ) {
$values = explode( ', ', $value );
foreach ( $values as $v ) {
add_post_meta( $metadata_post_id, $key, $v );
}
} elseif ( $key === 'pb_title' ) {
update_post_meta( $metadata_post_id, $key, $this->targetBookTitle );
} else {
update_post_meta( $metadata_post_id, $key, $value );
if ( $key === 'pb_book_license' ) {
wp_set_object_terms( $metadata_post_id, $value, \Pressbooks\Licensing::TAXONOMY ); // Link
}
}
}
// Remove the current user from the author field in Book Info
$user_data = get_userdata( get_current_user_id() );
$this->contributors->unlink( $user_data->user_nicename, $metadata_post_id );
return $metadata_post_id;
} | php | protected function cloneMetadata() {
$metadata_post_id = ( new \Pressbooks\Metadata )->getMetaPostId();
if ( ! $metadata_post_id ) {
return false;
}
$book_information = schema_to_book_information( $this->sourceBookMetadata );
// Cover image
if ( ! \Pressbooks\Image\is_default_cover( $book_information['pb_cover_image'] ) ) {
$new_cover_id = $this->downloads->fetchAndSaveUniqueImage( $book_information['pb_cover_image'] );
if ( $new_cover_id > 0 ) {
$this->clonedItems['media'][] = $new_cover_id; // Counter
$book_information['pb_cover_image'] = wp_get_attachment_url( $new_cover_id );
} else {
$book_information['pb_cover_image'] = default_cover_url();
}
} else {
$book_information['pb_cover_image'] = default_cover_url();
}
// Everything else
$book_information['pb_is_based_on'] = $this->sourceBookUrl;
$metadata_array_values = [ 'pb_keywords_tags', 'pb_bisac_subject', 'pb_additional_subjects' ];
foreach ( $book_information as $key => $value ) {
if ( $this->contributors->isValid( $key ) ) {
$values = oxford_comma_explode( $value );
foreach ( $values as $v ) {
$this->contributors->insert( $v, $metadata_post_id, $key );
}
} elseif ( in_array( $key, $metadata_array_values, true ) ) {
$values = explode( ', ', $value );
foreach ( $values as $v ) {
add_post_meta( $metadata_post_id, $key, $v );
}
} elseif ( $key === 'pb_title' ) {
update_post_meta( $metadata_post_id, $key, $this->targetBookTitle );
} else {
update_post_meta( $metadata_post_id, $key, $value );
if ( $key === 'pb_book_license' ) {
wp_set_object_terms( $metadata_post_id, $value, \Pressbooks\Licensing::TAXONOMY ); // Link
}
}
}
// Remove the current user from the author field in Book Info
$user_data = get_userdata( get_current_user_id() );
$this->contributors->unlink( $user_data->user_nicename, $metadata_post_id );
return $metadata_post_id;
} | [
"protected",
"function",
"cloneMetadata",
"(",
")",
"{",
"$",
"metadata_post_id",
"=",
"(",
"new",
"\\",
"Pressbooks",
"\\",
"Metadata",
")",
"->",
"getMetaPostId",
"(",
")",
";",
"if",
"(",
"!",
"$",
"metadata_post_id",
")",
"{",
"return",
"false",
";",
"}",
"$",
"book_information",
"=",
"schema_to_book_information",
"(",
"$",
"this",
"->",
"sourceBookMetadata",
")",
";",
"// Cover image",
"if",
"(",
"!",
"\\",
"Pressbooks",
"\\",
"Image",
"\\",
"is_default_cover",
"(",
"$",
"book_information",
"[",
"'pb_cover_image'",
"]",
")",
")",
"{",
"$",
"new_cover_id",
"=",
"$",
"this",
"->",
"downloads",
"->",
"fetchAndSaveUniqueImage",
"(",
"$",
"book_information",
"[",
"'pb_cover_image'",
"]",
")",
";",
"if",
"(",
"$",
"new_cover_id",
">",
"0",
")",
"{",
"$",
"this",
"->",
"clonedItems",
"[",
"'media'",
"]",
"[",
"]",
"=",
"$",
"new_cover_id",
";",
"// Counter",
"$",
"book_information",
"[",
"'pb_cover_image'",
"]",
"=",
"wp_get_attachment_url",
"(",
"$",
"new_cover_id",
")",
";",
"}",
"else",
"{",
"$",
"book_information",
"[",
"'pb_cover_image'",
"]",
"=",
"default_cover_url",
"(",
")",
";",
"}",
"}",
"else",
"{",
"$",
"book_information",
"[",
"'pb_cover_image'",
"]",
"=",
"default_cover_url",
"(",
")",
";",
"}",
"// Everything else",
"$",
"book_information",
"[",
"'pb_is_based_on'",
"]",
"=",
"$",
"this",
"->",
"sourceBookUrl",
";",
"$",
"metadata_array_values",
"=",
"[",
"'pb_keywords_tags'",
",",
"'pb_bisac_subject'",
",",
"'pb_additional_subjects'",
"]",
";",
"foreach",
"(",
"$",
"book_information",
"as",
"$",
"key",
"=>",
"$",
"value",
")",
"{",
"if",
"(",
"$",
"this",
"->",
"contributors",
"->",
"isValid",
"(",
"$",
"key",
")",
")",
"{",
"$",
"values",
"=",
"oxford_comma_explode",
"(",
"$",
"value",
")",
";",
"foreach",
"(",
"$",
"values",
"as",
"$",
"v",
")",
"{",
"$",
"this",
"->",
"contributors",
"->",
"insert",
"(",
"$",
"v",
",",
"$",
"metadata_post_id",
",",
"$",
"key",
")",
";",
"}",
"}",
"elseif",
"(",
"in_array",
"(",
"$",
"key",
",",
"$",
"metadata_array_values",
",",
"true",
")",
")",
"{",
"$",
"values",
"=",
"explode",
"(",
"', '",
",",
"$",
"value",
")",
";",
"foreach",
"(",
"$",
"values",
"as",
"$",
"v",
")",
"{",
"add_post_meta",
"(",
"$",
"metadata_post_id",
",",
"$",
"key",
",",
"$",
"v",
")",
";",
"}",
"}",
"elseif",
"(",
"$",
"key",
"===",
"'pb_title'",
")",
"{",
"update_post_meta",
"(",
"$",
"metadata_post_id",
",",
"$",
"key",
",",
"$",
"this",
"->",
"targetBookTitle",
")",
";",
"}",
"else",
"{",
"update_post_meta",
"(",
"$",
"metadata_post_id",
",",
"$",
"key",
",",
"$",
"value",
")",
";",
"if",
"(",
"$",
"key",
"===",
"'pb_book_license'",
")",
"{",
"wp_set_object_terms",
"(",
"$",
"metadata_post_id",
",",
"$",
"value",
",",
"\\",
"Pressbooks",
"\\",
"Licensing",
"::",
"TAXONOMY",
")",
";",
"// Link",
"}",
"}",
"}",
"// Remove the current user from the author field in Book Info",
"$",
"user_data",
"=",
"get_userdata",
"(",
"get_current_user_id",
"(",
")",
")",
";",
"$",
"this",
"->",
"contributors",
"->",
"unlink",
"(",
"$",
"user_data",
"->",
"user_nicename",
",",
"$",
"metadata_post_id",
")",
";",
"return",
"$",
"metadata_post_id",
";",
"}"
] | Clone book information to the target book.
@since 4.1.0
@return bool | int False if the creation failed; the ID of the new book's book information post if it succeeded. | [
"Clone",
"book",
"information",
"to",
"the",
"target",
"book",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/cloner/class-cloner.php#L1285-L1336 |
pressbooks/pressbooks | inc/cloner/class-cloner.php | Cloner.cloneSection | protected function cloneSection( $section_id, $post_type, $parent_id = null ) {
// Is the section license OK?
// The global license is for the 'collection' and within that collection you have stuff with licenses that differ from the global one...
$metadata = $this->retrieveSectionMetadata( $section_id, $post_type );
$is_source_clonable = $this->isSourceCloneable( $metadata['license'] ?? $this->sourceBookMetadata['license'] );
if ( ! $is_source_clonable ) {
return false;
}
$section = $this->locateSection( $section_id, $post_type );
if ( $section === false ) {
return false;
}
// _links key needs to be removed, pop it out into an ignored variable
$_links = array_pop( $section );
// Get permalink
$permalink = $section['link'];
// Remove source-specific properties
$bad_keys = [ 'author', 'id', 'link' ];
foreach ( $bad_keys as $bad_key ) {
unset( $section[ $bad_key ] );
}
// Set status
$section['status'] = 'publish';
// Download media (images, videos, `select * from wp_posts where post_type = 'attachment'` ... )
list( $content, $attachments ) = $this->retrieveSectionContent( $section );
// Download H5P
$content = $this->retrieveH5P( $content );
// Set title and content
$section['title'] = $section['title']['rendered'];
$section['content'] = $content;
// Set part
if ( $post_type === 'chapter' ) {
$section['part'] = $parent_id;
}
// Set menu order
static $menu_order_guess = 1;
if ( ! isset( $section['menu_order'] ) ) {
$section['menu_order'] = $menu_order_guess;
$menu_order_guess++;
}
// Set mapped term ID
if ( isset( $section[ "$post_type-type" ] ) ) {
foreach ( $section[ "$post_type-type" ] as $key => $term_id ) {
if ( isset( $this->termMap[ $term_id ] ) ) {
// Use map
$section[ "$post_type-type" ][ $key ] = $this->termMap[ $term_id ];
} elseif ( empty( $this->targetBookUrl ) ) {
// Try to match an existing term
foreach ( $this->sourceBookTerms as $source_term ) {
if ( $source_term['id'] === $term_id ) {
$term = get_term_by( 'slug', $source_term['slug'], $source_term['taxonomy'] );
if ( $term ) {
$section[ "$post_type-type" ][ $key ] = $term->term_id;
}
break;
}
}
}
}
}
// Determine endpoint based on $post_type
$endpoint = ( in_array( $post_type, [ 'chapter', 'part' ], true ) ) ? $post_type . 's' : $post_type;
// Remove items handled by cloneSectionMetadata()
unset( $section['meta']['pb_authors'], $section['meta']['pb_section_license'] );
// POST internal request
$request = new \WP_REST_Request( 'POST', "/pressbooks/v2/$endpoint" );
$request->set_body_params( $section );
$request->set_param( '_fields', 'id' );
$response = rest_do_request( $request )->get_data();
// Inform user of failure, bail
if ( is_wp_error( $response ) || @$response['data']['status'] >= 400 ) { // @codingStandardsIgnoreLine
return false;
}
// Set pb_is_based_on property
update_post_meta( $response['id'], 'pb_is_based_on', $permalink );
// Clone associated content
if ( $post_type !== 'part' ) {
$this->cloneSectionMetadata( $section_id, $post_type, $response['id'] );
}
// Attach attachments to post
foreach ( $attachments as $attachment ) {
wp_update_post(
[
'ID' => $attachment,
'post_parent' => $response['id'],
]
);
}
// Shortcode hacker, no ease up tonight.
$this->checkInternalShortcodes( $response['id'], $section['content'] );
// Store a transitional state
$this->createTransition( $post_type, $section_id, $response['id'] );
return $response['id'];
} | php | protected function cloneSection( $section_id, $post_type, $parent_id = null ) {
// Is the section license OK?
// The global license is for the 'collection' and within that collection you have stuff with licenses that differ from the global one...
$metadata = $this->retrieveSectionMetadata( $section_id, $post_type );
$is_source_clonable = $this->isSourceCloneable( $metadata['license'] ?? $this->sourceBookMetadata['license'] );
if ( ! $is_source_clonable ) {
return false;
}
$section = $this->locateSection( $section_id, $post_type );
if ( $section === false ) {
return false;
}
// _links key needs to be removed, pop it out into an ignored variable
$_links = array_pop( $section );
// Get permalink
$permalink = $section['link'];
// Remove source-specific properties
$bad_keys = [ 'author', 'id', 'link' ];
foreach ( $bad_keys as $bad_key ) {
unset( $section[ $bad_key ] );
}
// Set status
$section['status'] = 'publish';
// Download media (images, videos, `select * from wp_posts where post_type = 'attachment'` ... )
list( $content, $attachments ) = $this->retrieveSectionContent( $section );
// Download H5P
$content = $this->retrieveH5P( $content );
// Set title and content
$section['title'] = $section['title']['rendered'];
$section['content'] = $content;
// Set part
if ( $post_type === 'chapter' ) {
$section['part'] = $parent_id;
}
// Set menu order
static $menu_order_guess = 1;
if ( ! isset( $section['menu_order'] ) ) {
$section['menu_order'] = $menu_order_guess;
$menu_order_guess++;
}
// Set mapped term ID
if ( isset( $section[ "$post_type-type" ] ) ) {
foreach ( $section[ "$post_type-type" ] as $key => $term_id ) {
if ( isset( $this->termMap[ $term_id ] ) ) {
// Use map
$section[ "$post_type-type" ][ $key ] = $this->termMap[ $term_id ];
} elseif ( empty( $this->targetBookUrl ) ) {
// Try to match an existing term
foreach ( $this->sourceBookTerms as $source_term ) {
if ( $source_term['id'] === $term_id ) {
$term = get_term_by( 'slug', $source_term['slug'], $source_term['taxonomy'] );
if ( $term ) {
$section[ "$post_type-type" ][ $key ] = $term->term_id;
}
break;
}
}
}
}
}
// Determine endpoint based on $post_type
$endpoint = ( in_array( $post_type, [ 'chapter', 'part' ], true ) ) ? $post_type . 's' : $post_type;
// Remove items handled by cloneSectionMetadata()
unset( $section['meta']['pb_authors'], $section['meta']['pb_section_license'] );
// POST internal request
$request = new \WP_REST_Request( 'POST', "/pressbooks/v2/$endpoint" );
$request->set_body_params( $section );
$request->set_param( '_fields', 'id' );
$response = rest_do_request( $request )->get_data();
// Inform user of failure, bail
if ( is_wp_error( $response ) || @$response['data']['status'] >= 400 ) { // @codingStandardsIgnoreLine
return false;
}
// Set pb_is_based_on property
update_post_meta( $response['id'], 'pb_is_based_on', $permalink );
// Clone associated content
if ( $post_type !== 'part' ) {
$this->cloneSectionMetadata( $section_id, $post_type, $response['id'] );
}
// Attach attachments to post
foreach ( $attachments as $attachment ) {
wp_update_post(
[
'ID' => $attachment,
'post_parent' => $response['id'],
]
);
}
// Shortcode hacker, no ease up tonight.
$this->checkInternalShortcodes( $response['id'], $section['content'] );
// Store a transitional state
$this->createTransition( $post_type, $section_id, $response['id'] );
return $response['id'];
} | [
"protected",
"function",
"cloneSection",
"(",
"$",
"section_id",
",",
"$",
"post_type",
",",
"$",
"parent_id",
"=",
"null",
")",
"{",
"// Is the section license OK?",
"// The global license is for the 'collection' and within that collection you have stuff with licenses that differ from the global one...",
"$",
"metadata",
"=",
"$",
"this",
"->",
"retrieveSectionMetadata",
"(",
"$",
"section_id",
",",
"$",
"post_type",
")",
";",
"$",
"is_source_clonable",
"=",
"$",
"this",
"->",
"isSourceCloneable",
"(",
"$",
"metadata",
"[",
"'license'",
"]",
"??",
"$",
"this",
"->",
"sourceBookMetadata",
"[",
"'license'",
"]",
")",
";",
"if",
"(",
"!",
"$",
"is_source_clonable",
")",
"{",
"return",
"false",
";",
"}",
"$",
"section",
"=",
"$",
"this",
"->",
"locateSection",
"(",
"$",
"section_id",
",",
"$",
"post_type",
")",
";",
"if",
"(",
"$",
"section",
"===",
"false",
")",
"{",
"return",
"false",
";",
"}",
"// _links key needs to be removed, pop it out into an ignored variable",
"$",
"_links",
"=",
"array_pop",
"(",
"$",
"section",
")",
";",
"// Get permalink",
"$",
"permalink",
"=",
"$",
"section",
"[",
"'link'",
"]",
";",
"// Remove source-specific properties",
"$",
"bad_keys",
"=",
"[",
"'author'",
",",
"'id'",
",",
"'link'",
"]",
";",
"foreach",
"(",
"$",
"bad_keys",
"as",
"$",
"bad_key",
")",
"{",
"unset",
"(",
"$",
"section",
"[",
"$",
"bad_key",
"]",
")",
";",
"}",
"// Set status",
"$",
"section",
"[",
"'status'",
"]",
"=",
"'publish'",
";",
"// Download media (images, videos, `select * from wp_posts where post_type = 'attachment'` ... )",
"list",
"(",
"$",
"content",
",",
"$",
"attachments",
")",
"=",
"$",
"this",
"->",
"retrieveSectionContent",
"(",
"$",
"section",
")",
";",
"// Download H5P",
"$",
"content",
"=",
"$",
"this",
"->",
"retrieveH5P",
"(",
"$",
"content",
")",
";",
"// Set title and content",
"$",
"section",
"[",
"'title'",
"]",
"=",
"$",
"section",
"[",
"'title'",
"]",
"[",
"'rendered'",
"]",
";",
"$",
"section",
"[",
"'content'",
"]",
"=",
"$",
"content",
";",
"// Set part",
"if",
"(",
"$",
"post_type",
"===",
"'chapter'",
")",
"{",
"$",
"section",
"[",
"'part'",
"]",
"=",
"$",
"parent_id",
";",
"}",
"// Set menu order",
"static",
"$",
"menu_order_guess",
"=",
"1",
";",
"if",
"(",
"!",
"isset",
"(",
"$",
"section",
"[",
"'menu_order'",
"]",
")",
")",
"{",
"$",
"section",
"[",
"'menu_order'",
"]",
"=",
"$",
"menu_order_guess",
";",
"$",
"menu_order_guess",
"++",
";",
"}",
"// Set mapped term ID",
"if",
"(",
"isset",
"(",
"$",
"section",
"[",
"\"$post_type-type\"",
"]",
")",
")",
"{",
"foreach",
"(",
"$",
"section",
"[",
"\"$post_type-type\"",
"]",
"as",
"$",
"key",
"=>",
"$",
"term_id",
")",
"{",
"if",
"(",
"isset",
"(",
"$",
"this",
"->",
"termMap",
"[",
"$",
"term_id",
"]",
")",
")",
"{",
"// Use map",
"$",
"section",
"[",
"\"$post_type-type\"",
"]",
"[",
"$",
"key",
"]",
"=",
"$",
"this",
"->",
"termMap",
"[",
"$",
"term_id",
"]",
";",
"}",
"elseif",
"(",
"empty",
"(",
"$",
"this",
"->",
"targetBookUrl",
")",
")",
"{",
"// Try to match an existing term",
"foreach",
"(",
"$",
"this",
"->",
"sourceBookTerms",
"as",
"$",
"source_term",
")",
"{",
"if",
"(",
"$",
"source_term",
"[",
"'id'",
"]",
"===",
"$",
"term_id",
")",
"{",
"$",
"term",
"=",
"get_term_by",
"(",
"'slug'",
",",
"$",
"source_term",
"[",
"'slug'",
"]",
",",
"$",
"source_term",
"[",
"'taxonomy'",
"]",
")",
";",
"if",
"(",
"$",
"term",
")",
"{",
"$",
"section",
"[",
"\"$post_type-type\"",
"]",
"[",
"$",
"key",
"]",
"=",
"$",
"term",
"->",
"term_id",
";",
"}",
"break",
";",
"}",
"}",
"}",
"}",
"}",
"// Determine endpoint based on $post_type",
"$",
"endpoint",
"=",
"(",
"in_array",
"(",
"$",
"post_type",
",",
"[",
"'chapter'",
",",
"'part'",
"]",
",",
"true",
")",
")",
"?",
"$",
"post_type",
".",
"'s'",
":",
"$",
"post_type",
";",
"// Remove items handled by cloneSectionMetadata()",
"unset",
"(",
"$",
"section",
"[",
"'meta'",
"]",
"[",
"'pb_authors'",
"]",
",",
"$",
"section",
"[",
"'meta'",
"]",
"[",
"'pb_section_license'",
"]",
")",
";",
"// POST internal request",
"$",
"request",
"=",
"new",
"\\",
"WP_REST_Request",
"(",
"'POST'",
",",
"\"/pressbooks/v2/$endpoint\"",
")",
";",
"$",
"request",
"->",
"set_body_params",
"(",
"$",
"section",
")",
";",
"$",
"request",
"->",
"set_param",
"(",
"'_fields'",
",",
"'id'",
")",
";",
"$",
"response",
"=",
"rest_do_request",
"(",
"$",
"request",
")",
"->",
"get_data",
"(",
")",
";",
"// Inform user of failure, bail",
"if",
"(",
"is_wp_error",
"(",
"$",
"response",
")",
"||",
"@",
"$",
"response",
"[",
"'data'",
"]",
"[",
"'status'",
"]",
">=",
"400",
")",
"{",
"// @codingStandardsIgnoreLine",
"return",
"false",
";",
"}",
"// Set pb_is_based_on property",
"update_post_meta",
"(",
"$",
"response",
"[",
"'id'",
"]",
",",
"'pb_is_based_on'",
",",
"$",
"permalink",
")",
";",
"// Clone associated content",
"if",
"(",
"$",
"post_type",
"!==",
"'part'",
")",
"{",
"$",
"this",
"->",
"cloneSectionMetadata",
"(",
"$",
"section_id",
",",
"$",
"post_type",
",",
"$",
"response",
"[",
"'id'",
"]",
")",
";",
"}",
"// Attach attachments to post",
"foreach",
"(",
"$",
"attachments",
"as",
"$",
"attachment",
")",
"{",
"wp_update_post",
"(",
"[",
"'ID'",
"=>",
"$",
"attachment",
",",
"'post_parent'",
"=>",
"$",
"response",
"[",
"'id'",
"]",
",",
"]",
")",
";",
"}",
"// Shortcode hacker, no ease up tonight.",
"$",
"this",
"->",
"checkInternalShortcodes",
"(",
"$",
"response",
"[",
"'id'",
"]",
",",
"$",
"section",
"[",
"'content'",
"]",
")",
";",
"// Store a transitional state",
"$",
"this",
"->",
"createTransition",
"(",
"$",
"post_type",
",",
"$",
"section_id",
",",
"$",
"response",
"[",
"'id'",
"]",
")",
";",
"return",
"$",
"response",
"[",
"'id'",
"]",
";",
"}"
] | Clone a section (front matter, part, chapter, back matter, glossary) of a source book to a target book.
@since 4.1.0
@param int $section_id The ID of the section within the source book.
@param string $post_type The post type of the section.
@param int $parent_id The ID of the part to which the chapter should be added (only required for chapters) within the target book.
@return bool | int False if the clone failed; the ID of the new section if it succeeded. | [
"Clone",
"a",
"section",
"(",
"front",
"matter",
"part",
"chapter",
"back",
"matter",
"glossary",
")",
"of",
"a",
"source",
"book",
"to",
"a",
"target",
"book",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/cloner/class-cloner.php#L1349-L1463 |
pressbooks/pressbooks | inc/cloner/class-cloner.php | Cloner.locateSection | protected function locateSection( $section_id, $post_type ) {
if ( $post_type === 'glossary' ) {
foreach ( $this->sourceBookGlossary as $k => $v ) {
if ( $v['id'] === absint( $section_id ) ) {
return $this->sourceBookGlossary[ $k ];
}
}
} else {
foreach ( $this->sourceBookStructure['_embedded'][ $post_type ] as $k => $v ) {
if ( $v['id'] === absint( $section_id ) ) {
return $this->sourceBookStructure['_embedded'][ $post_type ][ $k ];
}
};
}
return false;
} | php | protected function locateSection( $section_id, $post_type ) {
if ( $post_type === 'glossary' ) {
foreach ( $this->sourceBookGlossary as $k => $v ) {
if ( $v['id'] === absint( $section_id ) ) {
return $this->sourceBookGlossary[ $k ];
}
}
} else {
foreach ( $this->sourceBookStructure['_embedded'][ $post_type ] as $k => $v ) {
if ( $v['id'] === absint( $section_id ) ) {
return $this->sourceBookStructure['_embedded'][ $post_type ][ $k ];
}
};
}
return false;
} | [
"protected",
"function",
"locateSection",
"(",
"$",
"section_id",
",",
"$",
"post_type",
")",
"{",
"if",
"(",
"$",
"post_type",
"===",
"'glossary'",
")",
"{",
"foreach",
"(",
"$",
"this",
"->",
"sourceBookGlossary",
"as",
"$",
"k",
"=>",
"$",
"v",
")",
"{",
"if",
"(",
"$",
"v",
"[",
"'id'",
"]",
"===",
"absint",
"(",
"$",
"section_id",
")",
")",
"{",
"return",
"$",
"this",
"->",
"sourceBookGlossary",
"[",
"$",
"k",
"]",
";",
"}",
"}",
"}",
"else",
"{",
"foreach",
"(",
"$",
"this",
"->",
"sourceBookStructure",
"[",
"'_embedded'",
"]",
"[",
"$",
"post_type",
"]",
"as",
"$",
"k",
"=>",
"$",
"v",
")",
"{",
"if",
"(",
"$",
"v",
"[",
"'id'",
"]",
"===",
"absint",
"(",
"$",
"section_id",
")",
")",
"{",
"return",
"$",
"this",
"->",
"sourceBookStructure",
"[",
"'_embedded'",
"]",
"[",
"$",
"post_type",
"]",
"[",
"$",
"k",
"]",
";",
"}",
"}",
";",
"}",
"return",
"false",
";",
"}"
] | @param int $section_id
@param string $post_type
@return mixed | [
"@param",
"int",
"$section_id",
"@param",
"string",
"$post_type"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/cloner/class-cloner.php#L1471-L1486 |
pressbooks/pressbooks | inc/cloner/class-cloner.php | Cloner.retrieveSectionContent | protected function retrieveSectionContent( $section ) {
if ( array_key_exists( 'raw', $section['content'] ) ) {
$source_content = $section['content']['raw'];
} else {
$source_content = $section['content']['rendered'];
}
// According to the html5 spec section 8.3: https://www.w3.org/TR/2013/CR-html5-20130806/syntax.html#serializing-html-fragments
// We should replace any occurrences of the U+00A0 NO-BREAK SPACE character (aka "\xc2\xa0") by the string " " when serializing HTML5
// When cloning, we don't want to modify whitespaces, so we hide them from the parser.
$characters_to_keep = [ "\xc2\xa0" ];
foreach ( $characters_to_keep as $c ) {
$md5 = md5( $c );
$source_content = str_replace( $c, "<!-- pb_fixme_{$md5} -->", $source_content );
}
// Load source content
$html5 = new \Pressbooks\HtmlParser();
$dom = $html5->loadHTML( $source_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'] );
// Fix internal links
$dom = $this->fixInternalLinks( $dom );
// Save the destination content
$content = $html5->saveHTML( $dom );
// Put back the hidden characters
foreach ( $characters_to_keep as $c ) {
$md5 = md5( $c );
$content = str_replace( "<!-- pb_fixme_{$md5} -->", $c, $content );
}
// Count attachments
foreach ( $attachments as $pid ) {
$this->clonedItems['media'][] = $pid;
}
return [ trim( $content ), $attachments ];
} | php | protected function retrieveSectionContent( $section ) {
if ( array_key_exists( 'raw', $section['content'] ) ) {
$source_content = $section['content']['raw'];
} else {
$source_content = $section['content']['rendered'];
}
// According to the html5 spec section 8.3: https://www.w3.org/TR/2013/CR-html5-20130806/syntax.html#serializing-html-fragments
// We should replace any occurrences of the U+00A0 NO-BREAK SPACE character (aka "\xc2\xa0") by the string " " when serializing HTML5
// When cloning, we don't want to modify whitespaces, so we hide them from the parser.
$characters_to_keep = [ "\xc2\xa0" ];
foreach ( $characters_to_keep as $c ) {
$md5 = md5( $c );
$source_content = str_replace( $c, "<!-- pb_fixme_{$md5} -->", $source_content );
}
// Load source content
$html5 = new \Pressbooks\HtmlParser();
$dom = $html5->loadHTML( $source_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'] );
// Fix internal links
$dom = $this->fixInternalLinks( $dom );
// Save the destination content
$content = $html5->saveHTML( $dom );
// Put back the hidden characters
foreach ( $characters_to_keep as $c ) {
$md5 = md5( $c );
$content = str_replace( "<!-- pb_fixme_{$md5} -->", $c, $content );
}
// Count attachments
foreach ( $attachments as $pid ) {
$this->clonedItems['media'][] = $pid;
}
return [ trim( $content ), $attachments ];
} | [
"protected",
"function",
"retrieveSectionContent",
"(",
"$",
"section",
")",
"{",
"if",
"(",
"array_key_exists",
"(",
"'raw'",
",",
"$",
"section",
"[",
"'content'",
"]",
")",
")",
"{",
"$",
"source_content",
"=",
"$",
"section",
"[",
"'content'",
"]",
"[",
"'raw'",
"]",
";",
"}",
"else",
"{",
"$",
"source_content",
"=",
"$",
"section",
"[",
"'content'",
"]",
"[",
"'rendered'",
"]",
";",
"}",
"// According to the html5 spec section 8.3: https://www.w3.org/TR/2013/CR-html5-20130806/syntax.html#serializing-html-fragments",
"// We should replace any occurrences of the U+00A0 NO-BREAK SPACE character (aka \"\\xc2\\xa0\") by the string \" \" when serializing HTML5",
"// When cloning, we don't want to modify whitespaces, so we hide them from the parser.",
"$",
"characters_to_keep",
"=",
"[",
"\"\\xc2\\xa0\"",
"]",
";",
"foreach",
"(",
"$",
"characters_to_keep",
"as",
"$",
"c",
")",
"{",
"$",
"md5",
"=",
"md5",
"(",
"$",
"c",
")",
";",
"$",
"source_content",
"=",
"str_replace",
"(",
"$",
"c",
",",
"\"<!-- pb_fixme_{$md5} -->\"",
",",
"$",
"source_content",
")",
";",
"}",
"// Load source content",
"$",
"html5",
"=",
"new",
"\\",
"Pressbooks",
"\\",
"HtmlParser",
"(",
")",
";",
"$",
"dom",
"=",
"$",
"html5",
"->",
"loadHTML",
"(",
"$",
"source_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'",
"]",
")",
";",
"// Fix internal links",
"$",
"dom",
"=",
"$",
"this",
"->",
"fixInternalLinks",
"(",
"$",
"dom",
")",
";",
"// Save the destination content",
"$",
"content",
"=",
"$",
"html5",
"->",
"saveHTML",
"(",
"$",
"dom",
")",
";",
"// Put back the hidden characters",
"foreach",
"(",
"$",
"characters_to_keep",
"as",
"$",
"c",
")",
"{",
"$",
"md5",
"=",
"md5",
"(",
"$",
"c",
")",
";",
"$",
"content",
"=",
"str_replace",
"(",
"\"<!-- pb_fixme_{$md5} -->\"",
",",
"$",
"c",
",",
"$",
"content",
")",
";",
"}",
"// Count attachments",
"foreach",
"(",
"$",
"attachments",
"as",
"$",
"pid",
")",
"{",
"$",
"this",
"->",
"clonedItems",
"[",
"'media'",
"]",
"[",
"]",
"=",
"$",
"pid",
";",
"}",
"return",
"[",
"trim",
"(",
"$",
"content",
")",
",",
"$",
"attachments",
"]",
";",
"}"
] | Download media found in a section's `post_content` node, change the href links to point to newly downloaded media, etc
@param array $section
@return array{content: string, attachments: array} | [
"Download",
"media",
"found",
"in",
"a",
"section",
"s",
"post_content",
"node",
"change",
"the",
"href",
"links",
"to",
"point",
"to",
"newly",
"downloaded",
"media",
"etc"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/cloner/class-cloner.php#L1495-L1543 |
pressbooks/pressbooks | inc/cloner/class-cloner.php | Cloner.retrieveH5P | protected function retrieveH5P( $content ) {
if ( $this->sourceHasH5pApi && $this->targetHasH5pApi ) {
// Download H5P
$h5p_ids = $this->downloads->h5p( $content );
foreach ( $h5p_ids as $h5p_id ) {
if ( str_starts_with( $h5p_id, '#fixme' ) ) {
// Flag problem, remove broken H5P shortcode
$this->targetHasFetchedAllTheH5p = false;
$h5p_id = str_remove_prefix( $h5p_id, '#fixme' );
$content = $this->h5p->replaceUncloneable( $content, $h5p_id );
} else {
$this->clonedItems['h5p'][] = $h5p_id;
}
}
} else {
// Remove all H5P shortcodes
$content = $this->h5p->replaceUncloneable( $content );
}
return $content;
} | php | protected function retrieveH5P( $content ) {
if ( $this->sourceHasH5pApi && $this->targetHasH5pApi ) {
// Download H5P
$h5p_ids = $this->downloads->h5p( $content );
foreach ( $h5p_ids as $h5p_id ) {
if ( str_starts_with( $h5p_id, '#fixme' ) ) {
// Flag problem, remove broken H5P shortcode
$this->targetHasFetchedAllTheH5p = false;
$h5p_id = str_remove_prefix( $h5p_id, '#fixme' );
$content = $this->h5p->replaceUncloneable( $content, $h5p_id );
} else {
$this->clonedItems['h5p'][] = $h5p_id;
}
}
} else {
// Remove all H5P shortcodes
$content = $this->h5p->replaceUncloneable( $content );
}
return $content;
} | [
"protected",
"function",
"retrieveH5P",
"(",
"$",
"content",
")",
"{",
"if",
"(",
"$",
"this",
"->",
"sourceHasH5pApi",
"&&",
"$",
"this",
"->",
"targetHasH5pApi",
")",
"{",
"// Download H5P",
"$",
"h5p_ids",
"=",
"$",
"this",
"->",
"downloads",
"->",
"h5p",
"(",
"$",
"content",
")",
";",
"foreach",
"(",
"$",
"h5p_ids",
"as",
"$",
"h5p_id",
")",
"{",
"if",
"(",
"str_starts_with",
"(",
"$",
"h5p_id",
",",
"'#fixme'",
")",
")",
"{",
"// Flag problem, remove broken H5P shortcode",
"$",
"this",
"->",
"targetHasFetchedAllTheH5p",
"=",
"false",
";",
"$",
"h5p_id",
"=",
"str_remove_prefix",
"(",
"$",
"h5p_id",
",",
"'#fixme'",
")",
";",
"$",
"content",
"=",
"$",
"this",
"->",
"h5p",
"->",
"replaceUncloneable",
"(",
"$",
"content",
",",
"$",
"h5p_id",
")",
";",
"}",
"else",
"{",
"$",
"this",
"->",
"clonedItems",
"[",
"'h5p'",
"]",
"[",
"]",
"=",
"$",
"h5p_id",
";",
"}",
"}",
"}",
"else",
"{",
"// Remove all H5P shortcodes",
"$",
"content",
"=",
"$",
"this",
"->",
"h5p",
"->",
"replaceUncloneable",
"(",
"$",
"content",
")",
";",
"}",
"return",
"$",
"content",
";",
"}"
] | @param string $content
@return string | [
"@param",
"string",
"$content"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/cloner/class-cloner.php#L1550-L1569 |
pressbooks/pressbooks | inc/cloner/class-cloner.php | Cloner.cloneSectionMetadata | protected function cloneSectionMetadata( $section_id, $post_type, $target_id ) {
$book_schema = $this->sourceBookMetadata;
if ( empty( $this->targetBookUrl ) ) {
// If there's no target then that means this data is going into the current book.
// Remove invalid $book_schema values so that $section_schema is used instead.
$book_schema['author'] = [];
$book_schema['license'] = '';
}
$section_information = schema_to_section_information(
$this->retrieveSectionMetadata( $section_id, $post_type ),
$book_schema
);
foreach ( $section_information as $key => $value ) {
if ( $this->contributors->isValid( $key ) ) {
$values = oxford_comma_explode( $value );
foreach ( $values as $v ) {
$this->contributors->insert( $v, $target_id, $key );
}
} else {
update_post_meta( $target_id, $key, $value );
if ( $key === 'pb_section_license' ) {
wp_set_object_terms( $target_id, $value, \Pressbooks\Licensing::TAXONOMY ); // Link
}
}
}
return true;
} | php | protected function cloneSectionMetadata( $section_id, $post_type, $target_id ) {
$book_schema = $this->sourceBookMetadata;
if ( empty( $this->targetBookUrl ) ) {
// If there's no target then that means this data is going into the current book.
// Remove invalid $book_schema values so that $section_schema is used instead.
$book_schema['author'] = [];
$book_schema['license'] = '';
}
$section_information = schema_to_section_information(
$this->retrieveSectionMetadata( $section_id, $post_type ),
$book_schema
);
foreach ( $section_information as $key => $value ) {
if ( $this->contributors->isValid( $key ) ) {
$values = oxford_comma_explode( $value );
foreach ( $values as $v ) {
$this->contributors->insert( $v, $target_id, $key );
}
} else {
update_post_meta( $target_id, $key, $value );
if ( $key === 'pb_section_license' ) {
wp_set_object_terms( $target_id, $value, \Pressbooks\Licensing::TAXONOMY ); // Link
}
}
}
return true;
} | [
"protected",
"function",
"cloneSectionMetadata",
"(",
"$",
"section_id",
",",
"$",
"post_type",
",",
"$",
"target_id",
")",
"{",
"$",
"book_schema",
"=",
"$",
"this",
"->",
"sourceBookMetadata",
";",
"if",
"(",
"empty",
"(",
"$",
"this",
"->",
"targetBookUrl",
")",
")",
"{",
"// If there's no target then that means this data is going into the current book.",
"// Remove invalid $book_schema values so that $section_schema is used instead.",
"$",
"book_schema",
"[",
"'author'",
"]",
"=",
"[",
"]",
";",
"$",
"book_schema",
"[",
"'license'",
"]",
"=",
"''",
";",
"}",
"$",
"section_information",
"=",
"schema_to_section_information",
"(",
"$",
"this",
"->",
"retrieveSectionMetadata",
"(",
"$",
"section_id",
",",
"$",
"post_type",
")",
",",
"$",
"book_schema",
")",
";",
"foreach",
"(",
"$",
"section_information",
"as",
"$",
"key",
"=>",
"$",
"value",
")",
"{",
"if",
"(",
"$",
"this",
"->",
"contributors",
"->",
"isValid",
"(",
"$",
"key",
")",
")",
"{",
"$",
"values",
"=",
"oxford_comma_explode",
"(",
"$",
"value",
")",
";",
"foreach",
"(",
"$",
"values",
"as",
"$",
"v",
")",
"{",
"$",
"this",
"->",
"contributors",
"->",
"insert",
"(",
"$",
"v",
",",
"$",
"target_id",
",",
"$",
"key",
")",
";",
"}",
"}",
"else",
"{",
"update_post_meta",
"(",
"$",
"target_id",
",",
"$",
"key",
",",
"$",
"value",
")",
";",
"if",
"(",
"$",
"key",
"===",
"'pb_section_license'",
")",
"{",
"wp_set_object_terms",
"(",
"$",
"target_id",
",",
"$",
"value",
",",
"\\",
"Pressbooks",
"\\",
"Licensing",
"::",
"TAXONOMY",
")",
";",
"// Link",
"}",
"}",
"}",
"return",
"true",
";",
"}"
] | Clone metadata of a section (front matter, part, chapter, back matter) from a source book to a target book.
@since 4.1.0
@param int $section_id The ID of the section within the source book.
@param string $post_type The post type of the section.
@param int $target_id The ID of the section within the target book.
@return bool False if the clone failed; true if it succeeded. | [
"Clone",
"metadata",
"of",
"a",
"section",
"(",
"front",
"matter",
"part",
"chapter",
"back",
"matter",
")",
"from",
"a",
"source",
"book",
"to",
"a",
"target",
"book",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/cloner/class-cloner.php#L1582-L1612 |
pressbooks/pressbooks | inc/cloner/class-cloner.php | Cloner.handleGetRequest | protected function handleGetRequest( $url, $namespace, $endpoint, $params = [], $paginate = true, $previous_results = [] ) {
global $blog_id;
// Is the book local? If so, is it the current book? If not, switch to it.
$local_book = $this->getBookId( $url );
if ( $local_book ) {
$switch = ( $local_book !== $blog_id ) ? true : false;
if ( $switch ) {
switch_to_blog( $local_book );
}
// Set up WP_REST_Request, retrieve response.
$_GET['_embed'] = 1;
$request = new \WP_REST_Request( 'GET', "/$namespace/$endpoint" );
if ( ! empty( $params ) ) {
$request->set_query_params( $params );
}
$response = rest_do_request( $request );
if ( $switch ) {
restore_current_blog();
}
// Handle errors
if ( is_wp_error( $response ) ) {
return $response;
} else {
$results = rest_get_server()->response_to_data( $response, true );
}
} else {
// Build request URL
$request_url = sprintf(
'%1$s/%2$s/%3$s/%4$s',
$this->sourceBookUrl,
$this->restBase,
$namespace,
$endpoint
);
// Add params
if ( ! empty( $params ) ) {
$request_url .= '?' . build_query( $params );
}
// GET response from API
$response = wp_remote_get( $request_url, $this->requestArgs );
// Handle errors
if ( is_wp_error( $response ) ) {
return $response;
} elseif ( isset( $response['response']['code'] ) && $response['response']['code'] >= 400 ) {
return new \WP_Error( $response['response']['code'], $response['response']['message'] );
} else {
$results = json_decode( $response['body'], true );
}
}
if ( ! empty( $previous_results ) ) {
$results = array_merge( $previous_results, $results );
}
if ( $paginate ) {
$next_url = $this->nextWebLink( $response );
if ( $next_url ) {
parse_str( wp_parse_url( $next_url, PHP_URL_QUERY ), $next_params );
$next_url = strtok( $next_url, '?' );
return $this->handleGetRequest( $next_url, $namespace, $endpoint, $next_params, $paginate, $results );
}
}
return $results;
} | php | protected function handleGetRequest( $url, $namespace, $endpoint, $params = [], $paginate = true, $previous_results = [] ) {
global $blog_id;
// Is the book local? If so, is it the current book? If not, switch to it.
$local_book = $this->getBookId( $url );
if ( $local_book ) {
$switch = ( $local_book !== $blog_id ) ? true : false;
if ( $switch ) {
switch_to_blog( $local_book );
}
// Set up WP_REST_Request, retrieve response.
$_GET['_embed'] = 1;
$request = new \WP_REST_Request( 'GET', "/$namespace/$endpoint" );
if ( ! empty( $params ) ) {
$request->set_query_params( $params );
}
$response = rest_do_request( $request );
if ( $switch ) {
restore_current_blog();
}
// Handle errors
if ( is_wp_error( $response ) ) {
return $response;
} else {
$results = rest_get_server()->response_to_data( $response, true );
}
} else {
// Build request URL
$request_url = sprintf(
'%1$s/%2$s/%3$s/%4$s',
$this->sourceBookUrl,
$this->restBase,
$namespace,
$endpoint
);
// Add params
if ( ! empty( $params ) ) {
$request_url .= '?' . build_query( $params );
}
// GET response from API
$response = wp_remote_get( $request_url, $this->requestArgs );
// Handle errors
if ( is_wp_error( $response ) ) {
return $response;
} elseif ( isset( $response['response']['code'] ) && $response['response']['code'] >= 400 ) {
return new \WP_Error( $response['response']['code'], $response['response']['message'] );
} else {
$results = json_decode( $response['body'], true );
}
}
if ( ! empty( $previous_results ) ) {
$results = array_merge( $previous_results, $results );
}
if ( $paginate ) {
$next_url = $this->nextWebLink( $response );
if ( $next_url ) {
parse_str( wp_parse_url( $next_url, PHP_URL_QUERY ), $next_params );
$next_url = strtok( $next_url, '?' );
return $this->handleGetRequest( $next_url, $namespace, $endpoint, $next_params, $paginate, $results );
}
}
return $results;
} | [
"protected",
"function",
"handleGetRequest",
"(",
"$",
"url",
",",
"$",
"namespace",
",",
"$",
"endpoint",
",",
"$",
"params",
"=",
"[",
"]",
",",
"$",
"paginate",
"=",
"true",
",",
"$",
"previous_results",
"=",
"[",
"]",
")",
"{",
"global",
"$",
"blog_id",
";",
"// Is the book local? If so, is it the current book? If not, switch to it.",
"$",
"local_book",
"=",
"$",
"this",
"->",
"getBookId",
"(",
"$",
"url",
")",
";",
"if",
"(",
"$",
"local_book",
")",
"{",
"$",
"switch",
"=",
"(",
"$",
"local_book",
"!==",
"$",
"blog_id",
")",
"?",
"true",
":",
"false",
";",
"if",
"(",
"$",
"switch",
")",
"{",
"switch_to_blog",
"(",
"$",
"local_book",
")",
";",
"}",
"// Set up WP_REST_Request, retrieve response.",
"$",
"_GET",
"[",
"'_embed'",
"]",
"=",
"1",
";",
"$",
"request",
"=",
"new",
"\\",
"WP_REST_Request",
"(",
"'GET'",
",",
"\"/$namespace/$endpoint\"",
")",
";",
"if",
"(",
"!",
"empty",
"(",
"$",
"params",
")",
")",
"{",
"$",
"request",
"->",
"set_query_params",
"(",
"$",
"params",
")",
";",
"}",
"$",
"response",
"=",
"rest_do_request",
"(",
"$",
"request",
")",
";",
"if",
"(",
"$",
"switch",
")",
"{",
"restore_current_blog",
"(",
")",
";",
"}",
"// Handle errors",
"if",
"(",
"is_wp_error",
"(",
"$",
"response",
")",
")",
"{",
"return",
"$",
"response",
";",
"}",
"else",
"{",
"$",
"results",
"=",
"rest_get_server",
"(",
")",
"->",
"response_to_data",
"(",
"$",
"response",
",",
"true",
")",
";",
"}",
"}",
"else",
"{",
"// Build request URL",
"$",
"request_url",
"=",
"sprintf",
"(",
"'%1$s/%2$s/%3$s/%4$s'",
",",
"$",
"this",
"->",
"sourceBookUrl",
",",
"$",
"this",
"->",
"restBase",
",",
"$",
"namespace",
",",
"$",
"endpoint",
")",
";",
"// Add params",
"if",
"(",
"!",
"empty",
"(",
"$",
"params",
")",
")",
"{",
"$",
"request_url",
".=",
"'?'",
".",
"build_query",
"(",
"$",
"params",
")",
";",
"}",
"// GET response from API",
"$",
"response",
"=",
"wp_remote_get",
"(",
"$",
"request_url",
",",
"$",
"this",
"->",
"requestArgs",
")",
";",
"// Handle errors",
"if",
"(",
"is_wp_error",
"(",
"$",
"response",
")",
")",
"{",
"return",
"$",
"response",
";",
"}",
"elseif",
"(",
"isset",
"(",
"$",
"response",
"[",
"'response'",
"]",
"[",
"'code'",
"]",
")",
"&&",
"$",
"response",
"[",
"'response'",
"]",
"[",
"'code'",
"]",
">=",
"400",
")",
"{",
"return",
"new",
"\\",
"WP_Error",
"(",
"$",
"response",
"[",
"'response'",
"]",
"[",
"'code'",
"]",
",",
"$",
"response",
"[",
"'response'",
"]",
"[",
"'message'",
"]",
")",
";",
"}",
"else",
"{",
"$",
"results",
"=",
"json_decode",
"(",
"$",
"response",
"[",
"'body'",
"]",
",",
"true",
")",
";",
"}",
"}",
"if",
"(",
"!",
"empty",
"(",
"$",
"previous_results",
")",
")",
"{",
"$",
"results",
"=",
"array_merge",
"(",
"$",
"previous_results",
",",
"$",
"results",
")",
";",
"}",
"if",
"(",
"$",
"paginate",
")",
"{",
"$",
"next_url",
"=",
"$",
"this",
"->",
"nextWebLink",
"(",
"$",
"response",
")",
";",
"if",
"(",
"$",
"next_url",
")",
"{",
"parse_str",
"(",
"wp_parse_url",
"(",
"$",
"next_url",
",",
"PHP_URL_QUERY",
")",
",",
"$",
"next_params",
")",
";",
"$",
"next_url",
"=",
"strtok",
"(",
"$",
"next_url",
",",
"'?'",
")",
";",
"return",
"$",
"this",
"->",
"handleGetRequest",
"(",
"$",
"next_url",
",",
"$",
"namespace",
",",
"$",
"endpoint",
",",
"$",
"next_params",
",",
"$",
"paginate",
",",
"$",
"results",
")",
";",
"}",
"}",
"return",
"$",
"results",
";",
"}"
] | Handle a get request against the REST API using either rest_do_request() or wp_remote_get() as appropriate.
@since 4.1.0
@param string $url The URL against which the request should be made (not including the REST base)
@param string $namespace The namespace for the request, e.g. 'pressbooks/v2'
@param string $endpoint The endpoint for the request, e.g. 'toc'
@param array $params URL parameters
@param bool $paginate (optional, if results are paginated then get next page)
@param array $previous_results (optional, used recursively for when results are paginated)
@return array|\WP_Error | [
"Handle",
"a",
"get",
"request",
"against",
"the",
"REST",
"API",
"using",
"either",
"rest_do_request",
"()",
"or",
"wp_remote_get",
"()",
"as",
"appropriate",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/cloner/class-cloner.php#L1628-L1699 |
pressbooks/pressbooks | inc/cloner/class-cloner.php | Cloner.nextWebLink | protected function nextWebLink( $response ) {
$header = $this->extractLinkHeader( $response );
$links = explode( ',', $header );
foreach ( $links as $link ) {
$link = $this->parseLinkHeader( $link );
if ( isset( $link['rel'] ) && strtolower( $link['rel'] ) === 'next' ) {
return $link['href'];
}
}
return false;
} | php | protected function nextWebLink( $response ) {
$header = $this->extractLinkHeader( $response );
$links = explode( ',', $header );
foreach ( $links as $link ) {
$link = $this->parseLinkHeader( $link );
if ( isset( $link['rel'] ) && strtolower( $link['rel'] ) === 'next' ) {
return $link['href'];
}
}
return false;
} | [
"protected",
"function",
"nextWebLink",
"(",
"$",
"response",
")",
"{",
"$",
"header",
"=",
"$",
"this",
"->",
"extractLinkHeader",
"(",
"$",
"response",
")",
";",
"$",
"links",
"=",
"explode",
"(",
"','",
",",
"$",
"header",
")",
";",
"foreach",
"(",
"$",
"links",
"as",
"$",
"link",
")",
"{",
"$",
"link",
"=",
"$",
"this",
"->",
"parseLinkHeader",
"(",
"$",
"link",
")",
";",
"if",
"(",
"isset",
"(",
"$",
"link",
"[",
"'rel'",
"]",
")",
"&&",
"strtolower",
"(",
"$",
"link",
"[",
"'rel'",
"]",
")",
"===",
"'next'",
")",
"{",
"return",
"$",
"link",
"[",
"'href'",
"]",
";",
"}",
"}",
"return",
"false",
";",
"}"
] | Format: <http://pressbooks.dev/test/wp-json/wp/v2/media?media_type=image&page=2>; rel="next"
Or: <http://pressbooks.dev/test/wp-json/wp/v2/media?media_type=image&page=1>; rel="prev", <http://pressbooks.dev/test/wp-json/wp/v2/media?media_type=image&page=3>; rel="next"
@param \WP_REST_Response|array $response
@return string|false | [
"Format",
":",
"<http",
":",
"//",
"pressbooks",
".",
"dev",
"/",
"test",
"/",
"wp",
"-",
"json",
"/",
"wp",
"/",
"v2",
"/",
"media?media_type",
"=",
"image&page",
"=",
"2",
">",
";",
"rel",
"=",
"next",
"Or",
":",
"<http",
":",
"//",
"pressbooks",
".",
"dev",
"/",
"test",
"/",
"wp",
"-",
"json",
"/",
"wp",
"/",
"v2",
"/",
"media?media_type",
"=",
"image&page",
"=",
"1",
">",
";",
"rel",
"=",
"prev",
"<http",
":",
"//",
"pressbooks",
".",
"dev",
"/",
"test",
"/",
"wp",
"-",
"json",
"/",
"wp",
"/",
"v2",
"/",
"media?media_type",
"=",
"image&page",
"=",
"3",
">",
";",
"rel",
"=",
"next"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/cloner/class-cloner.php#L1709-L1719 |
pressbooks/pressbooks | inc/cloner/class-cloner.php | Cloner.extractLinkHeader | protected function extractLinkHeader( $response ) {
if ( is_object( $response ) && property_exists( $response, 'headers' ) && is_array( $response->headers ) && isset( $response->headers['Link'] ) ) {
return $response->headers['Link'];
}
if ( is_array( $response ) && isset( $response['headers'], $response['headers']['Link'] ) ) {
return $response['headers']['Link'];
}
return '';
} | php | protected function extractLinkHeader( $response ) {
if ( is_object( $response ) && property_exists( $response, 'headers' ) && is_array( $response->headers ) && isset( $response->headers['Link'] ) ) {
return $response->headers['Link'];
}
if ( is_array( $response ) && isset( $response['headers'], $response['headers']['Link'] ) ) {
return $response['headers']['Link'];
}
return '';
} | [
"protected",
"function",
"extractLinkHeader",
"(",
"$",
"response",
")",
"{",
"if",
"(",
"is_object",
"(",
"$",
"response",
")",
"&&",
"property_exists",
"(",
"$",
"response",
",",
"'headers'",
")",
"&&",
"is_array",
"(",
"$",
"response",
"->",
"headers",
")",
"&&",
"isset",
"(",
"$",
"response",
"->",
"headers",
"[",
"'Link'",
"]",
")",
")",
"{",
"return",
"$",
"response",
"->",
"headers",
"[",
"'Link'",
"]",
";",
"}",
"if",
"(",
"is_array",
"(",
"$",
"response",
")",
"&&",
"isset",
"(",
"$",
"response",
"[",
"'headers'",
"]",
",",
"$",
"response",
"[",
"'headers'",
"]",
"[",
"'Link'",
"]",
")",
")",
"{",
"return",
"$",
"response",
"[",
"'headers'",
"]",
"[",
"'Link'",
"]",
";",
"}",
"return",
"''",
";",
"}"
] | @param \WP_REST_Response|array $response
@return string | [
"@param",
"\\",
"WP_REST_Response|array",
"$response"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/cloner/class-cloner.php#L1726-L1734 |
pressbooks/pressbooks | inc/cloner/class-cloner.php | Cloner.parseLinkHeader | protected function parseLinkHeader( $link ) {
$parts = explode( ';', $link );
$attrs = [
'href' => trim( array_shift( $parts ), '<>' ),
];
foreach ( $parts as $part ) {
if ( ! strpos( $part, '=' ) ) {
continue;
}
list( $key, $value ) = explode( '=', $part, 2 );
$key = trim( $key );
$value = trim( $value, '" ' );
$attrs[ $key ] = $value;
}
return $attrs;
} | php | protected function parseLinkHeader( $link ) {
$parts = explode( ';', $link );
$attrs = [
'href' => trim( array_shift( $parts ), '<>' ),
];
foreach ( $parts as $part ) {
if ( ! strpos( $part, '=' ) ) {
continue;
}
list( $key, $value ) = explode( '=', $part, 2 );
$key = trim( $key );
$value = trim( $value, '" ' );
$attrs[ $key ] = $value;
}
return $attrs;
} | [
"protected",
"function",
"parseLinkHeader",
"(",
"$",
"link",
")",
"{",
"$",
"parts",
"=",
"explode",
"(",
"';'",
",",
"$",
"link",
")",
";",
"$",
"attrs",
"=",
"[",
"'href'",
"=>",
"trim",
"(",
"array_shift",
"(",
"$",
"parts",
")",
",",
"'<>'",
")",
",",
"]",
";",
"foreach",
"(",
"$",
"parts",
"as",
"$",
"part",
")",
"{",
"if",
"(",
"!",
"strpos",
"(",
"$",
"part",
",",
"'='",
")",
")",
"{",
"continue",
";",
"}",
"list",
"(",
"$",
"key",
",",
"$",
"value",
")",
"=",
"explode",
"(",
"'='",
",",
"$",
"part",
",",
"2",
")",
";",
"$",
"key",
"=",
"trim",
"(",
"$",
"key",
")",
";",
"$",
"value",
"=",
"trim",
"(",
"$",
"value",
",",
"'\" '",
")",
";",
"$",
"attrs",
"[",
"$",
"key",
"]",
"=",
"$",
"value",
";",
"}",
"return",
"$",
"attrs",
";",
"}"
] | Parse a Link header into attributes.
@param string $link Link header from the response.
@return array Map of attribute key => attribute value, with link href in `href` key. | [
"Parse",
"a",
"Link",
"header",
"into",
"attributes",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/cloner/class-cloner.php#L1743-L1758 |
pressbooks/pressbooks | inc/cloner/class-cloner.php | Cloner.fixInternalLinks | protected function fixInternalLinks( $dom ) {
// Setup
$source_path = $this->getSubdomainOrSubdirectory( $this->sourceBookUrl );
$target_path = $this->getSubdomainOrSubdirectory( $this->targetBookUrl );
$is_subdomain_install = is_subdomain_install();
// Get links, loop through
$links = $dom->getElementsByTagName( 'a' );
foreach ( $links as $link ) {
/** @var \DOMElement $link */
$href = $link->getAttribute( 'href' );
if ( $is_subdomain_install ) {
if ( str_starts_with( $href, "/$source_path/" ) ) {
// Remove book path (cloning from subdirectory to subdomain)
$href = str_remove_prefix( $href, "/$source_path" );
}
} else {
if ( str_starts_with( $href, "/$source_path/" ) ) {
// Replace book path (cloning from subdirectory to subdirectory)
$href = str_replace( "/$source_path/", "/$target_path/", $href );
}
foreach ( [ 'front-matter', 'part', 'chapter', 'back-matter' ] as $post_type ) {
// Add book path (cloning from subdomain to subdirectory)
if ( str_starts_with( $href, "/$post_type/" ) ) {
$href = str_replace( "/$post_type/", "/$target_path/$post_type/", $href );
}
}
}
// Fix absolute URLs
$href = str_replace( untrailingslashit( $this->sourceBookUrl ), untrailingslashit( $this->targetBookUrl ), $href );
// Update href attribute with new href
$link->setAttribute( 'href', $href );
}
return $dom;
} | php | protected function fixInternalLinks( $dom ) {
// Setup
$source_path = $this->getSubdomainOrSubdirectory( $this->sourceBookUrl );
$target_path = $this->getSubdomainOrSubdirectory( $this->targetBookUrl );
$is_subdomain_install = is_subdomain_install();
// Get links, loop through
$links = $dom->getElementsByTagName( 'a' );
foreach ( $links as $link ) {
/** @var \DOMElement $link */
$href = $link->getAttribute( 'href' );
if ( $is_subdomain_install ) {
if ( str_starts_with( $href, "/$source_path/" ) ) {
// Remove book path (cloning from subdirectory to subdomain)
$href = str_remove_prefix( $href, "/$source_path" );
}
} else {
if ( str_starts_with( $href, "/$source_path/" ) ) {
// Replace book path (cloning from subdirectory to subdirectory)
$href = str_replace( "/$source_path/", "/$target_path/", $href );
}
foreach ( [ 'front-matter', 'part', 'chapter', 'back-matter' ] as $post_type ) {
// Add book path (cloning from subdomain to subdirectory)
if ( str_starts_with( $href, "/$post_type/" ) ) {
$href = str_replace( "/$post_type/", "/$target_path/$post_type/", $href );
}
}
}
// Fix absolute URLs
$href = str_replace( untrailingslashit( $this->sourceBookUrl ), untrailingslashit( $this->targetBookUrl ), $href );
// Update href attribute with new href
$link->setAttribute( 'href', $href );
}
return $dom;
} | [
"protected",
"function",
"fixInternalLinks",
"(",
"$",
"dom",
")",
"{",
"// Setup",
"$",
"source_path",
"=",
"$",
"this",
"->",
"getSubdomainOrSubdirectory",
"(",
"$",
"this",
"->",
"sourceBookUrl",
")",
";",
"$",
"target_path",
"=",
"$",
"this",
"->",
"getSubdomainOrSubdirectory",
"(",
"$",
"this",
"->",
"targetBookUrl",
")",
";",
"$",
"is_subdomain_install",
"=",
"is_subdomain_install",
"(",
")",
";",
"// Get links, loop through",
"$",
"links",
"=",
"$",
"dom",
"->",
"getElementsByTagName",
"(",
"'a'",
")",
";",
"foreach",
"(",
"$",
"links",
"as",
"$",
"link",
")",
"{",
"/** @var \\DOMElement $link */",
"$",
"href",
"=",
"$",
"link",
"->",
"getAttribute",
"(",
"'href'",
")",
";",
"if",
"(",
"$",
"is_subdomain_install",
")",
"{",
"if",
"(",
"str_starts_with",
"(",
"$",
"href",
",",
"\"/$source_path/\"",
")",
")",
"{",
"// Remove book path (cloning from subdirectory to subdomain)",
"$",
"href",
"=",
"str_remove_prefix",
"(",
"$",
"href",
",",
"\"/$source_path\"",
")",
";",
"}",
"}",
"else",
"{",
"if",
"(",
"str_starts_with",
"(",
"$",
"href",
",",
"\"/$source_path/\"",
")",
")",
"{",
"// Replace book path (cloning from subdirectory to subdirectory)",
"$",
"href",
"=",
"str_replace",
"(",
"\"/$source_path/\"",
",",
"\"/$target_path/\"",
",",
"$",
"href",
")",
";",
"}",
"foreach",
"(",
"[",
"'front-matter'",
",",
"'part'",
",",
"'chapter'",
",",
"'back-matter'",
"]",
"as",
"$",
"post_type",
")",
"{",
"// Add book path (cloning from subdomain to subdirectory)",
"if",
"(",
"str_starts_with",
"(",
"$",
"href",
",",
"\"/$post_type/\"",
")",
")",
"{",
"$",
"href",
"=",
"str_replace",
"(",
"\"/$post_type/\"",
",",
"\"/$target_path/$post_type/\"",
",",
"$",
"href",
")",
";",
"}",
"}",
"}",
"// Fix absolute URLs",
"$",
"href",
"=",
"str_replace",
"(",
"untrailingslashit",
"(",
"$",
"this",
"->",
"sourceBookUrl",
")",
",",
"untrailingslashit",
"(",
"$",
"this",
"->",
"targetBookUrl",
")",
",",
"$",
"href",
")",
";",
"// Update href attribute with new href",
"$",
"link",
"->",
"setAttribute",
"(",
"'href'",
",",
"$",
"href",
")",
";",
"}",
"return",
"$",
"dom",
";",
"}"
] | @param \DOMDocument $dom
@return \DOMDocument | [
"@param",
"\\",
"DOMDocument",
"$dom"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/cloner/class-cloner.php#L1765-L1801 |
pressbooks/pressbooks | inc/cloner/class-cloner.php | Cloner.validateNewBookName | public static function validateNewBookName( $blogname ) {
global $wpdb, $domain;
$current_network = get_network();
$base = $current_network->path;
$illegal_names = get_site_option( 'illegal_names' );
$minimum_site_name_length = apply_filters( 'minimum_site_name_length', 4 );
if ( is_subdomain_install() ) {
$host = wp_parse_url( esc_url( $domain ), PHP_URL_HOST );
$host = explode( '.', $host );
if ( count( $host ) > 2 ) {
array_shift( $host );
}
$baredomain = implode( '.', $host );
$mydomain = $blogname . '.' . $baredomain;
$path = $base;
} else {
$illegal_names = array_merge( $illegal_names, get_subdirectory_reserved_names() );
$mydomain = "$domain";
$path = $base . $blogname . '/';
}
if ( preg_match( '/[^a-z0-9]+/', $blogname ) ) {
return new \WP_Error( 'blogname', __( 'Your book URL can only contain lowercase letters (a-z) and numbers.', 'pressbooks' ) );
} elseif ( preg_match( '/^[0-9]*$/', $blogname ) ) {
return new \WP_Error( 'blogname', __( 'Your book URL must contain at least some letters.', 'pressbooks' ) );
} elseif ( in_array( $blogname, $illegal_names, true ) ) {
return new \WP_Error( 'blogname', __( 'That book URL is not allowed.', 'pressbooks' ) );
} elseif ( strlen( $blogname ) < $minimum_site_name_length ) {
return new \WP_Error( 'blogname', sprintf( _n( 'Your book URL must be at least %s character.', 'Your book URL must be at least %s characters.', $minimum_site_name_length, 'pressbooks' ), number_format_i18n( $minimum_site_name_length ) ) );
} elseif ( ! is_subdomain_install() && $wpdb->get_var( $wpdb->prepare( "SELECT post_name FROM " . $wpdb->get_blog_prefix( $current_network->site_id ) . "posts WHERE post_type = 'page' AND post_name = %s", $blogname ) ) ) { // @codingStandardsIgnoreLine
return new \WP_Error( 'blogname', __( 'Sorry, you may not use that book URL.', 'pressbooks' ) );
} elseif ( domain_exists( $mydomain, $path, $current_network->id ) ) {
return new \WP_Error( 'blogname', __( 'Sorry, that book URL already exists!', 'pressbooks' ) );
}
return $mydomain . $path;
} | php | public static function validateNewBookName( $blogname ) {
global $wpdb, $domain;
$current_network = get_network();
$base = $current_network->path;
$illegal_names = get_site_option( 'illegal_names' );
$minimum_site_name_length = apply_filters( 'minimum_site_name_length', 4 );
if ( is_subdomain_install() ) {
$host = wp_parse_url( esc_url( $domain ), PHP_URL_HOST );
$host = explode( '.', $host );
if ( count( $host ) > 2 ) {
array_shift( $host );
}
$baredomain = implode( '.', $host );
$mydomain = $blogname . '.' . $baredomain;
$path = $base;
} else {
$illegal_names = array_merge( $illegal_names, get_subdirectory_reserved_names() );
$mydomain = "$domain";
$path = $base . $blogname . '/';
}
if ( preg_match( '/[^a-z0-9]+/', $blogname ) ) {
return new \WP_Error( 'blogname', __( 'Your book URL can only contain lowercase letters (a-z) and numbers.', 'pressbooks' ) );
} elseif ( preg_match( '/^[0-9]*$/', $blogname ) ) {
return new \WP_Error( 'blogname', __( 'Your book URL must contain at least some letters.', 'pressbooks' ) );
} elseif ( in_array( $blogname, $illegal_names, true ) ) {
return new \WP_Error( 'blogname', __( 'That book URL is not allowed.', 'pressbooks' ) );
} elseif ( strlen( $blogname ) < $minimum_site_name_length ) {
return new \WP_Error( 'blogname', sprintf( _n( 'Your book URL must be at least %s character.', 'Your book URL must be at least %s characters.', $minimum_site_name_length, 'pressbooks' ), number_format_i18n( $minimum_site_name_length ) ) );
} elseif ( ! is_subdomain_install() && $wpdb->get_var( $wpdb->prepare( "SELECT post_name FROM " . $wpdb->get_blog_prefix( $current_network->site_id ) . "posts WHERE post_type = 'page' AND post_name = %s", $blogname ) ) ) { // @codingStandardsIgnoreLine
return new \WP_Error( 'blogname', __( 'Sorry, you may not use that book URL.', 'pressbooks' ) );
} elseif ( domain_exists( $mydomain, $path, $current_network->id ) ) {
return new \WP_Error( 'blogname', __( 'Sorry, that book URL already exists!', 'pressbooks' ) );
}
return $mydomain . $path;
} | [
"public",
"static",
"function",
"validateNewBookName",
"(",
"$",
"blogname",
")",
"{",
"global",
"$",
"wpdb",
",",
"$",
"domain",
";",
"$",
"current_network",
"=",
"get_network",
"(",
")",
";",
"$",
"base",
"=",
"$",
"current_network",
"->",
"path",
";",
"$",
"illegal_names",
"=",
"get_site_option",
"(",
"'illegal_names'",
")",
";",
"$",
"minimum_site_name_length",
"=",
"apply_filters",
"(",
"'minimum_site_name_length'",
",",
"4",
")",
";",
"if",
"(",
"is_subdomain_install",
"(",
")",
")",
"{",
"$",
"host",
"=",
"wp_parse_url",
"(",
"esc_url",
"(",
"$",
"domain",
")",
",",
"PHP_URL_HOST",
")",
";",
"$",
"host",
"=",
"explode",
"(",
"'.'",
",",
"$",
"host",
")",
";",
"if",
"(",
"count",
"(",
"$",
"host",
")",
">",
"2",
")",
"{",
"array_shift",
"(",
"$",
"host",
")",
";",
"}",
"$",
"baredomain",
"=",
"implode",
"(",
"'.'",
",",
"$",
"host",
")",
";",
"$",
"mydomain",
"=",
"$",
"blogname",
".",
"'.'",
".",
"$",
"baredomain",
";",
"$",
"path",
"=",
"$",
"base",
";",
"}",
"else",
"{",
"$",
"illegal_names",
"=",
"array_merge",
"(",
"$",
"illegal_names",
",",
"get_subdirectory_reserved_names",
"(",
")",
")",
";",
"$",
"mydomain",
"=",
"\"$domain\"",
";",
"$",
"path",
"=",
"$",
"base",
".",
"$",
"blogname",
".",
"'/'",
";",
"}",
"if",
"(",
"preg_match",
"(",
"'/[^a-z0-9]+/'",
",",
"$",
"blogname",
")",
")",
"{",
"return",
"new",
"\\",
"WP_Error",
"(",
"'blogname'",
",",
"__",
"(",
"'Your book URL can only contain lowercase letters (a-z) and numbers.'",
",",
"'pressbooks'",
")",
")",
";",
"}",
"elseif",
"(",
"preg_match",
"(",
"'/^[0-9]*$/'",
",",
"$",
"blogname",
")",
")",
"{",
"return",
"new",
"\\",
"WP_Error",
"(",
"'blogname'",
",",
"__",
"(",
"'Your book URL must contain at least some letters.'",
",",
"'pressbooks'",
")",
")",
";",
"}",
"elseif",
"(",
"in_array",
"(",
"$",
"blogname",
",",
"$",
"illegal_names",
",",
"true",
")",
")",
"{",
"return",
"new",
"\\",
"WP_Error",
"(",
"'blogname'",
",",
"__",
"(",
"'That book URL is not allowed.'",
",",
"'pressbooks'",
")",
")",
";",
"}",
"elseif",
"(",
"strlen",
"(",
"$",
"blogname",
")",
"<",
"$",
"minimum_site_name_length",
")",
"{",
"return",
"new",
"\\",
"WP_Error",
"(",
"'blogname'",
",",
"sprintf",
"(",
"_n",
"(",
"'Your book URL must be at least %s character.'",
",",
"'Your book URL must be at least %s characters.'",
",",
"$",
"minimum_site_name_length",
",",
"'pressbooks'",
")",
",",
"number_format_i18n",
"(",
"$",
"minimum_site_name_length",
")",
")",
")",
";",
"}",
"elseif",
"(",
"!",
"is_subdomain_install",
"(",
")",
"&&",
"$",
"wpdb",
"->",
"get_var",
"(",
"$",
"wpdb",
"->",
"prepare",
"(",
"\"SELECT post_name FROM \"",
".",
"$",
"wpdb",
"->",
"get_blog_prefix",
"(",
"$",
"current_network",
"->",
"site_id",
")",
".",
"\"posts WHERE post_type = 'page' AND post_name = %s\"",
",",
"$",
"blogname",
")",
")",
")",
"{",
"// @codingStandardsIgnoreLine",
"return",
"new",
"\\",
"WP_Error",
"(",
"'blogname'",
",",
"__",
"(",
"'Sorry, you may not use that book URL.'",
",",
"'pressbooks'",
")",
")",
";",
"}",
"elseif",
"(",
"domain_exists",
"(",
"$",
"mydomain",
",",
"$",
"path",
",",
"$",
"current_network",
"->",
"id",
")",
")",
"{",
"return",
"new",
"\\",
"WP_Error",
"(",
"'blogname'",
",",
"__",
"(",
"'Sorry, that book URL already exists!'",
",",
"'pressbooks'",
")",
")",
";",
"}",
"return",
"$",
"mydomain",
".",
"$",
"path",
";",
"}"
] | Given a book name, see if we can use it to create a new book. Sort of like wpmu_validate_blog_signup().
@since 4.1.0
@param string $blogname
@return string|\WP_Error | [
"Given",
"a",
"book",
"name",
"see",
"if",
"we",
"can",
"use",
"it",
"to",
"create",
"a",
"new",
"book",
".",
"Sort",
"of",
"like",
"wpmu_validate_blog_signup",
"()",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/cloner/class-cloner.php#L1812-L1849 |
pressbooks/pressbooks | inc/cloner/class-cloner.php | Cloner.isEnabled | public static function isEnabled() {
$enable_cloning = get_site_option( 'pressbooks_sharingandprivacy_options', [] );
$enable_cloning = isset( $enable_cloning['enable_cloning'] ) ? $enable_cloning['enable_cloning'] : SharingAndPrivacyOptions::getDefaults()['enable_cloning'];
return (bool) $enable_cloning;
} | php | public static function isEnabled() {
$enable_cloning = get_site_option( 'pressbooks_sharingandprivacy_options', [] );
$enable_cloning = isset( $enable_cloning['enable_cloning'] ) ? $enable_cloning['enable_cloning'] : SharingAndPrivacyOptions::getDefaults()['enable_cloning'];
return (bool) $enable_cloning;
} | [
"public",
"static",
"function",
"isEnabled",
"(",
")",
"{",
"$",
"enable_cloning",
"=",
"get_site_option",
"(",
"'pressbooks_sharingandprivacy_options'",
",",
"[",
"]",
")",
";",
"$",
"enable_cloning",
"=",
"isset",
"(",
"$",
"enable_cloning",
"[",
"'enable_cloning'",
"]",
")",
"?",
"$",
"enable_cloning",
"[",
"'enable_cloning'",
"]",
":",
"SharingAndPrivacyOptions",
"::",
"getDefaults",
"(",
")",
"[",
"'enable_cloning'",
"]",
";",
"return",
"(",
"bool",
")",
"$",
"enable_cloning",
";",
"}"
] | Is clonning feature enabled for this network?
@return bool | [
"Is",
"clonning",
"feature",
"enabled",
"for",
"this",
"network?"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/cloner/class-cloner.php#L1856-L1860 |
pressbooks/pressbooks | inc/class-contributors.php | Contributors.getAll | public function getAll( $post_id, $as_strings = true ) {
$contributors = [];
foreach ( $this->valid as $contributor_type ) {
if ( $as_strings ) {
$contributors[ $contributor_type ] = $this->get( $post_id, $contributor_type );
} else {
$contributors[ $contributor_type ] = $this->getArray( $post_id, $contributor_type );
}
}
return $contributors;
} | php | public function getAll( $post_id, $as_strings = true ) {
$contributors = [];
foreach ( $this->valid as $contributor_type ) {
if ( $as_strings ) {
$contributors[ $contributor_type ] = $this->get( $post_id, $contributor_type );
} else {
$contributors[ $contributor_type ] = $this->getArray( $post_id, $contributor_type );
}
}
return $contributors;
} | [
"public",
"function",
"getAll",
"(",
"$",
"post_id",
",",
"$",
"as_strings",
"=",
"true",
")",
"{",
"$",
"contributors",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"this",
"->",
"valid",
"as",
"$",
"contributor_type",
")",
"{",
"if",
"(",
"$",
"as_strings",
")",
"{",
"$",
"contributors",
"[",
"$",
"contributor_type",
"]",
"=",
"$",
"this",
"->",
"get",
"(",
"$",
"post_id",
",",
"$",
"contributor_type",
")",
";",
"}",
"else",
"{",
"$",
"contributors",
"[",
"$",
"contributor_type",
"]",
"=",
"$",
"this",
"->",
"getArray",
"(",
"$",
"post_id",
",",
"$",
"contributor_type",
")",
";",
"}",
"}",
"return",
"$",
"contributors",
";",
"}"
] | Retrieve all author/editor/etc lists for a given Post ID
@param int $post_id
@param bool $as_strings
@return array | [
"Retrieve",
"all",
"author",
"/",
"editor",
"/",
"etc",
"lists",
"for",
"a",
"given",
"Post",
"ID"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-contributors.php#L72-L82 |
pressbooks/pressbooks | inc/class-contributors.php | Contributors.get | public function get( $post_id, $contributor_type ) {
$contributors = $this->getArray( $post_id, $contributor_type );
return \Pressbooks\Utility\oxford_comma( $contributors );
} | php | public function get( $post_id, $contributor_type ) {
$contributors = $this->getArray( $post_id, $contributor_type );
return \Pressbooks\Utility\oxford_comma( $contributors );
} | [
"public",
"function",
"get",
"(",
"$",
"post_id",
",",
"$",
"contributor_type",
")",
"{",
"$",
"contributors",
"=",
"$",
"this",
"->",
"getArray",
"(",
"$",
"post_id",
",",
"$",
"contributor_type",
")",
";",
"return",
"\\",
"Pressbooks",
"\\",
"Utility",
"\\",
"oxford_comma",
"(",
"$",
"contributors",
")",
";",
"}"
] | Retrieve author/editor/etc lists for a given Post ID and Contributor type, returns string
@param int $post_id
@param string $contributor_type
@return string | [
"Retrieve",
"author",
"/",
"editor",
"/",
"etc",
"lists",
"for",
"a",
"given",
"Post",
"ID",
"and",
"Contributor",
"type",
"returns",
"string"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-contributors.php#L92-L95 |
pressbooks/pressbooks | inc/class-contributors.php | Contributors.getArray | public function getArray( $post_id, $contributor_type ) {
if ( ! str_starts_with( $contributor_type, 'pb_' ) ) {
$contributor_type = 'pb_' . $contributor_type;
}
if ( ! $this->isValid( $contributor_type ) ) {
return [];
}
// Look if contributors exist as taxonomies (new data model)
$contributors = [];
$meta = get_post_meta( $post_id, $contributor_type, false );
if ( is_array( $meta ) ) {
foreach ( $meta as $slug ) {
$name = $this->personalName( $slug );
if ( $name ) {
$contributors[] = $name;
}
}
}
if ( empty( $contributors ) ) {
// Look if contributors exist as metadata (old data model)
// If yes then convert to taxonomies (new data model)
$map = [
'pb_authors' => [ 'pb_author', 'pb_section_author', 'pb_author_file_as' ],
'pb_editors' => [ 'pb_editor' ],
'pb_translators' => [ 'pb_translator' ],
'pb_contributors' => [ 'pb_contributing_authors' ],
];
if ( isset( $map[ $contributor_type ] ) ) {
foreach ( $map[ $contributor_type ] as $slug ) {
$meta = get_post_meta( $post_id, $slug, false );
if ( is_array( $meta ) ) {
foreach ( $meta as $name ) {
$result = $this->insert( $name );
if ( $result !== false ) {
$added = $this->link( $result['term_id'], $post_id, $contributor_type );
if ( $added !== false ) {
$contributors[] = $name;
delete_post_meta( $post_id, $slug, $name );
}
}
}
}
}
}
}
return $contributors;
} | php | public function getArray( $post_id, $contributor_type ) {
if ( ! str_starts_with( $contributor_type, 'pb_' ) ) {
$contributor_type = 'pb_' . $contributor_type;
}
if ( ! $this->isValid( $contributor_type ) ) {
return [];
}
// Look if contributors exist as taxonomies (new data model)
$contributors = [];
$meta = get_post_meta( $post_id, $contributor_type, false );
if ( is_array( $meta ) ) {
foreach ( $meta as $slug ) {
$name = $this->personalName( $slug );
if ( $name ) {
$contributors[] = $name;
}
}
}
if ( empty( $contributors ) ) {
// Look if contributors exist as metadata (old data model)
// If yes then convert to taxonomies (new data model)
$map = [
'pb_authors' => [ 'pb_author', 'pb_section_author', 'pb_author_file_as' ],
'pb_editors' => [ 'pb_editor' ],
'pb_translators' => [ 'pb_translator' ],
'pb_contributors' => [ 'pb_contributing_authors' ],
];
if ( isset( $map[ $contributor_type ] ) ) {
foreach ( $map[ $contributor_type ] as $slug ) {
$meta = get_post_meta( $post_id, $slug, false );
if ( is_array( $meta ) ) {
foreach ( $meta as $name ) {
$result = $this->insert( $name );
if ( $result !== false ) {
$added = $this->link( $result['term_id'], $post_id, $contributor_type );
if ( $added !== false ) {
$contributors[] = $name;
delete_post_meta( $post_id, $slug, $name );
}
}
}
}
}
}
}
return $contributors;
} | [
"public",
"function",
"getArray",
"(",
"$",
"post_id",
",",
"$",
"contributor_type",
")",
"{",
"if",
"(",
"!",
"str_starts_with",
"(",
"$",
"contributor_type",
",",
"'pb_'",
")",
")",
"{",
"$",
"contributor_type",
"=",
"'pb_'",
".",
"$",
"contributor_type",
";",
"}",
"if",
"(",
"!",
"$",
"this",
"->",
"isValid",
"(",
"$",
"contributor_type",
")",
")",
"{",
"return",
"[",
"]",
";",
"}",
"// Look if contributors exist as taxonomies (new data model)",
"$",
"contributors",
"=",
"[",
"]",
";",
"$",
"meta",
"=",
"get_post_meta",
"(",
"$",
"post_id",
",",
"$",
"contributor_type",
",",
"false",
")",
";",
"if",
"(",
"is_array",
"(",
"$",
"meta",
")",
")",
"{",
"foreach",
"(",
"$",
"meta",
"as",
"$",
"slug",
")",
"{",
"$",
"name",
"=",
"$",
"this",
"->",
"personalName",
"(",
"$",
"slug",
")",
";",
"if",
"(",
"$",
"name",
")",
"{",
"$",
"contributors",
"[",
"]",
"=",
"$",
"name",
";",
"}",
"}",
"}",
"if",
"(",
"empty",
"(",
"$",
"contributors",
")",
")",
"{",
"// Look if contributors exist as metadata (old data model)",
"// If yes then convert to taxonomies (new data model)",
"$",
"map",
"=",
"[",
"'pb_authors'",
"=>",
"[",
"'pb_author'",
",",
"'pb_section_author'",
",",
"'pb_author_file_as'",
"]",
",",
"'pb_editors'",
"=>",
"[",
"'pb_editor'",
"]",
",",
"'pb_translators'",
"=>",
"[",
"'pb_translator'",
"]",
",",
"'pb_contributors'",
"=>",
"[",
"'pb_contributing_authors'",
"]",
",",
"]",
";",
"if",
"(",
"isset",
"(",
"$",
"map",
"[",
"$",
"contributor_type",
"]",
")",
")",
"{",
"foreach",
"(",
"$",
"map",
"[",
"$",
"contributor_type",
"]",
"as",
"$",
"slug",
")",
"{",
"$",
"meta",
"=",
"get_post_meta",
"(",
"$",
"post_id",
",",
"$",
"slug",
",",
"false",
")",
";",
"if",
"(",
"is_array",
"(",
"$",
"meta",
")",
")",
"{",
"foreach",
"(",
"$",
"meta",
"as",
"$",
"name",
")",
"{",
"$",
"result",
"=",
"$",
"this",
"->",
"insert",
"(",
"$",
"name",
")",
";",
"if",
"(",
"$",
"result",
"!==",
"false",
")",
"{",
"$",
"added",
"=",
"$",
"this",
"->",
"link",
"(",
"$",
"result",
"[",
"'term_id'",
"]",
",",
"$",
"post_id",
",",
"$",
"contributor_type",
")",
";",
"if",
"(",
"$",
"added",
"!==",
"false",
")",
"{",
"$",
"contributors",
"[",
"]",
"=",
"$",
"name",
";",
"delete_post_meta",
"(",
"$",
"post_id",
",",
"$",
"slug",
",",
"$",
"name",
")",
";",
"}",
"}",
"}",
"}",
"}",
"}",
"}",
"return",
"$",
"contributors",
";",
"}"
] | Retrieve author/editor/etc lists for a given Post ID and Contributor type, returns array
@param int $post_id
@param string $contributor_type
@return array | [
"Retrieve",
"author",
"/",
"editor",
"/",
"etc",
"lists",
"for",
"a",
"given",
"Post",
"ID",
"and",
"Contributor",
"type",
"returns",
"array"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-contributors.php#L105-L154 |
pressbooks/pressbooks | inc/class-contributors.php | Contributors.insert | public function insert( $full_name, $post_id = 0, $contributor_type = 'pb_authors' ) {
$full_name = trim( $full_name );
$slug = sanitize_title_with_dashes( remove_accents( $full_name ), '', 'save' );
$term = get_term_by( 'slug', $slug, self::TAXONOMY );
if ( $term ) {
$results = [
'term_id' => $term->term_id,
'term_taxonomy_id' => $term->term_taxonomy_id,
];
} else {
$results = wp_insert_term(
$full_name, self::TAXONOMY, [
'slug' => $slug,
]
);
}
if ( $post_id && is_array( $results ) ) {
$this->link( $results['term_id'], $post_id, $contributor_type );
}
return is_array( $results ) ? $results : false;
} | php | public function insert( $full_name, $post_id = 0, $contributor_type = 'pb_authors' ) {
$full_name = trim( $full_name );
$slug = sanitize_title_with_dashes( remove_accents( $full_name ), '', 'save' );
$term = get_term_by( 'slug', $slug, self::TAXONOMY );
if ( $term ) {
$results = [
'term_id' => $term->term_id,
'term_taxonomy_id' => $term->term_taxonomy_id,
];
} else {
$results = wp_insert_term(
$full_name, self::TAXONOMY, [
'slug' => $slug,
]
);
}
if ( $post_id && is_array( $results ) ) {
$this->link( $results['term_id'], $post_id, $contributor_type );
}
return is_array( $results ) ? $results : false;
} | [
"public",
"function",
"insert",
"(",
"$",
"full_name",
",",
"$",
"post_id",
"=",
"0",
",",
"$",
"contributor_type",
"=",
"'pb_authors'",
")",
"{",
"$",
"full_name",
"=",
"trim",
"(",
"$",
"full_name",
")",
";",
"$",
"slug",
"=",
"sanitize_title_with_dashes",
"(",
"remove_accents",
"(",
"$",
"full_name",
")",
",",
"''",
",",
"'save'",
")",
";",
"$",
"term",
"=",
"get_term_by",
"(",
"'slug'",
",",
"$",
"slug",
",",
"self",
"::",
"TAXONOMY",
")",
";",
"if",
"(",
"$",
"term",
")",
"{",
"$",
"results",
"=",
"[",
"'term_id'",
"=>",
"$",
"term",
"->",
"term_id",
",",
"'term_taxonomy_id'",
"=>",
"$",
"term",
"->",
"term_taxonomy_id",
",",
"]",
";",
"}",
"else",
"{",
"$",
"results",
"=",
"wp_insert_term",
"(",
"$",
"full_name",
",",
"self",
"::",
"TAXONOMY",
",",
"[",
"'slug'",
"=>",
"$",
"slug",
",",
"]",
")",
";",
"}",
"if",
"(",
"$",
"post_id",
"&&",
"is_array",
"(",
"$",
"results",
")",
")",
"{",
"$",
"this",
"->",
"link",
"(",
"$",
"results",
"[",
"'term_id'",
"]",
",",
"$",
"post_id",
",",
"$",
"contributor_type",
")",
";",
"}",
"return",
"is_array",
"(",
"$",
"results",
")",
"?",
"$",
"results",
":",
"false",
";",
"}"
] | @param string $full_name
@param int $post_id (optional)
@param string $contributor_type (optional)
@return array|false An array containing the `term_id` and `term_taxonomy_id`, false otherwise. | [
"@param",
"string",
"$full_name",
"@param",
"int",
"$post_id",
"(",
"optional",
")",
"@param",
"string",
"$contributor_type",
"(",
"optional",
")"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-contributors.php#L163-L185 |
pressbooks/pressbooks | inc/class-contributors.php | Contributors.link | public function link( $term_id, $post_id, $contributor_type = 'pb_authors' ) {
global $wpdb;
if ( ! str_starts_with( $contributor_type, 'pb_' ) ) {
$contributor_type = 'pb_' . $contributor_type;
}
if ( $this->isValid( $contributor_type ) ) {
if ( preg_match( '/\d+/', $term_id ) ) {
$term = get_term( $term_id, self::TAXONOMY ); // Get slug by Term ID
} else {
$term = get_term_by( 'slug', $term_id, self::TAXONOMY ); // Verify that slug is valid
}
if ( $term && ! is_wp_error( $term ) ) {
wp_set_object_terms( $post_id, $term->term_id, self::TAXONOMY, true );
if ( $wpdb->get_var( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = %s AND meta_value = %s", $post_id, $contributor_type, $term->slug ) ) ) {
return true;
} else {
return is_int( add_post_meta( $post_id, $contributor_type, $term->slug ) );
}
}
}
return false;
} | php | public function link( $term_id, $post_id, $contributor_type = 'pb_authors' ) {
global $wpdb;
if ( ! str_starts_with( $contributor_type, 'pb_' ) ) {
$contributor_type = 'pb_' . $contributor_type;
}
if ( $this->isValid( $contributor_type ) ) {
if ( preg_match( '/\d+/', $term_id ) ) {
$term = get_term( $term_id, self::TAXONOMY ); // Get slug by Term ID
} else {
$term = get_term_by( 'slug', $term_id, self::TAXONOMY ); // Verify that slug is valid
}
if ( $term && ! is_wp_error( $term ) ) {
wp_set_object_terms( $post_id, $term->term_id, self::TAXONOMY, true );
if ( $wpdb->get_var( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = %s AND meta_value = %s", $post_id, $contributor_type, $term->slug ) ) ) {
return true;
} else {
return is_int( add_post_meta( $post_id, $contributor_type, $term->slug ) );
}
}
}
return false;
} | [
"public",
"function",
"link",
"(",
"$",
"term_id",
",",
"$",
"post_id",
",",
"$",
"contributor_type",
"=",
"'pb_authors'",
")",
"{",
"global",
"$",
"wpdb",
";",
"if",
"(",
"!",
"str_starts_with",
"(",
"$",
"contributor_type",
",",
"'pb_'",
")",
")",
"{",
"$",
"contributor_type",
"=",
"'pb_'",
".",
"$",
"contributor_type",
";",
"}",
"if",
"(",
"$",
"this",
"->",
"isValid",
"(",
"$",
"contributor_type",
")",
")",
"{",
"if",
"(",
"preg_match",
"(",
"'/\\d+/'",
",",
"$",
"term_id",
")",
")",
"{",
"$",
"term",
"=",
"get_term",
"(",
"$",
"term_id",
",",
"self",
"::",
"TAXONOMY",
")",
";",
"// Get slug by Term ID",
"}",
"else",
"{",
"$",
"term",
"=",
"get_term_by",
"(",
"'slug'",
",",
"$",
"term_id",
",",
"self",
"::",
"TAXONOMY",
")",
";",
"// Verify that slug is valid",
"}",
"if",
"(",
"$",
"term",
"&&",
"!",
"is_wp_error",
"(",
"$",
"term",
")",
")",
"{",
"wp_set_object_terms",
"(",
"$",
"post_id",
",",
"$",
"term",
"->",
"term_id",
",",
"self",
"::",
"TAXONOMY",
",",
"true",
")",
";",
"if",
"(",
"$",
"wpdb",
"->",
"get_var",
"(",
"$",
"wpdb",
"->",
"prepare",
"(",
"\"SELECT meta_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = %s AND meta_value = %s\"",
",",
"$",
"post_id",
",",
"$",
"contributor_type",
",",
"$",
"term",
"->",
"slug",
")",
")",
")",
"{",
"return",
"true",
";",
"}",
"else",
"{",
"return",
"is_int",
"(",
"add_post_meta",
"(",
"$",
"post_id",
",",
"$",
"contributor_type",
",",
"$",
"term",
"->",
"slug",
")",
")",
";",
"}",
"}",
"}",
"return",
"false",
";",
"}"
] | Associate a Contributor's Term ID to a Post ID (Taxonomy + Meta)
Technically we are assigning the Term Slug to the Post ID. This function handles either.
@param int|string $term_id
@param int $post_id
@param string $contributor_type
@return bool | [
"Associate",
"a",
"Contributor",
"s",
"Term",
"ID",
"to",
"a",
"Post",
"ID",
"(",
"Taxonomy",
"+",
"Meta",
")",
"Technically",
"we",
"are",
"assigning",
"the",
"Term",
"Slug",
"to",
"the",
"Post",
"ID",
".",
"This",
"function",
"handles",
"either",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-contributors.php#L197-L218 |
pressbooks/pressbooks | inc/class-contributors.php | Contributors.unlink | public function unlink( $term_id, $post_id, $contributor_type = 'pb_authors' ) {
if ( ! str_starts_with( $contributor_type, 'pb_' ) ) {
$contributor_type = 'pb_' . $contributor_type;
}
if ( $this->isValid( $contributor_type ) ) {
if ( preg_match( '/\d+/', $term_id ) ) {
$term = get_term( $term_id, self::TAXONOMY ); // Get slug by Term ID
} else {
$term = get_term_by( 'slug', $term_id, self::TAXONOMY ); // Verify that slug is valid
}
if ( $term && ! is_wp_error( $term ) ) {
wp_remove_object_terms( $post_id, $term->term_id, self::TAXONOMY );
delete_post_meta( $post_id, $contributor_type, $term->slug );
return true;
}
}
return false;
} | php | public function unlink( $term_id, $post_id, $contributor_type = 'pb_authors' ) {
if ( ! str_starts_with( $contributor_type, 'pb_' ) ) {
$contributor_type = 'pb_' . $contributor_type;
}
if ( $this->isValid( $contributor_type ) ) {
if ( preg_match( '/\d+/', $term_id ) ) {
$term = get_term( $term_id, self::TAXONOMY ); // Get slug by Term ID
} else {
$term = get_term_by( 'slug', $term_id, self::TAXONOMY ); // Verify that slug is valid
}
if ( $term && ! is_wp_error( $term ) ) {
wp_remove_object_terms( $post_id, $term->term_id, self::TAXONOMY );
delete_post_meta( $post_id, $contributor_type, $term->slug );
return true;
}
}
return false;
} | [
"public",
"function",
"unlink",
"(",
"$",
"term_id",
",",
"$",
"post_id",
",",
"$",
"contributor_type",
"=",
"'pb_authors'",
")",
"{",
"if",
"(",
"!",
"str_starts_with",
"(",
"$",
"contributor_type",
",",
"'pb_'",
")",
")",
"{",
"$",
"contributor_type",
"=",
"'pb_'",
".",
"$",
"contributor_type",
";",
"}",
"if",
"(",
"$",
"this",
"->",
"isValid",
"(",
"$",
"contributor_type",
")",
")",
"{",
"if",
"(",
"preg_match",
"(",
"'/\\d+/'",
",",
"$",
"term_id",
")",
")",
"{",
"$",
"term",
"=",
"get_term",
"(",
"$",
"term_id",
",",
"self",
"::",
"TAXONOMY",
")",
";",
"// Get slug by Term ID",
"}",
"else",
"{",
"$",
"term",
"=",
"get_term_by",
"(",
"'slug'",
",",
"$",
"term_id",
",",
"self",
"::",
"TAXONOMY",
")",
";",
"// Verify that slug is valid",
"}",
"if",
"(",
"$",
"term",
"&&",
"!",
"is_wp_error",
"(",
"$",
"term",
")",
")",
"{",
"wp_remove_object_terms",
"(",
"$",
"post_id",
",",
"$",
"term",
"->",
"term_id",
",",
"self",
"::",
"TAXONOMY",
")",
";",
"delete_post_meta",
"(",
"$",
"post_id",
",",
"$",
"contributor_type",
",",
"$",
"term",
"->",
"slug",
")",
";",
"return",
"true",
";",
"}",
"}",
"return",
"false",
";",
"}"
] | Disassociate a Contributor's Term ID to a Post ID (Taxonomy + Meta)
@param int|string $term_id
@param int $post_id
@param string $contributor_type
@return bool | [
"Disassociate",
"a",
"Contributor",
"s",
"Term",
"ID",
"to",
"a",
"Post",
"ID",
"(",
"Taxonomy",
"+",
"Meta",
")"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-contributors.php#L229-L246 |
pressbooks/pressbooks | inc/class-contributors.php | Contributors.addBlogUser | public function addBlogUser( $user_id ) {
$user = get_userdata( $user_id );
if ( $user && user_can( $user, 'edit_posts' ) ) {
$slug = $user->user_nicename;
$name = trim( "{$user->first_name} {$user->last_name}" );
if ( empty( $name ) ) {
$name = $user->display_name;
if ( empty( $name ) ) {
$name = $slug;
}
}
$results = wp_insert_term(
$name, self::TAXONOMY, [
'slug' => $slug,
]
);
if ( is_array( $results ) ) {
add_term_meta( $results['term_id'], 'contributor_first_name', $user->first_name, true );
add_term_meta( $results['term_id'], 'contributor_last_name', $user->last_name, true );
return $results;
}
}
return false;
} | php | public function addBlogUser( $user_id ) {
$user = get_userdata( $user_id );
if ( $user && user_can( $user, 'edit_posts' ) ) {
$slug = $user->user_nicename;
$name = trim( "{$user->first_name} {$user->last_name}" );
if ( empty( $name ) ) {
$name = $user->display_name;
if ( empty( $name ) ) {
$name = $slug;
}
}
$results = wp_insert_term(
$name, self::TAXONOMY, [
'slug' => $slug,
]
);
if ( is_array( $results ) ) {
add_term_meta( $results['term_id'], 'contributor_first_name', $user->first_name, true );
add_term_meta( $results['term_id'], 'contributor_last_name', $user->last_name, true );
return $results;
}
}
return false;
} | [
"public",
"function",
"addBlogUser",
"(",
"$",
"user_id",
")",
"{",
"$",
"user",
"=",
"get_userdata",
"(",
"$",
"user_id",
")",
";",
"if",
"(",
"$",
"user",
"&&",
"user_can",
"(",
"$",
"user",
",",
"'edit_posts'",
")",
")",
"{",
"$",
"slug",
"=",
"$",
"user",
"->",
"user_nicename",
";",
"$",
"name",
"=",
"trim",
"(",
"\"{$user->first_name} {$user->last_name}\"",
")",
";",
"if",
"(",
"empty",
"(",
"$",
"name",
")",
")",
"{",
"$",
"name",
"=",
"$",
"user",
"->",
"display_name",
";",
"if",
"(",
"empty",
"(",
"$",
"name",
")",
")",
"{",
"$",
"name",
"=",
"$",
"slug",
";",
"}",
"}",
"$",
"results",
"=",
"wp_insert_term",
"(",
"$",
"name",
",",
"self",
"::",
"TAXONOMY",
",",
"[",
"'slug'",
"=>",
"$",
"slug",
",",
"]",
")",
";",
"if",
"(",
"is_array",
"(",
"$",
"results",
")",
")",
"{",
"add_term_meta",
"(",
"$",
"results",
"[",
"'term_id'",
"]",
",",
"'contributor_first_name'",
",",
"$",
"user",
"->",
"first_name",
",",
"true",
")",
";",
"add_term_meta",
"(",
"$",
"results",
"[",
"'term_id'",
"]",
",",
"'contributor_last_name'",
",",
"$",
"user",
"->",
"last_name",
",",
"true",
")",
";",
"return",
"$",
"results",
";",
"}",
"}",
"return",
"false",
";",
"}"
] | Create a matching Contributor term for a given User ID. Used when a user is added to a blog.
@param int $user_id
@return array|false An array containing the `term_id` and `term_taxonomy_id`, false otherwise. | [
"Create",
"a",
"matching",
"Contributor",
"term",
"for",
"a",
"given",
"User",
"ID",
".",
"Used",
"when",
"a",
"user",
"is",
"added",
"to",
"a",
"blog",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-contributors.php#L255-L278 |
pressbooks/pressbooks | inc/class-contributors.php | Contributors.updateBlogUser | public function updateBlogUser( $user_id, $old_user_data ) {
$user = get_userdata( $user_id );
if ( $user && user_can( $user, 'edit_posts' ) ) {
$slug = $user->user_nicename;
$name = trim( "{$user->first_name} {$user->last_name}" );
if ( empty( $name ) ) {
$name = $user->display_name;
if ( empty( $name ) ) {
$name = $slug;
}
}
$term = get_term_by( 'slug', $old_user_data->user_nicename, self::TAXONOMY );
if ( $term ) {
$results = wp_update_term(
$term->term_id, self::TAXONOMY, [
'name' => $name,
'slug' => $slug,
]
);
update_term_meta( $results['term_id'], 'contributor_first_name', $user->first_name );
update_term_meta( $results['term_id'], 'contributor_last_name', $user->last_name );
} else {
$results = wp_insert_term(
$name, self::TAXONOMY, [
'slug' => $slug,
]
);
add_term_meta( $results['term_id'], 'contributor_first_name', $user->first_name, true );
add_term_meta( $results['term_id'], 'contributor_last_name', $user->last_name, true );
}
if ( is_array( $results ) ) {
return $results;
}
}
return false;
} | php | public function updateBlogUser( $user_id, $old_user_data ) {
$user = get_userdata( $user_id );
if ( $user && user_can( $user, 'edit_posts' ) ) {
$slug = $user->user_nicename;
$name = trim( "{$user->first_name} {$user->last_name}" );
if ( empty( $name ) ) {
$name = $user->display_name;
if ( empty( $name ) ) {
$name = $slug;
}
}
$term = get_term_by( 'slug', $old_user_data->user_nicename, self::TAXONOMY );
if ( $term ) {
$results = wp_update_term(
$term->term_id, self::TAXONOMY, [
'name' => $name,
'slug' => $slug,
]
);
update_term_meta( $results['term_id'], 'contributor_first_name', $user->first_name );
update_term_meta( $results['term_id'], 'contributor_last_name', $user->last_name );
} else {
$results = wp_insert_term(
$name, self::TAXONOMY, [
'slug' => $slug,
]
);
add_term_meta( $results['term_id'], 'contributor_first_name', $user->first_name, true );
add_term_meta( $results['term_id'], 'contributor_last_name', $user->last_name, true );
}
if ( is_array( $results ) ) {
return $results;
}
}
return false;
} | [
"public",
"function",
"updateBlogUser",
"(",
"$",
"user_id",
",",
"$",
"old_user_data",
")",
"{",
"$",
"user",
"=",
"get_userdata",
"(",
"$",
"user_id",
")",
";",
"if",
"(",
"$",
"user",
"&&",
"user_can",
"(",
"$",
"user",
",",
"'edit_posts'",
")",
")",
"{",
"$",
"slug",
"=",
"$",
"user",
"->",
"user_nicename",
";",
"$",
"name",
"=",
"trim",
"(",
"\"{$user->first_name} {$user->last_name}\"",
")",
";",
"if",
"(",
"empty",
"(",
"$",
"name",
")",
")",
"{",
"$",
"name",
"=",
"$",
"user",
"->",
"display_name",
";",
"if",
"(",
"empty",
"(",
"$",
"name",
")",
")",
"{",
"$",
"name",
"=",
"$",
"slug",
";",
"}",
"}",
"$",
"term",
"=",
"get_term_by",
"(",
"'slug'",
",",
"$",
"old_user_data",
"->",
"user_nicename",
",",
"self",
"::",
"TAXONOMY",
")",
";",
"if",
"(",
"$",
"term",
")",
"{",
"$",
"results",
"=",
"wp_update_term",
"(",
"$",
"term",
"->",
"term_id",
",",
"self",
"::",
"TAXONOMY",
",",
"[",
"'name'",
"=>",
"$",
"name",
",",
"'slug'",
"=>",
"$",
"slug",
",",
"]",
")",
";",
"update_term_meta",
"(",
"$",
"results",
"[",
"'term_id'",
"]",
",",
"'contributor_first_name'",
",",
"$",
"user",
"->",
"first_name",
")",
";",
"update_term_meta",
"(",
"$",
"results",
"[",
"'term_id'",
"]",
",",
"'contributor_last_name'",
",",
"$",
"user",
"->",
"last_name",
")",
";",
"}",
"else",
"{",
"$",
"results",
"=",
"wp_insert_term",
"(",
"$",
"name",
",",
"self",
"::",
"TAXONOMY",
",",
"[",
"'slug'",
"=>",
"$",
"slug",
",",
"]",
")",
";",
"add_term_meta",
"(",
"$",
"results",
"[",
"'term_id'",
"]",
",",
"'contributor_first_name'",
",",
"$",
"user",
"->",
"first_name",
",",
"true",
")",
";",
"add_term_meta",
"(",
"$",
"results",
"[",
"'term_id'",
"]",
",",
"'contributor_last_name'",
",",
"$",
"user",
"->",
"last_name",
",",
"true",
")",
";",
"}",
"if",
"(",
"is_array",
"(",
"$",
"results",
")",
")",
"{",
"return",
"$",
"results",
";",
"}",
"}",
"return",
"false",
";",
"}"
] | Update a matching Contributor term given a User ID. Used when a blog user is updated
@param int $user_id
@param \WP_User $old_user_data
@return array|false An array containing the `term_id` and `term_taxonomy_id`, false otherwise. | [
"Update",
"a",
"matching",
"Contributor",
"term",
"given",
"a",
"User",
"ID",
".",
"Used",
"when",
"a",
"blog",
"user",
"is",
"updated"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-contributors.php#L288-L323 |
pressbooks/pressbooks | inc/class-contributors.php | Contributors.personalName | public function personalName( $slug ) {
init_book_data_models();
$name = '';
$term = get_term_by( 'slug', $slug, self::TAXONOMY );
if ( $term ) {
$first_name = get_term_meta( $term->term_id, 'contributor_first_name', true );
$last_name = get_term_meta( $term->term_id, 'contributor_last_name', true );
if ( ! empty( $first_name ) && ! empty( $last_name ) ) {
$name = "{$first_name} {$last_name}";
} elseif ( ! empty( $term->name ) ) {
$name = $term->name;
}
}
return $name;
} | php | public function personalName( $slug ) {
init_book_data_models();
$name = '';
$term = get_term_by( 'slug', $slug, self::TAXONOMY );
if ( $term ) {
$first_name = get_term_meta( $term->term_id, 'contributor_first_name', true );
$last_name = get_term_meta( $term->term_id, 'contributor_last_name', true );
if ( ! empty( $first_name ) && ! empty( $last_name ) ) {
$name = "{$first_name} {$last_name}";
} elseif ( ! empty( $term->name ) ) {
$name = $term->name;
}
}
return $name;
} | [
"public",
"function",
"personalName",
"(",
"$",
"slug",
")",
"{",
"init_book_data_models",
"(",
")",
";",
"$",
"name",
"=",
"''",
";",
"$",
"term",
"=",
"get_term_by",
"(",
"'slug'",
",",
"$",
"slug",
",",
"self",
"::",
"TAXONOMY",
")",
";",
"if",
"(",
"$",
"term",
")",
"{",
"$",
"first_name",
"=",
"get_term_meta",
"(",
"$",
"term",
"->",
"term_id",
",",
"'contributor_first_name'",
",",
"true",
")",
";",
"$",
"last_name",
"=",
"get_term_meta",
"(",
"$",
"term",
"->",
"term_id",
",",
"'contributor_last_name'",
",",
"true",
")",
";",
"if",
"(",
"!",
"empty",
"(",
"$",
"first_name",
")",
"&&",
"!",
"empty",
"(",
"$",
"last_name",
")",
")",
"{",
"$",
"name",
"=",
"\"{$first_name} {$last_name}\"",
";",
"}",
"elseif",
"(",
"!",
"empty",
"(",
"$",
"term",
"->",
"name",
")",
")",
"{",
"$",
"name",
"=",
"$",
"term",
"->",
"name",
";",
"}",
"}",
"return",
"$",
"name",
";",
"}"
] | Get personal name based on available data
Returns empty string is we can't find anything useful
A personal name is the set of names by which an individual is known and that can be recited as a word-group,
with the understanding that, taken together, they all relate to that one individual. In many cultures, the
term is synonymous with the birth name or legal name of the individual.
@param string $slug
@return string | [
"Get",
"personal",
"name",
"based",
"on",
"available",
"data"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-contributors.php#L338-L352 |
pressbooks/pressbooks | inc/class-contributors.php | Contributors.convert | public function convert( $old_slug, $names, $post_id ) {
$new_slug = $this->maybeUpgradeSlug( $old_slug );
if ( $new_slug === $old_slug ) {
return false; // Nothing to convert
}
if ( ! is_array( $names ) ) {
$names = [ $names ];
}
$result = false;
foreach ( $names as $contributors ) {
$values = oxford_comma_explode( $contributors );
foreach ( $values as $v ) {
$result = $this->insert( $v, $post_id, $new_slug );
}
}
return $result;
} | php | public function convert( $old_slug, $names, $post_id ) {
$new_slug = $this->maybeUpgradeSlug( $old_slug );
if ( $new_slug === $old_slug ) {
return false; // Nothing to convert
}
if ( ! is_array( $names ) ) {
$names = [ $names ];
}
$result = false;
foreach ( $names as $contributors ) {
$values = oxford_comma_explode( $contributors );
foreach ( $values as $v ) {
$result = $this->insert( $v, $post_id, $new_slug );
}
}
return $result;
} | [
"public",
"function",
"convert",
"(",
"$",
"old_slug",
",",
"$",
"names",
",",
"$",
"post_id",
")",
"{",
"$",
"new_slug",
"=",
"$",
"this",
"->",
"maybeUpgradeSlug",
"(",
"$",
"old_slug",
")",
";",
"if",
"(",
"$",
"new_slug",
"===",
"$",
"old_slug",
")",
"{",
"return",
"false",
";",
"// Nothing to convert",
"}",
"if",
"(",
"!",
"is_array",
"(",
"$",
"names",
")",
")",
"{",
"$",
"names",
"=",
"[",
"$",
"names",
"]",
";",
"}",
"$",
"result",
"=",
"false",
";",
"foreach",
"(",
"$",
"names",
"as",
"$",
"contributors",
")",
"{",
"$",
"values",
"=",
"oxford_comma_explode",
"(",
"$",
"contributors",
")",
";",
"foreach",
"(",
"$",
"values",
"as",
"$",
"v",
")",
"{",
"$",
"result",
"=",
"$",
"this",
"->",
"insert",
"(",
"$",
"v",
",",
"$",
"post_id",
",",
"$",
"new_slug",
")",
";",
"}",
"}",
"return",
"$",
"result",
";",
"}"
] | @param string $old_slug
@param string|array $names
@param int $post_id
@return array|false An array containing `term_id` and `term_taxonomy_id`, false otherwise. | [
"@param",
"string",
"$old_slug",
"@param",
"string|array",
"$names",
"@param",
"int",
"$post_id"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-contributors.php#L381-L401 |
pressbooks/pressbooks | inc/modules/themeoptions/class-weboptions.php | WebOptions.init | function init() {
$_option = 'pressbooks_theme_options_' . $this->getSlug();
$_page = $_option;
$_section = $this->getSlug() . '_options_section';
$meta = new Metadata();
if ( false === get_option( $_option ) ) {
add_option( $_option, $this->defaults );
}
add_settings_section(
$_section,
$this->getTitle(),
[ $this, 'display' ],
$_page
);
add_settings_field(
'social_media',
__( 'Enable Social Media', 'pressbooks' ),
[ $this, 'renderSocialMediaField' ],
$_page,
$_section,
[
__( 'Add buttons to cover page and each chapter so that readers may share links to your book through social media: Facebook, Twitter, Google+', 'pressbooks' ),
]
);
add_settings_field(
'webbook_width',
__( 'Webbook Width', 'pressbooks' ),
[ $this, 'renderWebbookWidthField' ],
$_page,
$_section,
[
'30em' => __( 'Narrow', 'pressbooks' ),
'40em' => __( 'Standard', 'pressbooks' ),
'48em' => __( 'Wide', 'pressbooks' ),
'label_for' => 'webbook_width',
]
);
add_settings_field(
'paragraph_separation',
__( 'Paragraph Separation', 'pressbooks' ),
[ $this, 'renderParagraphSeparationField' ],
$_page,
$_section,
[
'indent' => __( 'Indent paragraphs', 'pressbooks' ),
'skiplines' => __( 'Skip lines between paragraphs', 'pressbooks' ),
]
);
add_settings_field(
'part_title',
__( 'Display Part Title', 'pressbooks' ),
[ $this, 'renderPartTitle' ],
$_page,
$_section,
[
__( 'Display the Part title on each chapter', 'pressbooks' ),
]
);
if ( Container::get( 'Styles' )->hasBuckram() ) {
add_settings_field(
'collapse_sections',
__( 'Collapse Sections', 'pressbooks' ),
[ $this, 'renderCollapseSections' ],
$_page,
$_section,
[
__( 'Collapse sections within front matter, chapters, and back matter', 'pressbooks' ),
]
);
}
if ( get_post_meta( $meta->getMetaPostId(), 'pb_is_based_on', true ) ) {
add_settings_field(
'enable_source_comparison',
__( 'Enable Source Comparison', 'pressbooks' ),
[ $this, 'renderEnableSourceComparison' ],
$_page,
$_section,
[
__( 'Add comparison tool to the end of each front matter, part, chapter, and back matter', 'pressbooks' ),
__( 'Allows readers to compare content with the original book from which it was cloned.', 'pressbooks' ),
]
);
}
/**
* Add custom settings fields.
*
* @since 3.9.7
*
* @param string $arg1
* @param string $arg2
*/
do_action( 'pb_theme_options_web_add_settings_fields', $_page, $_section );
register_setting(
$_page,
$_option,
[ $this, 'sanitize' ]
);
} | php | function init() {
$_option = 'pressbooks_theme_options_' . $this->getSlug();
$_page = $_option;
$_section = $this->getSlug() . '_options_section';
$meta = new Metadata();
if ( false === get_option( $_option ) ) {
add_option( $_option, $this->defaults );
}
add_settings_section(
$_section,
$this->getTitle(),
[ $this, 'display' ],
$_page
);
add_settings_field(
'social_media',
__( 'Enable Social Media', 'pressbooks' ),
[ $this, 'renderSocialMediaField' ],
$_page,
$_section,
[
__( 'Add buttons to cover page and each chapter so that readers may share links to your book through social media: Facebook, Twitter, Google+', 'pressbooks' ),
]
);
add_settings_field(
'webbook_width',
__( 'Webbook Width', 'pressbooks' ),
[ $this, 'renderWebbookWidthField' ],
$_page,
$_section,
[
'30em' => __( 'Narrow', 'pressbooks' ),
'40em' => __( 'Standard', 'pressbooks' ),
'48em' => __( 'Wide', 'pressbooks' ),
'label_for' => 'webbook_width',
]
);
add_settings_field(
'paragraph_separation',
__( 'Paragraph Separation', 'pressbooks' ),
[ $this, 'renderParagraphSeparationField' ],
$_page,
$_section,
[
'indent' => __( 'Indent paragraphs', 'pressbooks' ),
'skiplines' => __( 'Skip lines between paragraphs', 'pressbooks' ),
]
);
add_settings_field(
'part_title',
__( 'Display Part Title', 'pressbooks' ),
[ $this, 'renderPartTitle' ],
$_page,
$_section,
[
__( 'Display the Part title on each chapter', 'pressbooks' ),
]
);
if ( Container::get( 'Styles' )->hasBuckram() ) {
add_settings_field(
'collapse_sections',
__( 'Collapse Sections', 'pressbooks' ),
[ $this, 'renderCollapseSections' ],
$_page,
$_section,
[
__( 'Collapse sections within front matter, chapters, and back matter', 'pressbooks' ),
]
);
}
if ( get_post_meta( $meta->getMetaPostId(), 'pb_is_based_on', true ) ) {
add_settings_field(
'enable_source_comparison',
__( 'Enable Source Comparison', 'pressbooks' ),
[ $this, 'renderEnableSourceComparison' ],
$_page,
$_section,
[
__( 'Add comparison tool to the end of each front matter, part, chapter, and back matter', 'pressbooks' ),
__( 'Allows readers to compare content with the original book from which it was cloned.', 'pressbooks' ),
]
);
}
/**
* Add custom settings fields.
*
* @since 3.9.7
*
* @param string $arg1
* @param string $arg2
*/
do_action( 'pb_theme_options_web_add_settings_fields', $_page, $_section );
register_setting(
$_page,
$_option,
[ $this, 'sanitize' ]
);
} | [
"function",
"init",
"(",
")",
"{",
"$",
"_option",
"=",
"'pressbooks_theme_options_'",
".",
"$",
"this",
"->",
"getSlug",
"(",
")",
";",
"$",
"_page",
"=",
"$",
"_option",
";",
"$",
"_section",
"=",
"$",
"this",
"->",
"getSlug",
"(",
")",
".",
"'_options_section'",
";",
"$",
"meta",
"=",
"new",
"Metadata",
"(",
")",
";",
"if",
"(",
"false",
"===",
"get_option",
"(",
"$",
"_option",
")",
")",
"{",
"add_option",
"(",
"$",
"_option",
",",
"$",
"this",
"->",
"defaults",
")",
";",
"}",
"add_settings_section",
"(",
"$",
"_section",
",",
"$",
"this",
"->",
"getTitle",
"(",
")",
",",
"[",
"$",
"this",
",",
"'display'",
"]",
",",
"$",
"_page",
")",
";",
"add_settings_field",
"(",
"'social_media'",
",",
"__",
"(",
"'Enable Social Media'",
",",
"'pressbooks'",
")",
",",
"[",
"$",
"this",
",",
"'renderSocialMediaField'",
"]",
",",
"$",
"_page",
",",
"$",
"_section",
",",
"[",
"__",
"(",
"'Add buttons to cover page and each chapter so that readers may share links to your book through social media: Facebook, Twitter, Google+'",
",",
"'pressbooks'",
")",
",",
"]",
")",
";",
"add_settings_field",
"(",
"'webbook_width'",
",",
"__",
"(",
"'Webbook Width'",
",",
"'pressbooks'",
")",
",",
"[",
"$",
"this",
",",
"'renderWebbookWidthField'",
"]",
",",
"$",
"_page",
",",
"$",
"_section",
",",
"[",
"'30em'",
"=>",
"__",
"(",
"'Narrow'",
",",
"'pressbooks'",
")",
",",
"'40em'",
"=>",
"__",
"(",
"'Standard'",
",",
"'pressbooks'",
")",
",",
"'48em'",
"=>",
"__",
"(",
"'Wide'",
",",
"'pressbooks'",
")",
",",
"'label_for'",
"=>",
"'webbook_width'",
",",
"]",
")",
";",
"add_settings_field",
"(",
"'paragraph_separation'",
",",
"__",
"(",
"'Paragraph Separation'",
",",
"'pressbooks'",
")",
",",
"[",
"$",
"this",
",",
"'renderParagraphSeparationField'",
"]",
",",
"$",
"_page",
",",
"$",
"_section",
",",
"[",
"'indent'",
"=>",
"__",
"(",
"'Indent paragraphs'",
",",
"'pressbooks'",
")",
",",
"'skiplines'",
"=>",
"__",
"(",
"'Skip lines between paragraphs'",
",",
"'pressbooks'",
")",
",",
"]",
")",
";",
"add_settings_field",
"(",
"'part_title'",
",",
"__",
"(",
"'Display Part Title'",
",",
"'pressbooks'",
")",
",",
"[",
"$",
"this",
",",
"'renderPartTitle'",
"]",
",",
"$",
"_page",
",",
"$",
"_section",
",",
"[",
"__",
"(",
"'Display the Part title on each chapter'",
",",
"'pressbooks'",
")",
",",
"]",
")",
";",
"if",
"(",
"Container",
"::",
"get",
"(",
"'Styles'",
")",
"->",
"hasBuckram",
"(",
")",
")",
"{",
"add_settings_field",
"(",
"'collapse_sections'",
",",
"__",
"(",
"'Collapse Sections'",
",",
"'pressbooks'",
")",
",",
"[",
"$",
"this",
",",
"'renderCollapseSections'",
"]",
",",
"$",
"_page",
",",
"$",
"_section",
",",
"[",
"__",
"(",
"'Collapse sections within front matter, chapters, and back matter'",
",",
"'pressbooks'",
")",
",",
"]",
")",
";",
"}",
"if",
"(",
"get_post_meta",
"(",
"$",
"meta",
"->",
"getMetaPostId",
"(",
")",
",",
"'pb_is_based_on'",
",",
"true",
")",
")",
"{",
"add_settings_field",
"(",
"'enable_source_comparison'",
",",
"__",
"(",
"'Enable Source Comparison'",
",",
"'pressbooks'",
")",
",",
"[",
"$",
"this",
",",
"'renderEnableSourceComparison'",
"]",
",",
"$",
"_page",
",",
"$",
"_section",
",",
"[",
"__",
"(",
"'Add comparison tool to the end of each front matter, part, chapter, and back matter'",
",",
"'pressbooks'",
")",
",",
"__",
"(",
"'Allows readers to compare content with the original book from which it was cloned.'",
",",
"'pressbooks'",
")",
",",
"]",
")",
";",
"}",
"/**\n\t\t * Add custom settings fields.\n\t\t *\n\t\t * @since 3.9.7\n\t\t *\n\t\t * @param string $arg1\n\t\t * @param string $arg2\n\t\t */",
"do_action",
"(",
"'pb_theme_options_web_add_settings_fields'",
",",
"$",
"_page",
",",
"$",
"_section",
")",
";",
"register_setting",
"(",
"$",
"_page",
",",
"$",
"_option",
",",
"[",
"$",
"this",
",",
"'sanitize'",
"]",
")",
";",
"}"
] | Configure the web options tab using the settings API. | [
"Configure",
"the",
"web",
"options",
"tab",
"using",
"the",
"settings",
"API",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/themeoptions/class-weboptions.php#L60-L167 |
pressbooks/pressbooks | inc/modules/themeoptions/class-weboptions.php | WebOptions.doInitialUpgrade | function doInitialUpgrade() {
$_option = $this->getSlug();
$options = get_option( 'pressbooks_theme_options_' . $_option, $this->defaults );
$deprecated = [
'toc_collapse',
'accessibility_fontsize',
];
foreach ( $options as $key => $value ) {
if ( in_array( $key, $deprecated, true ) ) {
unset( $options[ $key ] );
}
}
update_option( 'pressbooks_theme_options_' . $_option, $options );
} | php | function doInitialUpgrade() {
$_option = $this->getSlug();
$options = get_option( 'pressbooks_theme_options_' . $_option, $this->defaults );
$deprecated = [
'toc_collapse',
'accessibility_fontsize',
];
foreach ( $options as $key => $value ) {
if ( in_array( $key, $deprecated, true ) ) {
unset( $options[ $key ] );
}
}
update_option( 'pressbooks_theme_options_' . $_option, $options );
} | [
"function",
"doInitialUpgrade",
"(",
")",
"{",
"$",
"_option",
"=",
"$",
"this",
"->",
"getSlug",
"(",
")",
";",
"$",
"options",
"=",
"get_option",
"(",
"'pressbooks_theme_options_'",
".",
"$",
"_option",
",",
"$",
"this",
"->",
"defaults",
")",
";",
"$",
"deprecated",
"=",
"[",
"'toc_collapse'",
",",
"'accessibility_fontsize'",
",",
"]",
";",
"foreach",
"(",
"$",
"options",
"as",
"$",
"key",
"=>",
"$",
"value",
")",
"{",
"if",
"(",
"in_array",
"(",
"$",
"key",
",",
"$",
"deprecated",
",",
"true",
")",
")",
"{",
"unset",
"(",
"$",
"options",
"[",
"$",
"key",
"]",
")",
";",
"}",
"}",
"update_option",
"(",
"'pressbooks_theme_options_'",
".",
"$",
"_option",
",",
"$",
"options",
")",
";",
"}"
] | Remove deprecated keys from web options. | [
"Remove",
"deprecated",
"keys",
"from",
"web",
"options",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/themeoptions/class-weboptions.php#L196-L211 |
pressbooks/pressbooks | inc/modules/themeoptions/class-weboptions.php | WebOptions.renderSocialMediaField | function renderSocialMediaField( $args ) {
unset( $args['label_for'], $args['class'] );
$this->renderCheckbox(
[
'id' => 'social_media',
'name' => 'pressbooks_theme_options_' . $this->getSlug(),
'option' => 'social_media',
'value' => ( isset( $this->options['social_media'] ) ) ? $this->options['social_media'] : '',
'label' => $args[0],
]
);
} | php | function renderSocialMediaField( $args ) {
unset( $args['label_for'], $args['class'] );
$this->renderCheckbox(
[
'id' => 'social_media',
'name' => 'pressbooks_theme_options_' . $this->getSlug(),
'option' => 'social_media',
'value' => ( isset( $this->options['social_media'] ) ) ? $this->options['social_media'] : '',
'label' => $args[0],
]
);
} | [
"function",
"renderSocialMediaField",
"(",
"$",
"args",
")",
"{",
"unset",
"(",
"$",
"args",
"[",
"'label_for'",
"]",
",",
"$",
"args",
"[",
"'class'",
"]",
")",
";",
"$",
"this",
"->",
"renderCheckbox",
"(",
"[",
"'id'",
"=>",
"'social_media'",
",",
"'name'",
"=>",
"'pressbooks_theme_options_'",
".",
"$",
"this",
"->",
"getSlug",
"(",
")",
",",
"'option'",
"=>",
"'social_media'",
",",
"'value'",
"=>",
"(",
"isset",
"(",
"$",
"this",
"->",
"options",
"[",
"'social_media'",
"]",
")",
")",
"?",
"$",
"this",
"->",
"options",
"[",
"'social_media'",
"]",
":",
"''",
",",
"'label'",
"=>",
"$",
"args",
"[",
"0",
"]",
",",
"]",
")",
";",
"}"
] | Render the social_media checkbox.
@param array $args | [
"Render",
"the",
"social_media",
"checkbox",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/themeoptions/class-weboptions.php#L218-L229 |
pressbooks/pressbooks | inc/modules/themeoptions/class-weboptions.php | WebOptions.renderWebbookWidthField | function renderWebbookWidthField( $args ) {
unset( $args['label_for'], $args['class'] );
$this->renderSelect(
[
'id' => 'webbook_width',
'name' => 'pressbooks_theme_options_' . $this->getSlug(),
'option' => 'webbook_width',
'value' => ( isset( $this->options['webbook_width'] ) ) ? $this->options['webbook_width'] : $this->defaults['webbook_width'],
'choices' => $args,
]
);
} | php | function renderWebbookWidthField( $args ) {
unset( $args['label_for'], $args['class'] );
$this->renderSelect(
[
'id' => 'webbook_width',
'name' => 'pressbooks_theme_options_' . $this->getSlug(),
'option' => 'webbook_width',
'value' => ( isset( $this->options['webbook_width'] ) ) ? $this->options['webbook_width'] : $this->defaults['webbook_width'],
'choices' => $args,
]
);
} | [
"function",
"renderWebbookWidthField",
"(",
"$",
"args",
")",
"{",
"unset",
"(",
"$",
"args",
"[",
"'label_for'",
"]",
",",
"$",
"args",
"[",
"'class'",
"]",
")",
";",
"$",
"this",
"->",
"renderSelect",
"(",
"[",
"'id'",
"=>",
"'webbook_width'",
",",
"'name'",
"=>",
"'pressbooks_theme_options_'",
".",
"$",
"this",
"->",
"getSlug",
"(",
")",
",",
"'option'",
"=>",
"'webbook_width'",
",",
"'value'",
"=>",
"(",
"isset",
"(",
"$",
"this",
"->",
"options",
"[",
"'webbook_width'",
"]",
")",
")",
"?",
"$",
"this",
"->",
"options",
"[",
"'webbook_width'",
"]",
":",
"$",
"this",
"->",
"defaults",
"[",
"'webbook_width'",
"]",
",",
"'choices'",
"=>",
"$",
"args",
",",
"]",
")",
";",
"}"
] | Render the webbook_width dropdown.
@param array $args | [
"Render",
"the",
"webbook_width",
"dropdown",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/themeoptions/class-weboptions.php#L236-L247 |
pressbooks/pressbooks | inc/modules/themeoptions/class-weboptions.php | WebOptions.renderPartTitle | function renderPartTitle( $args ) {
unset( $args['label_for'], $args['class'] );
$this->renderCheckbox(
[
'id' => 'part_title',
'name' => 'pressbooks_theme_options_' . $this->getSlug(),
'option' => 'part_title',
'value' => ( isset( $this->options['part_title'] ) ) ? $this->options['part_title'] : '',
'label' => $args[0],
]
);
} | php | function renderPartTitle( $args ) {
unset( $args['label_for'], $args['class'] );
$this->renderCheckbox(
[
'id' => 'part_title',
'name' => 'pressbooks_theme_options_' . $this->getSlug(),
'option' => 'part_title',
'value' => ( isset( $this->options['part_title'] ) ) ? $this->options['part_title'] : '',
'label' => $args[0],
]
);
} | [
"function",
"renderPartTitle",
"(",
"$",
"args",
")",
"{",
"unset",
"(",
"$",
"args",
"[",
"'label_for'",
"]",
",",
"$",
"args",
"[",
"'class'",
"]",
")",
";",
"$",
"this",
"->",
"renderCheckbox",
"(",
"[",
"'id'",
"=>",
"'part_title'",
",",
"'name'",
"=>",
"'pressbooks_theme_options_'",
".",
"$",
"this",
"->",
"getSlug",
"(",
")",
",",
"'option'",
"=>",
"'part_title'",
",",
"'value'",
"=>",
"(",
"isset",
"(",
"$",
"this",
"->",
"options",
"[",
"'part_title'",
"]",
")",
")",
"?",
"$",
"this",
"->",
"options",
"[",
"'part_title'",
"]",
":",
"''",
",",
"'label'",
"=>",
"$",
"args",
"[",
"0",
"]",
",",
"]",
")",
";",
"}"
] | Render the part_title checkbox.
@param array $args | [
"Render",
"the",
"part_title",
"checkbox",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/themeoptions/class-weboptions.php#L272-L283 |
pressbooks/pressbooks | inc/modules/themeoptions/class-weboptions.php | WebOptions.renderCollapseSections | function renderCollapseSections( $args ) {
unset( $args['label_for'], $args['class'] );
$this->renderCheckbox(
[
'id' => 'collapse_sections',
'name' => 'pressbooks_theme_options_' . $this->getSlug(),
'option' => 'collapse_sections',
'value' => ( isset( $this->options['part_title'] ) ) ? $this->options['collapse_sections'] : '',
'label' => $args[0],
]
);
} | php | function renderCollapseSections( $args ) {
unset( $args['label_for'], $args['class'] );
$this->renderCheckbox(
[
'id' => 'collapse_sections',
'name' => 'pressbooks_theme_options_' . $this->getSlug(),
'option' => 'collapse_sections',
'value' => ( isset( $this->options['part_title'] ) ) ? $this->options['collapse_sections'] : '',
'label' => $args[0],
]
);
} | [
"function",
"renderCollapseSections",
"(",
"$",
"args",
")",
"{",
"unset",
"(",
"$",
"args",
"[",
"'label_for'",
"]",
",",
"$",
"args",
"[",
"'class'",
"]",
")",
";",
"$",
"this",
"->",
"renderCheckbox",
"(",
"[",
"'id'",
"=>",
"'collapse_sections'",
",",
"'name'",
"=>",
"'pressbooks_theme_options_'",
".",
"$",
"this",
"->",
"getSlug",
"(",
")",
",",
"'option'",
"=>",
"'collapse_sections'",
",",
"'value'",
"=>",
"(",
"isset",
"(",
"$",
"this",
"->",
"options",
"[",
"'part_title'",
"]",
")",
")",
"?",
"$",
"this",
"->",
"options",
"[",
"'collapse_sections'",
"]",
":",
"''",
",",
"'label'",
"=>",
"$",
"args",
"[",
"0",
"]",
",",
"]",
")",
";",
"}"
] | Render the collapse_sections checkbox.
@param array $args | [
"Render",
"the",
"collapse_sections",
"checkbox",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/themeoptions/class-weboptions.php#L290-L301 |
pressbooks/pressbooks | inc/modules/themeoptions/class-weboptions.php | WebOptions.renderEnableSourceComparison | function renderEnableSourceComparison( $args ) {
unset( $args['label_for'], $args['class'] );
$this->renderCheckbox(
[
'id' => 'enable_source_comparison',
'name' => 'pressbooks_theme_options_' . $this->getSlug(),
'option' => 'enable_source_comparison',
'value' => ( isset( $this->options['enable_source_comparison'] ) ) ? $this->options['enable_source_comparison'] : '',
'label' => $args[0],
'description' => $args[1],
]
);
} | php | function renderEnableSourceComparison( $args ) {
unset( $args['label_for'], $args['class'] );
$this->renderCheckbox(
[
'id' => 'enable_source_comparison',
'name' => 'pressbooks_theme_options_' . $this->getSlug(),
'option' => 'enable_source_comparison',
'value' => ( isset( $this->options['enable_source_comparison'] ) ) ? $this->options['enable_source_comparison'] : '',
'label' => $args[0],
'description' => $args[1],
]
);
} | [
"function",
"renderEnableSourceComparison",
"(",
"$",
"args",
")",
"{",
"unset",
"(",
"$",
"args",
"[",
"'label_for'",
"]",
",",
"$",
"args",
"[",
"'class'",
"]",
")",
";",
"$",
"this",
"->",
"renderCheckbox",
"(",
"[",
"'id'",
"=>",
"'enable_source_comparison'",
",",
"'name'",
"=>",
"'pressbooks_theme_options_'",
".",
"$",
"this",
"->",
"getSlug",
"(",
")",
",",
"'option'",
"=>",
"'enable_source_comparison'",
",",
"'value'",
"=>",
"(",
"isset",
"(",
"$",
"this",
"->",
"options",
"[",
"'enable_source_comparison'",
"]",
")",
")",
"?",
"$",
"this",
"->",
"options",
"[",
"'enable_source_comparison'",
"]",
":",
"''",
",",
"'label'",
"=>",
"$",
"args",
"[",
"0",
"]",
",",
"'description'",
"=>",
"$",
"args",
"[",
"1",
"]",
",",
"]",
")",
";",
"}"
] | Render the allow_comparison checkbox.
@param array $args | [
"Render",
"the",
"allow_comparison",
"checkbox",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/themeoptions/class-weboptions.php#L308-L320 |
pressbooks/pressbooks | inc/modules/themeoptions/class-weboptions.php | WebOptions.scssOverrides | static function scssOverrides( $scss ) {
$styles = Container::get( 'Styles' );
$v2_compatible = $styles->isCurrentThemeCompatible( 2 );
// Global Options
$options = get_option( 'pressbooks_theme_options_global' );
// Textbox colours.
if ( $v2_compatible ) {
foreach ( [
'edu_textbox_examples_header_color' => 'examples-header-color',
'edu_textbox_examples_header_background' => 'examples-header-background',
'edu_textbox_examples_background' => 'examples-background',
'edu_textbox_exercises_header_color' => 'exercises-header-color',
'edu_textbox_exercises_header_background' => 'exercises-header-background',
'edu_textbox_exercises_background' => 'exercises-background',
'edu_textbox_objectives_header_color' => 'learning-objectives-header-color',
'edu_textbox_objectives_header_background' => 'learning-objectives-header-background',
'edu_textbox_objectives_background' => 'learning-objectives-background',
'edu_textbox_takeaways_header_color' => 'key-takeaways-header-color',
'edu_textbox_takeaways_header_background' => 'key-takeaways-header-background',
'edu_textbox_takeaways_background' => 'key-takeaways-background',
] as $option => $variable ) {
if ( isset( $options[ $option ] ) ) {
$styles->getSass()->setVariables(
[
"$variable" => $options[ $option ],
]
);
}
}
}
$options = get_option( 'pressbooks_theme_options_web' );
$paragraph_separation = $options['paragraph_separation'] ?? 'skiplines';
if ( 'indent' === $paragraph_separation ) {
if ( $v2_compatible ) {
$styles->getSass()->setVariables(
[
'para-margin-top' => '0',
'para-indent' => '1em',
]
);
} else {
$scss .= "#content * + p { text-indent: 1em; margin-top: 0; margin-bottom: 0; } \n";
}
} elseif ( 'skiplines' === $paragraph_separation ) {
if ( $v2_compatible ) {
$styles->getSass()->setVariables(
[
'para-margin-top' => '1em',
'para-indent' => '0',
]
);
} else {
$scss .= "#content p + p { text-indent: 0em; margin-top: 1em; } \n";
}
}
return $scss;
} | php | static function scssOverrides( $scss ) {
$styles = Container::get( 'Styles' );
$v2_compatible = $styles->isCurrentThemeCompatible( 2 );
// Global Options
$options = get_option( 'pressbooks_theme_options_global' );
// Textbox colours.
if ( $v2_compatible ) {
foreach ( [
'edu_textbox_examples_header_color' => 'examples-header-color',
'edu_textbox_examples_header_background' => 'examples-header-background',
'edu_textbox_examples_background' => 'examples-background',
'edu_textbox_exercises_header_color' => 'exercises-header-color',
'edu_textbox_exercises_header_background' => 'exercises-header-background',
'edu_textbox_exercises_background' => 'exercises-background',
'edu_textbox_objectives_header_color' => 'learning-objectives-header-color',
'edu_textbox_objectives_header_background' => 'learning-objectives-header-background',
'edu_textbox_objectives_background' => 'learning-objectives-background',
'edu_textbox_takeaways_header_color' => 'key-takeaways-header-color',
'edu_textbox_takeaways_header_background' => 'key-takeaways-header-background',
'edu_textbox_takeaways_background' => 'key-takeaways-background',
] as $option => $variable ) {
if ( isset( $options[ $option ] ) ) {
$styles->getSass()->setVariables(
[
"$variable" => $options[ $option ],
]
);
}
}
}
$options = get_option( 'pressbooks_theme_options_web' );
$paragraph_separation = $options['paragraph_separation'] ?? 'skiplines';
if ( 'indent' === $paragraph_separation ) {
if ( $v2_compatible ) {
$styles->getSass()->setVariables(
[
'para-margin-top' => '0',
'para-indent' => '1em',
]
);
} else {
$scss .= "#content * + p { text-indent: 1em; margin-top: 0; margin-bottom: 0; } \n";
}
} elseif ( 'skiplines' === $paragraph_separation ) {
if ( $v2_compatible ) {
$styles->getSass()->setVariables(
[
'para-margin-top' => '1em',
'para-indent' => '0',
]
);
} else {
$scss .= "#content p + p { text-indent: 0em; margin-top: 1em; } \n";
}
}
return $scss;
} | [
"static",
"function",
"scssOverrides",
"(",
"$",
"scss",
")",
"{",
"$",
"styles",
"=",
"Container",
"::",
"get",
"(",
"'Styles'",
")",
";",
"$",
"v2_compatible",
"=",
"$",
"styles",
"->",
"isCurrentThemeCompatible",
"(",
"2",
")",
";",
"// Global Options",
"$",
"options",
"=",
"get_option",
"(",
"'pressbooks_theme_options_global'",
")",
";",
"// Textbox colours.",
"if",
"(",
"$",
"v2_compatible",
")",
"{",
"foreach",
"(",
"[",
"'edu_textbox_examples_header_color'",
"=>",
"'examples-header-color'",
",",
"'edu_textbox_examples_header_background'",
"=>",
"'examples-header-background'",
",",
"'edu_textbox_examples_background'",
"=>",
"'examples-background'",
",",
"'edu_textbox_exercises_header_color'",
"=>",
"'exercises-header-color'",
",",
"'edu_textbox_exercises_header_background'",
"=>",
"'exercises-header-background'",
",",
"'edu_textbox_exercises_background'",
"=>",
"'exercises-background'",
",",
"'edu_textbox_objectives_header_color'",
"=>",
"'learning-objectives-header-color'",
",",
"'edu_textbox_objectives_header_background'",
"=>",
"'learning-objectives-header-background'",
",",
"'edu_textbox_objectives_background'",
"=>",
"'learning-objectives-background'",
",",
"'edu_textbox_takeaways_header_color'",
"=>",
"'key-takeaways-header-color'",
",",
"'edu_textbox_takeaways_header_background'",
"=>",
"'key-takeaways-header-background'",
",",
"'edu_textbox_takeaways_background'",
"=>",
"'key-takeaways-background'",
",",
"]",
"as",
"$",
"option",
"=>",
"$",
"variable",
")",
"{",
"if",
"(",
"isset",
"(",
"$",
"options",
"[",
"$",
"option",
"]",
")",
")",
"{",
"$",
"styles",
"->",
"getSass",
"(",
")",
"->",
"setVariables",
"(",
"[",
"\"$variable\"",
"=>",
"$",
"options",
"[",
"$",
"option",
"]",
",",
"]",
")",
";",
"}",
"}",
"}",
"$",
"options",
"=",
"get_option",
"(",
"'pressbooks_theme_options_web'",
")",
";",
"$",
"paragraph_separation",
"=",
"$",
"options",
"[",
"'paragraph_separation'",
"]",
"??",
"'skiplines'",
";",
"if",
"(",
"'indent'",
"===",
"$",
"paragraph_separation",
")",
"{",
"if",
"(",
"$",
"v2_compatible",
")",
"{",
"$",
"styles",
"->",
"getSass",
"(",
")",
"->",
"setVariables",
"(",
"[",
"'para-margin-top'",
"=>",
"'0'",
",",
"'para-indent'",
"=>",
"'1em'",
",",
"]",
")",
";",
"}",
"else",
"{",
"$",
"scss",
".=",
"\"#content * + p { text-indent: 1em; margin-top: 0; margin-bottom: 0; } \\n\"",
";",
"}",
"}",
"elseif",
"(",
"'skiplines'",
"===",
"$",
"paragraph_separation",
")",
"{",
"if",
"(",
"$",
"v2_compatible",
")",
"{",
"$",
"styles",
"->",
"getSass",
"(",
")",
"->",
"setVariables",
"(",
"[",
"'para-margin-top'",
"=>",
"'1em'",
",",
"'para-indent'",
"=>",
"'0'",
",",
"]",
")",
";",
"}",
"else",
"{",
"$",
"scss",
".=",
"\"#content p + p { text-indent: 0em; margin-top: 1em; } \\n\"",
";",
"}",
"}",
"return",
"$",
"scss",
";",
"}"
] | Apply overrides.
@param string $scss
@return string
@since 3.9.8 | [
"Apply",
"overrides",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/themeoptions/class-weboptions.php#L475-L539 |
pressbooks/pressbooks | inc/modules/export/prince/class-pdf.php | Pdf.convert | 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
$prince = new \PrinceXMLPhp\PrinceWrapper( PB_PRINCE_COMMAND );
$prince->setHTML( true );
$prince->setCompress( true );
$prince->setHttpTimeout( max( ini_get( 'max_execution_time' ), 30 ) );
if ( defined( 'WP_ENV' ) && ( WP_ENV === 'development' ) ) {
$prince->setInsecure( true );
}
if ( $this->pdfProfile && $this->pdfOutputIntent ) {
$prince->setPDFProfile( $this->pdfProfile );
$prince->setPDFOutputIntent( $this->pdfOutputIntent );
}
$prince->addStyleSheet( $css_file );
if ( $this->exportScriptPath ) {
$prince->addScript( $this->exportScriptPath );
}
$prince->setLog( $this->logfile );
$retval = $prince->convert_file_to_file( $this->url, $this->outputPath, $msg );
// Prince XML is very flexible. There could be errors but Prince will still render a PDF.
// We want to log those errors but we won't alert the user.
if ( is_countable( $msg ) && count( $msg ) ) {
$this->logError( \Pressbooks\Utility\get_contents( $this->logfile ), [ 'warning' => 1 ] );
}
return $retval;
} | php | 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
$prince = new \PrinceXMLPhp\PrinceWrapper( PB_PRINCE_COMMAND );
$prince->setHTML( true );
$prince->setCompress( true );
$prince->setHttpTimeout( max( ini_get( 'max_execution_time' ), 30 ) );
if ( defined( 'WP_ENV' ) && ( WP_ENV === 'development' ) ) {
$prince->setInsecure( true );
}
if ( $this->pdfProfile && $this->pdfOutputIntent ) {
$prince->setPDFProfile( $this->pdfProfile );
$prince->setPDFOutputIntent( $this->pdfOutputIntent );
}
$prince->addStyleSheet( $css_file );
if ( $this->exportScriptPath ) {
$prince->addScript( $this->exportScriptPath );
}
$prince->setLog( $this->logfile );
$retval = $prince->convert_file_to_file( $this->url, $this->outputPath, $msg );
// Prince XML is very flexible. There could be errors but Prince will still render a PDF.
// We want to log those errors but we won't alert the user.
if ( is_countable( $msg ) && count( $msg ) ) {
$this->logError( \Pressbooks\Utility\get_contents( $this->logfile ), [ 'warning' => 1 ] );
}
return $retval;
} | [
"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",
"$",
"prince",
"=",
"new",
"\\",
"PrinceXMLPhp",
"\\",
"PrinceWrapper",
"(",
"PB_PRINCE_COMMAND",
")",
";",
"$",
"prince",
"->",
"setHTML",
"(",
"true",
")",
";",
"$",
"prince",
"->",
"setCompress",
"(",
"true",
")",
";",
"$",
"prince",
"->",
"setHttpTimeout",
"(",
"max",
"(",
"ini_get",
"(",
"'max_execution_time'",
")",
",",
"30",
")",
")",
";",
"if",
"(",
"defined",
"(",
"'WP_ENV'",
")",
"&&",
"(",
"WP_ENV",
"===",
"'development'",
")",
")",
"{",
"$",
"prince",
"->",
"setInsecure",
"(",
"true",
")",
";",
"}",
"if",
"(",
"$",
"this",
"->",
"pdfProfile",
"&&",
"$",
"this",
"->",
"pdfOutputIntent",
")",
"{",
"$",
"prince",
"->",
"setPDFProfile",
"(",
"$",
"this",
"->",
"pdfProfile",
")",
";",
"$",
"prince",
"->",
"setPDFOutputIntent",
"(",
"$",
"this",
"->",
"pdfOutputIntent",
")",
";",
"}",
"$",
"prince",
"->",
"addStyleSheet",
"(",
"$",
"css_file",
")",
";",
"if",
"(",
"$",
"this",
"->",
"exportScriptPath",
")",
"{",
"$",
"prince",
"->",
"addScript",
"(",
"$",
"this",
"->",
"exportScriptPath",
")",
";",
"}",
"$",
"prince",
"->",
"setLog",
"(",
"$",
"this",
"->",
"logfile",
")",
";",
"$",
"retval",
"=",
"$",
"prince",
"->",
"convert_file_to_file",
"(",
"$",
"this",
"->",
"url",
",",
"$",
"this",
"->",
"outputPath",
",",
"$",
"msg",
")",
";",
"// Prince XML is very flexible. There could be errors but Prince will still render a PDF.",
"// We want to log those errors but we won't alert the user.",
"if",
"(",
"is_countable",
"(",
"$",
"msg",
")",
"&&",
"count",
"(",
"$",
"msg",
")",
")",
"{",
"$",
"this",
"->",
"logError",
"(",
"\\",
"Pressbooks",
"\\",
"Utility",
"\\",
"get_contents",
"(",
"$",
"this",
"->",
"logfile",
")",
",",
"[",
"'warning'",
"=>",
"1",
"]",
")",
";",
"}",
"return",
"$",
"retval",
";",
"}"
] | Create $this->outputPath
@return bool | [
"Create",
"$this",
"-",
">",
"outputPath"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/export/prince/class-pdf.php#L94-L148 |
pressbooks/pressbooks | inc/modules/export/prince/class-pdf.php | Pdf.kneadCss | protected function kneadCss() {
$styles = Container::get( 'Styles' );
$scss = \Pressbooks\Utility\get_contents( $this->exportStylePath );
$custom_styles = $styles->getPrincePost();
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 = $styles->customize( 'prince', $scss, $this->cssOverrides );
$css = normalize_css_urls( $css, $this->urlPath() );
if ( WP_DEBUG ) {
Container::get( 'Sass' )->debug( $css, $scss, 'prince' );
}
return $css;
} | php | protected function kneadCss() {
$styles = Container::get( 'Styles' );
$scss = \Pressbooks\Utility\get_contents( $this->exportStylePath );
$custom_styles = $styles->getPrincePost();
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 = $styles->customize( 'prince', $scss, $this->cssOverrides );
$css = normalize_css_urls( $css, $this->urlPath() );
if ( WP_DEBUG ) {
Container::get( 'Sass' )->debug( $css, $scss, 'prince' );
}
return $css;
} | [
"protected",
"function",
"kneadCss",
"(",
")",
"{",
"$",
"styles",
"=",
"Container",
"::",
"get",
"(",
"'Styles'",
")",
";",
"$",
"scss",
"=",
"\\",
"Pressbooks",
"\\",
"Utility",
"\\",
"get_contents",
"(",
"$",
"this",
"->",
"exportStylePath",
")",
";",
"$",
"custom_styles",
"=",
"$",
"styles",
"->",
"getPrincePost",
"(",
")",
";",
"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",
"=",
"$",
"styles",
"->",
"customize",
"(",
"'prince'",
",",
"$",
"scss",
",",
"$",
"this",
"->",
"cssOverrides",
")",
";",
"$",
"css",
"=",
"normalize_css_urls",
"(",
"$",
"css",
",",
"$",
"this",
"->",
"urlPath",
"(",
")",
")",
";",
"if",
"(",
"WP_DEBUG",
")",
"{",
"Container",
"::",
"get",
"(",
"'Sass'",
")",
"->",
"debug",
"(",
"$",
"css",
",",
"$",
"scss",
",",
"'prince'",
")",
";",
"}",
"return",
"$",
"css",
";",
"}"
] | Return kneaded CSS string
@return string | [
"Return",
"kneaded",
"CSS",
"string"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/export/prince/class-pdf.php#L224-L245 |
pressbooks/pressbooks | inc/modules/export/prince/class-pdf.php | Pdf.urlPath | protected function urlPath() {
$dir = str_replace( Container::get( 'Styles' )->getDir(), '', pathinfo( $this->exportStylePath, PATHINFO_DIRNAME ) );
$dir = ltrim( $dir, '/' );
$url_path = trailingslashit( get_stylesheet_directory_uri() ) . $dir;
$url_path = set_url_scheme( $url_path );
return $url_path;
} | php | protected function urlPath() {
$dir = str_replace( Container::get( 'Styles' )->getDir(), '', pathinfo( $this->exportStylePath, PATHINFO_DIRNAME ) );
$dir = ltrim( $dir, '/' );
$url_path = trailingslashit( get_stylesheet_directory_uri() ) . $dir;
$url_path = set_url_scheme( $url_path );
return $url_path;
} | [
"protected",
"function",
"urlPath",
"(",
")",
"{",
"$",
"dir",
"=",
"str_replace",
"(",
"Container",
"::",
"get",
"(",
"'Styles'",
")",
"->",
"getDir",
"(",
")",
",",
"''",
",",
"pathinfo",
"(",
"$",
"this",
"->",
"exportStylePath",
",",
"PATHINFO_DIRNAME",
")",
")",
";",
"$",
"dir",
"=",
"ltrim",
"(",
"$",
"dir",
",",
"'/'",
")",
";",
"$",
"url_path",
"=",
"trailingslashit",
"(",
"get_stylesheet_directory_uri",
"(",
")",
")",
".",
"$",
"dir",
";",
"$",
"url_path",
"=",
"set_url_scheme",
"(",
"$",
"url_path",
")",
";",
"return",
"$",
"url_path",
";",
"}"
] | Convert the directory containing `$this->exportStylePath` to a URL that can be used by services like DocRaptor
Useful for sending assets like images/asterisk.png, images/em-dash.png, ...
@return string | [
"Convert",
"the",
"directory",
"containing",
"$this",
"-",
">",
"exportStylePath",
"to",
"a",
"URL",
"that",
"can",
"be",
"used",
"by",
"services",
"like",
"DocRaptor",
"Useful",
"for",
"sending",
"assets",
"like",
"images",
"/",
"asterisk",
".",
"png",
"images",
"/",
"em",
"-",
"dash",
".",
"png",
"..."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/export/prince/class-pdf.php#L253-L260 |
pressbooks/pressbooks | inc/modules/export/prince/class-pdf.php | Pdf.themeOptionsOverrides | protected function themeOptionsOverrides() {
// --------------------------------------------------------------------
// CSS
$scss = '';
$scss = apply_filters( 'pb_pdf_css_override', $scss ) . "\n";
// Copyright
// Please be kind, help Pressbooks grow by leaving this on!
if ( empty( $GLOBALS['PB_SECRET_SAUCE']['TURN_OFF_FREEBIE_NOTICES_PDF'] ) ) {
$freebie_notice = __( 'This book was produced with Pressbooks (https://pressbooks.com) and rendered with Prince.', 'pressbooks' );
$scss .= '#copyright-page .ugc > p:last-of-type::after { display:block; margin-top: 1em; content: "' . $freebie_notice . '" }' . "\n";
}
$this->cssOverrides = $scss;
// --------------------------------------------------------------------
// Hacks
$hacks = [];
$hacks = apply_filters( 'pb_pdf_hacks', $hacks );
// Append endnotes to URL?
if ( isset( $hacks['pdf_footnotes_style'] ) && 'endnotes' === $hacks['pdf_footnotes_style'] ) {
$this->url .= '&endnotes=true';
}
} | php | protected function themeOptionsOverrides() {
// --------------------------------------------------------------------
// CSS
$scss = '';
$scss = apply_filters( 'pb_pdf_css_override', $scss ) . "\n";
// Copyright
// Please be kind, help Pressbooks grow by leaving this on!
if ( empty( $GLOBALS['PB_SECRET_SAUCE']['TURN_OFF_FREEBIE_NOTICES_PDF'] ) ) {
$freebie_notice = __( 'This book was produced with Pressbooks (https://pressbooks.com) and rendered with Prince.', 'pressbooks' );
$scss .= '#copyright-page .ugc > p:last-of-type::after { display:block; margin-top: 1em; content: "' . $freebie_notice . '" }' . "\n";
}
$this->cssOverrides = $scss;
// --------------------------------------------------------------------
// Hacks
$hacks = [];
$hacks = apply_filters( 'pb_pdf_hacks', $hacks );
// Append endnotes to URL?
if ( isset( $hacks['pdf_footnotes_style'] ) && 'endnotes' === $hacks['pdf_footnotes_style'] ) {
$this->url .= '&endnotes=true';
}
} | [
"protected",
"function",
"themeOptionsOverrides",
"(",
")",
"{",
"// --------------------------------------------------------------------",
"// CSS",
"$",
"scss",
"=",
"''",
";",
"$",
"scss",
"=",
"apply_filters",
"(",
"'pb_pdf_css_override'",
",",
"$",
"scss",
")",
".",
"\"\\n\"",
";",
"// Copyright",
"// Please be kind, help Pressbooks grow by leaving this on!",
"if",
"(",
"empty",
"(",
"$",
"GLOBALS",
"[",
"'PB_SECRET_SAUCE'",
"]",
"[",
"'TURN_OFF_FREEBIE_NOTICES_PDF'",
"]",
")",
")",
"{",
"$",
"freebie_notice",
"=",
"__",
"(",
"'This book was produced with Pressbooks (https://pressbooks.com) and rendered with Prince.'",
",",
"'pressbooks'",
")",
";",
"$",
"scss",
".=",
"'#copyright-page .ugc > p:last-of-type::after { display:block; margin-top: 1em; content: \"'",
".",
"$",
"freebie_notice",
".",
"'\" }'",
".",
"\"\\n\"",
";",
"}",
"$",
"this",
"->",
"cssOverrides",
"=",
"$",
"scss",
";",
"// --------------------------------------------------------------------",
"// Hacks",
"$",
"hacks",
"=",
"[",
"]",
";",
"$",
"hacks",
"=",
"apply_filters",
"(",
"'pb_pdf_hacks'",
",",
"$",
"hacks",
")",
";",
"// Append endnotes to URL?",
"if",
"(",
"isset",
"(",
"$",
"hacks",
"[",
"'pdf_footnotes_style'",
"]",
")",
"&&",
"'endnotes'",
"===",
"$",
"hacks",
"[",
"'pdf_footnotes_style'",
"]",
")",
"{",
"$",
"this",
"->",
"url",
".=",
"'&endnotes=true'",
";",
"}",
"}"
] | Override based on Theme Options | [
"Override",
"based",
"on",
"Theme",
"Options"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/export/prince/class-pdf.php#L266-L294 |
pressbooks/pressbooks | inc/modules/export/prince/class-pdf.php | Pdf.fixLatexDpi | protected function fixLatexDpi() {
$fix = false;
if ( ! $fix && ! empty( $_GET['optimize-for-print'] ) ) {
$fix = true;
}
if ( ! $fix && strpos( $this->url, 'optimize-for-print=1' ) !== false ) {
$fix = true;
}
if ( ! $fix && stripos( get_class( $this ), 'print' ) !== false ) {
$fix = true;
}
if ( $fix ) {
$this->url .= '&pb-latex-zoom=3';
$this->cssOverrides .= "\n" . 'img.latex { prince-image-resolution: 300dpi; }' . "\n";
}
} | php | protected function fixLatexDpi() {
$fix = false;
if ( ! $fix && ! empty( $_GET['optimize-for-print'] ) ) {
$fix = true;
}
if ( ! $fix && strpos( $this->url, 'optimize-for-print=1' ) !== false ) {
$fix = true;
}
if ( ! $fix && stripos( get_class( $this ), 'print' ) !== false ) {
$fix = true;
}
if ( $fix ) {
$this->url .= '&pb-latex-zoom=3';
$this->cssOverrides .= "\n" . 'img.latex { prince-image-resolution: 300dpi; }' . "\n";
}
} | [
"protected",
"function",
"fixLatexDpi",
"(",
")",
"{",
"$",
"fix",
"=",
"false",
";",
"if",
"(",
"!",
"$",
"fix",
"&&",
"!",
"empty",
"(",
"$",
"_GET",
"[",
"'optimize-for-print'",
"]",
")",
")",
"{",
"$",
"fix",
"=",
"true",
";",
"}",
"if",
"(",
"!",
"$",
"fix",
"&&",
"strpos",
"(",
"$",
"this",
"->",
"url",
",",
"'optimize-for-print=1'",
")",
"!==",
"false",
")",
"{",
"$",
"fix",
"=",
"true",
";",
"}",
"if",
"(",
"!",
"$",
"fix",
"&&",
"stripos",
"(",
"get_class",
"(",
"$",
"this",
")",
",",
"'print'",
")",
"!==",
"false",
")",
"{",
"$",
"fix",
"=",
"true",
";",
"}",
"if",
"(",
"$",
"fix",
")",
"{",
"$",
"this",
"->",
"url",
".=",
"'&pb-latex-zoom=3'",
";",
"$",
"this",
"->",
"cssOverrides",
".=",
"\"\\n\"",
".",
"'img.latex { prince-image-resolution: 300dpi; }'",
".",
"\"\\n\"",
";",
"}",
"}"
] | Increase PB-LaTeX resolution to ~300 dpi
@see symbionts/pressbooks-latex/automattic-latex-wpcom.php | [
"Increase",
"PB",
"-",
"LaTeX",
"resolution",
"to",
"~300",
"dpi"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/modules/export/prince/class-pdf.php#L301-L317 |
pressbooks/pressbooks | inc/class-catalog.php | Catalog.get | function get() {
/** @var $wpdb \wpdb */
global $wpdb;
$sql = "SELECT * FROM {$this->dbTable} WHERE users_id = %d AND deleted = 0 ";
return $wpdb->get_results( $wpdb->prepare( $sql, $this->userId ), ARRAY_A ); // @codingStandardsIgnoreLine
} | php | function get() {
/** @var $wpdb \wpdb */
global $wpdb;
$sql = "SELECT * FROM {$this->dbTable} WHERE users_id = %d AND deleted = 0 ";
return $wpdb->get_results( $wpdb->prepare( $sql, $this->userId ), ARRAY_A ); // @codingStandardsIgnoreLine
} | [
"function",
"get",
"(",
")",
"{",
"/** @var $wpdb \\wpdb */",
"global",
"$",
"wpdb",
";",
"$",
"sql",
"=",
"\"SELECT * FROM {$this->dbTable} WHERE users_id = %d AND deleted = 0 \"",
";",
"return",
"$",
"wpdb",
"->",
"get_results",
"(",
"$",
"wpdb",
"->",
"prepare",
"(",
"$",
"sql",
",",
"$",
"this",
"->",
"userId",
")",
",",
"ARRAY_A",
")",
";",
"// @codingStandardsIgnoreLine",
"}"
] | Get an entire catalog.
@return array | [
"Get",
"an",
"entire",
"catalog",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-catalog.php#L125-L132 |
pressbooks/pressbooks | inc/class-catalog.php | Catalog.getAggregate | function getAggregate() {
// -----------------------------------------------------------------------------
// Is cached?
// -----------------------------------------------------------------------------
$cache_id = "cat-{$this->userId}";
$data = wp_cache_get( $cache_id, 'pb' );
if ( $data ) {
return $data;
}
// ----------------------------------------------------------------------------
// User Catalog
// ----------------------------------------------------------------------------
$cover_sizes = [
'thumbnail' => \Pressbooks\Image\default_cover_url( 'thumbnail' ),
'pb_cover_small' => \Pressbooks\Image\default_cover_url( 'small' ),
'pb_cover_medium' => \Pressbooks\Image\default_cover_url( 'medium' ),
'pb_cover_large' => \Pressbooks\Image\default_cover_url(),
];
$catalog = $this->get();
$usercatalog = new static( $this->userId );
$data = [];
$i = 0;
$already_loaded = [];
foreach ( $catalog as $val ) {
if ( ! get_site( $val['blogs_id'] ) ) {
$usercatalog->deleteBook( $val['blogs_id'], true );
} else {
switch_to_blog( $val['blogs_id'] );
$metadata = Book::getBookInformation();
$meta_version = get_option( 'pressbooks_metadata_version', 0 );
$data[ $i ]['ID'] = "{$val['users_id']}:{$val['blogs_id']}";
$data[ $i ]['users_id'] = $val['users_id'];
$data[ $i ]['blogs_id'] = $val['blogs_id'];
$data[ $i ]['featured'] = $val['featured'];
$data[ $i ]['deleted'] = 0;
$data[ $i ]['title'] = ! empty( $metadata['pb_title'] ) ? $metadata['pb_title'] : get_bloginfo( 'name' );
$data[ $i ]['author'] = ( ! \Pressbooks\Utility\empty_space( $metadata['pb_authors'] ) ) ? oxford_comma_explode( $metadata['pb_authors'] )[0] : '';
$data[ $i ]['pub_date'] = ! empty( $metadata['pb_publication_date'] ) ? date( 'Y-m-d', (int) $metadata['pb_publication_date'] ) : '';
$data[ $i ]['private'] = ( ! empty( get_option( 'blog_public' ) ) ? 0 : 1 );
// About
if ( ! empty( $metadata['pb_about_50'] ) ) {
$about = $metadata['pb_about_50'];
} elseif ( ! empty( $metadata['pb_about_140'] ) ) {
$about = $metadata['pb_about_140'];
} elseif ( ! empty( $metadata['pb_about_unlimited'] ) ) {
$about = $metadata['pb_about_unlimited'];
} else {
$about = '';
}
$data[ $i ]['about'] = $about;
// Cover Full
if ( $meta_version < 7 ) {
$cover = \Pressbooks\Image\default_cover_url();
} elseif ( empty( $metadata['pb_cover_image'] ) ) {
$cover = \Pressbooks\Image\default_cover_url();
} elseif ( \Pressbooks\Image\is_default_cover( $metadata['pb_cover_image'] ) ) {
$cover = \Pressbooks\Image\default_cover_url();
} else {
$cover = \Pressbooks\Image\thumbnail_from_url( $metadata['pb_cover_image'], 'full' );
}
$data[ $i ]['cover_url']['full'] = $cover;
// Cover Thumbnails
/**
* Exposes $cover variable to be changed as-needed for cover images.
*
* Some users store their images on an outside server, which can result
* in cover images not displaying correctly. This gives users the option
* of altering $cover to point to the correct path to the cover image.
*
* @since 3.9.5.1
*
* @param string $cover The url to cover image.
* @param string $original The original url to the cover image.
*/
$cid = \Pressbooks\Image\attachment_id_from_url( apply_filters( 'pb_cover_image', $cover, $metadata['pb_cover_image'] ?? '' ) );
foreach ( $cover_sizes as $size => $default ) {
$cid_thumb = wp_get_attachment_image_src( $cid, $size );
if ( $cid_thumb ) {
$data[ $i ]['cover_url'][ $size ] = $cid_thumb[0];
} else {
$data[ $i ]['cover_url'][ $size ] = $default;
}
}
// Tags
for ( $j = 1; $j <= self::MAX_TAGS_GROUP; ++$j ) {
$data[ $i ][ "tag_{$j}" ] = $this->getTagsByBook( $val['blogs_id'], $j );
}
$already_loaded[ $val['blogs_id'] ] = true;
++$i;
restore_current_blog();
}
}
$userblogs = get_blogs_of_user( $this->userId );
foreach ( $userblogs as $book ) {
// Skip
if ( is_main_site( $book->userblog_id ) ) {
continue;
}
if ( isset( $already_loaded[ $book->userblog_id ] ) ) {
continue;
}
switch_to_blog( $book->userblog_id );
$metadata = Book::getBookInformation();
$meta_version = get_option( 'pressbooks_metadata_version', 0 );
$data[ $i ]['ID'] = "{$this->userId}:{$book->userblog_id}";
$data[ $i ]['users_id'] = $this->userId;
$data[ $i ]['blogs_id'] = $book->userblog_id;
$data[ $i ]['featured'] = 0;
$data[ $i ]['deleted'] = 1;
$data[ $i ]['title'] = ! empty( $metadata['pb_title'] ) ? $metadata['pb_title'] : get_bloginfo( 'name' );
$data[ $i ]['author'] = ( ! \Pressbooks\Utility\empty_space( $metadata['pb_authors'] ) ) ? oxford_comma_explode( $metadata['pb_authors'] )[0] : '';
$data[ $i ]['pub_date'] = ! empty( $metadata['pb_publication_date'] ) ? date( 'Y-m-d', (int) $metadata['pb_publication_date'] ) : '';
$data[ $i ]['private'] = ( ! empty( get_option( 'blog_public' ) ) ? 0 : 1 );
// About
if ( ! empty( $metadata['pb_about_50'] ) ) {
$about = $metadata['pb_about_50'];
} elseif ( ! empty( $metadata['pb_about_140'] ) ) {
$about = $metadata['pb_about_140'];
} elseif ( ! empty( $metadata['pb_about_unlimited'] ) ) {
$about = $metadata['pb_about_unlimited'];
} else {
$about = '';
}
$data[ $i ]['about'] = $about;
// Cover Full
if ( $meta_version < 7 ) {
$cover = \Pressbooks\Image\default_cover_url();
} elseif ( empty( $metadata['pb_cover_image'] ) ) {
$cover = \Pressbooks\Image\default_cover_url();
} elseif ( \Pressbooks\Image\is_default_cover( $metadata['pb_cover_image'] ) ) {
$cover = \Pressbooks\Image\default_cover_url();
} else {
$cover = \Pressbooks\Image\thumbnail_from_url( $metadata['pb_cover_image'], 'full' );
}
$data[ $i ]['cover_url']['full'] = $cover;
// Cover Thumbnails
/** This filter is documented in pressbooks/includes/class-pb-catalog.php */
$cid = \Pressbooks\Image\attachment_id_from_url( apply_filters( 'pb_cover_image', $cover, $metadata['pb_cover_image'] ) );
foreach ( $cover_sizes as $size => $default ) {
$cid_thumb = wp_get_attachment_image_src( $cid, $size );
if ( $cid_thumb ) {
$data[ $i ]['cover_url'][ $size ] = $cid_thumb[0];
} else {
$data[ $i ]['cover_url'][ $size ] = $default;
}
}
// Tags
for ( $j = 1; $j <= self::MAX_TAGS_GROUP; ++$j ) {
$data[ $i ][ "tag_{$j}" ] = $this->getTagsByBook( $book->userblog_id, $j );
}
++$i;
restore_current_blog();
}
// -----------------------------------------------------------------------------
// Cache & Return
// -----------------------------------------------------------------------------
wp_cache_set( $cache_id, $data, 'pb', DAY_IN_SECONDS );
return $data;
} | php | function getAggregate() {
// -----------------------------------------------------------------------------
// Is cached?
// -----------------------------------------------------------------------------
$cache_id = "cat-{$this->userId}";
$data = wp_cache_get( $cache_id, 'pb' );
if ( $data ) {
return $data;
}
// ----------------------------------------------------------------------------
// User Catalog
// ----------------------------------------------------------------------------
$cover_sizes = [
'thumbnail' => \Pressbooks\Image\default_cover_url( 'thumbnail' ),
'pb_cover_small' => \Pressbooks\Image\default_cover_url( 'small' ),
'pb_cover_medium' => \Pressbooks\Image\default_cover_url( 'medium' ),
'pb_cover_large' => \Pressbooks\Image\default_cover_url(),
];
$catalog = $this->get();
$usercatalog = new static( $this->userId );
$data = [];
$i = 0;
$already_loaded = [];
foreach ( $catalog as $val ) {
if ( ! get_site( $val['blogs_id'] ) ) {
$usercatalog->deleteBook( $val['blogs_id'], true );
} else {
switch_to_blog( $val['blogs_id'] );
$metadata = Book::getBookInformation();
$meta_version = get_option( 'pressbooks_metadata_version', 0 );
$data[ $i ]['ID'] = "{$val['users_id']}:{$val['blogs_id']}";
$data[ $i ]['users_id'] = $val['users_id'];
$data[ $i ]['blogs_id'] = $val['blogs_id'];
$data[ $i ]['featured'] = $val['featured'];
$data[ $i ]['deleted'] = 0;
$data[ $i ]['title'] = ! empty( $metadata['pb_title'] ) ? $metadata['pb_title'] : get_bloginfo( 'name' );
$data[ $i ]['author'] = ( ! \Pressbooks\Utility\empty_space( $metadata['pb_authors'] ) ) ? oxford_comma_explode( $metadata['pb_authors'] )[0] : '';
$data[ $i ]['pub_date'] = ! empty( $metadata['pb_publication_date'] ) ? date( 'Y-m-d', (int) $metadata['pb_publication_date'] ) : '';
$data[ $i ]['private'] = ( ! empty( get_option( 'blog_public' ) ) ? 0 : 1 );
// About
if ( ! empty( $metadata['pb_about_50'] ) ) {
$about = $metadata['pb_about_50'];
} elseif ( ! empty( $metadata['pb_about_140'] ) ) {
$about = $metadata['pb_about_140'];
} elseif ( ! empty( $metadata['pb_about_unlimited'] ) ) {
$about = $metadata['pb_about_unlimited'];
} else {
$about = '';
}
$data[ $i ]['about'] = $about;
// Cover Full
if ( $meta_version < 7 ) {
$cover = \Pressbooks\Image\default_cover_url();
} elseif ( empty( $metadata['pb_cover_image'] ) ) {
$cover = \Pressbooks\Image\default_cover_url();
} elseif ( \Pressbooks\Image\is_default_cover( $metadata['pb_cover_image'] ) ) {
$cover = \Pressbooks\Image\default_cover_url();
} else {
$cover = \Pressbooks\Image\thumbnail_from_url( $metadata['pb_cover_image'], 'full' );
}
$data[ $i ]['cover_url']['full'] = $cover;
// Cover Thumbnails
/**
* Exposes $cover variable to be changed as-needed for cover images.
*
* Some users store their images on an outside server, which can result
* in cover images not displaying correctly. This gives users the option
* of altering $cover to point to the correct path to the cover image.
*
* @since 3.9.5.1
*
* @param string $cover The url to cover image.
* @param string $original The original url to the cover image.
*/
$cid = \Pressbooks\Image\attachment_id_from_url( apply_filters( 'pb_cover_image', $cover, $metadata['pb_cover_image'] ?? '' ) );
foreach ( $cover_sizes as $size => $default ) {
$cid_thumb = wp_get_attachment_image_src( $cid, $size );
if ( $cid_thumb ) {
$data[ $i ]['cover_url'][ $size ] = $cid_thumb[0];
} else {
$data[ $i ]['cover_url'][ $size ] = $default;
}
}
// Tags
for ( $j = 1; $j <= self::MAX_TAGS_GROUP; ++$j ) {
$data[ $i ][ "tag_{$j}" ] = $this->getTagsByBook( $val['blogs_id'], $j );
}
$already_loaded[ $val['blogs_id'] ] = true;
++$i;
restore_current_blog();
}
}
$userblogs = get_blogs_of_user( $this->userId );
foreach ( $userblogs as $book ) {
// Skip
if ( is_main_site( $book->userblog_id ) ) {
continue;
}
if ( isset( $already_loaded[ $book->userblog_id ] ) ) {
continue;
}
switch_to_blog( $book->userblog_id );
$metadata = Book::getBookInformation();
$meta_version = get_option( 'pressbooks_metadata_version', 0 );
$data[ $i ]['ID'] = "{$this->userId}:{$book->userblog_id}";
$data[ $i ]['users_id'] = $this->userId;
$data[ $i ]['blogs_id'] = $book->userblog_id;
$data[ $i ]['featured'] = 0;
$data[ $i ]['deleted'] = 1;
$data[ $i ]['title'] = ! empty( $metadata['pb_title'] ) ? $metadata['pb_title'] : get_bloginfo( 'name' );
$data[ $i ]['author'] = ( ! \Pressbooks\Utility\empty_space( $metadata['pb_authors'] ) ) ? oxford_comma_explode( $metadata['pb_authors'] )[0] : '';
$data[ $i ]['pub_date'] = ! empty( $metadata['pb_publication_date'] ) ? date( 'Y-m-d', (int) $metadata['pb_publication_date'] ) : '';
$data[ $i ]['private'] = ( ! empty( get_option( 'blog_public' ) ) ? 0 : 1 );
// About
if ( ! empty( $metadata['pb_about_50'] ) ) {
$about = $metadata['pb_about_50'];
} elseif ( ! empty( $metadata['pb_about_140'] ) ) {
$about = $metadata['pb_about_140'];
} elseif ( ! empty( $metadata['pb_about_unlimited'] ) ) {
$about = $metadata['pb_about_unlimited'];
} else {
$about = '';
}
$data[ $i ]['about'] = $about;
// Cover Full
if ( $meta_version < 7 ) {
$cover = \Pressbooks\Image\default_cover_url();
} elseif ( empty( $metadata['pb_cover_image'] ) ) {
$cover = \Pressbooks\Image\default_cover_url();
} elseif ( \Pressbooks\Image\is_default_cover( $metadata['pb_cover_image'] ) ) {
$cover = \Pressbooks\Image\default_cover_url();
} else {
$cover = \Pressbooks\Image\thumbnail_from_url( $metadata['pb_cover_image'], 'full' );
}
$data[ $i ]['cover_url']['full'] = $cover;
// Cover Thumbnails
/** This filter is documented in pressbooks/includes/class-pb-catalog.php */
$cid = \Pressbooks\Image\attachment_id_from_url( apply_filters( 'pb_cover_image', $cover, $metadata['pb_cover_image'] ) );
foreach ( $cover_sizes as $size => $default ) {
$cid_thumb = wp_get_attachment_image_src( $cid, $size );
if ( $cid_thumb ) {
$data[ $i ]['cover_url'][ $size ] = $cid_thumb[0];
} else {
$data[ $i ]['cover_url'][ $size ] = $default;
}
}
// Tags
for ( $j = 1; $j <= self::MAX_TAGS_GROUP; ++$j ) {
$data[ $i ][ "tag_{$j}" ] = $this->getTagsByBook( $book->userblog_id, $j );
}
++$i;
restore_current_blog();
}
// -----------------------------------------------------------------------------
// Cache & Return
// -----------------------------------------------------------------------------
wp_cache_set( $cache_id, $data, 'pb', DAY_IN_SECONDS );
return $data;
} | [
"function",
"getAggregate",
"(",
")",
"{",
"// -----------------------------------------------------------------------------",
"// Is cached?",
"// -----------------------------------------------------------------------------",
"$",
"cache_id",
"=",
"\"cat-{$this->userId}\"",
";",
"$",
"data",
"=",
"wp_cache_get",
"(",
"$",
"cache_id",
",",
"'pb'",
")",
";",
"if",
"(",
"$",
"data",
")",
"{",
"return",
"$",
"data",
";",
"}",
"// ----------------------------------------------------------------------------",
"// User Catalog",
"// ----------------------------------------------------------------------------",
"$",
"cover_sizes",
"=",
"[",
"'thumbnail'",
"=>",
"\\",
"Pressbooks",
"\\",
"Image",
"\\",
"default_cover_url",
"(",
"'thumbnail'",
")",
",",
"'pb_cover_small'",
"=>",
"\\",
"Pressbooks",
"\\",
"Image",
"\\",
"default_cover_url",
"(",
"'small'",
")",
",",
"'pb_cover_medium'",
"=>",
"\\",
"Pressbooks",
"\\",
"Image",
"\\",
"default_cover_url",
"(",
"'medium'",
")",
",",
"'pb_cover_large'",
"=>",
"\\",
"Pressbooks",
"\\",
"Image",
"\\",
"default_cover_url",
"(",
")",
",",
"]",
";",
"$",
"catalog",
"=",
"$",
"this",
"->",
"get",
"(",
")",
";",
"$",
"usercatalog",
"=",
"new",
"static",
"(",
"$",
"this",
"->",
"userId",
")",
";",
"$",
"data",
"=",
"[",
"]",
";",
"$",
"i",
"=",
"0",
";",
"$",
"already_loaded",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"catalog",
"as",
"$",
"val",
")",
"{",
"if",
"(",
"!",
"get_site",
"(",
"$",
"val",
"[",
"'blogs_id'",
"]",
")",
")",
"{",
"$",
"usercatalog",
"->",
"deleteBook",
"(",
"$",
"val",
"[",
"'blogs_id'",
"]",
",",
"true",
")",
";",
"}",
"else",
"{",
"switch_to_blog",
"(",
"$",
"val",
"[",
"'blogs_id'",
"]",
")",
";",
"$",
"metadata",
"=",
"Book",
"::",
"getBookInformation",
"(",
")",
";",
"$",
"meta_version",
"=",
"get_option",
"(",
"'pressbooks_metadata_version'",
",",
"0",
")",
";",
"$",
"data",
"[",
"$",
"i",
"]",
"[",
"'ID'",
"]",
"=",
"\"{$val['users_id']}:{$val['blogs_id']}\"",
";",
"$",
"data",
"[",
"$",
"i",
"]",
"[",
"'users_id'",
"]",
"=",
"$",
"val",
"[",
"'users_id'",
"]",
";",
"$",
"data",
"[",
"$",
"i",
"]",
"[",
"'blogs_id'",
"]",
"=",
"$",
"val",
"[",
"'blogs_id'",
"]",
";",
"$",
"data",
"[",
"$",
"i",
"]",
"[",
"'featured'",
"]",
"=",
"$",
"val",
"[",
"'featured'",
"]",
";",
"$",
"data",
"[",
"$",
"i",
"]",
"[",
"'deleted'",
"]",
"=",
"0",
";",
"$",
"data",
"[",
"$",
"i",
"]",
"[",
"'title'",
"]",
"=",
"!",
"empty",
"(",
"$",
"metadata",
"[",
"'pb_title'",
"]",
")",
"?",
"$",
"metadata",
"[",
"'pb_title'",
"]",
":",
"get_bloginfo",
"(",
"'name'",
")",
";",
"$",
"data",
"[",
"$",
"i",
"]",
"[",
"'author'",
"]",
"=",
"(",
"!",
"\\",
"Pressbooks",
"\\",
"Utility",
"\\",
"empty_space",
"(",
"$",
"metadata",
"[",
"'pb_authors'",
"]",
")",
")",
"?",
"oxford_comma_explode",
"(",
"$",
"metadata",
"[",
"'pb_authors'",
"]",
")",
"[",
"0",
"]",
":",
"''",
";",
"$",
"data",
"[",
"$",
"i",
"]",
"[",
"'pub_date'",
"]",
"=",
"!",
"empty",
"(",
"$",
"metadata",
"[",
"'pb_publication_date'",
"]",
")",
"?",
"date",
"(",
"'Y-m-d'",
",",
"(",
"int",
")",
"$",
"metadata",
"[",
"'pb_publication_date'",
"]",
")",
":",
"''",
";",
"$",
"data",
"[",
"$",
"i",
"]",
"[",
"'private'",
"]",
"=",
"(",
"!",
"empty",
"(",
"get_option",
"(",
"'blog_public'",
")",
")",
"?",
"0",
":",
"1",
")",
";",
"// About",
"if",
"(",
"!",
"empty",
"(",
"$",
"metadata",
"[",
"'pb_about_50'",
"]",
")",
")",
"{",
"$",
"about",
"=",
"$",
"metadata",
"[",
"'pb_about_50'",
"]",
";",
"}",
"elseif",
"(",
"!",
"empty",
"(",
"$",
"metadata",
"[",
"'pb_about_140'",
"]",
")",
")",
"{",
"$",
"about",
"=",
"$",
"metadata",
"[",
"'pb_about_140'",
"]",
";",
"}",
"elseif",
"(",
"!",
"empty",
"(",
"$",
"metadata",
"[",
"'pb_about_unlimited'",
"]",
")",
")",
"{",
"$",
"about",
"=",
"$",
"metadata",
"[",
"'pb_about_unlimited'",
"]",
";",
"}",
"else",
"{",
"$",
"about",
"=",
"''",
";",
"}",
"$",
"data",
"[",
"$",
"i",
"]",
"[",
"'about'",
"]",
"=",
"$",
"about",
";",
"// Cover Full",
"if",
"(",
"$",
"meta_version",
"<",
"7",
")",
"{",
"$",
"cover",
"=",
"\\",
"Pressbooks",
"\\",
"Image",
"\\",
"default_cover_url",
"(",
")",
";",
"}",
"elseif",
"(",
"empty",
"(",
"$",
"metadata",
"[",
"'pb_cover_image'",
"]",
")",
")",
"{",
"$",
"cover",
"=",
"\\",
"Pressbooks",
"\\",
"Image",
"\\",
"default_cover_url",
"(",
")",
";",
"}",
"elseif",
"(",
"\\",
"Pressbooks",
"\\",
"Image",
"\\",
"is_default_cover",
"(",
"$",
"metadata",
"[",
"'pb_cover_image'",
"]",
")",
")",
"{",
"$",
"cover",
"=",
"\\",
"Pressbooks",
"\\",
"Image",
"\\",
"default_cover_url",
"(",
")",
";",
"}",
"else",
"{",
"$",
"cover",
"=",
"\\",
"Pressbooks",
"\\",
"Image",
"\\",
"thumbnail_from_url",
"(",
"$",
"metadata",
"[",
"'pb_cover_image'",
"]",
",",
"'full'",
")",
";",
"}",
"$",
"data",
"[",
"$",
"i",
"]",
"[",
"'cover_url'",
"]",
"[",
"'full'",
"]",
"=",
"$",
"cover",
";",
"// Cover Thumbnails",
"/**\n\t\t\t\t * Exposes $cover variable to be changed as-needed for cover images.\n\t\t\t\t *\n\t\t\t\t * Some users store their images on an outside server, which can result\n\t\t\t\t * in cover images not displaying correctly. This gives users the option\n\t\t\t\t * of altering $cover to point to the correct path to the cover image.\n\t\t\t\t *\n\t\t\t\t * @since 3.9.5.1\n\t\t\t\t *\n\t\t\t\t * @param string $cover The url to cover image.\n\t\t\t\t * @param string $original The original url to the cover image.\n\t\t\t\t */",
"$",
"cid",
"=",
"\\",
"Pressbooks",
"\\",
"Image",
"\\",
"attachment_id_from_url",
"(",
"apply_filters",
"(",
"'pb_cover_image'",
",",
"$",
"cover",
",",
"$",
"metadata",
"[",
"'pb_cover_image'",
"]",
"??",
"''",
")",
")",
";",
"foreach",
"(",
"$",
"cover_sizes",
"as",
"$",
"size",
"=>",
"$",
"default",
")",
"{",
"$",
"cid_thumb",
"=",
"wp_get_attachment_image_src",
"(",
"$",
"cid",
",",
"$",
"size",
")",
";",
"if",
"(",
"$",
"cid_thumb",
")",
"{",
"$",
"data",
"[",
"$",
"i",
"]",
"[",
"'cover_url'",
"]",
"[",
"$",
"size",
"]",
"=",
"$",
"cid_thumb",
"[",
"0",
"]",
";",
"}",
"else",
"{",
"$",
"data",
"[",
"$",
"i",
"]",
"[",
"'cover_url'",
"]",
"[",
"$",
"size",
"]",
"=",
"$",
"default",
";",
"}",
"}",
"// Tags",
"for",
"(",
"$",
"j",
"=",
"1",
";",
"$",
"j",
"<=",
"self",
"::",
"MAX_TAGS_GROUP",
";",
"++",
"$",
"j",
")",
"{",
"$",
"data",
"[",
"$",
"i",
"]",
"[",
"\"tag_{$j}\"",
"]",
"=",
"$",
"this",
"->",
"getTagsByBook",
"(",
"$",
"val",
"[",
"'blogs_id'",
"]",
",",
"$",
"j",
")",
";",
"}",
"$",
"already_loaded",
"[",
"$",
"val",
"[",
"'blogs_id'",
"]",
"]",
"=",
"true",
";",
"++",
"$",
"i",
";",
"restore_current_blog",
"(",
")",
";",
"}",
"}",
"$",
"userblogs",
"=",
"get_blogs_of_user",
"(",
"$",
"this",
"->",
"userId",
")",
";",
"foreach",
"(",
"$",
"userblogs",
"as",
"$",
"book",
")",
"{",
"// Skip",
"if",
"(",
"is_main_site",
"(",
"$",
"book",
"->",
"userblog_id",
")",
")",
"{",
"continue",
";",
"}",
"if",
"(",
"isset",
"(",
"$",
"already_loaded",
"[",
"$",
"book",
"->",
"userblog_id",
"]",
")",
")",
"{",
"continue",
";",
"}",
"switch_to_blog",
"(",
"$",
"book",
"->",
"userblog_id",
")",
";",
"$",
"metadata",
"=",
"Book",
"::",
"getBookInformation",
"(",
")",
";",
"$",
"meta_version",
"=",
"get_option",
"(",
"'pressbooks_metadata_version'",
",",
"0",
")",
";",
"$",
"data",
"[",
"$",
"i",
"]",
"[",
"'ID'",
"]",
"=",
"\"{$this->userId}:{$book->userblog_id}\"",
";",
"$",
"data",
"[",
"$",
"i",
"]",
"[",
"'users_id'",
"]",
"=",
"$",
"this",
"->",
"userId",
";",
"$",
"data",
"[",
"$",
"i",
"]",
"[",
"'blogs_id'",
"]",
"=",
"$",
"book",
"->",
"userblog_id",
";",
"$",
"data",
"[",
"$",
"i",
"]",
"[",
"'featured'",
"]",
"=",
"0",
";",
"$",
"data",
"[",
"$",
"i",
"]",
"[",
"'deleted'",
"]",
"=",
"1",
";",
"$",
"data",
"[",
"$",
"i",
"]",
"[",
"'title'",
"]",
"=",
"!",
"empty",
"(",
"$",
"metadata",
"[",
"'pb_title'",
"]",
")",
"?",
"$",
"metadata",
"[",
"'pb_title'",
"]",
":",
"get_bloginfo",
"(",
"'name'",
")",
";",
"$",
"data",
"[",
"$",
"i",
"]",
"[",
"'author'",
"]",
"=",
"(",
"!",
"\\",
"Pressbooks",
"\\",
"Utility",
"\\",
"empty_space",
"(",
"$",
"metadata",
"[",
"'pb_authors'",
"]",
")",
")",
"?",
"oxford_comma_explode",
"(",
"$",
"metadata",
"[",
"'pb_authors'",
"]",
")",
"[",
"0",
"]",
":",
"''",
";",
"$",
"data",
"[",
"$",
"i",
"]",
"[",
"'pub_date'",
"]",
"=",
"!",
"empty",
"(",
"$",
"metadata",
"[",
"'pb_publication_date'",
"]",
")",
"?",
"date",
"(",
"'Y-m-d'",
",",
"(",
"int",
")",
"$",
"metadata",
"[",
"'pb_publication_date'",
"]",
")",
":",
"''",
";",
"$",
"data",
"[",
"$",
"i",
"]",
"[",
"'private'",
"]",
"=",
"(",
"!",
"empty",
"(",
"get_option",
"(",
"'blog_public'",
")",
")",
"?",
"0",
":",
"1",
")",
";",
"// About",
"if",
"(",
"!",
"empty",
"(",
"$",
"metadata",
"[",
"'pb_about_50'",
"]",
")",
")",
"{",
"$",
"about",
"=",
"$",
"metadata",
"[",
"'pb_about_50'",
"]",
";",
"}",
"elseif",
"(",
"!",
"empty",
"(",
"$",
"metadata",
"[",
"'pb_about_140'",
"]",
")",
")",
"{",
"$",
"about",
"=",
"$",
"metadata",
"[",
"'pb_about_140'",
"]",
";",
"}",
"elseif",
"(",
"!",
"empty",
"(",
"$",
"metadata",
"[",
"'pb_about_unlimited'",
"]",
")",
")",
"{",
"$",
"about",
"=",
"$",
"metadata",
"[",
"'pb_about_unlimited'",
"]",
";",
"}",
"else",
"{",
"$",
"about",
"=",
"''",
";",
"}",
"$",
"data",
"[",
"$",
"i",
"]",
"[",
"'about'",
"]",
"=",
"$",
"about",
";",
"// Cover Full",
"if",
"(",
"$",
"meta_version",
"<",
"7",
")",
"{",
"$",
"cover",
"=",
"\\",
"Pressbooks",
"\\",
"Image",
"\\",
"default_cover_url",
"(",
")",
";",
"}",
"elseif",
"(",
"empty",
"(",
"$",
"metadata",
"[",
"'pb_cover_image'",
"]",
")",
")",
"{",
"$",
"cover",
"=",
"\\",
"Pressbooks",
"\\",
"Image",
"\\",
"default_cover_url",
"(",
")",
";",
"}",
"elseif",
"(",
"\\",
"Pressbooks",
"\\",
"Image",
"\\",
"is_default_cover",
"(",
"$",
"metadata",
"[",
"'pb_cover_image'",
"]",
")",
")",
"{",
"$",
"cover",
"=",
"\\",
"Pressbooks",
"\\",
"Image",
"\\",
"default_cover_url",
"(",
")",
";",
"}",
"else",
"{",
"$",
"cover",
"=",
"\\",
"Pressbooks",
"\\",
"Image",
"\\",
"thumbnail_from_url",
"(",
"$",
"metadata",
"[",
"'pb_cover_image'",
"]",
",",
"'full'",
")",
";",
"}",
"$",
"data",
"[",
"$",
"i",
"]",
"[",
"'cover_url'",
"]",
"[",
"'full'",
"]",
"=",
"$",
"cover",
";",
"// Cover Thumbnails",
"/** This filter is documented in pressbooks/includes/class-pb-catalog.php */",
"$",
"cid",
"=",
"\\",
"Pressbooks",
"\\",
"Image",
"\\",
"attachment_id_from_url",
"(",
"apply_filters",
"(",
"'pb_cover_image'",
",",
"$",
"cover",
",",
"$",
"metadata",
"[",
"'pb_cover_image'",
"]",
")",
")",
";",
"foreach",
"(",
"$",
"cover_sizes",
"as",
"$",
"size",
"=>",
"$",
"default",
")",
"{",
"$",
"cid_thumb",
"=",
"wp_get_attachment_image_src",
"(",
"$",
"cid",
",",
"$",
"size",
")",
";",
"if",
"(",
"$",
"cid_thumb",
")",
"{",
"$",
"data",
"[",
"$",
"i",
"]",
"[",
"'cover_url'",
"]",
"[",
"$",
"size",
"]",
"=",
"$",
"cid_thumb",
"[",
"0",
"]",
";",
"}",
"else",
"{",
"$",
"data",
"[",
"$",
"i",
"]",
"[",
"'cover_url'",
"]",
"[",
"$",
"size",
"]",
"=",
"$",
"default",
";",
"}",
"}",
"// Tags",
"for",
"(",
"$",
"j",
"=",
"1",
";",
"$",
"j",
"<=",
"self",
"::",
"MAX_TAGS_GROUP",
";",
"++",
"$",
"j",
")",
"{",
"$",
"data",
"[",
"$",
"i",
"]",
"[",
"\"tag_{$j}\"",
"]",
"=",
"$",
"this",
"->",
"getTagsByBook",
"(",
"$",
"book",
"->",
"userblog_id",
",",
"$",
"j",
")",
";",
"}",
"++",
"$",
"i",
";",
"restore_current_blog",
"(",
")",
";",
"}",
"// -----------------------------------------------------------------------------",
"// Cache & Return",
"// -----------------------------------------------------------------------------",
"wp_cache_set",
"(",
"$",
"cache_id",
",",
"$",
"data",
",",
"'pb'",
",",
"DAY_IN_SECONDS",
")",
";",
"return",
"$",
"data",
";",
"}"
] | Get all data for an entire catalog, cached
@return array | [
"Get",
"all",
"data",
"for",
"an",
"entire",
"catalog",
"cached"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-catalog.php#L140-L326 |
pressbooks/pressbooks | inc/class-catalog.php | Catalog.getByTagId | function getByTagId( $tag_group, $tag_id ) {
/** @var $wpdb \wpdb */
global $wpdb;
$sql = "SELECT DISTINCT {$this->dbTable}.* FROM {$this->dbTable}
INNER JOIN {$this->dbLinkTable} ON {$this->dbLinkTable}.blogs_id = {$this->dbTable}.blogs_id
INNER JOIN {$this->dbTagsTable} ON {$this->dbTagsTable}.id = {$this->dbLinkTable}.tags_id
WHERE {$this->dbLinkTable}.users_id = %d AND {$this->dbLinkTable}.tags_group = %d AND {$this->dbLinkTable}.tags_id = %d AND {$this->dbTable}.deleted = 0 ";
return $wpdb->get_results( $wpdb->prepare( $sql, $this->userId, $tag_group, $tag_id ), ARRAY_A ); // @codingStandardsIgnoreLine
} | php | function getByTagId( $tag_group, $tag_id ) {
/** @var $wpdb \wpdb */
global $wpdb;
$sql = "SELECT DISTINCT {$this->dbTable}.* FROM {$this->dbTable}
INNER JOIN {$this->dbLinkTable} ON {$this->dbLinkTable}.blogs_id = {$this->dbTable}.blogs_id
INNER JOIN {$this->dbTagsTable} ON {$this->dbTagsTable}.id = {$this->dbLinkTable}.tags_id
WHERE {$this->dbLinkTable}.users_id = %d AND {$this->dbLinkTable}.tags_group = %d AND {$this->dbLinkTable}.tags_id = %d AND {$this->dbTable}.deleted = 0 ";
return $wpdb->get_results( $wpdb->prepare( $sql, $this->userId, $tag_group, $tag_id ), ARRAY_A ); // @codingStandardsIgnoreLine
} | [
"function",
"getByTagId",
"(",
"$",
"tag_group",
",",
"$",
"tag_id",
")",
"{",
"/** @var $wpdb \\wpdb */",
"global",
"$",
"wpdb",
";",
"$",
"sql",
"=",
"\"SELECT DISTINCT {$this->dbTable}.* FROM {$this->dbTable}\n\t\t\t\tINNER JOIN {$this->dbLinkTable} ON {$this->dbLinkTable}.blogs_id = {$this->dbTable}.blogs_id\n \t\t\t\tINNER JOIN {$this->dbTagsTable} ON {$this->dbTagsTable}.id = {$this->dbLinkTable}.tags_id\n \t\t\t\tWHERE {$this->dbLinkTable}.users_id = %d AND {$this->dbLinkTable}.tags_group = %d AND {$this->dbLinkTable}.tags_id = %d AND {$this->dbTable}.deleted = 0 \"",
";",
"return",
"$",
"wpdb",
"->",
"get_results",
"(",
"$",
"wpdb",
"->",
"prepare",
"(",
"$",
"sql",
",",
"$",
"this",
"->",
"userId",
",",
"$",
"tag_group",
",",
"$",
"tag_id",
")",
",",
"ARRAY_A",
")",
";",
"// @codingStandardsIgnoreLine",
"}"
] | Get catalog by tag id
@param int $tag_group
@param int $tag_id
@return array | [
"Get",
"catalog",
"by",
"tag",
"id"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-catalog.php#L337-L348 |
pressbooks/pressbooks | inc/class-catalog.php | Catalog.save | function save( array $items ) {
foreach ( $items as $item ) {
if ( isset( $item['blogs_id'] ) ) {
$this->saveBook( $this->userId, $item );
}
}
} | php | function save( array $items ) {
foreach ( $items as $item ) {
if ( isset( $item['blogs_id'] ) ) {
$this->saveBook( $this->userId, $item );
}
}
} | [
"function",
"save",
"(",
"array",
"$",
"items",
")",
"{",
"foreach",
"(",
"$",
"items",
"as",
"$",
"item",
")",
"{",
"if",
"(",
"isset",
"(",
"$",
"item",
"[",
"'blogs_id'",
"]",
")",
")",
"{",
"$",
"this",
"->",
"saveBook",
"(",
"$",
"this",
"->",
"userId",
",",
"$",
"item",
")",
";",
"}",
"}",
"}"
] | Save an entire catalog.
@param array $items | [
"Save",
"an",
"entire",
"catalog",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-catalog.php#L356-L363 |
pressbooks/pressbooks | inc/class-catalog.php | Catalog.delete | function delete( $for_real = false ) {
/** @var $wpdb \wpdb */
global $wpdb;
if ( $for_real ) {
return $wpdb->delete(
$this->dbTable, [
'users_id' => $this->userId,
], [ '%d' ]
);
} else {
return $wpdb->update(
$this->dbTable, [
'deleted' => 1,
], [
'users_id' => $this->userId,
], [ '%d' ], [ '%d' ]
);
}
} | php | function delete( $for_real = false ) {
/** @var $wpdb \wpdb */
global $wpdb;
if ( $for_real ) {
return $wpdb->delete(
$this->dbTable, [
'users_id' => $this->userId,
], [ '%d' ]
);
} else {
return $wpdb->update(
$this->dbTable, [
'deleted' => 1,
], [
'users_id' => $this->userId,
], [ '%d' ], [ '%d' ]
);
}
} | [
"function",
"delete",
"(",
"$",
"for_real",
"=",
"false",
")",
"{",
"/** @var $wpdb \\wpdb */",
"global",
"$",
"wpdb",
";",
"if",
"(",
"$",
"for_real",
")",
"{",
"return",
"$",
"wpdb",
"->",
"delete",
"(",
"$",
"this",
"->",
"dbTable",
",",
"[",
"'users_id'",
"=>",
"$",
"this",
"->",
"userId",
",",
"]",
",",
"[",
"'%d'",
"]",
")",
";",
"}",
"else",
"{",
"return",
"$",
"wpdb",
"->",
"update",
"(",
"$",
"this",
"->",
"dbTable",
",",
"[",
"'deleted'",
"=>",
"1",
",",
"]",
",",
"[",
"'users_id'",
"=>",
"$",
"this",
"->",
"userId",
",",
"]",
",",
"[",
"'%d'",
"]",
",",
"[",
"'%d'",
"]",
")",
";",
"}",
"}"
] | Delete an entire catalog.
@param bool $for_real (optional)
@return int|false | [
"Delete",
"an",
"entire",
"catalog",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-catalog.php#L373-L393 |
pressbooks/pressbooks | inc/class-catalog.php | Catalog.getBook | function getBook( $blog_id ) {
/** @var $wpdb \wpdb */
global $wpdb;
$sql = "SELECT * FROM {$this->dbTable} WHERE users_id = %d AND blogs_id = %d AND deleted = 0 ";
return $wpdb->get_row( $wpdb->prepare( $sql, $this->userId, $blog_id ), ARRAY_A ); // @codingStandardsIgnoreLine
} | php | function getBook( $blog_id ) {
/** @var $wpdb \wpdb */
global $wpdb;
$sql = "SELECT * FROM {$this->dbTable} WHERE users_id = %d AND blogs_id = %d AND deleted = 0 ";
return $wpdb->get_row( $wpdb->prepare( $sql, $this->userId, $blog_id ), ARRAY_A ); // @codingStandardsIgnoreLine
} | [
"function",
"getBook",
"(",
"$",
"blog_id",
")",
"{",
"/** @var $wpdb \\wpdb */",
"global",
"$",
"wpdb",
";",
"$",
"sql",
"=",
"\"SELECT * FROM {$this->dbTable} WHERE users_id = %d AND blogs_id = %d AND deleted = 0 \"",
";",
"return",
"$",
"wpdb",
"->",
"get_row",
"(",
"$",
"wpdb",
"->",
"prepare",
"(",
"$",
"sql",
",",
"$",
"this",
"->",
"userId",
",",
"$",
"blog_id",
")",
",",
"ARRAY_A",
")",
";",
"// @codingStandardsIgnoreLine",
"}"
] | Get a book from a user catalog.
@param int $blog_id
@return array | [
"Get",
"a",
"book",
"from",
"a",
"user",
"catalog",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-catalog.php#L403-L411 |
pressbooks/pressbooks | inc/class-catalog.php | Catalog.getBookIds | function getBookIds() {
/** @var $wpdb \wpdb */
global $wpdb;
$sql = "SELECT blogs_id FROM {$this->dbTable} WHERE users_id = %d AND deleted = 0 ";
return $wpdb->get_col( $wpdb->prepare( $sql, $this->userId ) ); // @codingStandardsIgnoreLine
} | php | function getBookIds() {
/** @var $wpdb \wpdb */
global $wpdb;
$sql = "SELECT blogs_id FROM {$this->dbTable} WHERE users_id = %d AND deleted = 0 ";
return $wpdb->get_col( $wpdb->prepare( $sql, $this->userId ) ); // @codingStandardsIgnoreLine
} | [
"function",
"getBookIds",
"(",
")",
"{",
"/** @var $wpdb \\wpdb */",
"global",
"$",
"wpdb",
";",
"$",
"sql",
"=",
"\"SELECT blogs_id FROM {$this->dbTable} WHERE users_id = %d AND deleted = 0 \"",
";",
"return",
"$",
"wpdb",
"->",
"get_col",
"(",
"$",
"wpdb",
"->",
"prepare",
"(",
"$",
"sql",
",",
"$",
"this",
"->",
"userId",
")",
")",
";",
"// @codingStandardsIgnoreLine",
"}"
] | Get only blog IDs.
@return array | [
"Get",
"only",
"blog",
"IDs",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-catalog.php#L419-L427 |
pressbooks/pressbooks | inc/class-catalog.php | Catalog.saveBook | function saveBook( $blog_id, array $item ) {
/** @var $wpdb \wpdb */
global $wpdb;
unset( $item['users_id'], $item['blogs_id'], $item['deleted'] ); // Don't allow spoofing
$data = [
'users_id' => $this->userId,
'blogs_id' => $blog_id,
'deleted' => 0,
];
$format = [
'users_id' => $this->dbColumns['users_id'],
'blogs_id' => $this->dbColumns['blogs_id'],
'deleted' => $this->dbColumns['deleted'],
];
foreach ( $item as $key => $val ) {
if ( isset( $this->dbColumns[ $key ] ) ) {
$data[ $key ] = $val;
$format[ $key ] = $this->dbColumns[ $key ];
}
}
// INSERT ... ON DUPLICATE KEY UPDATE
// @see http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
$args = [];
$sql = "INSERT INTO {$this->dbTable} ( ";
foreach ( $data as $key => $val ) {
$sql .= "`$key`, ";
}
$sql = rtrim( $sql, ', ' ) . ' ) VALUES ( ';
foreach ( $format as $key => $val ) {
$sql .= $val . ', ';
$args[] = $data[ $key ];
}
$sql = rtrim( $sql, ', ' ) . ' ) ON DUPLICATE KEY UPDATE ';
$i = 0;
foreach ( $data as $key => $val ) {
if ( 'users_id' === $key || 'blogs_id' === $key ) {
continue;
}
$sql .= "`$key` = {$format[$key]}, ";
$args[] = $val;
++$i;
}
$sql = rtrim( $sql, ', ' );
if ( ! $i ) {
$sql .= ' users_id = users_id '; // Do nothing
}
return $wpdb->query( $wpdb->prepare( $sql, $args ) ); // @codingStandardsIgnoreLine
} | php | function saveBook( $blog_id, array $item ) {
/** @var $wpdb \wpdb */
global $wpdb;
unset( $item['users_id'], $item['blogs_id'], $item['deleted'] ); // Don't allow spoofing
$data = [
'users_id' => $this->userId,
'blogs_id' => $blog_id,
'deleted' => 0,
];
$format = [
'users_id' => $this->dbColumns['users_id'],
'blogs_id' => $this->dbColumns['blogs_id'],
'deleted' => $this->dbColumns['deleted'],
];
foreach ( $item as $key => $val ) {
if ( isset( $this->dbColumns[ $key ] ) ) {
$data[ $key ] = $val;
$format[ $key ] = $this->dbColumns[ $key ];
}
}
// INSERT ... ON DUPLICATE KEY UPDATE
// @see http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
$args = [];
$sql = "INSERT INTO {$this->dbTable} ( ";
foreach ( $data as $key => $val ) {
$sql .= "`$key`, ";
}
$sql = rtrim( $sql, ', ' ) . ' ) VALUES ( ';
foreach ( $format as $key => $val ) {
$sql .= $val . ', ';
$args[] = $data[ $key ];
}
$sql = rtrim( $sql, ', ' ) . ' ) ON DUPLICATE KEY UPDATE ';
$i = 0;
foreach ( $data as $key => $val ) {
if ( 'users_id' === $key || 'blogs_id' === $key ) {
continue;
}
$sql .= "`$key` = {$format[$key]}, ";
$args[] = $val;
++$i;
}
$sql = rtrim( $sql, ', ' );
if ( ! $i ) {
$sql .= ' users_id = users_id '; // Do nothing
}
return $wpdb->query( $wpdb->prepare( $sql, $args ) ); // @codingStandardsIgnoreLine
} | [
"function",
"saveBook",
"(",
"$",
"blog_id",
",",
"array",
"$",
"item",
")",
"{",
"/** @var $wpdb \\wpdb */",
"global",
"$",
"wpdb",
";",
"unset",
"(",
"$",
"item",
"[",
"'users_id'",
"]",
",",
"$",
"item",
"[",
"'blogs_id'",
"]",
",",
"$",
"item",
"[",
"'deleted'",
"]",
")",
";",
"// Don't allow spoofing",
"$",
"data",
"=",
"[",
"'users_id'",
"=>",
"$",
"this",
"->",
"userId",
",",
"'blogs_id'",
"=>",
"$",
"blog_id",
",",
"'deleted'",
"=>",
"0",
",",
"]",
";",
"$",
"format",
"=",
"[",
"'users_id'",
"=>",
"$",
"this",
"->",
"dbColumns",
"[",
"'users_id'",
"]",
",",
"'blogs_id'",
"=>",
"$",
"this",
"->",
"dbColumns",
"[",
"'blogs_id'",
"]",
",",
"'deleted'",
"=>",
"$",
"this",
"->",
"dbColumns",
"[",
"'deleted'",
"]",
",",
"]",
";",
"foreach",
"(",
"$",
"item",
"as",
"$",
"key",
"=>",
"$",
"val",
")",
"{",
"if",
"(",
"isset",
"(",
"$",
"this",
"->",
"dbColumns",
"[",
"$",
"key",
"]",
")",
")",
"{",
"$",
"data",
"[",
"$",
"key",
"]",
"=",
"$",
"val",
";",
"$",
"format",
"[",
"$",
"key",
"]",
"=",
"$",
"this",
"->",
"dbColumns",
"[",
"$",
"key",
"]",
";",
"}",
"}",
"// INSERT ... ON DUPLICATE KEY UPDATE",
"// @see http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html",
"$",
"args",
"=",
"[",
"]",
";",
"$",
"sql",
"=",
"\"INSERT INTO {$this->dbTable} ( \"",
";",
"foreach",
"(",
"$",
"data",
"as",
"$",
"key",
"=>",
"$",
"val",
")",
"{",
"$",
"sql",
".=",
"\"`$key`, \"",
";",
"}",
"$",
"sql",
"=",
"rtrim",
"(",
"$",
"sql",
",",
"', '",
")",
".",
"' ) VALUES ( '",
";",
"foreach",
"(",
"$",
"format",
"as",
"$",
"key",
"=>",
"$",
"val",
")",
"{",
"$",
"sql",
".=",
"$",
"val",
".",
"', '",
";",
"$",
"args",
"[",
"]",
"=",
"$",
"data",
"[",
"$",
"key",
"]",
";",
"}",
"$",
"sql",
"=",
"rtrim",
"(",
"$",
"sql",
",",
"', '",
")",
".",
"' ) ON DUPLICATE KEY UPDATE '",
";",
"$",
"i",
"=",
"0",
";",
"foreach",
"(",
"$",
"data",
"as",
"$",
"key",
"=>",
"$",
"val",
")",
"{",
"if",
"(",
"'users_id'",
"===",
"$",
"key",
"||",
"'blogs_id'",
"===",
"$",
"key",
")",
"{",
"continue",
";",
"}",
"$",
"sql",
".=",
"\"`$key` = {$format[$key]}, \"",
";",
"$",
"args",
"[",
"]",
"=",
"$",
"val",
";",
"++",
"$",
"i",
";",
"}",
"$",
"sql",
"=",
"rtrim",
"(",
"$",
"sql",
",",
"', '",
")",
";",
"if",
"(",
"!",
"$",
"i",
")",
"{",
"$",
"sql",
".=",
"' users_id = users_id '",
";",
"// Do nothing",
"}",
"return",
"$",
"wpdb",
"->",
"query",
"(",
"$",
"wpdb",
"->",
"prepare",
"(",
"$",
"sql",
",",
"$",
"args",
")",
")",
";",
"// @codingStandardsIgnoreLine",
"}"
] | Save a book to a user catalog.
@param $blog_id
@param array $item
@return int|false | [
"Save",
"a",
"book",
"to",
"a",
"user",
"catalog",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-catalog.php#L438-L494 |
pressbooks/pressbooks | inc/class-catalog.php | Catalog.getTags | function getTags( $tag_group, $show_hidden_tags = true ) {
/** @var $wpdb \wpdb */
global $wpdb;
$sql = "SELECT DISTINCT {$this->dbTagsTable}.id, {$this->dbTagsTable}.tag FROM {$this->dbTagsTable}
INNER JOIN {$this->dbLinkTable} ON {$this->dbLinkTable}.tags_id = {$this->dbTagsTable}.id
INNER JOIN {$this->dbTable} ON {$this->dbTable}.users_id = {$this->dbLinkTable}.users_id AND {$this->dbTable}.blogs_id = {$this->dbLinkTable}.blogs_id
WHERE {$this->dbLinkTable}.tags_group = %d AND {$this->dbLinkTable}.users_id = %d ";
if ( true !== $show_hidden_tags ) {
$sql .= "AND {$this->dbTable}.deleted = 0 ";
}
$sql .= "ORDER BY {$this->dbTagsTable}.tag ASC ";
return $wpdb->get_results( $wpdb->prepare( $sql, $tag_group, $this->userId ), ARRAY_A ); // @codingStandardsIgnoreLine
} | php | function getTags( $tag_group, $show_hidden_tags = true ) {
/** @var $wpdb \wpdb */
global $wpdb;
$sql = "SELECT DISTINCT {$this->dbTagsTable}.id, {$this->dbTagsTable}.tag FROM {$this->dbTagsTable}
INNER JOIN {$this->dbLinkTable} ON {$this->dbLinkTable}.tags_id = {$this->dbTagsTable}.id
INNER JOIN {$this->dbTable} ON {$this->dbTable}.users_id = {$this->dbLinkTable}.users_id AND {$this->dbTable}.blogs_id = {$this->dbLinkTable}.blogs_id
WHERE {$this->dbLinkTable}.tags_group = %d AND {$this->dbLinkTable}.users_id = %d ";
if ( true !== $show_hidden_tags ) {
$sql .= "AND {$this->dbTable}.deleted = 0 ";
}
$sql .= "ORDER BY {$this->dbTagsTable}.tag ASC ";
return $wpdb->get_results( $wpdb->prepare( $sql, $tag_group, $this->userId ), ARRAY_A ); // @codingStandardsIgnoreLine
} | [
"function",
"getTags",
"(",
"$",
"tag_group",
",",
"$",
"show_hidden_tags",
"=",
"true",
")",
"{",
"/** @var $wpdb \\wpdb */",
"global",
"$",
"wpdb",
";",
"$",
"sql",
"=",
"\"SELECT DISTINCT {$this->dbTagsTable}.id, {$this->dbTagsTable}.tag FROM {$this->dbTagsTable}\n \t\t\t\tINNER JOIN {$this->dbLinkTable} ON {$this->dbLinkTable}.tags_id = {$this->dbTagsTable}.id\n \t\t\t\tINNER JOIN {$this->dbTable} ON {$this->dbTable}.users_id = {$this->dbLinkTable}.users_id AND {$this->dbTable}.blogs_id = {$this->dbLinkTable}.blogs_id\n \t\t\t\tWHERE {$this->dbLinkTable}.tags_group = %d AND {$this->dbLinkTable}.users_id = %d \"",
";",
"if",
"(",
"true",
"!==",
"$",
"show_hidden_tags",
")",
"{",
"$",
"sql",
".=",
"\"AND {$this->dbTable}.deleted = 0 \"",
";",
"}",
"$",
"sql",
".=",
"\"ORDER BY {$this->dbTagsTable}.tag ASC \"",
";",
"return",
"$",
"wpdb",
"->",
"get_results",
"(",
"$",
"wpdb",
"->",
"prepare",
"(",
"$",
"sql",
",",
"$",
"tag_group",
",",
"$",
"this",
"->",
"userId",
")",
",",
"ARRAY_A",
")",
";",
"// @codingStandardsIgnoreLine",
"}"
] | Get tags
@param int $tag_group
@param bool $show_hidden_tags (optional)
@return array | [
"Get",
"tags"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-catalog.php#L538-L554 |
pressbooks/pressbooks | inc/class-catalog.php | Catalog.getTagsByBook | function getTagsByBook( $blog_id, $tag_group ) {
/** @var $wpdb \wpdb */
global $wpdb;
$sql = "SELECT DISTINCT {$this->dbTagsTable}.id, {$this->dbTagsTable}.tag FROM {$this->dbTagsTable}
INNER JOIN {$this->dbLinkTable} ON {$this->dbLinkTable}.tags_id = {$this->dbTagsTable}.id
INNER JOIN {$this->dbTable} ON {$this->dbTable}.users_id = {$this->dbLinkTable}.users_id AND {$this->dbTable}.blogs_id = {$this->dbLinkTable}.blogs_id
WHERE {$this->dbLinkTable}.tags_group = %d AND {$this->dbLinkTable}.users_id = %d AND {$this->dbLinkTable}.blogs_id = %d
ORDER BY {$this->dbTagsTable}.tag ASC ";
return $wpdb->get_results( $wpdb->prepare( $sql, $tag_group, $this->userId, $blog_id ), ARRAY_A ); // @codingStandardsIgnoreLine
} | php | function getTagsByBook( $blog_id, $tag_group ) {
/** @var $wpdb \wpdb */
global $wpdb;
$sql = "SELECT DISTINCT {$this->dbTagsTable}.id, {$this->dbTagsTable}.tag FROM {$this->dbTagsTable}
INNER JOIN {$this->dbLinkTable} ON {$this->dbLinkTable}.tags_id = {$this->dbTagsTable}.id
INNER JOIN {$this->dbTable} ON {$this->dbTable}.users_id = {$this->dbLinkTable}.users_id AND {$this->dbTable}.blogs_id = {$this->dbLinkTable}.blogs_id
WHERE {$this->dbLinkTable}.tags_group = %d AND {$this->dbLinkTable}.users_id = %d AND {$this->dbLinkTable}.blogs_id = %d
ORDER BY {$this->dbTagsTable}.tag ASC ";
return $wpdb->get_results( $wpdb->prepare( $sql, $tag_group, $this->userId, $blog_id ), ARRAY_A ); // @codingStandardsIgnoreLine
} | [
"function",
"getTagsByBook",
"(",
"$",
"blog_id",
",",
"$",
"tag_group",
")",
"{",
"/** @var $wpdb \\wpdb */",
"global",
"$",
"wpdb",
";",
"$",
"sql",
"=",
"\"SELECT DISTINCT {$this->dbTagsTable}.id, {$this->dbTagsTable}.tag FROM {$this->dbTagsTable}\n \t\t\t\tINNER JOIN {$this->dbLinkTable} ON {$this->dbLinkTable}.tags_id = {$this->dbTagsTable}.id\n \t\t\t\tINNER JOIN {$this->dbTable} ON {$this->dbTable}.users_id = {$this->dbLinkTable}.users_id AND {$this->dbTable}.blogs_id = {$this->dbLinkTable}.blogs_id\n \t\t\t\tWHERE {$this->dbLinkTable}.tags_group = %d AND {$this->dbLinkTable}.users_id = %d AND {$this->dbLinkTable}.blogs_id = %d\n \t\t\t\tORDER BY {$this->dbTagsTable}.tag ASC \"",
";",
"return",
"$",
"wpdb",
"->",
"get_results",
"(",
"$",
"wpdb",
"->",
"prepare",
"(",
"$",
"sql",
",",
"$",
"tag_group",
",",
"$",
"this",
"->",
"userId",
",",
"$",
"blog_id",
")",
",",
"ARRAY_A",
")",
";",
"// @codingStandardsIgnoreLine",
"}"
] | Get all tags for a book
@param int $blog_id
@param int $tag_group
@return array | [
"Get",
"all",
"tags",
"for",
"a",
"book"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-catalog.php#L565-L577 |
pressbooks/pressbooks | inc/class-catalog.php | Catalog.saveTag | function saveTag( $tag, $blog_id, $tag_group ) {
/** @var $wpdb \wpdb */
global $wpdb;
$tag = strip_tags( $tag );
$tag = trim( $tag );
// INSERT ... ON DUPLICATE KEY UPDATE
// @see http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
$sql = "INSERT INTO {$this->dbTagsTable} ( users_id, tag ) VALUES ( %d, %s ) ON DUPLICATE KEY UPDATE id = id ";
$_ = $wpdb->query( $wpdb->prepare( $sql, $this->userId, $tag ) ); // @codingStandardsIgnoreLine
// Get ID
$sql = "SELECT id FROM {$this->dbTagsTable} WHERE tag = %s ";
$tag_id = $wpdb->get_var( $wpdb->prepare( $sql, $tag ) ); // @codingStandardsIgnoreLine
// Create JOIN
$sql = "INSERT INTO {$this->dbLinkTable} ( users_id, blogs_id, tags_id, tags_group ) VALUES ( %d, %d, %d, %d ) ON DUPLICATE KEY UPDATE users_id = users_id ";
$result = $wpdb->query( $wpdb->prepare( $sql, $this->userId, $blog_id, $tag_id, $tag_group ) ); // @codingStandardsIgnoreLine
return $result;
} | php | function saveTag( $tag, $blog_id, $tag_group ) {
/** @var $wpdb \wpdb */
global $wpdb;
$tag = strip_tags( $tag );
$tag = trim( $tag );
// INSERT ... ON DUPLICATE KEY UPDATE
// @see http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
$sql = "INSERT INTO {$this->dbTagsTable} ( users_id, tag ) VALUES ( %d, %s ) ON DUPLICATE KEY UPDATE id = id ";
$_ = $wpdb->query( $wpdb->prepare( $sql, $this->userId, $tag ) ); // @codingStandardsIgnoreLine
// Get ID
$sql = "SELECT id FROM {$this->dbTagsTable} WHERE tag = %s ";
$tag_id = $wpdb->get_var( $wpdb->prepare( $sql, $tag ) ); // @codingStandardsIgnoreLine
// Create JOIN
$sql = "INSERT INTO {$this->dbLinkTable} ( users_id, blogs_id, tags_id, tags_group ) VALUES ( %d, %d, %d, %d ) ON DUPLICATE KEY UPDATE users_id = users_id ";
$result = $wpdb->query( $wpdb->prepare( $sql, $this->userId, $blog_id, $tag_id, $tag_group ) ); // @codingStandardsIgnoreLine
return $result;
} | [
"function",
"saveTag",
"(",
"$",
"tag",
",",
"$",
"blog_id",
",",
"$",
"tag_group",
")",
"{",
"/** @var $wpdb \\wpdb */",
"global",
"$",
"wpdb",
";",
"$",
"tag",
"=",
"strip_tags",
"(",
"$",
"tag",
")",
";",
"$",
"tag",
"=",
"trim",
"(",
"$",
"tag",
")",
";",
"// INSERT ... ON DUPLICATE KEY UPDATE",
"// @see http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html",
"$",
"sql",
"=",
"\"INSERT INTO {$this->dbTagsTable} ( users_id, tag ) VALUES ( %d, %s ) ON DUPLICATE KEY UPDATE id = id \"",
";",
"$",
"_",
"=",
"$",
"wpdb",
"->",
"query",
"(",
"$",
"wpdb",
"->",
"prepare",
"(",
"$",
"sql",
",",
"$",
"this",
"->",
"userId",
",",
"$",
"tag",
")",
")",
";",
"// @codingStandardsIgnoreLine",
"// Get ID",
"$",
"sql",
"=",
"\"SELECT id FROM {$this->dbTagsTable} WHERE tag = %s \"",
";",
"$",
"tag_id",
"=",
"$",
"wpdb",
"->",
"get_var",
"(",
"$",
"wpdb",
"->",
"prepare",
"(",
"$",
"sql",
",",
"$",
"tag",
")",
")",
";",
"// @codingStandardsIgnoreLine",
"// Create JOIN",
"$",
"sql",
"=",
"\"INSERT INTO {$this->dbLinkTable} ( users_id, blogs_id, tags_id, tags_group ) VALUES ( %d, %d, %d, %d ) ON DUPLICATE KEY UPDATE users_id = users_id \"",
";",
"$",
"result",
"=",
"$",
"wpdb",
"->",
"query",
"(",
"$",
"wpdb",
"->",
"prepare",
"(",
"$",
"sql",
",",
"$",
"this",
"->",
"userId",
",",
"$",
"blog_id",
",",
"$",
"tag_id",
",",
"$",
"tag_group",
")",
")",
";",
"// @codingStandardsIgnoreLine",
"return",
"$",
"result",
";",
"}"
] | Save tag
@param string $tag
@param int $blog_id
@param int $tag_group
@return int|false | [
"Save",
"tag"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-catalog.php#L589-L614 |
pressbooks/pressbooks | inc/class-catalog.php | Catalog.deleteTag | function deleteTag( $tag, $blog_id, $tag_group, $for_real = false ) {
/** @var $wpdb \wpdb */
global $wpdb;
// Get ID
$sql = "SELECT id FROM {$this->dbTagsTable} WHERE tag = %s ";
$tag_id = $wpdb->get_var( $wpdb->prepare( $sql, $tag ) ); // @codingStandardsIgnoreLine
if ( ! $tag_id ) {
return false;
}
if ( $for_real && is_super_admin() ) {
$wpdb->delete(
$this->dbLinkTable, [
'tags_id' => $tag_id,
], [ '%d' ]
);
$wpdb->delete(
$this->dbTagsTable, [
'id' => $tag_id,
], [ '%d' ]
);
$result = 1;
} else {
$result = $wpdb->delete(
$this->dbLinkTable,
[
'users_id' => $this->userId,
'blogs_id' => $blog_id,
'tags_id' => $tag_id,
'tags_group' => $tag_group,
],
[ '%d', '%d', '%d', '%d' ]
);
}
// TODO:
// Optimize the links table: $wpdb->query( "OPTIMIZE TABLE {$this->dbLinkTable} " );
// Optimize the tags table: $wpdb->query( "OPTIMIZE TABLE {$this->dbTagsTable} " );
return $result;
} | php | function deleteTag( $tag, $blog_id, $tag_group, $for_real = false ) {
/** @var $wpdb \wpdb */
global $wpdb;
// Get ID
$sql = "SELECT id FROM {$this->dbTagsTable} WHERE tag = %s ";
$tag_id = $wpdb->get_var( $wpdb->prepare( $sql, $tag ) ); // @codingStandardsIgnoreLine
if ( ! $tag_id ) {
return false;
}
if ( $for_real && is_super_admin() ) {
$wpdb->delete(
$this->dbLinkTable, [
'tags_id' => $tag_id,
], [ '%d' ]
);
$wpdb->delete(
$this->dbTagsTable, [
'id' => $tag_id,
], [ '%d' ]
);
$result = 1;
} else {
$result = $wpdb->delete(
$this->dbLinkTable,
[
'users_id' => $this->userId,
'blogs_id' => $blog_id,
'tags_id' => $tag_id,
'tags_group' => $tag_group,
],
[ '%d', '%d', '%d', '%d' ]
);
}
// TODO:
// Optimize the links table: $wpdb->query( "OPTIMIZE TABLE {$this->dbLinkTable} " );
// Optimize the tags table: $wpdb->query( "OPTIMIZE TABLE {$this->dbTagsTable} " );
return $result;
} | [
"function",
"deleteTag",
"(",
"$",
"tag",
",",
"$",
"blog_id",
",",
"$",
"tag_group",
",",
"$",
"for_real",
"=",
"false",
")",
"{",
"/** @var $wpdb \\wpdb */",
"global",
"$",
"wpdb",
";",
"// Get ID",
"$",
"sql",
"=",
"\"SELECT id FROM {$this->dbTagsTable} WHERE tag = %s \"",
";",
"$",
"tag_id",
"=",
"$",
"wpdb",
"->",
"get_var",
"(",
"$",
"wpdb",
"->",
"prepare",
"(",
"$",
"sql",
",",
"$",
"tag",
")",
")",
";",
"// @codingStandardsIgnoreLine",
"if",
"(",
"!",
"$",
"tag_id",
")",
"{",
"return",
"false",
";",
"}",
"if",
"(",
"$",
"for_real",
"&&",
"is_super_admin",
"(",
")",
")",
"{",
"$",
"wpdb",
"->",
"delete",
"(",
"$",
"this",
"->",
"dbLinkTable",
",",
"[",
"'tags_id'",
"=>",
"$",
"tag_id",
",",
"]",
",",
"[",
"'%d'",
"]",
")",
";",
"$",
"wpdb",
"->",
"delete",
"(",
"$",
"this",
"->",
"dbTagsTable",
",",
"[",
"'id'",
"=>",
"$",
"tag_id",
",",
"]",
",",
"[",
"'%d'",
"]",
")",
";",
"$",
"result",
"=",
"1",
";",
"}",
"else",
"{",
"$",
"result",
"=",
"$",
"wpdb",
"->",
"delete",
"(",
"$",
"this",
"->",
"dbLinkTable",
",",
"[",
"'users_id'",
"=>",
"$",
"this",
"->",
"userId",
",",
"'blogs_id'",
"=>",
"$",
"blog_id",
",",
"'tags_id'",
"=>",
"$",
"tag_id",
",",
"'tags_group'",
"=>",
"$",
"tag_group",
",",
"]",
",",
"[",
"'%d'",
",",
"'%d'",
",",
"'%d'",
",",
"'%d'",
"]",
")",
";",
"}",
"// TODO:",
"// Optimize the links table: $wpdb->query( \"OPTIMIZE TABLE {$this->dbLinkTable} \" );",
"// Optimize the tags table: $wpdb->query( \"OPTIMIZE TABLE {$this->dbTagsTable} \" );",
"return",
"$",
"result",
";",
"}"
] | Delete a tag.
IMPORTANT: The 'for_real' option is extremely destructive. Do not use unless you know what you are doing.
@param string $tag
@param int $blog_id
@param int $tag_group
@param bool $for_real (optional)
@return int|false | [
"Delete",
"a",
"tag",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-catalog.php#L629-L673 |
pressbooks/pressbooks | inc/class-catalog.php | Catalog.deleteTags | function deleteTags( $blog_id, $tag_group ) {
/** @var $wpdb \wpdb */
global $wpdb;
$result = $wpdb->delete(
$this->dbLinkTable, [
'users_id' => $this->userId,
'blogs_id' => $blog_id,
'tags_group' => $tag_group,
], [ '%d', '%d', '%d' ]
);
// TODO:
// Optimize the links table: $wpdb->query( "OPTIMIZE TABLE {$this->dbLinkTable} " );
return $result;
} | php | function deleteTags( $blog_id, $tag_group ) {
/** @var $wpdb \wpdb */
global $wpdb;
$result = $wpdb->delete(
$this->dbLinkTable, [
'users_id' => $this->userId,
'blogs_id' => $blog_id,
'tags_group' => $tag_group,
], [ '%d', '%d', '%d' ]
);
// TODO:
// Optimize the links table: $wpdb->query( "OPTIMIZE TABLE {$this->dbLinkTable} " );
return $result;
} | [
"function",
"deleteTags",
"(",
"$",
"blog_id",
",",
"$",
"tag_group",
")",
"{",
"/** @var $wpdb \\wpdb */",
"global",
"$",
"wpdb",
";",
"$",
"result",
"=",
"$",
"wpdb",
"->",
"delete",
"(",
"$",
"this",
"->",
"dbLinkTable",
",",
"[",
"'users_id'",
"=>",
"$",
"this",
"->",
"userId",
",",
"'blogs_id'",
"=>",
"$",
"blog_id",
",",
"'tags_group'",
"=>",
"$",
"tag_group",
",",
"]",
",",
"[",
"'%d'",
",",
"'%d'",
",",
"'%d'",
"]",
")",
";",
"// TODO:",
"// Optimize the links table: $wpdb->query( \"OPTIMIZE TABLE {$this->dbLinkTable} \" );",
"return",
"$",
"result",
";",
"}"
] | Delete all tags from a user catalog
Note: Doesn't actually delete a tag, just removes the association in dbLinkTable
@param $blog_id
@param $tag_group
@return int|false | [
"Delete",
"all",
"tags",
"from",
"a",
"user",
"catalog"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-catalog.php#L686-L704 |
pressbooks/pressbooks | inc/class-catalog.php | Catalog.getProfile | function getProfile() {
$profile['users_id'] = $this->userId;
foreach ( $this->profileMetaKeys as $key => $type ) {
$profile[ $key ] = get_user_meta( $this->userId, $key, true );
}
return $profile;
} | php | function getProfile() {
$profile['users_id'] = $this->userId;
foreach ( $this->profileMetaKeys as $key => $type ) {
$profile[ $key ] = get_user_meta( $this->userId, $key, true );
}
return $profile;
} | [
"function",
"getProfile",
"(",
")",
"{",
"$",
"profile",
"[",
"'users_id'",
"]",
"=",
"$",
"this",
"->",
"userId",
";",
"foreach",
"(",
"$",
"this",
"->",
"profileMetaKeys",
"as",
"$",
"key",
"=>",
"$",
"type",
")",
"{",
"$",
"profile",
"[",
"$",
"key",
"]",
"=",
"get_user_meta",
"(",
"$",
"this",
"->",
"userId",
",",
"$",
"key",
",",
"true",
")",
";",
"}",
"return",
"$",
"profile",
";",
"}"
] | Get catalog profile.
@return array | [
"Get",
"catalog",
"profile",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-catalog.php#L721-L729 |
pressbooks/pressbooks | inc/class-catalog.php | Catalog.saveProfile | function saveProfile( array $item ) {
// Sanitize
$item = array_intersect_key( $item, $this->profileMetaKeys );
foreach ( $item as $key => $val ) {
if ( 'pb_catalog_logo' === $key ) {
continue; // Skip, dev should use uploadLogo() instead
}
if ( 'pb_catalog_url' === $key && $val ) {
$val = \Pressbooks\Sanitize\canonicalize_url( $val );
}
if ( '%d' === $this->profileMetaKeys[ $key ] ) {
$val = (int) $val;
} elseif ( '%f' === $this->profileMetaKeys[ $key ] ) {
$val = (float) $val;
} else {
$val = (string) $val;
}
update_user_meta( $this->userId, $key, $val );
}
} | php | function saveProfile( array $item ) {
// Sanitize
$item = array_intersect_key( $item, $this->profileMetaKeys );
foreach ( $item as $key => $val ) {
if ( 'pb_catalog_logo' === $key ) {
continue; // Skip, dev should use uploadLogo() instead
}
if ( 'pb_catalog_url' === $key && $val ) {
$val = \Pressbooks\Sanitize\canonicalize_url( $val );
}
if ( '%d' === $this->profileMetaKeys[ $key ] ) {
$val = (int) $val;
} elseif ( '%f' === $this->profileMetaKeys[ $key ] ) {
$val = (float) $val;
} else {
$val = (string) $val;
}
update_user_meta( $this->userId, $key, $val );
}
} | [
"function",
"saveProfile",
"(",
"array",
"$",
"item",
")",
"{",
"// Sanitize",
"$",
"item",
"=",
"array_intersect_key",
"(",
"$",
"item",
",",
"$",
"this",
"->",
"profileMetaKeys",
")",
";",
"foreach",
"(",
"$",
"item",
"as",
"$",
"key",
"=>",
"$",
"val",
")",
"{",
"if",
"(",
"'pb_catalog_logo'",
"===",
"$",
"key",
")",
"{",
"continue",
";",
"// Skip, dev should use uploadLogo() instead",
"}",
"if",
"(",
"'pb_catalog_url'",
"===",
"$",
"key",
"&&",
"$",
"val",
")",
"{",
"$",
"val",
"=",
"\\",
"Pressbooks",
"\\",
"Sanitize",
"\\",
"canonicalize_url",
"(",
"$",
"val",
")",
";",
"}",
"if",
"(",
"'%d'",
"===",
"$",
"this",
"->",
"profileMetaKeys",
"[",
"$",
"key",
"]",
")",
"{",
"$",
"val",
"=",
"(",
"int",
")",
"$",
"val",
";",
"}",
"elseif",
"(",
"'%f'",
"===",
"$",
"this",
"->",
"profileMetaKeys",
"[",
"$",
"key",
"]",
")",
"{",
"$",
"val",
"=",
"(",
"float",
")",
"$",
"val",
";",
"}",
"else",
"{",
"$",
"val",
"=",
"(",
"string",
")",
"$",
"val",
";",
"}",
"update_user_meta",
"(",
"$",
"this",
"->",
"userId",
",",
"$",
"key",
",",
"$",
"val",
")",
";",
"}",
"}"
] | Save catalog profile
@param array $item | [
"Save",
"catalog",
"profile"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-catalog.php#L737-L762 |
pressbooks/pressbooks | inc/class-catalog.php | Catalog.deleteCacheByBookId | function deleteCacheByBookId( $book_id ) {
/** @var $wpdb \wpdb */
global $wpdb;
$sql = "SELECT users_id FROM {$this->dbTable} WHERE blogs_id = %d ";
$results = $wpdb->get_col( $wpdb->prepare( $sql, $book_id ) ); // @codingStandardsIgnoreLine
foreach ( $results as $user_id ) {
wp_cache_delete( "cat-$user_id", 'pb' );
}
} | php | function deleteCacheByBookId( $book_id ) {
/** @var $wpdb \wpdb */
global $wpdb;
$sql = "SELECT users_id FROM {$this->dbTable} WHERE blogs_id = %d ";
$results = $wpdb->get_col( $wpdb->prepare( $sql, $book_id ) ); // @codingStandardsIgnoreLine
foreach ( $results as $user_id ) {
wp_cache_delete( "cat-$user_id", 'pb' );
}
} | [
"function",
"deleteCacheByBookId",
"(",
"$",
"book_id",
")",
"{",
"/** @var $wpdb \\wpdb */",
"global",
"$",
"wpdb",
";",
"$",
"sql",
"=",
"\"SELECT users_id FROM {$this->dbTable} WHERE blogs_id = %d \"",
";",
"$",
"results",
"=",
"$",
"wpdb",
"->",
"get_col",
"(",
"$",
"wpdb",
"->",
"prepare",
"(",
"$",
"sql",
",",
"$",
"book_id",
")",
")",
";",
"// @codingStandardsIgnoreLine",
"foreach",
"(",
"$",
"results",
"as",
"$",
"user_id",
")",
"{",
"wp_cache_delete",
"(",
"\"cat-$user_id\"",
",",
"'pb'",
")",
";",
"}",
"}"
] | Delete the cache(s) by Book ID
@param int $book_id | [
"Delete",
"the",
"cache",
"(",
"s",
")",
"by",
"Book",
"ID"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-catalog.php#L845-L856 |
pressbooks/pressbooks | inc/class-catalog.php | Catalog.createOrUpdateTables | protected function createOrUpdateTables() {
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
$sql = "CREATE TABLE {$this->dbTable} (
users_id INT(11) NOT null,
blogs_id INT(11) NOT null,
deleted TINYINT(1) NOT null,
featured INT(11) DEFAULT 0 NOT null ,
PRIMARY KEY (users_id,blogs_id),
KEY featured (featured)
); ";
dbDelta( $sql );
$sql = "CREATE TABLE {$this->dbLinkTable} (
users_id INT(11) NOT null,
blogs_id INT(11) NOT null,
tags_id INT(11) NOT null,
tags_group INT(3) NOT null,
PRIMARY KEY (users_id,blogs_id,tags_id,tags_group)
); ";
dbDelta( $sql );
$sql = "CREATE TABLE {$this->dbTagsTable} (
id INT(11) NOT null AUTO_INCREMENT,
users_id INT(11) NOT null,
tag VARCHAR(200) NOT null,
PRIMARY KEY (id),
UNIQUE KEY tag (tag(191))
); ";
dbDelta( $sql );
} | php | protected function createOrUpdateTables() {
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
$sql = "CREATE TABLE {$this->dbTable} (
users_id INT(11) NOT null,
blogs_id INT(11) NOT null,
deleted TINYINT(1) NOT null,
featured INT(11) DEFAULT 0 NOT null ,
PRIMARY KEY (users_id,blogs_id),
KEY featured (featured)
); ";
dbDelta( $sql );
$sql = "CREATE TABLE {$this->dbLinkTable} (
users_id INT(11) NOT null,
blogs_id INT(11) NOT null,
tags_id INT(11) NOT null,
tags_group INT(3) NOT null,
PRIMARY KEY (users_id,blogs_id,tags_id,tags_group)
); ";
dbDelta( $sql );
$sql = "CREATE TABLE {$this->dbTagsTable} (
id INT(11) NOT null AUTO_INCREMENT,
users_id INT(11) NOT null,
tag VARCHAR(200) NOT null,
PRIMARY KEY (id),
UNIQUE KEY tag (tag(191))
); ";
dbDelta( $sql );
} | [
"protected",
"function",
"createOrUpdateTables",
"(",
")",
"{",
"require_once",
"(",
"ABSPATH",
".",
"'wp-admin/includes/upgrade.php'",
")",
";",
"$",
"sql",
"=",
"\"CREATE TABLE {$this->dbTable} (\n\t\t\t\tusers_id INT(11) NOT null,\n \t\t\t\tblogs_id INT(11) NOT null,\n \t\t\t\tdeleted TINYINT(1) NOT null,\n \t\t\t\tfeatured INT(11) DEFAULT 0 NOT null ,\n \t\t\t\tPRIMARY KEY (users_id,blogs_id),\n \t\t\t\tKEY featured (featured)\n\t\t\t\t); \"",
";",
"dbDelta",
"(",
"$",
"sql",
")",
";",
"$",
"sql",
"=",
"\"CREATE TABLE {$this->dbLinkTable} (\n\t\t\t\tusers_id INT(11) NOT null,\n \t\t\t\tblogs_id INT(11) NOT null,\n \t\t\t\ttags_id INT(11) NOT null,\n \t\t\t\ttags_group INT(3) NOT null,\n \t\t\t\tPRIMARY KEY (users_id,blogs_id,tags_id,tags_group)\n\t\t\t\t); \"",
";",
"dbDelta",
"(",
"$",
"sql",
")",
";",
"$",
"sql",
"=",
"\"CREATE TABLE {$this->dbTagsTable} (\n\t\t\t\tid INT(11) NOT null AUTO_INCREMENT,\n \t\t\t\tusers_id INT(11) NOT null,\n \t\t\t\ttag VARCHAR(200) NOT null,\n \t\t\t\tPRIMARY KEY (id),\n \t\t\t\tUNIQUE KEY tag (tag(191))\n\t\t\t\t); \"",
";",
"dbDelta",
"(",
"$",
"sql",
")",
";",
"}"
] | DB Delta the initial Catalog tables.
If you change this, then don't forget to also change $this->dbColumns
@see dbColumns
@see http://codex.wordpress.org/Creating_Tables_with_Plugins#Creating_or_Updating_the_Table | [
"DB",
"Delta",
"the",
"initial",
"Catalog",
"tables",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-catalog.php#L885-L916 |
pressbooks/pressbooks | inc/class-catalog.php | Catalog.stringToTags | static function stringToTags( $tags ) {
$tags = mb_split( ',', $tags );
foreach ( $tags as $key => &$val ) {
$val = strip_tags( $val );
$val = mb_convert_case( $val, MB_CASE_TITLE, 'UTF-8' );
$val = mb_split( '\W', $val ); // Split on negated \w
$val = implode( ' ', $val ); // Put back together with spaces
$val = trim( $val );
if ( ! $val ) {
unset( $tags[ $key ] );
}
}
return $tags;
} | php | static function stringToTags( $tags ) {
$tags = mb_split( ',', $tags );
foreach ( $tags as $key => &$val ) {
$val = strip_tags( $val );
$val = mb_convert_case( $val, MB_CASE_TITLE, 'UTF-8' );
$val = mb_split( '\W', $val ); // Split on negated \w
$val = implode( ' ', $val ); // Put back together with spaces
$val = trim( $val );
if ( ! $val ) {
unset( $tags[ $key ] );
}
}
return $tags;
} | [
"static",
"function",
"stringToTags",
"(",
"$",
"tags",
")",
"{",
"$",
"tags",
"=",
"mb_split",
"(",
"','",
",",
"$",
"tags",
")",
";",
"foreach",
"(",
"$",
"tags",
"as",
"$",
"key",
"=>",
"&",
"$",
"val",
")",
"{",
"$",
"val",
"=",
"strip_tags",
"(",
"$",
"val",
")",
";",
"$",
"val",
"=",
"mb_convert_case",
"(",
"$",
"val",
",",
"MB_CASE_TITLE",
",",
"'UTF-8'",
")",
";",
"$",
"val",
"=",
"mb_split",
"(",
"'\\W'",
",",
"$",
"val",
")",
";",
"// Split on negated \\w",
"$",
"val",
"=",
"implode",
"(",
"' '",
",",
"$",
"val",
")",
";",
"// Put back together with spaces",
"$",
"val",
"=",
"trim",
"(",
"$",
"val",
")",
";",
"if",
"(",
"!",
"$",
"val",
")",
"{",
"unset",
"(",
"$",
"tags",
"[",
"$",
"key",
"]",
")",
";",
"}",
"}",
"return",
"$",
"tags",
";",
"}"
] | Return an array of tags from a comma delimited string
@param string $tags
@return array | [
"Return",
"an",
"array",
"of",
"tags",
"from",
"a",
"comma",
"delimited",
"string"
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-catalog.php#L931-L947 |
pressbooks/pressbooks | inc/class-catalog.php | Catalog.tagsToString | static function tagsToString( array $tags ) {
$tags = wp_list_sort( $tags, 'tag', 'asc' );
$str = '';
foreach ( $tags as $tag ) {
$str .= $tag['tag'] . ', ';
}
return rtrim( $str, ', ' );
} | php | static function tagsToString( array $tags ) {
$tags = wp_list_sort( $tags, 'tag', 'asc' );
$str = '';
foreach ( $tags as $tag ) {
$str .= $tag['tag'] . ', ';
}
return rtrim( $str, ', ' );
} | [
"static",
"function",
"tagsToString",
"(",
"array",
"$",
"tags",
")",
"{",
"$",
"tags",
"=",
"wp_list_sort",
"(",
"$",
"tags",
",",
"'tag'",
",",
"'asc'",
")",
";",
"$",
"str",
"=",
"''",
";",
"foreach",
"(",
"$",
"tags",
"as",
"$",
"tag",
")",
"{",
"$",
"str",
".=",
"$",
"tag",
"[",
"'tag'",
"]",
".",
"', '",
";",
"}",
"return",
"rtrim",
"(",
"$",
"str",
",",
"', '",
")",
";",
"}"
] | Return a comma delimited string from an SQL array of tags, in alphabetical order.
@param array $tags
@return string | [
"Return",
"a",
"comma",
"delimited",
"string",
"from",
"an",
"SQL",
"array",
"of",
"tags",
"in",
"alphabetical",
"order",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-catalog.php#L957-L967 |
pressbooks/pressbooks | inc/class-catalog.php | Catalog.thumbnailFromUserId | static function thumbnailFromUserId( $user_id, $size ) {
$image_url = get_user_meta( $user_id, 'pb_catalog_logo', true );
$book = get_active_blog_for_user( $user_id );
if ( $book ) {
switch_to_blog( $book->blog_id );
$image_url = \Pressbooks\Image\thumbnail_from_url( $image_url, $size );
restore_current_blog();
}
return $image_url;
} | php | static function thumbnailFromUserId( $user_id, $size ) {
$image_url = get_user_meta( $user_id, 'pb_catalog_logo', true );
$book = get_active_blog_for_user( $user_id );
if ( $book ) {
switch_to_blog( $book->blog_id );
$image_url = \Pressbooks\Image\thumbnail_from_url( $image_url, $size );
restore_current_blog();
}
return $image_url;
} | [
"static",
"function",
"thumbnailFromUserId",
"(",
"$",
"user_id",
",",
"$",
"size",
")",
"{",
"$",
"image_url",
"=",
"get_user_meta",
"(",
"$",
"user_id",
",",
"'pb_catalog_logo'",
",",
"true",
")",
";",
"$",
"book",
"=",
"get_active_blog_for_user",
"(",
"$",
"user_id",
")",
";",
"if",
"(",
"$",
"book",
")",
"{",
"switch_to_blog",
"(",
"$",
"book",
"->",
"blog_id",
")",
";",
"$",
"image_url",
"=",
"\\",
"Pressbooks",
"\\",
"Image",
"\\",
"thumbnail_from_url",
"(",
"$",
"image_url",
",",
"$",
"size",
")",
";",
"restore_current_blog",
"(",
")",
";",
"}",
"return",
"$",
"image_url",
";",
"}"
] | Catalog image is stored in user's active Media Library.
@param int $user_id
@param string $size
@return string | [
"Catalog",
"image",
"is",
"stored",
"in",
"user",
"s",
"active",
"Media",
"Library",
"."
] | train | https://github.com/pressbooks/pressbooks/blob/1a2294414a3abb7b97c5b669cee72470a8fb5806/inc/class-catalog.php#L978-L989 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.