";
echo "" . $toc . "";
echo "−";
ai_post_generator_parse_toc($headings, 0, 0);
echo "";
return ob_get_clean();
}
}
if (!function_exists('ai_post_generator_parse_toc')){
function ai_post_generator_parse_toc($headings, $index, $recursive_counter) {
// prevent errors
if($recursive_counter > 60 || !count($headings)) return;
// get all needed elements
$last_element = $index > 0 ? $headings[$index - 1] : NULL;
$current_element = $headings[$index];
$next_element = NULL;
if($index < count($headings) && isset($headings[$index + 1])) {
$next_element = $headings[$index + 1];
}
// end recursive calls
if($current_element == NULL) return;
// get all needed variables
$tag = intval($headings[$index]["tag"]);
$id = $headings[$index]["id"];
$classes = isset($headings[$index]["classes"]) ? $headings[$index]["classes"] : array();
$name = $headings[$index]["name"];
// element not in toc
if(isset($current_element["classes"]) && $current_element["classes"] && in_array("nitoc", $current_element["classes"])) {
ai_post_generator_parse_toc($headings, $index + 1, $recursive_counter + 1);
return;
}
// parse toc begin or toc subpart begin
if($last_element == NULL) echo "
";
if($last_element != NULL && $last_element["tag"] < $tag) {
for($i = 0; $i < $tag - $last_element["tag"]; $i++) {
echo "";
}
}
// build li class
$li_classes = "";
if(isset($current_element["classes"]) && $current_element["classes"] && in_array("toc-bold", $current_element["classes"])) $li_classes = " class='bold'";
// parse line begin
echo "- ";
// only parse name, when li is not bold
if(isset($current_element["classes"]) && $current_element["classes"] && in_array("toc-bold", $current_element["classes"])) {
echo $name;
} else {
echo "" . $name . "";
}
if($next_element && intval($next_element["tag"]) > $tag) {
ai_post_generator_parse_toc($headings, $index + 1, $recursive_counter + 1);
}
// parse line end
echo "
";
// parse next line
if($next_element && intval($next_element["tag"]) == $tag) {
ai_post_generator_parse_toc($headings, $index + 1, $recursive_counter + 1);
}
// parse toc end or toc subpart end
if ($next_element == NULL || ($next_element && $next_element["tag"] < $tag)) {
echo "
";
if ($next_element && $tag - intval($next_element["tag"]) >= 2) {
echo "";
for($i = 1; $i < $tag - intval($next_element["tag"]); $i++) {
echo "
";
}
}
}
// parse top subpart
if($next_element != NULL && $next_element["tag"] < $tag) {
ai_post_generator_parse_toc($headings, $index + 1, $recursive_counter + 1);
}
}
}
if (!function_exists('get_ai_post_generator_headings')){
function get_ai_post_generator_headings($content, $from_tag = 1, $to_tag = 6) {
$headings = array();
preg_match_all("/(.*)<\/h[" . $from_tag . "-" . $to_tag . "]>/", $content, $matches);
for($i = 0; $i < count($matches[1]); $i++) {
$headings[$i]["tag"] = $matches[1][$i];
// get id
$att_string = $matches[2][$i];
preg_match("/id=\"([^\"]*)\"/", $att_string , $id_matches);
$headings[$i]["id"] = $id_matches[1];
// get classes
$att_string = $matches[2][$i];
preg_match_all("/class=\"([^\"]*)\"/", $att_string , $class_matches);
for($j = 0; $j < count($class_matches[1]); $j++) {
$headings[$i]["classes"] = explode(" ", $class_matches[1][$j]);
}
$headings[$i]["name"] = strip_tags($matches[3][$i]);
}
return $headings;
}
}
if (!function_exists('ai_post_generator_auto_id_headings')){
/**
* Automatically add IDs to headings such as
*/
function ai_post_generator_auto_id_headings( $content ) {
$content = preg_replace_callback('/(\(.*)(<\/h[1-6]>)/i', function( $matches ) {
if(!stripos($matches[0], 'id=')) {
$matches[0] = $matches[1] . $matches[2] . ' id="' . sanitize_title( $matches[3] ) . '">' . $matches[3] . $matches[4];
}
return $matches[0];
}, $content);
return $content;
}
}
if (!function_exists('ai_post_generator_download_img')){
function ai_post_generator_download_img($image_url, $name, $sanitazed_name) {
// it allows us to use download_url() and wp_handle_sideload() functions
require_once( ABSPATH . 'wp-admin/includes/file.php' );
// download to temp dir
$temp_file = download_url( $image_url );
if( is_wp_error( $temp_file ) ) {
return false;
}
$info = getimagesize($temp_file);
$extension = image_type_to_extension($info[2]);
if ($extension!=".jpeg" && $extension!=".gif" && $extension!=".png" && $extension!=".jpg" && $extension!=".webp"){
return false;
}
// move the temp file into the uploads directory
$file = array(
'name' => $sanitazed_name . $extension,
'type' => mime_content_type( $temp_file ),
'tmp_name' => $temp_file,
'size' => filesize( $temp_file ),
);
$sideload = wp_handle_sideload(
$file,
array(
'test_form' => false // no needs to check 'action' parameter
)
);
if( ! empty( $sideload[ 'error' ] ) ) {
// you may return error message if you want
return false;
}
// it is time to add our uploaded image into WordPress media library
$attachment_id = wp_insert_attachment(
array(
'guid' => $sideload[ 'url' ],
'post_mime_type' => $sideload[ 'type' ],
'post_title' => $name,
'post_content' => '',
'post_status' => 'inherit',
),
$sideload[ 'file' ]
);
if( is_wp_error( $attachment_id ) || ! $attachment_id ) {
return false;
}
// update medatata, regenerate image sizes
require_once( ABSPATH . 'wp-admin/includes/image.php' );
wp_update_attachment_metadata(
$attachment_id,
wp_generate_attachment_metadata( $attachment_id, $sideload[ 'file' ] )
);
return $attachment_id;
}
}
?>