idx
int64
0
60.3k
question
stringlengths
101
6.21k
target
stringlengths
7
803
7,900
protected function setMargins ( ) { $ this -> _margeLeft = $ this -> _defaultLeft + ( isset ( $ this -> _background [ 'left' ] ) ? $ this -> _background [ 'left' ] : 0 ) ; $ this -> _margeRight = $ this -> _defaultRight + ( isset ( $ this -> _background [ 'right' ] ) ? $ this -> _background [ 'right' ] : 0 ) ; $ this -> _margeTop = $ this -> _defaultTop + ( isset ( $ this -> _background [ 'top' ] ) ? $ this -> _background [ 'top' ] : 0 ) ; $ this -> _margeBottom = $ this -> _defaultBottom + ( isset ( $ this -> _background [ 'bottom' ] ) ? $ this -> _background [ 'bottom' ] : 0 ) ; $ this -> pdf -> SetMargins ( $ this -> _margeLeft , $ this -> _margeTop , $ this -> _margeRight ) ; $ this -> pdf -> SetAutoPageBreak ( false , $ this -> _margeBottom ) ; $ this -> _pageMarges = array ( ) ; if ( $ this -> _isInParagraph !== false ) { $ this -> _pageMarges [ floor ( $ this -> _margeTop * 100 ) ] = array ( $ this -> _isInParagraph [ 0 ] , $ this -> pdf -> getW ( ) - $ this -> _isInParagraph [ 1 ] ) ; } else { $ this -> _pageMarges [ floor ( $ this -> _margeTop * 100 ) ] = array ( $ this -> _margeLeft , $ this -> pdf -> getW ( ) - $ this -> _margeRight ) ; } }
set the real margin using the default margins and the page margins
7,901
protected function _addMargins ( $ float , $ xLeft , $ yTop , $ xRight , $ yBottom ) { $ oldTop = $ this -> _getMargins ( $ yTop ) ; $ oldBottom = $ this -> _getMargins ( $ yBottom ) ; if ( $ float === 'left' && $ oldTop [ 0 ] < $ xRight ) { $ oldTop [ 0 ] = $ xRight ; } if ( $ float === 'right' && $ oldTop [ 1 ] > $ xLeft ) { $ oldTop [ 1 ] = $ xLeft ; } $ yTop = floor ( $ yTop * 100 ) ; $ yBottom = floor ( $ yBottom * 100 ) ; foreach ( $ this -> _pageMarges as $ mY => $ mX ) { if ( $ mY < $ yTop ) { continue ; } if ( $ mY > $ yBottom ) { break ; } if ( $ float === 'left' && $ this -> _pageMarges [ $ mY ] [ 0 ] < $ xRight ) { unset ( $ this -> _pageMarges [ $ mY ] ) ; } if ( $ float === 'right' && $ this -> _pageMarges [ $ mY ] [ 1 ] > $ xLeft ) { unset ( $ this -> _pageMarges [ $ mY ] ) ; } } $ this -> _pageMarges [ $ yTop ] = $ oldTop ; $ this -> _pageMarges [ $ yBottom ] = $ oldBottom ; ksort ( $ this -> _pageMarges ) ; $ this -> _isAfterFloat = true ; }
Add margins for a float
7,902
protected function _setPageHeader ( ) { if ( ! count ( $ this -> _subHEADER ) ) { return false ; } if ( in_array ( $ this -> pdf -> getPage ( ) , $ this -> _hideHeader ) ) { return false ; } $ oldParsePos = $ this -> _parsePos ; $ oldParseCode = $ this -> parsingHtml -> code ; $ this -> _parsePos = 0 ; $ this -> parsingHtml -> code = $ this -> _subHEADER ; $ this -> _makeHTMLcode ( ) ; $ this -> _parsePos = $ oldParsePos ; $ this -> parsingHtml -> code = $ oldParseCode ; }
draw the PDF header with the HTML in page_header
7,903
protected function _setPageFooter ( ) { if ( ! count ( $ this -> _subFOOTER ) ) { return false ; } if ( in_array ( $ this -> pdf -> getPage ( ) , $ this -> _hideFooter ) ) { return false ; } $ oldParsePos = $ this -> _parsePos ; $ oldParseCode = $ this -> parsingHtml -> code ; $ this -> _parsePos = 0 ; $ this -> parsingHtml -> code = $ this -> _subFOOTER ; $ this -> _isInFooter = true ; $ this -> _makeHTMLcode ( ) ; $ this -> _isInFooter = false ; $ this -> _parsePos = $ oldParsePos ; $ this -> parsingHtml -> code = $ oldParseCode ; }
draw the PDF footer with the HTML in page_footer
7,904
protected function _setNewLine ( $ h , $ curr = null ) { $ this -> pdf -> Ln ( $ h ) ; $ this -> _setNewPositionForNewLine ( $ curr ) ; }
new line with a specific height
7,905
protected function createSubHTML ( ) { if ( self :: $ _subobj === null ) { $ this -> _prepareSubObj ( ) ; } if ( $ this -> parsingCss -> value [ 'width' ] ) { $ marge = $ this -> parsingCss -> value [ 'padding' ] [ 'l' ] + $ this -> parsingCss -> value [ 'padding' ] [ 'r' ] ; $ marge += $ this -> parsingCss -> value [ 'border' ] [ 'l' ] [ 'width' ] + $ this -> parsingCss -> value [ 'border' ] [ 'r' ] [ 'width' ] ; $ marge = $ this -> pdf -> getW ( ) - $ this -> parsingCss -> value [ 'width' ] + $ marge ; } else { $ marge = $ this -> _margeLeft + $ this -> _margeRight ; } self :: $ _subobj -> pdf -> getPage ( ) ; $ subHtml = clone self :: $ _subobj ; $ subHtml -> parsingCss -> table = $ this -> parsingCss -> table ; $ subHtml -> parsingCss -> value = $ this -> parsingCss -> value ; $ subHtml -> initSubHtml ( $ this -> _format , $ this -> _orientation , $ marge , $ this -> _page , $ this -> _defList , $ this -> pdf -> getMyLastPageGroup ( ) , $ this -> pdf -> getMyLastPageGroupNb ( ) ) ; return $ subHtml ; }
create a sub Html2Pdf to calculate the multi - tables
7,906
protected function _listeArab2Rom ( $ nbArabic ) { $ nbBaseTen = array ( 'I' , 'X' , 'C' , 'M' ) ; $ nbBaseFive = array ( 'V' , 'L' , 'D' ) ; $ nbRoman = '' ; if ( $ nbArabic < 1 ) { return $ nbArabic ; } if ( $ nbArabic > 3999 ) { return $ nbArabic ; } for ( $ i = 3 ; $ i >= 0 ; $ i -- ) { $ digit = floor ( $ nbArabic / pow ( 10 , $ i ) ) ; if ( $ digit >= 1 ) { $ nbArabic -= $ digit * pow ( 10 , $ i ) ; if ( $ digit <= 3 ) { for ( $ j = $ digit ; $ j >= 1 ; $ j -- ) { $ nbRoman .= $ nbBaseTen [ $ i ] ; } } elseif ( $ digit == 9 ) { $ nbRoman .= $ nbBaseTen [ $ i ] . $ nbBaseTen [ $ i + 1 ] ; } elseif ( $ digit == 4 ) { $ nbRoman .= $ nbBaseTen [ $ i ] . $ nbBaseFive [ $ i ] ; } else { $ nbRoman .= $ nbBaseFive [ $ i ] ; for ( $ j = $ digit - 5 ; $ j >= 1 ; $ j -- ) { $ nbRoman .= $ nbBaseTen [ $ i ] ; } } } } return $ nbRoman ; }
Convert an arabic number into a roman number
7,907
protected function _listeGetLi ( ) { $ im = $ this -> _defList [ count ( $ this -> _defList ) - 1 ] [ 'img' ] ; $ st = $ this -> _defList [ count ( $ this -> _defList ) - 1 ] [ 'style' ] ; $ nb = $ this -> _defList [ count ( $ this -> _defList ) - 1 ] [ 'nb' ] ; $ up = ( substr ( $ st , 0 , 6 ) === 'upper-' ) ; if ( $ im ) { return array ( false , false , $ im ) ; } switch ( $ st ) { case 'none' : return array ( 'helvetica' , true , ' ' ) ; case 'upper-alpha' : case 'lower-alpha' : $ str = '' ; while ( $ nb > 26 ) { $ str = chr ( 96 + $ nb % 26 ) . $ str ; $ nb = floor ( $ nb / 26 ) ; } $ str = chr ( 96 + $ nb ) . $ str ; return array ( 'helvetica' , false , ( $ up ? strtoupper ( $ str ) : $ str ) . '.' ) ; case 'upper-roman' : case 'lower-roman' : $ str = $ this -> _listeArab2Rom ( $ nb ) ; return array ( 'helvetica' , false , ( $ up ? strtoupper ( $ str ) : $ str ) . '.' ) ; case 'decimal' : return array ( 'helvetica' , false , $ nb . '.' ) ; case 'square' : return array ( 'zapfdingbats' , true , chr ( 110 ) ) ; case 'circle' : return array ( 'zapfdingbats' , true , chr ( 109 ) ) ; case 'disc' : default : return array ( 'zapfdingbats' , true , chr ( 108 ) ) ; } }
get the information of the li on the current level
7,908
protected function _listeAddLevel ( $ type = 'ul' , $ style = '' , $ img = null , $ start = null ) { if ( $ img ) { if ( preg_match ( '/^url\(([^)]+)\)$/isU' , trim ( $ img ) , $ match ) ) { $ img = $ match [ 1 ] ; } else { $ img = null ; } } else { $ img = null ; } if ( ! in_array ( $ type , array ( 'ul' , 'ol' ) ) ) { $ type = 'ul' ; } if ( ! in_array ( $ style , array ( 'lower-alpha' , 'upper-alpha' , 'upper-roman' , 'lower-roman' , 'decimal' , 'square' , 'circle' , 'disc' , 'none' ) ) ) { $ style = '' ; } if ( ! $ style ) { if ( $ type === 'ul' ) { $ style = 'disc' ; } else { $ style = 'decimal' ; } } if ( is_null ( $ start ) || ( int ) $ start < 1 ) { $ start = 0 ; } else { $ start -- ; } $ this -> _defList [ count ( $ this -> _defList ) ] = array ( 'style' => $ style , 'nb' => $ start , 'img' => $ img ) ; }
add a level to the list
7,909
protected function _listeDelLevel ( ) { if ( count ( $ this -> _defList ) ) { unset ( $ this -> _defList [ count ( $ this -> _defList ) - 1 ] ) ; $ this -> _defList = array_values ( $ this -> _defList ) ; } }
remove a level from the list
7,910
protected function _makeHTMLcode ( ) { $ amountHtmlCode = count ( $ this -> parsingHtml -> code ) ; for ( $ this -> _parsePos = 0 ; $ this -> _parsePos < $ amountHtmlCode ; $ this -> _parsePos ++ ) { $ action = $ this -> parsingHtml -> code [ $ this -> _parsePos ] ; if ( in_array ( $ action -> getName ( ) , array ( 'table' , 'ul' , 'ol' ) ) && ! $ action -> isClose ( ) ) { $ this -> _subPart = true ; $ tagOpen = $ action -> getName ( ) ; $ this -> _tempPos = $ this -> _parsePos ; while ( isset ( $ this -> parsingHtml -> code [ $ this -> _tempPos ] ) && ! ( $ this -> parsingHtml -> code [ $ this -> _tempPos ] -> getName ( ) == $ tagOpen && $ this -> parsingHtml -> code [ $ this -> _tempPos ] -> isClose ( ) ) ) { $ this -> _executeAction ( $ this -> parsingHtml -> code [ $ this -> _tempPos ] ) ; $ this -> _tempPos ++ ; } if ( isset ( $ this -> parsingHtml -> code [ $ this -> _tempPos ] ) ) { $ this -> _executeAction ( $ this -> parsingHtml -> code [ $ this -> _tempPos ] ) ; } $ this -> _subPart = false ; } $ this -> _executeAction ( $ action ) ; } }
execute the actions to convert the html
7,911
protected function _executeAction ( Node $ action ) { $ name = strtoupper ( $ action -> getName ( ) ) ; if ( $ this -> _firstPage && $ name !== 'PAGE' && ! $ action -> isClose ( ) ) { $ this -> _setNewPage ( ) ; } $ properties = $ action -> getParams ( ) ; $ fnc = ( $ action -> isClose ( ) ? '_tag_close_' : '_tag_open_' ) . $ name ; $ tagObject = $ this -> getTagObject ( $ action -> getName ( ) ) ; if ( ! is_null ( $ tagObject ) ) { if ( $ action -> isClose ( ) ) { $ res = $ tagObject -> close ( $ properties ) ; } else { $ res = $ tagObject -> open ( $ properties ) ; } } elseif ( is_callable ( array ( $ this , $ fnc ) ) ) { $ res = $ this -> { $ fnc } ( $ properties ) ; } else { $ e = new HtmlParsingException ( 'The html tag [' . $ action -> getName ( ) . '] is not known by Html2Pdf. ' . 'You can create it and push it on the Html2Pdf GitHub project.' ) ; $ e -> setInvalidTag ( $ action -> getName ( ) ) ; $ e -> setHtmlLine ( $ action -> getLine ( ) ) ; throw $ e ; } $ this -> _previousCall = $ fnc ; return $ res ; }
execute the action from the parsing
7,912
protected function _getElementY ( $ h ) { if ( $ this -> _subPart || $ this -> _isSubPart || ! $ this -> _currentH || $ this -> _currentH < $ h ) { return 0 ; } return ( $ this -> _currentH - $ h ) * 0.8 ; }
get the position of the element on the current line depending on its height
7,913
protected function _makeBreakLine ( $ h , $ curr = null ) { if ( $ h ) { if ( ( $ this -> pdf -> GetY ( ) + $ h < $ this -> pdf -> getH ( ) - $ this -> pdf -> getbMargin ( ) ) || $ this -> _isInOverflow || $ this -> _isInFooter ) { $ this -> _setNewLine ( $ h , $ curr ) ; } else { $ this -> _setNewPage ( null , '' , null , $ curr ) ; } } else { $ this -> _setNewPositionForNewLine ( $ curr ) ; } $ this -> _maxH = 0 ; $ this -> _maxE = 0 ; }
make a break line
7,914
protected function _tag_close_PAGE_FOOTER_SUB ( $ param ) { if ( $ this -> _isForOneLine ) { return false ; } $ this -> parsingCss -> load ( ) ; $ this -> parsingCss -> value = $ this -> _subSTATES [ 's' ] ; $ this -> parsingCss -> table = $ this -> _subSTATES [ 't' ] ; $ this -> _pageMarges = $ this -> _subSTATES [ 'mp' ] ; $ this -> _margeLeft = $ this -> _subSTATES [ 'ml' ] ; $ this -> _margeRight = $ this -> _subSTATES [ 'mr' ] ; $ this -> _margeTop = $ this -> _subSTATES [ 'mt' ] ; $ this -> _margeBottom = $ this -> _subSTATES [ 'mb' ] ; $ this -> pdf -> SetMargins ( $ this -> _margeLeft , $ this -> _margeTop , $ this -> _margeRight ) ; $ this -> pdf -> SetAutoPageBreak ( false , $ this -> _margeBottom ) ; $ this -> pdf -> SetXY ( $ this -> _subSTATES [ 'x' ] , $ this -> _subSTATES [ 'y' ] ) ; $ this -> parsingCss -> fontSet ( ) ; $ this -> _maxH = 0 ; return true ; }
It is not a real tag . Do not use it directly
7,915
public function _INDEX_NewPage ( & $ page ) { if ( $ page ) { $ oldPage = $ this -> pdf -> getPage ( ) ; $ this -> pdf -> setPage ( $ page ) ; $ this -> pdf -> SetXY ( $ this -> _margeLeft , $ this -> _margeTop ) ; $ this -> _maxH = 0 ; $ page ++ ; return $ oldPage ; } else { $ this -> _setNewPage ( ) ; return null ; } }
new page for the automatic Index do not use this method . Only myPdf could use it !!!!
7,916
public function Footer ( ) { $ txt = '' ; if ( $ this -> _footerParam [ 'form' ] ) { $ txt = Locale :: get ( 'pdf05' ) ; } if ( $ this -> _footerParam [ 'date' ] && $ this -> _footerParam [ 'time' ] ) { $ txt .= ( $ txt ? ' - ' : '' ) . Locale :: get ( 'pdf03' ) ; } if ( $ this -> _footerParam [ 'date' ] && ! $ this -> _footerParam [ 'time' ] ) { $ txt .= ( $ txt ? ' - ' : '' ) . Locale :: get ( 'pdf01' ) ; } if ( ! $ this -> _footerParam [ 'date' ] && $ this -> _footerParam [ 'time' ] ) { $ txt .= ( $ txt ? ' - ' : '' ) . Locale :: get ( 'pdf02' ) ; } if ( $ this -> _footerParam [ 'page' ] ) { $ txt .= ( $ txt ? ' - ' : '' ) . Locale :: get ( 'pdf04' ) ; } if ( strlen ( $ txt ) > 0 ) { $ toReplace = array ( '[[date_d]]' => date ( 'd' ) , '[[date_m]]' => date ( 'm' ) , '[[date_y]]' => date ( 'Y' ) , '[[date_h]]' => date ( 'H' ) , '[[date_i]]' => date ( 'i' ) , '[[date_s]]' => date ( 's' ) , '[[page_cu]]' => $ this -> getMyNumPage ( ) , '[[page_nb]]' => $ this -> getMyAliasNbPages ( ) , ) ; $ txt = str_replace ( array_keys ( $ toReplace ) , array_values ( $ toReplace ) , $ txt ) ; parent :: SetY ( - 11 ) ; $ this -> SetFont ( 'helvetica' , 'I' , 8 ) ; $ this -> Cell ( 0 , 10 , $ txt , 0 , 0 , 'R' ) ; } }
This function is call automatically by TCPDF at the end of a page It takes no parameters
7,917
public function cloneFontFrom ( & $ pdf ) { $ this -> n = & $ pdf -> getN ( ) ; $ this -> fonts = & $ pdf -> getFonts ( ) ; $ this -> FontFiles = & $ pdf -> getFontFiles ( ) ; $ this -> diffs = & $ pdf -> getDiffs ( ) ; $ this -> fontlist = & $ pdf -> getFontList ( ) ; $ this -> numfonts = & $ pdf -> getNumFonts ( ) ; $ this -> fontkeys = & $ pdf -> getFontKeys ( ) ; $ this -> font_obj_ids = & $ pdf -> getFontObjIds ( ) ; $ this -> annotation_fonts = & $ pdf -> getAnnotFonts ( ) ; }
after cloning a object we does not want to clone all the front informations because it take a lot a time and a lot of memory = > we use reference
7,918
public function isLoadedFont ( $ fontKey ) { if ( isset ( $ this -> fonts [ $ fontKey ] ) ) { return true ; } if ( isset ( $ this -> CoreFonts [ $ fontKey ] ) ) { return true ; } return false ; }
Verify that a Font is already loaded
7,919
public function setWordSpacing ( $ ws = 0. ) { $ this -> ws = $ ws ; $ this -> _out ( sprintf ( '%.3F Tw' , $ ws * $ this -> k ) ) ; }
set the Word Spacing
7,920
public function setTranslate ( $ xT , $ yT ) { $ tm [ 0 ] = 1 ; $ tm [ 1 ] = 0 ; $ tm [ 2 ] = 0 ; $ tm [ 3 ] = 1 ; $ tm [ 4 ] = $ xT * $ this -> k ; $ tm [ 5 ] = - $ yT * $ this -> k ; $ this -> _out ( sprintf ( '%.3F %.3F %.3F %.3F %.3F %.3F cm' , $ tm [ 0 ] , $ tm [ 1 ] , $ tm [ 2 ] , $ tm [ 3 ] , $ tm [ 4 ] , $ tm [ 5 ] ) ) ; }
add a Translate transformation
7,921
public function setRotation ( $ angle , $ xC = null , $ yC = null ) { if ( $ xC === null ) { $ xC = $ this -> x ; } if ( $ yC === null ) { $ yC = $ this -> y ; } $ yC = ( $ this -> h - $ yC ) * $ this -> k ; $ xC *= $ this -> k ; $ tm [ 0 ] = cos ( deg2rad ( $ angle ) ) ; $ tm [ 1 ] = sin ( deg2rad ( $ angle ) ) ; $ tm [ 2 ] = - $ tm [ 1 ] ; $ tm [ 3 ] = $ tm [ 0 ] ; $ tm [ 4 ] = $ xC + $ tm [ 1 ] * $ yC - $ tm [ 0 ] * $ xC ; $ tm [ 5 ] = $ yC - $ tm [ 0 ] * $ yC - $ tm [ 1 ] * $ xC ; $ this -> _out ( sprintf ( '%.3F %.3F %.3F %.3F %.3F %.3F cm' , $ tm [ 0 ] , $ tm [ 1 ] , $ tm [ 2 ] , $ tm [ 3 ] , $ tm [ 4 ] , $ tm [ 5 ] ) ) ; }
add a Rotate transformation
7,922
public function svgLine ( $ x1 , $ y1 , $ x2 , $ y2 ) { $ op = 'S' ; $ this -> _Point ( $ x1 , $ y1 , true ) ; $ this -> _Line ( $ x2 , $ y2 , true ) ; $ this -> _out ( $ op ) ; }
SVG - make a Line
7,923
public function svgEllipse ( $ x0 , $ y0 , $ rx , $ ry , $ style ) { if ( $ style === 'F' ) { $ op = 'f' ; } elseif ( $ style === 'FD' || $ style === 'DF' ) { $ op = 'B' ; } else { $ op = 'S' ; } $ this -> _Arc ( $ x0 , $ y0 , $ rx , $ ry , 0 , 2 * M_PI , true , true , true ) ; $ this -> _out ( $ op ) ; }
SVG - make a Ellipse
7,924
public function doTransform ( $ n = null ) { $ nb = count ( $ this -> _transf ) ; if ( $ nb ) { $ m = $ this -> _transf [ $ nb - 1 ] ; } else { $ m = array ( 1 , 0 , 0 , 1 , 0 , 0 ) ; } if ( ! $ n ) { $ n = array ( 1 , 0 , 0 , 1 , 0 , 0 ) ; } $ this -> _transf [ ] = array ( $ m [ 0 ] * $ n [ 0 ] + $ m [ 2 ] * $ n [ 1 ] , $ m [ 1 ] * $ n [ 0 ] + $ m [ 3 ] * $ n [ 1 ] , $ m [ 0 ] * $ n [ 2 ] + $ m [ 2 ] * $ n [ 3 ] , $ m [ 1 ] * $ n [ 2 ] + $ m [ 3 ] * $ n [ 3 ] , $ m [ 0 ] * $ n [ 4 ] + $ m [ 2 ] * $ n [ 5 ] + $ m [ 4 ] , $ m [ 1 ] * $ n [ 4 ] + $ m [ 3 ] * $ n [ 5 ] + $ m [ 5 ] ) ; }
SVG - add a transformation Matric
7,925
public function myBarcode ( $ code , $ type , $ x , $ y , $ w , $ h , $ labelFontsize , $ color , $ dimension = '1D' ) { $ style = array ( 'position' => 'S' , 'text' => ( $ labelFontsize ? true : false ) , 'fgcolor' => $ color , 'bgcolor' => false , ) ; if ( $ dimension === '2D' ) { $ this -> write2DBarcode ( $ code , $ type , $ x , $ y , $ w , $ h , '' , $ style , 'N' ) ; } else { $ this -> write1DBarcode ( $ code , $ type , $ x , $ y , $ w , $ h , '' , $ style , 'N' ) ; } if ( $ labelFontsize ) { $ h += $ labelFontsize ; } return array ( $ w , $ h ) ; }
Convert a Html2Pdf barcode in a TCPDF barcode
7,926
public function getMyAliasNbPages ( ) { if ( $ this -> _myLastPageGroupNb == 0 ) { return $ this -> getAliasNbPages ( ) ; } else { $ old = $ this -> currpagegroup ; $ this -> currpagegroup = '{nb' . $ this -> _myLastPageGroupNb . '}' ; $ new = $ this -> getPageGroupAlias ( ) ; $ this -> currpagegroup = $ old ; return $ new ; } }
Returns the string alias used for the total number of pages .
7,927
public function tokenize ( $ html ) { $ tokens = array ( ) ; $ reg = '/(<\/?\w[^<>]*>)|([^<]+|<)/is' ; $ commentRegex = '/(<!--.* ; $ str = '' ; $ offset = 0 ; $ line = 1 ; $ length = strlen ( $ html ) ; while ( $ offset < $ length ) { if ( strpos ( $ html , '<!--' , $ offset ) === $ offset && preg_match ( $ commentRegex , $ html , $ match , PREG_OFFSET_CAPTURE , $ offset ) ) { $ line += substr_count ( $ match [ 1 ] [ 0 ] , "\n" ) ; $ offset = $ match [ 0 ] [ 1 ] + strlen ( $ match [ 0 ] [ 0 ] ) ; continue ; } preg_match ( $ reg , $ html , $ parse , PREG_OFFSET_CAPTURE , $ offset ) ; if ( $ parse [ 1 ] [ 0 ] ) { if ( $ str !== '' ) { $ tokens [ ] = new Token ( 'txt' , $ str ) ; } $ tokens [ ] = new Token ( 'code' , trim ( $ parse [ 1 ] [ 0 ] ) , $ line ) ; $ line += substr_count ( $ parse [ 1 ] [ 0 ] , "\n" ) ; $ str = '' ; } else { $ str .= $ parse [ 2 ] [ 0 ] ; $ line += substr_count ( $ parse [ 2 ] [ 0 ] , "\n" ) ; } $ offset = $ parse [ 0 ] [ 1 ] + strlen ( $ parse [ 0 ] [ 0 ] ) ; unset ( $ parse ) ; } if ( $ str !== '' ) { $ tokens [ ] = new Token ( 'txt' , $ str ) ; } return $ tokens ; }
Tokenize the HTML code
7,928
protected function getAdditionalData ( Html2PdfException $ e ) { $ data = array ( ) ; switch ( $ e -> getCode ( ) ) { case HtmlParsingException :: ERROR_CODE : $ data [ 'invalid tag' ] = $ e -> getInvalidTag ( ) ; $ data [ 'html line' ] = $ e -> getHtmlLine ( ) ; break ; case ImageException :: ERROR_CODE : $ data [ 'image src' ] = $ e -> getImage ( ) ; break ; case LongSentenceException :: ERROR_CODE : $ data [ 'sentence' ] = $ e -> getSentence ( ) ; $ data [ 'box width' ] = $ e -> getWidthBox ( ) ; $ data [ 'length' ] = $ e -> getLength ( ) ; break ; case TableException :: ERROR_CODE : case Html2PdfException :: ERROR_CODE : default : break ; } return $ data ; }
get the additional data from the exception
7,929
protected function buildTextMessage ( Html2PdfException $ e , $ data ) { $ this -> message = 'Html2Pdf Error [' . $ e -> getCode ( ) . ']' . "\n" ; $ this -> message .= $ e -> getMessage ( ) . "\n" ; $ this -> message .= ' File: ' . $ e -> getFile ( ) . "\n" ; $ this -> message .= ' Line: ' . $ e -> getLine ( ) . "\n" ; if ( ! empty ( $ data ) ) { foreach ( $ data as $ key => $ value ) { $ this -> message .= ' ' . ucwords ( $ key ) . ': ' . trim ( $ value ) . "\n" ; } } }
Build the text message
7,930
protected function buildHtmlMessage ( Html2PdfException $ e , $ data ) { $ this -> htmlMessage = '<span style="color: #A00; font-weight: bold;">' ; $ this -> htmlMessage .= 'Html2Pdf Error [' . $ e -> getCode ( ) . ']' ; $ this -> htmlMessage .= '</span><br />' . "\n" ; $ this -> htmlMessage .= htmlentities ( $ e -> getMessage ( ) ) . "<br />\n" ; $ this -> htmlMessage .= ' File: ' . $ e -> getFile ( ) . "<br />\n" ; $ this -> htmlMessage .= ' Line: ' . $ e -> getLine ( ) . "<br />\n" ; if ( ! empty ( $ data ) ) { foreach ( $ data as $ key => $ value ) { $ this -> htmlMessage .= ' ' . ucwords ( $ key ) . ': ' . trim ( htmlentities ( $ value ) ) . "<br />\n" ; } } }
build the html message
7,931
public function getCloneCodes ( ) { $ codes = array ( ) ; foreach ( $ this -> code as $ key => $ code ) { $ codes [ $ key ] = clone $ code ; } return $ codes ; }
Get the list of the codes but cloned
7,932
protected function verifyMustContain ( & $ actions , $ mainTag , $ mustTag ) { $ inMainTag = 0 ; $ foundMustTag = false ; foreach ( $ actions as $ action ) { if ( $ action -> getName ( ) == $ mainTag && ! $ action -> isClose ( ) ) { $ inMainTag ++ ; $ foundMustTag = false ; } if ( $ action -> getName ( ) == $ mustTag && $ inMainTag > 0 ) { $ foundMustTag = true ; } if ( $ action -> getName ( ) == $ mainTag && $ action -> isClose ( ) ) { if ( ! $ foundMustTag ) { $ exception = new HtmlParsingException ( "The tag [$mainTag] must contain at least one tag [$mustTag]" ) ; $ exception -> setInvalidTag ( $ action -> getName ( ) ) ; $ exception -> setHtmlLine ( $ action -> getLine ( ) ) ; throw $ exception ; } $ inMainTag -- ; } } return true ; }
Verify some tags that must contain other tags
7,933
protected function getTextAction ( Token $ token ) { $ tagPreBr = new Node ( 'br' , array ( 'style' => array ( ) , 'num' => 0 ) , false ) ; $ actions = array ( ) ; if ( ! $ this -> tagPreIn ) { $ actions [ ] = new Node ( 'write' , array ( 'txt' => $ this -> textParser -> prepareTxt ( $ token -> getData ( ) ) ) , false ) ; } else { $ data = str_replace ( "\r" , '' , $ token -> getData ( ) ) ; $ lines = explode ( "\n" , $ data ) ; foreach ( $ lines as $ k => $ txt ) { $ txt = str_replace ( "\t" , self :: HTML_TAB , $ txt ) ; $ txt = str_replace ( ' ' , '&nbsp;' , $ txt ) ; if ( $ k > 0 ) { $ actions [ ] = clone $ tagPreBr ; } $ actions [ ] = new Node ( 'write' , array ( 'txt' => $ this -> textParser -> prepareTxt ( $ txt , false ) ) , false ) ; } } return $ actions ; }
get the Text action
7,934
public function prepareHtml ( $ html ) { if ( preg_match ( '/<body/isU' , $ html ) ) { $ html = $ this -> getHtmlFromRealPage ( $ html ) ; } $ html = str_replace ( '[[date_y]]' , date ( 'Y' ) , $ html ) ; $ html = str_replace ( '[[date_m]]' , date ( 'm' ) , $ html ) ; $ html = str_replace ( '[[date_d]]' , date ( 'd' ) , $ html ) ; $ html = str_replace ( '[[date_h]]' , date ( 'H' ) , $ html ) ; $ html = str_replace ( '[[date_i]]' , date ( 'i' ) , $ html ) ; $ html = str_replace ( '[[date_s]]' , date ( 's' ) , $ html ) ; return $ html ; }
prepare the HTML
7,935
protected function getHtmlFromRealPage ( $ html ) { $ html = str_replace ( '<BODY' , '<body' , $ html ) ; $ html = str_replace ( '</BODY' , '</body' , $ html ) ; $ res = explode ( '<body' , $ html ) ; $ content = '<page' . $ res [ 1 ] ; $ content = explode ( '</body' , $ content ) ; $ content = $ content [ 0 ] . '</page>' ; preg_match_all ( '/<link ([^>]*)[\/]?>/isU' , $ html , $ match ) ; foreach ( $ match [ 1 ] as $ src ) { $ content = '<link ' . $ src . '/>' . $ content ; } preg_match_all ( '/<style[^>]*>(.*)<\/style[^>]*>/isU' , $ html , $ match ) ; foreach ( $ match [ 0 ] as $ src ) { $ content = $ src . $ content ; } return $ content ; }
convert the HTML of a real page to a code adapted to Html2Pdf
7,936
public function generate ( $ minify = false ) { $ this -> loadWebMasterTags ( ) ; $ title = $ this -> getTitle ( ) ; $ description = $ this -> getDescription ( ) ; $ keywords = $ this -> getKeywords ( ) ; $ metatags = $ this -> getMetatags ( ) ; $ canonical = $ this -> getCanonical ( ) ; $ amphtml = $ this -> getAmpHtml ( ) ; $ prev = $ this -> getPrev ( ) ; $ next = $ this -> getNext ( ) ; $ languages = $ this -> getAlternateLanguages ( ) ; $ robots = $ this -> getRobots ( ) ; $ html = [ ] ; if ( $ title ) { $ html [ ] = "<title>$title</title>" ; } if ( $ description ) { $ html [ ] = "<meta name=\"description\" content=\"{$description}\">" ; } if ( ! empty ( $ keywords ) ) { $ keywords = implode ( ', ' , $ keywords ) ; $ html [ ] = "<meta name=\"keywords\" content=\"{$keywords}\">" ; } foreach ( $ metatags as $ key => $ value ) { $ name = $ value [ 0 ] ; $ content = $ value [ 1 ] ; if ( empty ( $ content ) ) { continue ; } $ html [ ] = "<meta {$name}=\"{$key}\" content=\"{$content}\">" ; } if ( $ canonical ) { $ html [ ] = "<link rel=\"canonical\" href=\"{$canonical}\"/>" ; } if ( $ amphtml ) { $ html [ ] = "<link rel=\"amphtml\" href=\"{$amphtml}\"/>" ; } if ( $ prev ) { $ html [ ] = "<link rel=\"prev\" href=\"{$prev}\"/>" ; } if ( $ next ) { $ html [ ] = "<link rel=\"next\" href=\"{$next}\"/>" ; } foreach ( $ languages as $ lang ) { $ html [ ] = "<link rel=\"alternate\" hreflang=\"{$lang['lang']}\" href=\"{$lang['url']}\"/>" ; } if ( $ robots ) { $ html [ ] = "<meta name=\"robots\" content=\"{$robots}\">" ; } return ( $ minify ) ? implode ( '' , $ html ) : implode ( PHP_EOL , $ html ) ; }
Generates meta tags .
7,937
public function getDefaultTitle ( ) { if ( empty ( $ this -> title_default ) ) { return $ this -> config -> get ( 'defaults.title' , null ) ; } return $ this -> title_default ; }
Takes the default title .
7,938
public function getDescription ( ) { if ( false === $ this -> description ) { return ; } return $ this -> description ? : $ this -> config -> get ( 'defaults.description' , null ) ; }
Get the Meta description .
7,939
public function getCanonical ( ) { $ canonical_config = $ this -> config -> get ( 'defaults.canonical' , false ) ; return $ this -> canonical ? : ( ( $ canonical_config === null ) ? app ( 'url' ) -> full ( ) : $ canonical_config ) ; }
Get the canonical URL .
7,940
protected function loadWebMasterTags ( ) { foreach ( $ this -> config -> get ( 'webmaster_tags' , [ ] ) as $ name => $ value ) { if ( ! empty ( $ value ) ) { $ meta = array_get ( $ this -> webmasterTags , $ name , $ name ) ; $ this -> addMeta ( $ meta , $ value ) ; } } }
Load webmaster tags from configuration .
7,941
public function setTitle ( $ title , $ appendDefault = true ) { $ this -> metatags ( ) -> setTitle ( $ title , $ appendDefault ) ; $ this -> opengraph ( ) -> setTitle ( $ title ) ; $ this -> twitter ( ) -> setTitle ( $ title ) ; $ this -> jsonLd ( ) -> setTitle ( $ title ) ; return $ this ; }
Setup title for all seo providers .
7,942
public function setDescription ( $ description ) { $ this -> metatags ( ) -> setDescription ( $ description ) ; $ this -> opengraph ( ) -> setDescription ( $ description ) ; $ this -> twitter ( ) -> setDescription ( $ description ) ; $ this -> jsonLd ( ) -> setDescription ( $ description ) ; return $ this ; }
Setup description for all seo providers .
7,943
public function getTitle ( $ session = false ) { if ( $ session ) { return $ this -> metatags ( ) -> getTitleSession ( ) ; } return $ this -> metatags ( ) -> getTitle ( ) ; }
Get current title from metatags .
7,944
public function generate ( $ minify = false ) { $ html = $ this -> metatags ( ) -> generate ( ) ; $ html .= PHP_EOL ; $ html .= $ this -> opengraph ( ) -> generate ( ) ; $ html .= PHP_EOL ; $ html .= $ this -> twitter ( ) -> generate ( ) ; $ html .= PHP_EOL ; $ html .= $ this -> jsonLd ( ) -> generate ( ) ; return ( $ minify ) ? str_replace ( PHP_EOL , '' , $ html ) : $ html ; }
Generate from all seo providers .
7,945
public function generate ( $ minify = false ) { $ this -> setupDefaults ( ) ; $ output = $ this -> eachProperties ( $ this -> properties ) ; $ props = [ 'images' => [ 'image' , true ] , 'articleProperties' => [ 'article' , false ] , 'profileProperties' => [ 'profile' , false ] , 'bookProperties' => [ 'book' , false ] , 'musicSongProperties' => [ 'music' , false ] , 'musicAlbumProperties' => [ 'music' , false ] , 'musicPlaylistProperties' => [ 'music' , false ] , 'musicRadioStationProperties' => [ 'music' , false ] , 'videoMovieProperties' => [ 'video' , false ] , 'videoEpisodeProperties' => [ 'video' , false ] , 'videoTVShowProperties' => [ 'video' , false ] , 'videoOtherProperties' => [ 'video' , false ] , 'videoProperties' => [ 'video' , true ] , 'audioProperties' => [ 'audio' , true ] , 'placeProperties' => [ 'place' , false ] , 'productProperties' => [ 'product' , false ] , ] ; foreach ( $ props as $ prop => $ options ) { $ output .= $ this -> eachProperties ( $ this -> { $ prop } , $ options [ 0 ] , $ options [ 1 ] ) ; } return ( $ minify ) ? str_replace ( PHP_EOL , '' , $ output ) : $ output ; }
Generates open graph tags .
7,946
protected function eachProperties ( array $ properties , $ prefix = null , $ ogPrefix = true ) { $ html = [ ] ; foreach ( $ properties as $ property => $ value ) { if ( is_array ( $ value ) ) { $ subListPrefix = ( is_string ( $ property ) ) ? $ property : $ prefix ; $ subList = $ this -> eachProperties ( $ value , $ subListPrefix ) ; $ html [ ] = $ subList ; } else { if ( is_string ( $ prefix ) ) { $ key = ( is_string ( $ property ) ) ? $ prefix . ':' . $ property : $ prefix ; } else { $ key = $ property ; } if ( empty ( $ value ) ) { continue ; } $ html [ ] = $ this -> makeTag ( $ key , $ value , $ ogPrefix ) ; } } return implode ( $ html ) ; }
Make list of open graph tags .
7,947
protected function makeTag ( $ key = null , $ value = null , $ ogPrefix = false ) { return sprintf ( '<meta property="%s%s" content="%s" />%s' , $ ogPrefix ? $ this -> og_prefix : '' , strip_tags ( $ key ) , strip_tags ( $ value ) , PHP_EOL ) ; }
Make a og tag .
7,948
protected function setupDefaults ( ) { $ defaults = ( isset ( $ this -> config [ 'defaults' ] ) ) ? $ this -> config [ 'defaults' ] : [ ] ; foreach ( $ defaults as $ key => $ value ) { if ( $ key == 'images' ) { if ( empty ( $ this -> images ) ) { $ this -> images = $ value ; } } elseif ( $ key == 'url' && $ value === null ) { $ this -> setUrl ( app ( 'url' ) -> current ( ) ) ; } elseif ( ! empty ( $ value ) && ! array_key_exists ( $ key , $ this -> properties ) ) { $ this -> addProperty ( $ key , $ value ) ; } } }
Add or update property .
7,949
public function addVideo ( $ source = null , $ attributes = [ ] ) { $ validKeys = [ 'url' , 'secure_url' , 'type' , 'width' , 'height' , ] ; $ this -> videoProperties [ ] = [ $ source , $ this -> cleanProperties ( $ attributes , $ validKeys ) , ] ; return $ this ; }
Add video properties .
7,950
public function addAudio ( $ source = null , $ attributes = [ ] ) { $ validKeys = [ 'url' , 'secure_url' , 'type' , ] ; $ this -> audioProperties [ ] = [ $ source , $ this -> cleanProperties ( $ attributes , $ validKeys ) , ] ; return $ this ; }
Add audio properties .
7,951
protected function cleanProperties ( $ attributes = [ ] , $ validKeys = [ ] ) { $ array = [ ] ; foreach ( $ attributes as $ attribute => $ value ) { if ( in_array ( $ attribute , $ validKeys ) ) { $ array [ $ attribute ] = $ value ; } } return $ array ; }
Clean invalid properties .
7,952
public function addImage ( $ source = null , $ attributes = [ ] ) { $ validKeys = [ 'url' , 'secure_url' , 'type' , 'width' , 'height' , ] ; if ( is_array ( $ source ) ) { $ this -> images [ ] = $ this -> cleanProperties ( $ source , $ validKeys ) ; } else { $ this -> images [ ] = [ $ source , $ this -> cleanProperties ( $ attributes , $ validKeys ) , ] ; } return $ this ; }
Add image to properties .
7,953
protected function eachValue ( array $ values , $ prefix = null ) { foreach ( $ values as $ key => $ value ) : if ( is_array ( $ value ) ) : $ this -> eachValue ( $ value , $ key ) ; else : if ( is_numeric ( $ key ) ) : $ key = $ prefix . $ key ; elseif ( is_string ( $ prefix ) ) : $ key = $ prefix . ':' . $ key ; endif ; $ this -> html [ ] = $ this -> makeTag ( $ key , $ value ) ; endif ; endforeach ; }
Make tags .
7,954
public function choose_template ( $ templates ) { if ( ! is_array ( $ templates ) ) { $ templates = ( array ) $ templates ; } $ loader = $ this -> get_loader ( ) ; foreach ( $ templates as $ template ) { $ template = trim ( $ template ) ; if ( $ loader -> exists ( $ template ) ) { return $ template ; } } return false ; }
Get first existing template .
7,955
public function import ( $ info , $ force = false ) { if ( is_object ( $ info ) ) { $ info = get_object_vars ( $ info ) ; } if ( is_array ( $ info ) ) { foreach ( $ info as $ key => $ value ) { if ( $ key === '' || ord ( $ key [ 0 ] ) === 0 ) { continue ; } if ( ! empty ( $ key ) && $ force ) { $ this -> $ key = $ value ; } else if ( ! empty ( $ key ) && ! method_exists ( $ this , $ key ) ) { $ this -> $ key = $ value ; } } } }
Takes an array or object and adds the properties to the parent object
7,956
public static function is_external ( $ url ) { $ has_http = strstr ( strtolower ( $ url ) , 'http' ) || strstr ( strtolower ( $ url ) , '//' ) ; $ on_domain = strstr ( $ url , self :: get_host ( ) ) ; if ( $ has_http && ! $ on_domain ) { return true ; } return false ; }
Checks if URL is external or internal . Works with domains subdomains and protocol relative domains .
7,957
public static function remove_url_component ( $ haystack , $ needle ) { $ haystack = str_replace ( $ needle , '' , $ haystack ) ; $ needle = self :: swap_protocol ( $ needle ) ; return str_replace ( $ needle , '' , $ haystack ) ; }
Removes the subcomponent of a URL regardless of protocol
7,958
public static function swap_protocol ( $ url ) { if ( stristr ( $ url , 'http:' ) ) { return str_replace ( 'http:' , 'https:' , $ url ) ; } if ( stristr ( $ url , 'https:' ) ) { return str_replace ( 'https:' , 'http:' , $ url ) ; } return $ url ; }
Swaps whatever protocol of a URL is sent . http becomes https and vice versa
7,959
public static function user_trailingslashit ( $ link ) { $ link_parts = parse_url ( $ link ) ; if ( ! $ link_parts ) { return $ link ; } if ( isset ( $ link_parts [ 'path' ] ) && $ link_parts [ 'path' ] != '/' ) { $ new_path = user_trailingslashit ( $ link_parts [ 'path' ] ) ; if ( $ new_path != $ link_parts [ 'path' ] ) { $ link = str_replace ( $ link_parts [ 'path' ] , $ new_path , $ link ) ; } } return $ link ; }
Pass links through user_trailingslashit handling query strings properly
7,960
protected static function process_term_args ( $ maybe_taxonomy , $ TermClass ) { if ( class_exists ( $ maybe_taxonomy ) ) { $ tc = new $ maybe_taxonomy ; if ( is_subclass_of ( $ tc , 'Timber\Term' ) ) { return array ( 'taxonomy' => '' , 'TermClass' => $ maybe_taxonomy ) ; } } return array ( 'taxonomy' => $ maybe_taxonomy , 'TermClass' => $ TermClass ) ; }
Process the arguments for handle_term_object to determine what arguments the user is sending
7,961
public function add_timber_escapers ( $ twig ) { $ twig -> getExtension ( 'Twig_Extension_Core' ) -> setEscaper ( 'esc_url' , function ( \ Twig_Environment $ env , $ string ) { return esc_url ( $ string ) ; } ) ; $ twig -> getExtension ( 'Twig_Extension_Core' ) -> setEscaper ( 'wp_kses_post' , function ( \ Twig_Environment $ env , $ string ) { return wp_kses_post ( $ string ) ; } ) ; $ twig -> getExtension ( 'Twig_Extension_Core' ) -> setEscaper ( 'esc_html' , function ( \ Twig_Environment $ env , $ string ) { return esc_html ( $ string ) ; } ) ; $ twig -> getExtension ( 'Twig_Extension_Core' ) -> setEscaper ( 'esc_js' , function ( \ Twig_Environment $ env , $ string ) { return esc_js ( $ string ) ; } ) ; return $ twig ; }
Adds escapers to Twig .
7,962
public function add_to_twig ( $ twig ) { $ wrapper = $ this ; try { $ twig -> addFunction ( new Twig_Function ( $ this -> _function , function ( ) use ( $ wrapper ) { return call_user_func_array ( array ( $ wrapper , 'call' ) , func_get_args ( ) ) ; } ) ) ; } catch ( \ Exception $ e ) { } return $ twig ; }
Make function available in Twig .
7,963
public static function resize ( $ src , $ w , $ h = 0 , $ crop = 'default' , $ force = false ) { if ( ! is_numeric ( $ w ) && is_string ( $ w ) ) { if ( $ sizes = self :: find_wp_dimensions ( $ w ) ) { $ w = $ sizes [ 'w' ] ; $ h = $ sizes [ 'h' ] ; } else { return $ src ; } } $ op = new Image \ Operation \ Resize ( $ w , $ h , $ crop ) ; return self :: _operate ( $ src , $ op , $ force ) ; }
Generates a new image with the specified dimensions . New dimensions are achieved by cropping to maintain ratio .
7,964
private static function find_wp_dimensions ( $ size ) { global $ _wp_additional_image_sizes ; if ( isset ( $ _wp_additional_image_sizes [ $ size ] ) ) { $ w = $ _wp_additional_image_sizes [ $ size ] [ 'width' ] ; $ h = $ _wp_additional_image_sizes [ $ size ] [ 'height' ] ; } else if ( in_array ( $ size , array ( 'thumbnail' , 'medium' , 'large' ) ) ) { $ w = get_option ( $ size . '_size_w' ) ; $ h = get_option ( $ size . '_size_h' ) ; } if ( isset ( $ w ) && isset ( $ h ) && ( $ w || $ h ) ) { return array ( 'w' => $ w , 'h' => $ h ) ; } return false ; }
Find the sizes of an image based on a defined image size
7,965
public static function retina_resize ( $ src , $ multiplier = 2 , $ force = false ) { $ op = new Image \ Operation \ Retina ( $ multiplier ) ; return self :: _operate ( $ src , $ op , $ force ) ; }
Generates a new image with increased size for display on Retina screens .
7,966
public static function is_animated_gif ( $ file ) { if ( strpos ( strtolower ( $ file ) , '.gif' ) === false ) { return false ; } if ( ! ( $ fh = @ fopen ( $ file , 'rb' ) ) ) { return false ; } $ count = 0 ; while ( ! feof ( $ fh ) && $ count < 2 ) { $ chunk = fread ( $ fh , 1024 * 100 ) ; $ count += preg_match_all ( '#\x00\x21\xF9\x04.{4}\x00[\x2C\x21]#s' , $ chunk , $ matches ) ; } fclose ( $ fh ) ; return $ count > 1 ; }
checks to see if the given file is an aimated gif
7,967
public static function is_svg ( $ file_path ) { if ( ! isset ( $ file_path ) || '' === $ file_path || ! file_exists ( $ file_path ) ) { return false ; } if ( TextHelper :: ends_with ( strtolower ( $ file_path ) , '.svg' ) ) { return true ; } $ mime = wp_check_filetype_and_ext ( $ file_path , basename ( $ file_path ) , array ( 'svg' => 'image/svg+xml' , ) ) ; return in_array ( $ mime [ 'type' ] , array ( 'image/svg+xml' , 'text/html' , 'text/plain' , 'image/svg' , ) ) ; }
Checks if file is an SVG .
7,968
public static function letterbox ( $ src , $ w , $ h , $ color = false , $ force = false ) { $ op = new Letterbox ( $ w , $ h , $ color ) ; return self :: _operate ( $ src , $ op , $ force ) ; }
Generate a new image with the specified dimensions . New dimensions are achieved by adding colored bands to maintain ratio .
7,969
public static function img_to_jpg ( $ src , $ bghex = '#FFFFFF' , $ force = false ) { $ op = new Image \ Operation \ ToJpg ( $ bghex ) ; return self :: _operate ( $ src , $ op , $ force ) ; }
Generates a new image by converting the source GIF or PNG into JPG
7,970
public static function img_to_webp ( $ src , $ quality = 80 , $ force = false ) { $ op = new Image \ Operation \ ToWebp ( $ quality ) ; return self :: _operate ( $ src , $ op , $ force ) ; }
Generates a new image by converting the source into WEBP if supported by the server
7,971
public static function _delete_generated_if_image ( $ post_id ) { if ( wp_attachment_is_image ( $ post_id ) ) { $ attachment = new Image ( $ post_id ) ; if ( $ attachment -> file_loc ) { ImageHelper :: delete_generated_files ( $ attachment -> file_loc ) ; } } }
Checks if attachment is an image before deleting generated files
7,972
static function delete_generated_files ( $ local_file ) { if ( URLHelper :: is_absolute ( $ local_file ) ) { $ local_file = URLHelper :: url_to_file_system ( $ local_file ) ; } $ info = pathinfo ( $ local_file ) ; $ dir = $ info [ 'dirname' ] ; $ ext = $ info [ 'extension' ] ; $ filename = $ info [ 'filename' ] ; self :: process_delete_generated_files ( $ filename , $ ext , $ dir , '-[0-9999999]*' , '-[0-9]*x[0-9]*-c-[a-z]*.' ) ; self :: process_delete_generated_files ( $ filename , $ ext , $ dir , '-lbox-[0-9999999]*' , '-lbox-[0-9]*x[0-9]*-[a-zA-Z0-9]*.' ) ; self :: process_delete_generated_files ( $ filename , 'jpg' , $ dir , '-tojpg.*' ) ; self :: process_delete_generated_files ( $ filename , 'jpg' , $ dir , '-tojpg-[0-9999999]*' ) ; }
Deletes the auto - generated files for resize and letterboxing created by Timber
7,973
protected static function process_delete_generated_files ( $ filename , $ ext , $ dir , $ search_pattern , $ match_pattern = null ) { $ searcher = '/' . $ filename . $ search_pattern ; $ files = glob ( $ dir . $ searcher ) ; if ( $ files === false || empty ( $ files ) ) { return ; } foreach ( $ files as $ found_file ) { $ pattern = '/' . preg_quote ( $ dir , '/' ) . '\/' . preg_quote ( $ filename , '/' ) . $ match_pattern . preg_quote ( $ ext , '/' ) . '/' ; $ match = preg_match ( $ pattern , $ found_file ) ; if ( ! $ match_pattern || $ match ) { unlink ( $ found_file ) ; } } }
Deletes resized versions of the supplied file name . So if passed a value like my - pic . jpg this function will delete my - pic - 500x200 - c - left . jpg my - pic - 400x400 - c - default . jpg etc .
7,974
public static function get_server_location ( $ url ) { if ( 0 === strpos ( $ url , ABSPATH ) ) { return $ url ; } $ au = self :: analyze_url ( $ url ) ; $ result = self :: _get_file_path ( $ au [ 'base' ] , $ au [ 'subdir' ] , $ au [ 'basename' ] ) ; return $ result ; }
Determines the filepath corresponding to a given URL
7,975
public static function get_sideloaded_file_loc ( $ file ) { $ upload = wp_upload_dir ( ) ; $ dir = $ upload [ 'path' ] ; $ filename = $ file ; $ file = parse_url ( $ file ) ; $ path_parts = pathinfo ( $ file [ 'path' ] ) ; $ basename = md5 ( $ filename ) ; $ ext = 'jpg' ; if ( isset ( $ path_parts [ 'extension' ] ) ) { $ ext = $ path_parts [ 'extension' ] ; } return $ dir . '/' . $ basename . '.' . $ ext ; }
Determines the filepath where a given external file will be stored .
7,976
public static function sideload_image ( $ file ) { $ loc = self :: get_sideloaded_file_loc ( $ file ) ; if ( file_exists ( $ loc ) ) { return URLHelper :: file_system_to_url ( $ loc ) ; } if ( ! function_exists ( 'download_url' ) ) { require_once ABSPATH . '/wp-admin/includes/file.php' ; } $ tmp = download_url ( $ file ) ; preg_match ( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i' , $ file , $ matches ) ; $ file_array = array ( ) ; $ file_array [ 'name' ] = basename ( $ matches [ 0 ] ) ; $ file_array [ 'tmp_name' ] = $ tmp ; if ( is_wp_error ( $ tmp ) ) { @ unlink ( $ file_array [ 'tmp_name' ] ) ; $ file_array [ 'tmp_name' ] = '' ; } $ locinfo = pathinfo ( $ loc ) ; $ file = wp_upload_bits ( $ locinfo [ 'basename' ] , null , file_get_contents ( $ file_array [ 'tmp_name' ] ) ) ; return $ file [ 'url' ] ; }
downloads an external image to the server and stores it on the server
7,977
public static function analyze_url ( $ url ) { $ result = array ( 'url' => $ url , 'absolute' => URLHelper :: is_absolute ( $ url ) , 'base' => 0 , 'subdir' => '' , 'filename' => '' , 'extension' => '' , 'basename' => '' , ) ; $ upload_dir = wp_upload_dir ( ) ; $ tmp = $ url ; if ( TextHelper :: starts_with ( $ tmp , ABSPATH ) || TextHelper :: starts_with ( $ tmp , '/srv/www/' ) ) { $ result [ 'absolute' ] = true ; if ( TextHelper :: starts_with ( $ tmp , $ upload_dir [ 'basedir' ] ) ) { $ result [ 'base' ] = self :: BASE_UPLOADS ; $ tmp = URLHelper :: remove_url_component ( $ tmp , $ upload_dir [ 'basedir' ] ) ; } if ( TextHelper :: starts_with ( $ tmp , WP_CONTENT_DIR ) ) { $ result [ 'base' ] = self :: BASE_CONTENT ; $ tmp = URLHelper :: remove_url_component ( $ tmp , WP_CONTENT_DIR ) ; } } else { if ( ! $ result [ 'absolute' ] ) { $ tmp = untrailingslashit ( network_home_url ( ) ) . $ tmp ; } if ( URLHelper :: starts_with ( $ tmp , $ upload_dir [ 'baseurl' ] ) ) { $ result [ 'base' ] = self :: BASE_UPLOADS ; $ tmp = URLHelper :: remove_url_component ( $ tmp , $ upload_dir [ 'baseurl' ] ) ; } else if ( URLHelper :: starts_with ( $ tmp , content_url ( ) ) ) { $ result [ 'base' ] = self :: BASE_CONTENT ; $ tmp = self :: theme_url_to_dir ( $ tmp ) ; $ tmp = URLHelper :: remove_url_component ( $ tmp , WP_CONTENT_DIR ) ; } } $ parts = pathinfo ( $ tmp ) ; $ result [ 'subdir' ] = ( $ parts [ 'dirname' ] === '/' ) ? '' : $ parts [ 'dirname' ] ; $ result [ 'filename' ] = $ parts [ 'filename' ] ; $ result [ 'extension' ] = strtolower ( $ parts [ 'extension' ] ) ; $ result [ 'basename' ] = $ parts [ 'basename' ] ; return $ result ; }
Takes in an URL and breaks it into components that will then be used in the different steps of image processing . The image is expected to be either part of a theme plugin or an upload .
7,978
static function theme_url_to_dir ( $ src ) { $ site_root = trailingslashit ( get_theme_root_uri ( ) ) . get_stylesheet ( ) ; $ tmp = str_replace ( $ site_root , '' , $ src ) ; $ tmp = get_stylesheet_directory ( ) . $ tmp ; if ( realpath ( $ tmp ) ) { return realpath ( $ tmp ) ; } return $ tmp ; }
Converts a URL located in a theme directory into the raw file path
7,979
protected static function is_in_theme_dir ( $ path ) { $ root = realpath ( get_stylesheet_directory ( ) ) ; if ( false === $ root ) { return false ; } if ( 0 === strpos ( $ path , ( string ) $ root ) ) { return true ; } else { return false ; } }
Checks if uploaded image is located in theme .
7,980
private static function _get_file_url ( $ base , $ subdir , $ filename , $ absolute ) { $ url = '' ; if ( self :: BASE_UPLOADS == $ base ) { $ upload_dir = wp_upload_dir ( ) ; $ url = $ upload_dir [ 'baseurl' ] ; } if ( self :: BASE_CONTENT == $ base ) { $ url = content_url ( ) ; } if ( ! empty ( $ subdir ) ) { $ url .= $ subdir ; } $ url .= '/' . $ filename ; if ( ! $ absolute ) { $ url = str_replace ( site_url ( ) , '' , $ url ) ; } return $ url ; }
Builds the public URL of a file based on its different components
7,981
private static function _get_file_path ( $ base , $ subdir , $ filename ) { if ( URLHelper :: is_url ( $ subdir ) ) { $ subdir = URLHelper :: url_to_file_system ( $ subdir ) ; } $ subdir = self :: maybe_realpath ( $ subdir ) ; $ path = '' ; if ( self :: BASE_UPLOADS == $ base ) { $ upload_dir = wp_upload_dir ( ) ; $ path = $ upload_dir [ 'basedir' ] ; } else if ( self :: BASE_CONTENT == $ base ) { $ path = WP_CONTENT_DIR ; } if ( self :: is_in_theme_dir ( trailingslashit ( $ subdir ) . $ filename ) ) { return trailingslashit ( $ subdir ) . $ filename ; } if ( ! empty ( $ subdir ) ) { $ path = trailingslashit ( $ path ) . $ subdir ; } $ path = trailingslashit ( $ path ) . $ filename ; return URLHelper :: remove_double_slashes ( $ path ) ; }
Builds the absolute file system location of a file based on its different components
7,982
public function pagination ( $ prefs = array ( ) ) { if ( ! $ this -> pagination && is_a ( $ this -> queryIterator , 'Timber\QueryIterator' ) ) { $ this -> pagination = $ this -> queryIterator -> get_pagination ( $ prefs , $ this -> get_query ( ) ) ; } return $ this -> pagination ; }
Set pagination for the collection . Optionally could be used to get pagination with custom preferences .
7,983
public function find_parent_item_in_menu ( $ menu_items , $ parent_id ) { foreach ( $ menu_items as & $ item ) { if ( $ item -> ID == $ parent_id ) { return $ item ; } } }
Find a parent menu item in a set of menu items .
7,984
public static function in_plugin_update_message ( $ plugin_data , $ r ) { $ current_version = $ plugin_data [ 'Version' ] ; $ new_version = $ plugin_data [ 'new_version' ] ; $ upgrade_magnitude = self :: get_upgrade_magnitude ( $ current_version , $ new_version ) ; if ( $ upgrade_magnitude == 'milestone' ) { $ message = self :: update_message_milestone ( ) ; echo '<br />' . sprintf ( $ message ) ; return ; } elseif ( $ upgrade_magnitude == 'major' ) { $ message = self :: update_message_major ( ) ; echo '<br />' . sprintf ( $ message ) ; return ; } $ message = self :: update_message_minor ( ) ; echo '<br />' . ( $ message ) ; return ; }
Displays an update message for plugin list screens . Shows only the version updates from the current until the newest version
7,985
public static function trim_characters ( $ text , $ num_chars = 60 , $ more = '&hellip;' ) { $ text = wp_strip_all_tags ( $ text ) ; $ text = mb_strimwidth ( $ text , 0 , $ num_chars , $ more ) ; return $ text ; }
Trims text to a certain number of characters . This function can be useful for excerpt of the post As opposed to wp_trim_words trims characters that makes text to take the same amount of space in each post for example
7,986
public static function get_calling_script_file ( $ offset = 0 ) { $ callers = array ( ) ; $ backtrace = debug_backtrace ( ) ; foreach ( $ backtrace as $ trace ) { if ( array_key_exists ( 'file' , $ trace ) && $ trace [ 'file' ] != __FILE__ ) { $ callers [ ] = $ trace [ 'file' ] ; } } $ callers = array_unique ( $ callers ) ; $ callers = array_values ( $ callers ) ; return $ callers [ $ offset ] ; }
Get calling script file .
7,987
public static function get_calling_script_dir ( $ offset = 0 ) { $ caller = self :: get_calling_script_file ( $ offset ) ; if ( ! is_null ( $ caller ) ) { $ pathinfo = pathinfo ( $ caller ) ; $ dir = $ pathinfo [ 'dirname' ] ; return $ dir ; } }
Get calling script dir .
7,988
public function filename ( $ src_filename , $ src_extension ) { $ newbase = $ src_filename . '@' . $ this -> factor . 'x' ; $ new_name = $ newbase . '.' . $ src_extension ; return $ new_name ; }
Generates the final filename based on the source s name and extension
7,989
public static function get_sites ( $ blog_ids = false ) { if ( ! is_array ( $ blog_ids ) ) { global $ wpdb ; $ blog_ids = $ wpdb -> get_col ( "SELECT blog_id FROM $wpdb->blogs ORDER BY blog_id ASC" ) ; } $ return = array ( ) ; foreach ( $ blog_ids as $ blog_id ) { $ return [ ] = new Site ( $ blog_id ) ; } return $ return ; }
Get sites .
7,990
public static function compile ( $ filenames , $ data = array ( ) , $ expires = false , $ cache_mode = Loader :: CACHE_USE_DEFAULT , $ via_render = false ) { if ( ! defined ( 'TIMBER_LOADED' ) ) { self :: init ( ) ; } $ caller = LocationManager :: get_calling_script_dir ( 1 ) ; $ loader = new Loader ( $ caller ) ; $ file = $ loader -> choose_template ( $ filenames ) ; $ caller_file = LocationManager :: get_calling_script_file ( 1 ) ; apply_filters ( 'timber/calling_php_file' , $ caller_file ) ; if ( $ via_render ) { $ file = apply_filters ( 'timber_render_file' , $ file ) ; } else { $ file = apply_filters ( 'timber_compile_file' , $ file ) ; } $ output = false ; if ( $ file !== false ) { if ( is_null ( $ data ) ) { $ data = array ( ) ; } if ( $ via_render ) { $ data = apply_filters ( 'timber_render_data' , $ data ) ; } else { $ data = apply_filters ( 'timber_compile_data' , $ data ) ; } $ output = $ loader -> render ( $ file , $ data , $ expires , $ cache_mode ) ; } do_action ( 'timber_compile_done' ) ; return $ output ; }
Compile a Twig file .
7,991
public static function compile_string ( $ string , $ data = array ( ) ) { $ dummy_loader = new Loader ( ) ; $ twig = $ dummy_loader -> get_twig ( ) ; $ template = $ twig -> createTemplate ( $ string ) ; return $ template -> render ( $ data ) ; }
Compile a string .
7,992
public static function fetch ( $ filenames , $ data = array ( ) , $ expires = false , $ cache_mode = Loader :: CACHE_USE_DEFAULT ) { $ output = self :: compile ( $ filenames , $ data , $ expires , $ cache_mode , true ) ; $ output = apply_filters ( 'timber_compile_result' , $ output ) ; return $ output ; }
Fetch function .
7,993
public static function render_string ( $ string , $ data = array ( ) ) { $ compiled = self :: compile_string ( $ string , $ data ) ; echo $ compiled ; return $ compiled ; }
Render a string with Twig variables .
7,994
public static function get_sidebar ( $ sidebar = 'sidebar.php' , $ data = array ( ) ) { if ( strstr ( strtolower ( $ sidebar ) , '.php' ) ) { return self :: get_sidebar_from_php ( $ sidebar , $ data ) ; } return self :: compile ( $ sidebar , $ data ) ; }
Get sidebar .
7,995
public static function get_sidebar_from_php ( $ sidebar = '' , $ data ) { $ caller = LocationManager :: get_calling_script_dir ( 1 ) ; $ uris = LocationManager :: get_locations ( $ caller ) ; ob_start ( ) ; $ found = false ; foreach ( $ uris as $ uri ) { if ( file_exists ( trailingslashit ( $ uri ) . $ sidebar ) ) { include trailingslashit ( $ uri ) . $ sidebar ; $ found = true ; break ; } } if ( ! $ found ) { Helper :: error_log ( 'error loading your sidebar, check to make sure the file exists' ) ; } $ ret = ob_get_contents ( ) ; ob_end_clean ( ) ; return $ ret ; }
Get sidebar from PHP
7,996
public static function hexrgb ( $ hexstr ) { if ( ! strstr ( $ hexstr , '#' ) ) { $ hexstr = '#' . $ hexstr ; } if ( strlen ( $ hexstr ) == 4 ) { $ hexstr = '#' . $ hexstr [ 1 ] . $ hexstr [ 1 ] . $ hexstr [ 2 ] . $ hexstr [ 2 ] . $ hexstr [ 3 ] . $ hexstr [ 3 ] ; } $ int = hexdec ( $ hexstr ) ; return array ( "red" => 0xFF & ( $ int >> 0x10 ) , "green" => 0xFF & ( $ int >> 0x8 ) , "blue" => 0xFF & $ int ) ; }
Helper method to convert hex string to rgb array
7,997
public function get_posts ( $ numberposts_or_args = 10 , $ post_type_or_class = 'any' , $ post_class = '' ) { if ( ! strlen ( $ post_class ) ) { $ post_class = $ this -> PostClass ; } $ default_tax_query = array ( array ( 'field' => 'term_id' , 'terms' => $ this -> ID , 'taxonomy' => $ this -> taxonomy , ) ) ; if ( is_string ( $ numberposts_or_args ) && strstr ( $ numberposts_or_args , '=' ) ) { $ args = $ numberposts_or_args ; $ new_args = array ( ) ; parse_str ( $ args , $ new_args ) ; $ args = $ new_args ; $ args [ 'tax_query' ] = $ default_tax_query ; if ( ! isset ( $ args [ 'post_type' ] ) ) { $ args [ 'post_type' ] = 'any' ; } if ( class_exists ( $ post_type_or_class ) ) { $ post_class = $ post_type_or_class ; } } else if ( is_array ( $ numberposts_or_args ) ) { $ args = $ numberposts_or_args ; if ( ! isset ( $ args [ 'tax_query' ] ) ) { $ args [ 'tax_query' ] = $ default_tax_query ; } if ( class_exists ( $ post_type_or_class ) ) { $ post_class = $ post_type_or_class ; } if ( ! isset ( $ args [ 'post_type' ] ) ) { $ args [ 'post_type' ] = 'any' ; } } else { $ args = array ( 'numberposts' => $ numberposts_or_args , 'tax_query' => $ default_tax_query , 'post_type' => $ post_type_or_class ) ; } return Timber :: get_posts ( $ args , $ post_class ) ; }
Get posts that have the current term assigned .
7,998
public static function fix_number_posts_wp_quirk ( $ query ) { if ( isset ( $ query -> query ) && isset ( $ query -> query [ 'numberposts' ] ) && ! isset ( $ query -> query [ 'posts_per_page' ] ) ) { $ query -> set ( 'posts_per_page' , $ query -> query [ 'numberposts' ] ) ; } return $ query ; }
get_posts users numberposts
7,999
public static function fix_cat_wp_quirk ( $ query ) { if ( isset ( $ query -> query ) && isset ( $ query -> query [ 'category' ] ) && ! isset ( $ query -> query [ 'cat' ] ) ) { $ query -> set ( 'cat' , $ query -> query [ 'category' ] ) ; unset ( $ query -> query [ 'category' ] ) ; } return $ query ; }
get_posts uses category WP_Query uses cat . Why? who knows ...